Commit 1bf26f7a authored by Douwe Maan's avatar Douwe Maan

Move some functions to utils

parent 3c9e556b
/* eslint-disable class-methods-use-this */ /* eslint-disable class-methods-use-this */
/*jshint esversion: 6 */ /*jshint esversion: 6 */
/*= require lib/utils/common_utils */
(() => { (() => {
const gfmRules = { const gfmRules = {
// The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert // The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert
...@@ -233,7 +236,7 @@ ...@@ -233,7 +236,7 @@
let clipboardData = e.originalEvent.clipboardData; let clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return; if (!clipboardData) return;
let documentFragment = CopyAsGFM.getSelectedFragment(); let documentFragment = window.gl.utils.getSelectedFragment();
if (!documentFragment) return; if (!documentFragment) return;
e.preventDefault(); e.preventDefault();
...@@ -252,36 +255,7 @@ ...@@ -252,36 +255,7 @@
e.preventDefault(); e.preventDefault();
CopyAsGFM.insertText(e.target, gfm); window.gl.utils.insertText(e.target, gfm);
}
static getSelectedFragment() {
if (!window.getSelection) return null;
let selection = window.getSelection();
if (!selection.rangeCount || selection.rangeCount === 0) return null;
let documentFragment = selection.getRangeAt(0).cloneContents();
if (!documentFragment) return null;
if (documentFragment.textContent.length === 0) return null;
return documentFragment;
}
static insertText(target, text) {
// Firefox doesn't support `document.execCommand('insertText', false, text)` on textareas
let selectionStart = target.selectionStart;
let selectionEnd = target.selectionEnd;
let value = target.value;
let textBefore = value.substring(0, selectionStart);
let textAfter = value.substring(selectionEnd, value.length);
let newText = textBefore + text + textAfter;
target.value = newText;
target.selectionStart = target.selectionEnd = selectionStart + text.length;
} }
static nodeToGFM(node) { static nodeToGFM(node) {
...@@ -301,7 +275,7 @@ ...@@ -301,7 +275,7 @@
for (let selector in rules) { for (let selector in rules) {
let func = rules[selector]; let func = rules[selector];
if (!CopyAsGFM.nodeMatchesSelector(node, selector)) continue; if (!window.gl.utils.nodeMatchesSelector(node, selector)) continue;
let result = func(node, text); let result = func(node, text);
if (result === false) continue; if (result === false) continue;
...@@ -324,38 +298,13 @@ ...@@ -324,38 +298,13 @@
let clonedNode = clonedNodes[i]; let clonedNode = clonedNodes[i];
let text = this.nodeToGFM(node); let text = this.nodeToGFM(node);
// `clonedNode.replaceWith(text)` is not yet widely supported // `clonedNode.replaceWith(text)` is not yet widely supported
clonedNode.parentNode.replaceChild(document.createTextNode(text), clonedNode); clonedNode.parentNode.replaceChild(document.createTextNode(text), clonedNode);
} }
return clonedParentNode.innerText || clonedParentNode.textContent; return clonedParentNode.innerText || clonedParentNode.textContent;
} }
static nodeMatchesSelector(node, selector) {
let matches = Element.prototype.matches ||
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
Element.prototype.oMatchesSelector ||
Element.prototype.webkitMatchesSelector;
if (matches) {
return matches.call(node, selector);
}
// IE11 doesn't support `node.matches(selector)`
let parentNode = node.parentNode;
if (!parentNode) {
parentNode = document.createElement('div');
node = node.cloneNode(true);
parentNode.appendChild(node);
}
let matchingNodes = parentNode.querySelectorAll(selector);
return Array.prototype.indexOf.call(matchingNodes, node) !== -1;
}
} }
window.gl = window.gl || {}; window.gl = window.gl || {};
......
...@@ -160,6 +160,59 @@ ...@@ -160,6 +160,59 @@
return decodeURIComponent(results[2].replace(/\+/g, ' ')); return decodeURIComponent(results[2].replace(/\+/g, ' '));
}; };
w.gl.utils.getSelectedFragment = () => {
if (!window.getSelection) return null;
let selection = window.getSelection();
if (!selection.rangeCount || selection.rangeCount === 0) return null;
let documentFragment = selection.getRangeAt(0).cloneContents();
if (!documentFragment) return null;
if (documentFragment.textContent.length === 0) return null;
return documentFragment;
}
w.gl.utils.insertText = (target, text) => {
// Firefox doesn't support `document.execCommand('insertText', false, text)` on textareas
let selectionStart = target.selectionStart;
let selectionEnd = target.selectionEnd;
let value = target.value;
let textBefore = value.substring(0, selectionStart);
let textAfter = value.substring(selectionEnd, value.length);
let newText = textBefore + text + textAfter;
target.value = newText;
target.selectionStart = target.selectionEnd = selectionStart + text.length;
}
w.gl.utils.nodeMatchesSelector = (node, selector) => {
let matches = Element.prototype.matches ||
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
Element.prototype.oMatchesSelector ||
Element.prototype.webkitMatchesSelector;
if (matches) {
return matches.call(node, selector);
}
// IE11 doesn't support `node.matches(selector)`
let parentNode = node.parentNode;
if (!parentNode) {
parentNode = document.createElement('div');
node = node.cloneNode(true);
parentNode.appendChild(node);
}
let matchingNodes = parentNode.querySelectorAll(selector);
return Array.prototype.indexOf.call(matchingNodes, node) !== -1;
}
})(window); })(window);
}).call(this); }).call(this);
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
ShortcutsIssuable.prototype.replyWithSelectedText = function() { ShortcutsIssuable.prototype.replyWithSelectedText = function() {
var quote, replyField, documentFragment, selected, separator; var quote, replyField, documentFragment, selected, separator;
documentFragment = window.gl.CopyAsGFM.getSelectedFragment(); documentFragment = window.gl.utils.getSelectedFragment();
if (!documentFragment) return; if (!documentFragment) return;
selected = window.gl.CopyAsGFM.nodeToGFM(documentFragment); selected = window.gl.CopyAsGFM.nodeToGFM(documentFragment);
......
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
}); });
return describe('#replyWithSelectedText', function() { return describe('#replyWithSelectedText', function() {
var stubSelection; var stubSelection;
// Stub window.gl.CopyAsGFM.getSelectedFragment to return a node with the provided HTML. // Stub window.gl.utils.getSelectedFragment to return a node with the provided HTML.
stubSelection = function(html) { stubSelection = function(html) {
window.gl.CopyAsGFM.getSelectedFragment = function() { window.gl.utils.getSelectedFragment = function() {
var node = document.createElement('div'); var node = document.createElement('div');
node.innerHTML = html; node.innerHTML = html;
return node; return node;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment