Mergely Documentation
Mergely
Mergely is a JavaScript component for differencing and merging files interactively in a browser (diff/merge). It provides a rich API that enables you to easily integrate Mergely into your existing web application. It is suitable for comparing text files online, such as .txt, .html, .xml, .c, .cpp, .java, .js, etc.
Mergely has a JavaScript implementation of the Longest Common Subsequence (LCS) diff algorithm, and a customizable markup engine. It computes the diff within the browser.
This is the latest version 5. The previous version 4 can be found here.
Usage
Usage via React
The easiest and recommended way to use Mergely is with the mergely-react component.
npm install mergely-react
Usage via Angular
There is an Angular component for Mergely mergely-angular, but it is out of date. I will accept MR for anyone willing to do the work.
Usage via webpack
The source repository mergely-webpack contains an example of how to get started with a new project that uses mergely and webpack.
git clone --depth 1 https://github.com/wickedest/mergely-webpack.git my-project
cd my-project
rm -rf .git
Usage via CDN
Add the following to the <head>
of your target HTML source file. Note that codemirror
is bundled.
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mergely/5.0.0/mergely.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mergely/5.0.0/mergely.css" />
Synchronous initialization
If the editor contents are retrieved asynchronously (recommended), then retrieve the editor contents and set them:
<body>
<div id="compare"></div>
<script>
const doc = new Mergely('#compare', {
lhs: 'the quick red fox\njumped over the hairy dog',
rhs: 'the quick brown fox\njumped over the lazy dog'
;
})</script>
</body>
Asynchronous initialization
Mergely will emit an updated
event when the editor is first initialized, and each time one of the editors changes. You can listen for one event to perform one-time initialization.
<body>
<div id="compare"></div>
<script>
const doc = new Mergely('#compare');
.once('updated', () => {
doc.lhs('the quick red fox\njumped over the hairy dog');
doc.rhs('the quick brown fox\njumped over the lazy dog');
doc// Scroll to first change on next update
.once('updated', () => {
doc.scrollToDiff('next');
doc;
});
})</script>
</body>
Visualization modes
Mergely supports the following CodeMirror visualizations for mode:
- go
- javascript
- htmlmixed
- markdown
- python
Options
Option | Type | Default value | Description |
---|---|---|---|
autoupdate | boolean | true | Controls whether or not the changed event will trigger a diff. |
bgcolor | string | #eeeeee |
The background color for the left-hand and right-hand margins. |
change_timeout | number | 150 |
The timeout, after a text change, before Mergely calculates a diff. Only used when readonly is enabled. |
cmsettings | object | {mode: 'text/plain', readOnly: false} |
CodeMirror settings (see CodeMirror) that are combined with lhs_cmsettings and rhs_cmsettings . |
ignorews | boolean | false |
Ignores white-space. |
ignorecase | boolean | false |
Ignores case. |
ignoreaccents | boolean | false |
Ignores accented characters. |
lcs | boolean | true |
Enables/disables LCS computation for paragraphs (char-by-char changes). Disabling can give a performance gain for large documents. |
lhs | boolean,function handler(setValue) |
null |
Sets the value of the editor on the left-hand side. |
license | string | lgpl |
The choice of license to use with Mergely. Valid values are: lgpl , gpl , mpl or lgpl-separate-notice , gpl-separate-notice , mpl-separate-notice (the license requirements are met in a separate notice file). |
line_numbers | boolean | true |
Enables/disables line numbers. Enabling line numbers will toggle the visibility of the line number margins. |
lhs_cmsettings | object | {} |
The CodeMirror settings (see CodeMirror) for the left-hand side editor. |
resize_timeout | number | 500 |
The timeout, after a resize, before Mergely auto-resizes. Only used when autoresize enabled. |
rhs | boolean,function handler(setValue) |
null |
Sets the value of the editor on the right-hand side. |
rhs_cmsettings | object | {} |
The CodeMirror settings (see CodeMirror) for the right-hand side editor. |
rhs_margin | string | right |
Location for the rhs markup margin. Possible values: right, left. |
sidebar | boolean | true |
Enables/disables sidebar markers. Disabling can give a performance gain for large documents. |
vpcolor | string | rgba(0, 0, 200, 0.5) |
The margin/viewport indicator color. |
viewport | boolean | false |
Enables/disables the viewport. Enabling the viewport can give a performance gain for large documents. |
wrap_lines | boolean | false |
Enables/disables line wrapping. Enabling wrapping will wrap text to fit the editors. |
Constructor
constructor(selector: string, options?: object)
Parameters
Name | Type | Description |
---|---|---|
selector | string | A CSS selector used to uniquely identify the DOM element that should be used to bind the instance of Mergely. |
options | object | Configuration options for the instance. |
Example
new Mergely('#editor', { ignorews: true });
Methods
clear(side: string)
Clears the editor contents for the specified side
.
Parameters
Name | Type | Description |
---|---|---|
side | string | The editor side, either lhs or rhs . |
Example
.clear('lhs'); doc
cm(side: string)
Gets the CodeMirror editor for the specified side
.
Parameters
Name | Type | Description |
---|---|---|
side | string | The editor side, either lhs or rhs . |
Example
.cm('lhs'); doc
diff()
Calculates and returns the current .diff file.
Parameters
None.
Example
.diff(); doc
get(side: string)
Gets the text editor contents for the specified side
.
Parameters
Name | Type | Description |
---|---|---|
side | string | The editor side, either lhs or rhs . |
Example
.get('lhs'); doc
lhs(value: string)
Sets the value of the left-hand editor.
Parameters
Name | Type | Description |
---|---|---|
value | string | The editor text. |
Example
.lhs('This is text'); doc
merge(side: string)
Merges whole file from the specified side
to the opposite side.
Parameters
Name | Type | Description |
---|---|---|
side | string | The editor side, either lhs or rhs . |
Example
.merge('lhs'); doc
mergeCurrentChange(side: string)
Merges the current change from the specified side
to the opposite side.
Parameters
Name | Type | Description |
---|---|---|
side | string | The editor side, either lhs or rhs . |
Example
.mergeCurrentChange('lhs'); doc
on(event: string, handler: function)
Sets up a function
to be called when the specified event
is emitted. The event handler will be automatically deregistered on unbind.
Parameters
None.
Example
.on('updated', () => console.log('updated!')); doc
once(event: string, handler: function)
Sets up a function
to be called when the specified event
is emitted. The event handler will be automatically deregistered after the handler
is called.
Parameters
None.
Example
.unbind(); doc
options(options?: object)
Gets or sets the editor Options. With no arguments, the function will return the currenty configured options. When supplied options to change, the editor will automatically update with the new settings.
Parameters
Name | Type | Description |
---|---|---|
options | object | The options to set. |
Example
const currentOptions = doc.options();
.options({ line_numbers: true }); doc
resize()
Resizes the editor. It must be called explicitly if autoresize
is disabled.
Parameters
None.
Example
.resize(); doc
rhs(value: string)
Sets the value of the right-hand editor.
Parameters
Name | Type | Description |
---|---|---|
value | string | The editor text. |
Example
.rhs('This is text'); doc
scrollTo(side: string, lineNum: integer)
Scrolls the editor side
to line number specified by lineNum
.
Parameters
Name | Type | Description |
---|---|---|
side | string | The editor side, either lhs or rhs . |
lineNum | number | The line number. |
Example
.scrollTo('lhs', 100); doc
scrollToDiff(direction: string)
Scrolls to the next change specified by direction
.
Parameters
Name | Type | Description |
---|---|---|
direction | string | The direction to scroll, either prev or next . |
Example
.scrollToDiff('next'); doc
search(side: string, needle: string, direction: string = ‘next’)
Search the editor for needle
, scrolling to the next available match. Repeating the call will find the next available token.
Parameters
Name | Type | Description |
---|---|---|
side | string | The editor side, either lhs or rhs . |
needle | string | The text for which to search. |
direction | string | The direction to search, either prev or next . |
Example
.search('lhs', 'banana'); doc
summary()
Gets a summary of the editors. Returns an object with summarized properties:
Name | Description |
---|---|
a | The number of added lines. |
c | The number of changed lines. |
d | The number of deleted lines. |
lhsLength | The number of characters in the lhs text. |
rhsLength | The number of characters in the rhs text. |
numChanges | The total number of changed lines. |
Parameters
None.
Example
console.log(doc.summary());
// { a: 0, c: 1, d: 0, lhsLength: 44, rhsLength: 45, numChanges: 1 }
swap()
Swaps the content of the left and right editors. The content cannot be swapped if either editor is read-only.
Parameters
None.
Example
.swap(); doc
unmarkup()
Clears the editor markup.
Parameters
None.
Example
.unmarkup(); doc
unbind()
Unbinds and destroys the editor DOM.
Parameters
None.
Example
.unbind(); doc
Events
Event handlers are automatically unregistered when unbind is called.
changed
Triggered when one of the editors change, e.g. text was altered. The change_timeout controls how much time should pass after the changed
event (e.g. keypress) before the updated
event is triggered.
Example
.once('changed', () => { console.log('changed!'); }
mergely
.on('changed', () => { console.log('changed!'); } mergely
resized
Triggered after the editor is resized.
Example
.once('resized', () => { console.log('resized!'); }
mergely
.on('resized', () => { console.log('resized!'); } mergely
updated
Triggered after the editor finishes rendering. For example, text updates, options, or scroll events may trigger renders. This event is useful for handling a once-off initialization that should occur after the editor’s first render.
Example
.once('updated', () => { console.log('updated!'); }
mergely
.on('updated', () => { console.log('updated!'); } mergely