Files
Mergely/src/dom.js
Jamie Peabody 37fedd05c8 v5.0 (#174)
* feat: v5

BREAKING CHANGE: Mergely is no longer a jQuery plugin.

BREAKING CHANGE: Removed `options.autoresize`

BREAKING CHANGE: Removed `options.editor_width`

BREAKING CHANGE: Removed `options.editor_height`

BREAKING CHANGE: Removed `options.fadein`

BREAKING CHANGE: Removed `options.fgcolor`

BREAKING CHANGE: Removed `options.resize`

BREAKING CHANGE: Removed `options.width`

BREAKING CHANGE: Removed `options.height`

BREAKING CHANGE: Removed `options.loaded` callback

BREAKING CHANGE: Removed `options.resized` callback

BREAKING CHANGE: Removed styles `.mergely-resizer`, `.mergely-full-screen-0`, and `.mergely-full-screen-8`

BREAKING CHANGE: Changed default for `options.change_timeout` changed from `150` to `50`.

BREAKING CHANGE: No longer automatically scrolls to first change.

feat: CodeMirror is now an explicit dependency.

feat: No longer necessary to separately require codemirror/addon/search/searchcursor

feat: No longer necessary to separately require codemirror/addon/selection/mark-selection

feat: `mergely.js` is now unminimized, and added new minimized version `mergely.min.js`

feat: Gutter click now scrolls to any line

feat: Mergely now emits `resize` event on resize

feat: The UI is now non-blocking as diff now runs in background

feat: Added support to provide `options.lhs` and `options.rhs` as strings

feat: #16 added titles to editor.mergely.com

fix: #165 block of changes at end of file are now distinguishable

fix: #140 fixed performance issue with large files

fix: Fixed issue where canvas markup was not rendered when `viewport` enabled

fix: Fixed timing issue where swap sides may not work as expected.

fix: Fixed issue where unmarkup did not emit an updated event.

fix: Fixed documentation issue where `merge` incorrectly stated: from the specified `side` to the opposite side.

fix: Fixed performance issue scrolling

fix: Fixed issue where initial render scrolled to first change, showing it at the bottom (as opposed to middle as expected)

fix: Fixed issue where line-diffs failed to diff non-alphanumeric characters

* chore: tweaked no-start/end styles

* feat: dark mode

* chore: updated examples

* chore(ci): updated webpack

* chore(ci): alpha, beta, next branches

* chore(ci): test

* chore(ci): package-lock.json

* chore(ci): ignore alpha, beta, next on branch

* fix: fixes firefox scroll-linked effect issue

* fix: fixes firefox scroll-linked effect issue

* chore: fix css

* chore: debug
2023-04-23 16:29:59 +01:00

113 lines
3.2 KiB
JavaScript

/**
* @param {String} HTML representing a single element
* @return {Element}
*/
function htmlToElement(html) {
var template = document.createElement('template');
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return template.content.firstChild;
}
function getColors(el) {
// get current diff border color from user-defined css
const classes = ['mergely-editor', ...el.classList];
const text = `
<div style="display:none" class="${classes.join(' ')}">
<div class="mergely current start"></div>
<div class="mergely start end rhs a CodeMirror-linebackground"></div>
<div class="mergely start end lhs d CodeMirror-linebackground"></div>
<div class="mergely start end lhs c CodeMirror-linebackground"></div>
<div class="mergely ch rhs a"></div>
<div class="mergely ch rhs ina"></div>
<div class="mergely ch lhs d"></div>
<div class="mergely ch lhs ind"></div>
</div>
`;
const node = htmlToElement(text);
el.appendChild(node);
const currentStyle = window.getComputedStyle(node.children[0]);
const aStyle = window.getComputedStyle(node.children[1]);
const dStyle = window.getComputedStyle(node.children[2]);
const cStyle = window.getComputedStyle(node.children[3]);
const achStyle = window.getComputedStyle(node.children[4]);
const ainStyle = window.getComputedStyle(node.children[5]);
const dchStyle = window.getComputedStyle(node.children[6]);
const dinStyle = window.getComputedStyle(node.children[7]);
const colors = {
current: {
border: currentStyle.borderTopColor
},
a: {
border: aStyle.borderTopColor,
bg: aStyle.backgroundColor,
fg: aStyle.color,
ch: achStyle.color,
in: ainStyle.color
},
d: {
border: dStyle.borderTopColor,
bg: dStyle.backgroundColor,
fg: dStyle.color,
ch: dchStyle.color,
in: dinStyle.color
},
c: {
border: cStyle.borderTopColor,
bg: cStyle.backgroundColor,
fg: cStyle.color,
}
};
node.remove();
return colors;
}
function getMergelyContainer({ clazz = '' }) {
const classes = [ 'mergely-editor', clazz ]
return htmlToElement(`\
<div class="${classes.join(' ')}" style="display:flex;height:100%;position:relative;overflow:hidden;"></div>`);
}
function getMarginTemplate({ id }) {
return htmlToElement(`\
<div class="mergely-margin">
<canvas id="${id}-margin" width="8px"></canvas>
</div>`);
}
function getEditorTemplate({ id }) {
return htmlToElement(`\
<textarea id="${id}" class="mergely-column"></textarea>`);
}
function getCenterCanvasTemplate({ id }) {
return htmlToElement(`\
<div class="mergely-canvas">
<canvas id="${id}-lhs-rhs-canvas" width="28px"></canvas>
</div>`);
}
function getSplash({ notice, left, top }) {
return htmlToElement(`\
<div class="mergely-splash" style="left: ${left}px; top: ${top}px">
<p>
<span class="mergely-icon"></span>
This software is a Combined Work using Mergely and is covered by the
${notice} license. For the full license, see
<a target="_blank" href="http://www.mergely.com">http://www.mergely.com/license</a>.
</p>
</div>`);
}
module.exports = {
htmlToElement,
getColors,
getMergelyContainer,
getMarginTemplate,
getEditorTemplate,
getCenterCanvasTemplate,
getSplash
};