From 049a2719dcc62fdb1fec0f661040cd79ad71826e Mon Sep 17 00:00:00 2001 From: Jamie Peabody Date: Fri, 11 Oct 2024 20:45:08 +0100 Subject: [PATCH] fix(#209): Fixes issue with options not being preserved when individually set. --- src/diff-view.js | 1 - src/mergely.js | 6 +-- test/markup.spec.js | 3 -- test/mergely.spec.js | 90 ++++++++++++++++++++++++++------------------ 4 files changed, 55 insertions(+), 45 deletions(-) diff --git a/src/diff-view.js b/src/diff-view.js index 9507d80..94a24ee 100644 --- a/src/diff-view.js +++ b/src/diff-view.js @@ -449,7 +449,6 @@ CodeMirrorDiffView.prototype.bind = function(container) { ev.preventDefault(); return; } - const ed = this.editor[side]; // See if the user clicked the line number of a difference: let found = false; for (let i = 0; i < this.changes.length; ++i) { diff --git a/src/mergely.js b/src/mergely.js index 1d027be..44a1ed6 100644 --- a/src/mergely.js +++ b/src/mergely.js @@ -122,13 +122,11 @@ class Mergely { const colors = dom.getColors(this.el); this._options = { ...defaultOptions,//lgpl - ...this._initOptions, + ...(this._options || this._initOptions), ...options//lgpl-separate-notice }; this._viewOptions = { - ...defaultOptions, - ...this._initOptions, - ...options, + ...this._options, _colors: colors }; } diff --git a/test/markup.spec.js b/test/markup.spec.js index f80eb09..ecd8d50 100644 --- a/test/markup.spec.js +++ b/test/markup.spec.js @@ -340,7 +340,6 @@ describe('markup', () => { name: 'Changes with non-letter chars', lhs: '~# 00 == ! (dog) \n', rhs: '~? 11 ++ ] (fox) .\n', - only: true, check: (editor) => { expect(editor.querySelectorAll(LHS_CHANGE_START_AND_END + '.cid-0')).to.have.length(1); expect(editor.querySelectorAll(LHS_INLINE_TEXT + '.cid-0')).to.have.length(6); @@ -355,8 +354,6 @@ describe('markup', () => { for (const value of rhs_changes.values()) { rhs_values.push(value.innerText); } - console.log(lhs_values); - console.log(rhs_values); expect(lhs_values).to.deep.equal(['#', '00', '==', '!', 'd', 'g']); expect(rhs_values).to.deep.equal(['?', '11', '++', ']', 'f', 'x', '.']); } diff --git a/test/mergely.spec.js b/test/mergely.spec.js index 9f4690c..de86bbf 100644 --- a/test/mergely.spec.js +++ b/test/mergely.spec.js @@ -62,19 +62,17 @@ describe('mergely', function () { try { const { children } = editor.el.children[0]; const items = Array.from(children).map(a => a.className); + expect(items[0] == 'mergely-margin'); + expect(items[1] == 'mergely-column'); // NOTE: if running karma debug, these tests can fail because // the debugger grabs the focus and the CodeMirror instance // loses `CodeMirror-focused` - expect(items).to.deep.equal([ - 'mergely-margin', - 'mergely-column', - 'CodeMirror cm-s-default CodeMirror-focused', - 'mergely-canvas', - 'mergely-column', - 'CodeMirror cm-s-default', - 'mergely-margin', - 'mergely-splash' - ]); + expect(items[2].indexOf('CodeMirror cm-s-default' == 0)); + expect(items[3] == 'mergely-margin'); + expect(items[4] == 'mergely-column'); + expect(items[5] == 'CodeMirror cm-s-default'); + expect(items[6] == 'mergely-column'); + expect(items[7] == 'mergely-splash'); expect(editor.get('lhs')).to.equal(''); expect(editor.get('rhs')).to.equal(''); done(); @@ -99,15 +97,17 @@ describe('mergely', function () { // NOTE: if running karma debug, these tests can fail because // the debugger grabs the focus and the CodeMirror instance // loses `CodeMirror-focused` - expect(items).to.deep.equal([ - 'mergely-margin', - 'mergely-column', - 'CodeMirror cm-s-default CodeMirror-focused', - 'mergely-canvas', - 'mergely-column', - 'CodeMirror cm-s-default', - 'mergely-margin' - ]); + expect(items[0] == 'mergely-margin'); + expect(items[1] == 'mergely-column'); + // NOTE: if running karma debug, these tests can fail because + // the debugger grabs the focus and the CodeMirror instance + // loses `CodeMirror-focused` + expect(items[2].indexOf('CodeMirror cm-s-default' == 0)); + expect(items[3] == 'mergely-margin'); + expect(items[4] == 'mergely-column'); + expect(items[5] == 'CodeMirror cm-s-default'); + expect(items[6] == 'mergely-column'); + expect(items[7] == 'mergely-splash'); expect(editor.get('lhs')).to.equal('left-hand side text'); expect(editor.get('rhs')).to.equal('right-hand side text'); done(); @@ -129,18 +129,17 @@ describe('mergely', function () { try { const { children } = editor.el.children[0]; const items = Array.from(children).map(a => a.className); + expect(items[0] == 'mergely-margin'); + expect(items[1] == 'mergely-column'); // NOTE: if running karma debug, these tests can fail because // the debugger grabs the focus and the CodeMirror instance // loses `CodeMirror-focused` - expect(items).to.deep.equal([ - 'mergely-margin', - 'mergely-column', - 'CodeMirror cm-s-default CodeMirror-focused', - 'mergely-canvas', - 'mergely-column', - 'CodeMirror cm-s-default', - 'mergely-margin' - ]); + expect(items[2].indexOf('CodeMirror cm-s-default' == 0)); + expect(items[3] == 'mergely-margin'); + expect(items[4] == 'mergely-column'); + expect(items[5] == 'CodeMirror cm-s-default'); + expect(items[6] == 'mergely-column'); + expect(items[7] == 'mergely-splash'); expect(editor.get('lhs')).to.equal('left-hand side text'); expect(editor.get('rhs')).to.equal('right-hand side text'); done(); @@ -161,18 +160,17 @@ describe('mergely', function () { try { const { children } = editor.el.children[0]; const items = Array.from(children).map(a => a.className); + expect(items[0] == 'mergely-margin'); + expect(items[1] == 'mergely-column'); // NOTE: if running karma debug, these tests can fail because // the debugger grabs the focus and the CodeMirror instance // loses `CodeMirror-focused` - expect(items).to.deep.equal([ - 'mergely-margin', - 'mergely-column', - 'CodeMirror cm-s-default CodeMirror-focused', - 'mergely-canvas', - 'mergely-column', - 'CodeMirror cm-s-default', - 'mergely-margin' - ]); + expect(items[2].indexOf('CodeMirror cm-s-default' == 0)); + expect(items[3] == 'mergely-margin'); + expect(items[4] == 'mergely-column'); + expect(items[5] == 'CodeMirror cm-s-default'); + expect(items[6] == 'mergely-column'); + expect(items[7] == 'mergely-splash'); expect(children[0].style.visibility).to.equal('hidden'); expect(children[6].style.visibility).to.equal('hidden'); done(); @@ -503,6 +501,24 @@ describe('mergely', function () { }); }); + it('should preserve changed option', function (done) { + const editor = init({ + change_timeout: 0, + license: 'lgpl-separate-notice', + lhs: 'left-hand side text', + rhs: 'right-hand side text' + }); + editor.once('updated', () => { + editor.options({ sidebar: false }); + expect(editor.options().wrap_lines).to.equal(false, 'wrap_lines'); + expect(editor.options().sidebar).to.equal(false, 'sidebar'); + editor.options({ wrap_lines: false }); + expect(editor.options().wrap_lines).to.equal(false, 'wrap_lines-2'); + expect(editor.options().sidebar).to.equal(false, 'sidebar-2'); + done(); + }); + }); + it('should ignore white-space', function (done) { const editor = init({ height: 100,