5.1.3 • Published 5 days ago

mergely v5.1.3

Weekly downloads
478
License
(GPL-3.0 OR LGPL-...
Repository
github
Last release
5 days ago

Mergely

mergely logo

https://mergely.com

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');
    doc.once('updated', () => {
      doc.lhs('the quick red fox\njumped over the hairy dog');
      doc.rhs('the quick brown fox\njumped over the lazy dog');
      // Scroll to first change on next update
      doc.once('updated', () => {
        doc.scrollToDiff('next');
      });
    });
  </script>
</body>

Visualization modes

Mergely supports the following CodeMirror visualizations for mode:

  • go
  • javascript
  • htmlmixed
  • markdown
  • python

Options

OptionTypeDefault valueDescription
autoupdatebooleantrueControls whether or not the changed event will trigger a diff.
bgcolorstring#eeeeeeThe background color for the left-hand and right-hand margins.
change_timeoutnumber150The timeout, after a text change, before Mergely calculates a diff. Only used when readonly is enabled.
cmsettingsobject{mode: 'text/plain', readOnly: false}CodeMirror settings (see CodeMirror) that are combined with lhs_cmsettings and rhs_cmsettings.
ignorewsbooleanfalseIgnores white-space.
ignorecasebooleanfalseIgnores case.
ignoreaccentsbooleanfalseIgnores accented characters.
lcsbooleantrueEnables/disables LCS computation for paragraphs (char-by-char changes). Disabling can give a performance gain for large documents.
lhsboolean,function handler(setValue)nullSets the value of the editor on the left-hand side.
licensestringlgplThe 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_numbersbooleantrueEnables/disables line numbers. Enabling line numbers will toggle the visibility of the line number margins.
lhs_cmsettingsobject{}The CodeMirror settings (see CodeMirror) for the left-hand side editor.
resize_timeoutnumber500The timeout, after a resize, before Mergely auto-resizes. Only used when autoresize enabled.
rhsboolean,function handler(setValue)nullSets the value of the editor on the right-hand side.
rhs_cmsettingsobject{}The CodeMirror settings (see CodeMirror) for the right-hand side editor.
rhs_marginstringrightLocation for the rhs markup margin. Possible values: right, left.
sidebarbooleantrueEnables/disables sidebar markers. Disabling can give a performance gain for large documents.
vpcolorstringrgba(0, 0, 200, 0.5)The margin/viewport indicator color.
viewportbooleanfalseEnables/disables the viewport. Enabling the viewport can give a performance gain for large documents.
wrap_linesbooleanfalseEnables/disables line wrapping. Enabling wrapping will wrap text to fit the editors.

Constructor

constructor(selector: string, options?: object)

Parameters

NameTypeDescription
selectorstringA CSS selector used to uniquely identify the DOM element that should be used to bind the instance of Mergely.
optionsobjectConfiguration options for the instance.

Example

new Mergely('#editor', { ignorews: true });

Methods

clear(side: string)

Clears the editor contents for the specified side.

Parameters

NameTypeDescription
sidestringThe editor side, either lhs or rhs.

Example

doc.clear('lhs');

cm(side: string)

Gets the CodeMirror editor for the specified side.

Parameters

NameTypeDescription
sidestringThe editor side, either lhs or rhs.

Example

doc.cm('lhs');

diff()

Calculates and returns the current .diff file.

Parameters

None.

Example

doc.diff();

get(side: string)

Gets the text editor contents for the specified side.

Parameters

NameTypeDescription
sidestringThe editor side, either lhs or rhs.

Example

doc.get('lhs');

lhs(value: string)

Sets the value of the left-hand editor.

Parameters

NameTypeDescription
valuestringThe editor text.

Example

doc.lhs('This is text');

merge(side: string)

Merges whole file from the specified side to the opposite side.

Parameters

NameTypeDescription
sidestringThe editor side, either lhs or rhs.

Example

doc.merge('lhs');

mergeCurrentChange(side: string)

Merges the current change from the specified side to the opposite side.

Parameters

NameTypeDescription
sidestringThe editor side, either lhs or rhs.

Example

doc.mergeCurrentChange('lhs');

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

doc.on('updated', () => console.log('updated!'));

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

doc.unbind();

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

NameTypeDescription
optionsobjectThe options to set.

Example

const currentOptions = doc.options();
doc.options({ line_numbers: true });

resize()

Resizes the editor. It must be called explicitly if autoresize is disabled.

Parameters

None.

Example

doc.resize();

rhs(value: string)

Sets the value of the right-hand editor.

Parameters

NameTypeDescription
valuestringThe editor text.

Example

doc.rhs('This is text');

scrollTo(side: string, lineNum: integer)

Scrolls the editor side to line number specified by lineNum.

Parameters

NameTypeDescription
sidestringThe editor side, either lhs or rhs.
lineNumnumberThe line number.

Example

doc.scrollTo('lhs', 100);

scrollToDiff(direction: string)

Scrolls to the next change specified by direction.

Parameters

NameTypeDescription
directionstringThe direction to scroll, either prev or next.

Example

doc.scrollToDiff('next');

search(side: string, needle: string)

Search the editor for needle, scrolling to the next available match. Repeating the call will find the next available token.

Parameters

NameTypeDescription
sidestringThe editor side, either lhs or rhs.
needlestringThe text for which to search.

Example

doc.search('lhs', 'banana');

summary()

Gets a summary of the editors. Returns an object with summarized properties:

NameDescription
aThe number of added lines.
cThe number of changed lines.
dThe number of deleted lines.
lhsLengthThe number of characters in the lhs text.
rhsLengthThe number of characters in the rhs text.
numChangesThe 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

doc.swap();

unmarkup()

Clears the editor markup.

Parameters

None.

Example

doc.unmarkup();

unbind()

Unbinds and destroys the editor DOM.

Parameters

None.

Example

doc.unbind();

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

mergely.once('changed', () => { console.log('changed!'); }

mergely.on('changed', () => { console.log('changed!'); }

resized

Triggered after the editor is resized.

Example

mergely.once('resized', () => { console.log('resized!'); }

mergely.on('resized', () => { console.log('resized!'); }

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

mergely.once('updated', () => { console.log('updated!'); }

mergely.on('updated', () => { console.log('updated!'); }
5.1.3

5 days ago

5.1.2

5 days ago

5.1.1

2 months ago

5.0.4

9 months ago

5.0.3

9 months ago

5.1.0

9 months ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

5.0.0-alpha.3

1 year ago

5.0.0-alpha.2

1 year ago

5.0.0-alpha.1

1 year ago

4.3.9

2 years ago

4.3.6

2 years ago

4.3.8

2 years ago

4.3.7

2 years ago

4.3.5

3 years ago

4.3.4

3 years ago

4.3.2

3 years ago

4.2.3

3 years ago

4.3.1

3 years ago

4.2.4

3 years ago

4.3.0

3 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.15

4 years ago

4.0.14

4 years ago

4.0.13

4 years ago

4.0.11

5 years ago

4.0.10

5 years ago

4.0.9

5 years ago

4.0.8

5 years ago

4.0.7

6 years ago

4.0.6

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.4.0

8 years ago