Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
1bf26f7a
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1bf26f7a
编写于
1月 18, 2017
作者:
D
Douwe Maan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move some functions to utils
上级
3c9e556b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
63 addition
and
61 deletion
+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
未找到文件。
app/assets/javascripts/copy_as_gfm.js.es6
浏览文件 @
1bf26f7a
/* 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 || {};
...
...
app/assets/javascripts/lib/utils/common_utils.js.es6
浏览文件 @
1bf26f7a
...
...
@@ -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);
app/assets/javascripts/shortcuts_issuable.js
浏览文件 @
1bf26f7a
...
...
@@ -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
);
...
...
spec/javascripts/shortcuts_issuable_spec.js
浏览文件 @
1bf26f7a
...
...
@@ -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%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录