1.2.0 • Published 3 months ago

@mismerge/core v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

A web-based merge editor

Mismerge is a modern two-way and one-way merge editor for the web, built with Svelte. You can visit the demo and start merging now, or use it as a component for your project. It is also available in React and Vue.

Features

  • ▶️ One way merge editor
  • 🔀 Two way merge editor
  • 📑 Support lines wrapping
  • 🌈 Support syntax highlighting
  • ➖ Can ignore whitespace
  • 📜 Custom input history
  • 🔠 Can ignore case
  • 🔢 Blocks, words and chars counter
  • ✅ Works in SvelteKit & TypeScript
  • 🌎 Available in React & Vue

Installation

npm i @mismerge/core

Usage

Svelte

<script>
	import { MisMerge3 } from '@mismerge/core';
	// Core styles, required for the editor to work properly
	import '@mismerge/core/styles.css';

	import '@mismerge/core/light.css';
	// Or  '@mismerge/core/dark.css';

	let lhs = 'foo';
	let ctr = 'bar';
	let rhs = 'baz';
</script>

<!-- Left-hand side and right-hand side constant text -->
<MisMerge3 {lhs} {rhs} bind:ctr />

<!-- All sides editable -->
<MisMerge3 bind:lhs bind:ctr bind:rhs lhsEditable rhsEditable >

<style>
  :global(.mismerge) {
    font-family: monospace;
    min-height: 600px;
  }
</style>

React

Install the additional adapter package:

npm i @mismerge/react
import { DefaultDarkColors, MisMerge3 } from './lib';
import { useEffect, useState } from 'react';
import '@mismerge/core/styles.css';
import '@mismerge/core/dark.css';

function App() {
	const [ctr, setCtr] = useState('Hello world!');

	useEffect(() => {
		console.log(ctr);
	}, [ctr]);

	return (
		<>
			<MisMerge3
				lhs="Hello world!"
				ctr={ctr}
				rhs="Hello world!"
				onCtrChange={setCtr}
				colors={DefaultDarkColors}
				wrapLines={true}
			/>
		</>
	);
}

Vue

Install the additional adapter package:

npm i @mismerge/vue

!NOTE
Due to some differences in how Vue treats boolean attributes, some default properties may not correspond to the ones described in the API section.

<script setup lang="ts">
import { MisMerge3, DefaultDarkColors } from './lib';
import '@mismerge/core/styles.css';
import '@mismerge/core/dark.css';
</script>

<template>
	<MisMerge3
		lhs="Hello"
		ctr="World"
		rhs="!"
		ctr-editable
		:colors="DefaultDarkColors"
		:on-ctr-change="console.log"
	/>
</template>

Customization

Adding syntax highlighting

You need to provide your own syntax highlighter. Example and demo using Shiki-JS. The highlighter can be either sync or async.

<script>
	import { codeToHtml } from 'shiki';
	// ...

	const highlight = async (text: string) =>
		await codeToHtml(text, {
			lang: 'js',
			theme: 'min-dark'
		});
</script>

<MisMerge3 ... {highlight} />

Changing connections colors

<script>
	import { DefaultDarkColors } from '@mismerge/core';
	// ...
</script>

<MisMerge3 ... colors={DefaultDarkColors} />

Styles

If you want to customize the editor styles, you can copy the default light or dark theme and adapt it to your need.

Here is a basic explanation of how the the rendered html looks like:

<div class="mismerge">
	<div>
		<!-- Main -->
		<div class="msm__main">
			<!-- View -->
			<div class="msm__view">
				<!-- Content -->
				<div class="msm__view-content">
					<!-- Blocks wrapper -->
					<div class="msm__wrapper">
						<!-- Blocks -->
						<div data-component-id="abcdefgh" class="msm__block block-type">
							<!-- Lines -->
							<div class="msm__line">
								<!-- ... -->
							</div>
							<!-- ... -->
						</div>
						<!-- ... -->
					</div>

					<!-- Highlight overlay -->
					<div class="msm__highlight-overlay">
						<!-- ... -->
					</div>

					<!-- Input -->
					<textarea />
				</div>

				<!-- Side panel -->
				<div class="msm__side-panel">
					<!-- ... -->
				</div>
			</div>
		</div>
	</div>

	<!-- Footer -->
	<div class="msm__footer">
		<!-- ... -->
	</div>
</div>

API

A list of properties for <MisMerge2>(2), <MisMerge3>(3), or both.

PropertyTypeDefaultDescriptionComponent
lhsstring""Left-hand side textBoth
ctrstring""Center text3
rhsstring""Right-hand side textBoth
colorsEditorColorsDefaultLightColorsConnections colorsBoth
highlight(text: string) => string \| Promise<string>undefinedSyntax highlighterBoth
lhsEditablebooleantrue(2), false(3)Can edit left panelBoth
ctrEditablebooleantrueCan edit center panel3
rhsEditablebooleantrue(2), false(3)Can edit right panelBoth
lineDiffAlgorithm'characters' \| 'words' \| 'words_with_space'words_with_spaceDiff algorithm for same line side by side diffBoth
disableMergingbooleanfalseDisables mergingBoth
wrapLinesbooleanfalseEnables lines wrappingBoth
disableWordsCounterbooleanfalseDisables words counterBoth
disableCharsCounterbooleanfalseDisables chars counterBoth
disableBlocksCountersbooleanfalseDisables blocks counterBoth
disableFooterbooleanfalseDisables footerBoth
ignoreWhitespacebooleanfalseIgnore whitespace in diffBoth
ignoreCasebooleanfalseIgnore case in diffBoth
conflictsResolvedboolean-Binding for when all conflicts have been resolved3

Events (available for Svelte):

NameDescription
on:mergeFired when a block is merged from one side to an adjacent one
on:resolveFired when a conflict has its resolved status toggled
on:deleteFired when a block is deleted in the center side
on:inputDefault textarea event
on:keydownDefault textarea event
on:keypressDefault textarea event
on:keyupDefault textarea event

Contributing

Project setup

Clone the repo:

git clone https://github.com/BearToCode/mismerge.git
cd mismerge

Download dependencies for all packages in the monorepo:

npm install

The core package

The core package is inside packages/core. You can run the associated sveltekit app using npm run core or cd packages/core & npm run dev.

The demo

The demo is inside the demo root folder. You can run it from root using npm run demo or cd demo & npm run dev. It is automatically deployed to Github Pages with every push to master.

Committing

This repository uses commitizen to enforce similar commit messages. Commit using:

npm run commit
# or
git cz
1.2.0

3 months ago

1.1.4

3 months ago

1.1.3

3 months ago

1.1.2

3 months ago

1.1.1

3 months ago

1.1.0

3 months ago

1.0.4

3 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago

0.0.1

7 months ago