diff --git a/examples/app.js b/examples/app.js index 3656ef4..34362e7 100644 --- a/examples/app.js +++ b/examples/app.js @@ -13,9 +13,13 @@ jumped over the lazy dog `; $(document).ready(function () { - $('#mergely').mergely({ + console.log('here', document.innerHTML); + + window.mergely('#mergely', { license: 'lgpl', ignorews: true, + wrap_lines: true, + //_debug: 'scroll', cmsettings: { readOnly: false }, @@ -26,4 +30,22 @@ $(document).ready(function () { setValue(rhs); } }); + + /* + $('#mergely').mergely({ + license: 'lgpl', + ignorews: true, + wrap_lines: true, + //_debug: 'scroll', + cmsettings: { + readOnly: false + }, + lhs: function(setValue) { + setValue(lhs); + }, + rhs: function(setValue) { + setValue(rhs); + } + }); + */ }); diff --git a/src/diff-view.js b/src/diff-view.js index 611d44f..8265a31 100644 --- a/src/diff-view.js +++ b/src/diff-view.js @@ -38,7 +38,7 @@ function CodeMirrorDiffView(el, options, { jQuery, CodeMirror }) { this.init(el, options); }; -CodeMirrorDiffView.prototype.init = function(el, options) { +CodeMirrorDiffView.prototype.init = function(el, options = {}) { const { jQuery } = this; this.settings = { autoupdate: true, @@ -103,15 +103,16 @@ CodeMirrorDiffView.prototype.init = function(el, options) { const rhsCM = this._queryElement(`#${this.id}-editor-rhs .cm-s-default`); rhsCM.style.width = `${contentWidth}px`; rhsCM.style.height = `${contentHeight}px`; + const lhsMargin = this._queryElement(`#${this.id}-lhs-margin`); + lhsMargin.style.height = `${contentHeight}px`; + lhsMargin.height = `${contentHeight}`; + const midCanvas = this._queryElement(`.mergely-canvas canvas`); + midCanvas.style.height = `${contentHeight}px`; + midCanvas.height = `${contentHeight}`; + const rhsMargin = this._queryElement(`#${this.id}-rhs-margin`); + rhsMargin.style.height = `${contentHeight}px`; + rhsMargin.height = `${contentHeight}`; - if (el.style.display === 'none') { - if (settings.fadein !== false) { - this.element.fadeIn(settings.fadein); - } - else { - this.element.show(); - } - } if (settings.resized) { settings.resized(); } @@ -123,6 +124,7 @@ CodeMirrorDiffView.prototype.init = function(el, options) { }; // save this element for faster queries this.element = jQuery(el); + this.el = el; this.lhs_cmsettings = { ...this.settings.cmsettings, @@ -141,25 +143,16 @@ CodeMirrorDiffView.prototype.init = function(el, options) { gutters: (this.settings.line_numbers && [ 'merge', 'CodeMirror-linenumbers' ]) || [], }; - // bind if the element is destroyed - this.element.bind('destroyed', jQuery.proxy(this.teardown, this)); - - // save this instance in jQuery data, binding this view to the node - jQuery.data(el, 'mergely', this); - this._setOptions(options); }; CodeMirrorDiffView.prototype.unbind = function() { - const { jQuery } = this; if (this.changed_timeout != null) clearTimeout(this.changed_timeout); this.editor.lhs.toTextArea(); this.editor.rhs.toTextArea(); - jQuery(window).off('.mergely'); }; CodeMirrorDiffView.prototype.destroy = function() { - this.element.unbind('destroyed', this.teardown); this.teardown(); }; @@ -373,9 +366,10 @@ CodeMirrorDiffView.prototype.diff = function() { }; CodeMirrorDiffView.prototype.bind = function(el) { - const { jQuery, CodeMirror } = this; + const { CodeMirror } = this; this.trace('init', 'bind'); - this.element.hide(); + el.style.visibility = 'hidden'; + el.style.opacity = '0'; this.id = el.id; const found = document.querySelector(`#${this.id}`); if (!found) { @@ -398,7 +392,7 @@ CodeMirrorDiffView.prototype.bind = function(el) { this.merge_rhs_button = htmlToElement(rhsTemplate); // create the textarea and canvas elements - this.element.get(0).className += ' mergely-editor'; + el.className += ' mergely-editor'; const canvasLhs = htmlToElement(getMarginTemplate({ id: this.id, side: 'lhs' @@ -419,18 +413,18 @@ CodeMirrorDiffView.prototype.bind = function(el) { id: this.id })); - this.element.append(canvasLhs); - this.element.append(editorLhs); - this.element.append(canvasMid); + el.append(canvasLhs); + el.append(editorLhs); + el.append(canvasMid); if (this.settings.rhs_margin == 'left') { - this.element.append(canvasRhs); + el.append(canvasRhs); } - this.element.append(editorRhs); + el.append(editorRhs); if (this.settings.rhs_margin != 'left') { - this.element.append(canvasRhs); + el.append(canvasRhs); } if (!this.settings.sidebar) { - this.element.find('.mergely-margin').css({ display: 'none' }); + el.find('.mergely-margin').css({ display: 'none' }); } if (NOTICES.indexOf(this.settings.license) < 0) { const noticeTypes = { @@ -442,17 +436,17 @@ CodeMirrorDiffView.prototype.bind = function(el) { if (!notice) { notice = noticeTypes.lgpl; } + const editor = this._queryElement('.mergely-editor'); const splash = htmlToElement(getSplash({ icon: MERGELY_ICON, notice, - left: (this.element.parent().width() - 300) / 2 + left: (editor.offsetWidth - 300) / 2 })); - const editor = this._queryElement('.mergely-editor'); editor.addEventListener('click', () => { splash.style.cssText += 'visibility: hidden; opacity: 0; transition: visibility 0s 100ms, opacity 100ms linear;'; setTimeout(() => splash.remove(), 110); }, { once: true }); - this.element.append(splash); + el.append(splash); } // check initialization @@ -466,8 +460,11 @@ CodeMirrorDiffView.prototype.bind = function(el) { } // get current diff border color from user-defined css - this.current_diff_color = jQuery('