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

Move some functions to utils

parent 3c9e556b
/* eslint-disable class-methods-use-this */
/*jshint esversion: 6 */
/*= require lib/utils/common_utils */
(() => {
const gfmRules = {
// The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert
......@@ -233,7 +236,7 @@
let clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return;
let documentFragment = CopyAsGFM.getSelectedFragment();
let documentFragment = window.gl.utils.getSelectedFragment();
if (!documentFragment) return;
e.preventDefault();
......@@ -252,36 +255,7 @@
e.preventDefault();
CopyAsGFM.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;
window.gl.utils.insertText(e.target, gfm);
}
static nodeToGFM(node) {
......@@ -301,7 +275,7 @@
for (let selector in rules) {
let func = rules[selector];
if (!CopyAsGFM.nodeMatchesSelector(node, selector)) continue;
if (!window.gl.utils.nodeMatchesSelector(node, selector)) continue;
let result = func(node, text);
if (result === false) continue;
......@@ -331,31 +305,6 @@
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 || {};
......
......@@ -160,6 +160,59 @@
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);
}).call(this);
......@@ -41,7 +41,7 @@
ShortcutsIssuable.prototype.replyWithSelectedText = function() {
var quote, replyField, documentFragment, selected, separator;
documentFragment = window.gl.CopyAsGFM.getSelectedFragment();
documentFragment = window.gl.utils.getSelectedFragment();
if (!documentFragment) return;
selected = window.gl.CopyAsGFM.nodeToGFM(documentFragment);
......
......@@ -15,9 +15,9 @@
});
return describe('#replyWithSelectedText', function() {
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) {
window.gl.CopyAsGFM.getSelectedFragment = function() {
window.gl.utils.getSelectedFragment = function() {
var node = document.createElement('div');
node.innerHTML = html;
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