summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-01-18 16:19:51 -0600
committerDouwe Maan <douwe@selenight.nl>2017-01-18 16:19:51 -0600
commit1bf26f7aadd94e32a9cd7f78ce0b21f185f7cae3 (patch)
tree292aad931f4445e35f37517db88311db1b60120b
parent3c9e556b9e1da01df0cf6527cc3468f9fed9dfeb (diff)
downloadgitlab-ce-1bf26f7aadd94e32a9cd7f78ce0b21f185f7cae3.tar.gz
Move some functions to utils
-rw-r--r--app/assets/javascripts/copy_as_gfm.js.es665
-rw-r--r--app/assets/javascripts/lib/utils/common_utils.js.es653
-rw-r--r--app/assets/javascripts/shortcuts_issuable.js2
-rw-r--r--spec/javascripts/shortcuts_issuable_spec.js4
4 files changed, 63 insertions, 61 deletions
diff --git a/app/assets/javascripts/copy_as_gfm.js.es6 b/app/assets/javascripts/copy_as_gfm.js.es6
index 8e7f4c54213..0059dc3f60f 100644
--- a/app/assets/javascripts/copy_as_gfm.js.es6
+++ b/app/assets/javascripts/copy_as_gfm.js.es6
@@ -1,6 +1,9 @@
/* 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;
@@ -324,38 +298,13 @@
let clonedNode = clonedNodes[i];
let text = this.nodeToGFM(node);
-
+
// `clonedNode.replaceWith(text)` is not yet widely supported
clonedNode.parentNode.replaceChild(document.createTextNode(text), clonedNode);
}
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 || {};
diff --git a/app/assets/javascripts/lib/utils/common_utils.js.es6 b/app/assets/javascripts/lib/utils/common_utils.js.es6
index 0c6a3cc3170..59b01668688 100644
--- a/app/assets/javascripts/lib/utils/common_utils.js.es6
+++ b/app/assets/javascripts/lib/utils/common_utils.js.es6
@@ -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);
diff --git a/app/assets/javascripts/shortcuts_issuable.js b/app/assets/javascripts/shortcuts_issuable.js
index e9ede122ab7..363379f49ae 100644
--- a/app/assets/javascripts/shortcuts_issuable.js
+++ b/app/assets/javascripts/shortcuts_issuable.js
@@ -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);
diff --git a/spec/javascripts/shortcuts_issuable_spec.js b/spec/javascripts/shortcuts_issuable_spec.js
index 7e5c0e2f144..c2894d6f3ea 100644
--- a/spec/javascripts/shortcuts_issuable_spec.js
+++ b/spec/javascripts/shortcuts_issuable_spec.js
@@ -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;