const Timer = require('./timer'); const diff = require('./diff'); const DiffParser = require('./diff-parser'); const LCS = require('./lcs'); /** CHANGES: BREAKING: Added `.mergely-editor` to the DOM to scope all the CSS changes. CSS now prefixes `.mergely-editor`. Current active change gutter line number style changed from `.CodeMirror-linenumber` to `.CodeMirror-gutter-background`. FEATURE: Gutter click now scrolls to any line. FIX: Fixed issue where canvas markup was not rendered when `viewport` enabled. */ const MERGELY_ICON = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAABuCAIAAABJObGsAAAAFXRFWHRDcmVhdGlvbiBUaW1lAAfbCw8UOxvjZ6kDAAAAB3RJTUUH2wsPFQESa9FGmQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAFDBJREFUeNrtXQtQVFeavk2DvF/yZlUetg+EJIqPIvgIEo1xIxArcWqiMZWqsVK1Mbprand0NVuVrY1WnN2NW8ZNzWasqawmupPAqMRdFDdG81BGGYIjLKiooA4QQUF5N4/ezz7w9+He233PbRoaCX9R1Onbp/9z/u/8/3/+87wGi8UijZMryMPdFRg75OnuCtglmEtLS8tDK+FjkJUCAwMNBoO7q6ZOowLKurq68vLySitVVFRcu3atubm5tbVV6XyAY0BAQEhIyLRp05KSkmZaKTk5OSYmxt1CSAY3+sqSkpJjx47l5+eXlpYOkdXs2bOzs7NzcnJSU1PdJc4jOxpJMpvNhYWFGzdunDx58nCIA7ZgjiJQ0AiLNkJaCa93/Phx6GBBQQHzfQ7I09MTbtF7gCZMmICHgKZrgMChp6fHMRNwWLlyJfR01apV8LAjIOOwQwnJP/roo507d967d89enrCwMJPJFB4eHmYloOC4b0GdgeY9KzU2NlZVVTlmvmPHjjfffBOt8rhC2dfXd+jQoXfeeaempkalYIMhNjaW9RsRERFDLKuhoYH1WrW1taoSxcXFvffee2vXrvXwGK74b7igPHHixLZt2y5duiR7bjQa4+PjGYLDYXfwJAzT6urq3t5e2bdPPfXU+++///zzzw+HyK6Hsri4eOvWradPn5Y9R7ySnp6OIMbHx2c4JJFRZ2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WPc4FvBkB2zZxEVz80FR+sl64UPFBihyE2bNik3CypvKVDyYWXLbntQJeJmjxXPUJybg7oxblQ3afAswcOHDz/88ENqgIKCAmcufGAETSSPCyVVjpbGNuXn59PhnMzMTGiog8waAfbu3buplUpLS69fv+5u6UaOICwdGQEIgMJxfg0o4YzXrFnD0jCQ3NxcNy77jCRBTAhLPgEgaF5Jr30nG7rv+fPn0+QSevYNGzaM7Quw0E3t37+fgjyEKBcvXtS8jF57BM3uYSfsUEBeXt7IvFrGLQTRICDhCMEhvsil/kKTEQsWLEAr0UcM7E+d0n2U4XEhiMZP4kBwwev8Red11q1bx9+khbG98q0lY4AgFD9tsW3bNvGL/HXc9Qsf/OKLL9LFqohmX3/99bF0seqdO3c++eQTWiDLyso6evSo+BX+49cm99PQr00ev8z7EbnhMm9G41fMq9L4iw/c+uIDRuOv45DR+Eti+smdL4lhNP7qIqLxF2qNmhdqEY2/5m385YOj7+WDROOvxHQxjb+o1ZU0/vpgF9P4S61dTD+FV63bFotHhoBIYWHhxo0baae2awlswRxFsFsTR5JGSCtVqaSkBHqKYS+tkTpNGF9lZ2dDB+3tQhkBcieURIhXysvLWb9RUVGBIKa5uZndOyOvrvWwdUhICIKqpKQk1mslJyfz52fcRaMCSlWyWO+deWglyer7QPwVAKONRi+Ujx0NV5D1E6T/BwkHUltwIapAAAAAAElFTkSuQmCC'; const NOTICES = [ 'lgpl-separate-notice', 'gpl-separate-notice', 'mpl-separate-notice', 'commercial' ]; function CodeMirrorDiffView(el, options, { jQuery, CodeMirror }) { CodeMirror.defineExtension('centerOnCursor', function() { const coords = this.cursorCoords(null, 'local'); this.scrollTo(null, (coords.top + coords.bottom) / 2 - (this.getScrollerElement().clientHeight / 2)); }); this.jQuery = jQuery; this.CodeMirror = CodeMirror; this.init(el, options); }; CodeMirrorDiffView.prototype.init = function(el, options) { const { jQuery } = this; this.settings = { autoupdate: true, autoresize: true, rhs_margin: 'right', wrap_lines: false, line_numbers: true, lcs: true, sidebar: true, viewport: false, ignorews: false, ignorecase: false, ignoreaccents: false, fadein: 'fast', resize_timeout: 500, change_timeout: 150, fgcolor: {a:'#4ba3fa',c:'#a3a3a3',d:'#ff7f7f', // color for differences (soft color) ca:'#4b73ff',cc:'#434343',cd:'#ff4f4f'}, // color for currently active difference (bright color) bgcolor: '#eee', vpcolor: 'rgba(0, 0, 200, 0.5)', license: 'lgpl', width: 'auto', height: 'auto', cmsettings: { styleSelectedText: true }, lhs_cmsettings: {}, rhs_cmsettings: {}, lhs: function(setValue) { }, rhs: function(setValue) { }, loaded: function() { }, resize: function(init) { const parent = jQuery(el).parent(); let w; let h; if (this.width == 'auto') { w = parent.width(); } else { w = this.width; } if (this.height == 'auto') { h = parent.height() - 2; } else { h = this.height; } const content_width = w / 2.0 - 2 * 8 - 8; const content_height = h; const self = jQuery(el); self.find('.mergely-column').css({ width: content_width + 'px' }); self.find('.mergely-column, .mergely-canvas, .mergely-margin, .mergely-column textarea, .CodeMirror-scroll, .cm-s-default').css({ height: content_height + 'px' }); self.find('.mergely-canvas').css({ height: content_height + 'px' }); self.find('.mergely-column textarea').css({ width: content_width + 'px' }); self.css({ width: w, height: h, clear: 'both' }); if (self.css('display') === 'none') { if (this.fadein != false) { self.fadeIn(this.fadein); } else { self.show(); } } if (this.resized) this.resized(); }, _debug: '', //scroll,draw,calc,diff,markup,change,init resized: function() { }, // user supplied options ...options }; // save this element for faster queries this.element = jQuery(el); this.lhs_cmsettings = { ...this.settings.cmsettings, ...this.settings.lhs_cmsettings, // these override any user-defined CodeMirror settings lineWrapping: this.settings.wrap_lines, lineNumbers: this.settings.line_numbers, gutters: (this.settings.line_numbers && ['merge', 'CodeMirror-linenumbers']) || [], }; this.rhs_cmsettings = { ...this.settings.cmsettings, ...this.settings.rhs_cmsettings, // these override any user-defined CodeMirror settings lineWrapping: this.settings.wrap_lines, lineNumbers: this.settings.line_numbers, 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[this.id + '-lhs'].toTextArea(); this.editor[this.id + '-rhs'].toTextArea(); jQuery(window).off('.mergely'); }; CodeMirrorDiffView.prototype.destroy = function() { this.element.unbind('destroyed', this.teardown); this.teardown(); }; CodeMirrorDiffView.prototype.teardown = function() { this.unbind(); }; CodeMirrorDiffView.prototype.lhs = function(text) { // invalidate existing changes and current position this.changes = []; delete this._current_diff; this.editor[this.id + '-lhs'].setValue(text); }; CodeMirrorDiffView.prototype.rhs = function(text) { // invalidate existing changes and current position this.changes = []; delete this._current_diff; this.editor[this.id + '-rhs'].setValue(text); }; CodeMirrorDiffView.prototype.update = function() { this._changing(); }; CodeMirrorDiffView.prototype.unmarkup = function() { this._clear(); }; CodeMirrorDiffView.prototype.scrollToDiff = function(direction) { if (!this.changes.length) return; if (direction === 'next') { if (this._current_diff == this.changes.length - 1) { this._current_diff = 0; } else { this._current_diff = Math.min(++this._current_diff, this.changes.length - 1); } } else if (direction === 'prev') { if (this._current_diff == 0) { this._current_diff = this.changes.length - 1; } else { this._current_diff = Math.max(--this._current_diff, 0); } } this._scroll_to_change(this.changes[this._current_diff]); this._changed(); }; CodeMirrorDiffView.prototype.mergeCurrentChange = function(side) { if (!this.changes.length) return; if (side == 'lhs' && !this.lhs_cmsettings.readOnly) { this._merge_change(this.changes[this._current_diff], 'rhs', 'lhs'); } else if (side == 'rhs' && !this.rhs_cmsettings.readOnly) { this._merge_change(this.changes[this._current_diff], 'lhs', 'rhs'); } }; CodeMirrorDiffView.prototype.scrollTo = function(side, num) { const ed = this.editor[`${this.id}-${side}`]; ed.setCursor(num); ed.centerOnCursor(); }; CodeMirrorDiffView.prototype._setOptions = function(opts) { const { jQuery } = this; this.settings = { ...this.settings, ...opts }; if (this.settings.hasOwnProperty('rhs_margin')) { // dynami259*-6+cally swap the margin if (this.settings.rhs_margin == 'left') { this.element.find('.mergely-margin:last-child').insertAfter( this.element.find('.mergely-canvas')); } else { const target = this.element.find('.mergely-margin').last(); target.appendTo(target.parent()); } } if (this.settings.hasOwnProperty('sidebar')) { // dynamically enable sidebars if (this.settings.sidebar) { this.element.find('.mergely-margin').css({display: 'block'}); } else { this.element.find('.mergely-margin').css({display: 'none'}); } } // if options set after init if (this.editor) { const le = this.editor[`${this.id}-lhs`]; const re = this.editor[`${this.id}-rhs`]; if (this.settings.hasOwnProperty('wrap_lines')) { le.setOption('lineWrapping', this.settings.wrap_lines); re.setOption('lineWrapping', this.settings.wrap_lines); } if (this.settings.hasOwnProperty('line_numbers')) { le.setOption('lineNumbers', this.settings.line_numbers); re.setOption('lineNumbers', this.settings.line_numbers); } } }; CodeMirrorDiffView.prototype.options = function(opts) { if (opts) { this._setOptions(opts); if (this.settings.autoresize) this.resize(); if (this.settings.autoupdate) this.update(); } else { return this.settings; } }; CodeMirrorDiffView.prototype.swap = function() { if (this.lhs_cmsettings.readOnly || this.rhs_cmsettings.readOnly) { return; } const le = this.editor[`${this.id}-lhs`]; const re = this.editor[`${this.id}-rhs`]; re.setValue(le.getValue()); le.setValue(re.getValue()); }; CodeMirrorDiffView.prototype.merge = function(side) { const le = this.editor[`${this.id}-lhs`]; const re = this.editor[`${this.id}-rhs`]; if (side == 'lhs' && !this.lhs_cmsettings.readOnly) { le.setValue(re.getValue()); } else if (!this.rhs_cmsettings.readOnly) { re.setValue(le.getValue()); } }; CodeMirrorDiffView.prototype.summary = function() { const le = this.editor[`${this.id}-lhs`]; const re = this.editor[`${this.id}-rhs`]; return { numChanges: this.changes.length, lhsLength: le.getValue().length, rhsLength: re.getValue().length, c: this.changes.filter(function (a) { return a.op === 'c'; }).length, a: this.changes.filter(function (a) { return a.op === 'a'; }).length, d: this.changes.filter(function (a) { return a.op === 'd'; }).length } }; CodeMirrorDiffView.prototype.get = function(side) { const ed = this.editor[`${this.id}-${side}`]; const value = ed.getValue(); if (value === undefined) { return ''; } return value; }; CodeMirrorDiffView.prototype.clear = function(side) { if (side == 'lhs' && this.lhs_cmsettings.readOnly) return; if (side == 'rhs' && this.rhs_cmsettings.readOnly) return; const ed = this.editor[`${this.id}-${side}`]; ed.setValue(''); delete this._current_diff; }; CodeMirrorDiffView.prototype.cm = function(side) { return this.editor[`${this.id}-${side}`]; }; CodeMirrorDiffView.prototype.search = function(side, query, direction) { const editor = this.editor[`${this.id}-${side}`]; const searchDirection = (direction === 'prev') ? 'findPrevious' : 'findNext'; const start = { line: 0, ch: 0 }; if ((editor.getSelection().length == 0) || (this.prev_query[side] != query)) { this.cursor[this.id] = editor.getSearchCursor(query, start, false); this.prev_query[side] = query; } const cursor = this.cursor[this.id]; if (cursor[searchDirection]()) { editor.setSelection(cursor.from(), cursor.to()); } else { cursor = editor.getSearchCursor(query, start, false); } }; CodeMirrorDiffView.prototype.resize = function() { // recalculate line height as it may be zoomed this.em_height = null; this.settings.resize(); this._changing(); this._set_top_offset(this.id + '-lhs'); }; CodeMirrorDiffView.prototype.diff = function() { const le = this.editor[`${this.id}-lhs`]; const re = this.editor[`${this.id}-rhs`]; const lhs = le.getValue(); const rhs = re.getValue(); const comparison = new diff(lhs, rhs, this.settings); return comparison.normal_form(); }; CodeMirrorDiffView.prototype.bind = function(el) { const { jQuery, CodeMirror } = this; this.trace('init', 'bind'); this.element.hide(); this.id = jQuery(el).attr('id'); this.lhsId = `${this.id}-lhs`; this.rhsId = `${this.id}-rhs`; try { // ensure the id is valid for jQuery jQuery(`#${this.id}`); } catch (ex) { console.error(`jQuery failed to find mergely: #${this.id}`); return; } this.changed_timeout = null; this.chfns = {}; this.chfns[this.lhsId] = []; this.chfns[this.rhsId] = []; this.prev_query = []; this.cursor = []; this._skipscroll = {}; this.change_exp = new RegExp(/(\d+(?:,\d+)?)([acd])(\d+(?:,\d+)?)/); if (jQuery.button !== undefined) { //jquery ui this.merge_lhs_button = document.createElement('button'); this.merge_lhs_button.title = 'Merge left'; this.merge_rhs_button = document.createElement('button'); this.merge_rhs_button.title = 'Merge right'; } else { // homebrew const style = 'opacity:0.6;height:16px;background-color:#bfbfbf;cursor:pointer;text-align:center;color:#eee;border:1px solid #848484;margin-right:-15px;margin-top:-2px;'; const lhsTemplate = `
<
`; const rhsTemplate = `
>
`; this.merge_lhs_button = htmlToElement(lhsTemplate); this.merge_rhs_button = htmlToElement(rhsTemplate); } // create the textarea and canvas elements const height = '10px'; const width = '10px'; this.element.get(0).className += ' mergely-editor'; const canvasLhs = htmlToElement(getMarginTemplate({ id: this.id, side: 'lhs' })); const canvasRhs = htmlToElement(getMarginTemplate({ id: this.id, side: 'rhs' })); const editorLhs = htmlToElement(getEditorTemplate({ id: this.id, side: 'lhs' })); const editorRhs = htmlToElement(getEditorTemplate({ id: this.id, side: 'rhs' })); const canvasMid = htmlToElement(getCenterCanvasTemplate({ id: this.id })); this.element.append(canvasLhs); this.element.append(editorLhs); this.element.append(canvasMid); if (this.settings.rhs_margin == 'left') { this.element.append(canvasRhs); } this.element.append(editorRhs); if (this.settings.rhs_margin != 'left') { this.element.append(canvasRhs); } if (!this.settings.sidebar) { this.element.find('.mergely-margin').css({ display: 'none' }); } if (NOTICES.indexOf(this.settings.license) < 0) { const noticeTypes = { 'lgpl': 'GNU LGPL v3.0', 'gpl': 'GNU GPL v3.0', 'mpl': 'MPL 1.1' }; const notice = noticeTypes[this.settings.license]; if (!notice) { notice = noticeTypes.lgpl; } const splash = htmlToElement(getSplash({ icon: MERGELY_ICON, notice, left: (this.element.parent().width() - 300) / 2 })); // FIXME: use editor jQuery('body').one('click', function () { splash.style.cssText += 'visibility: hidden; opacity: 0; transition: visibility 0s 100ms, opacity 100ms linear;'; setTimeout(() => splash.remove(), 110); }); this.element.append(splash); } // check initialization const lhstx = document.querySelector(`#${this.id}-lhs`); const rhstx = document.querySelector(`#${this.id}-rhs`); if (!lhstx) { console.error('lhs textarea not defined - Mergely not initialized properly'); } if (!rhstx) { console.error('rhs textarea not defined - Mergely not initialized properly'); } // get current diff border color from user-defined css this.current_diff_color = jQuery('