Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
1bf26f7a
Commit
1bf26f7a
authored
Jan 18, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move some functions to utils
parent
3c9e556b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
63 additions
and
61 deletions
+63
-61
app/assets/javascripts/copy_as_gfm.js.es6
app/assets/javascripts/copy_as_gfm.js.es6
+7
-58
app/assets/javascripts/lib/utils/common_utils.js.es6
app/assets/javascripts/lib/utils/common_utils.js.es6
+53
-0
app/assets/javascripts/shortcuts_issuable.js
app/assets/javascripts/shortcuts_issuable.js
+1
-1
spec/javascripts/shortcuts_issuable_spec.js
spec/javascripts/shortcuts_issuable_spec.js
+2
-2
No files found.
app/assets/javascripts/copy_as_gfm.js.es6
View file @
1bf26f7a
/* 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;
...
@@ -331,31 +305,6 @@
...
@@ -331,31 +305,6 @@
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 || {};
...
...
app/assets/javascripts/lib/utils/common_utils.js.es6
View file @
1bf26f7a
...
@@ -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);
app/assets/javascripts/shortcuts_issuable.js
View file @
1bf26f7a
...
@@ -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
);
...
...
spec/javascripts/shortcuts_issuable_spec.js
View file @
1bf26f7a
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment