summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/common_utils.js
diff options
context:
space:
mode:
authorFilipa Lacerda <filipa@gitlab.com>2018-11-21 10:35:54 +0000
committerFilipa Lacerda <filipa@gitlab.com>2018-11-21 10:35:54 +0000
commitd55650f045335f52ad833b5ba00990549bb032f5 (patch)
treedde3529f894604b7f0487ff5a83dd4bfb69b677a /app/assets/javascripts/lib/utils/common_utils.js
parent2133baaf9944da24859e54bb68d86f8cf6f89d28 (diff)
parent98fd36274a530423399a9a4a0920e48ab82147cd (diff)
downloadgitlab-ce-d55650f045335f52ad833b5ba00990549bb032f5.tar.gz
Merge branch 'master' into 54282-tooltip-stuck54282-tooltip-stuck
* master: (24 commits) Adds a PHILOSOPHY.md Externalize strings from `/app/views/abuse_reports` Fix Issuable container element selector for shortcuts handling Enable Rubocop on lib/gitlab Add user docs for Pages access control Added information about syntax highlighting in Web IDE. Fixes gitlab-org/gitlab-ce#53571 Align issue status label and confidential icon Externalize strings from `/app/views/repository_check_mailer` Remove instances of `@extend .monospace` Resolve "The reply shortcut can add any text of the page to the "comment" text area" Upgrade escape_utils to 1.2.1 Add dedicated runner tags to assets job Upgrade gitlab eslint config to latest Fix broken karma specs Disable conflicting eslint rules Prettify all the things Upgrade prettier to 1.15.2 Added a test for open merge request files Update CHANGELOG.md for 11.4.7 Open first 10 merge request files in IDE ...
Diffstat (limited to 'app/assets/javascripts/lib/utils/common_utils.js')
-rw-r--r--app/assets/javascripts/lib/utils/common_utils.js24
1 files changed, 21 insertions, 3 deletions
diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js
index e14fff7a610..3186ae9c133 100644
--- a/app/assets/javascripts/lib/utils/common_utils.js
+++ b/app/assets/javascripts/lib/utils/common_utils.js
@@ -226,7 +226,17 @@ export const getParameterByName = (name, urlToParse) => {
return decodeURIComponent(results[2].replace(/\+/g, ' '));
};
-const handleSelectedRange = range => {
+const handleSelectedRange = (range, restrictToNode) => {
+ // Make sure this range is within the restricting container
+ if (restrictToNode && !range.intersectsNode(restrictToNode)) return null;
+
+ // If only a part of the range is within the wanted container, we need to restrict the range to it
+ if (restrictToNode && !restrictToNode.contains(range.commonAncestorContainer)) {
+ if (!restrictToNode.contains(range.startContainer)) range.setStart(restrictToNode, 0);
+ if (!restrictToNode.contains(range.endContainer))
+ range.setEnd(restrictToNode, restrictToNode.childNodes.length);
+ }
+
const container = range.commonAncestorContainer;
// add context to fragment if needed
if (container.tagName === 'OL') {
@@ -237,14 +247,22 @@ const handleSelectedRange = range => {
return range.cloneContents();
};
-export const getSelectedFragment = () => {
+export const getSelectedFragment = restrictToNode => {
const selection = window.getSelection();
if (selection.rangeCount === 0) return null;
+ // Most usages of the selection only want text from a part of the page (e.g. discussion)
+ if (restrictToNode && !selection.containsNode(restrictToNode, true)) return null;
+
const documentFragment = document.createDocumentFragment();
+ documentFragment.originalNodes = [];
for (let i = 0; i < selection.rangeCount; i += 1) {
const range = selection.getRangeAt(i);
- documentFragment.appendChild(handleSelectedRange(range));
+ const handledRange = handleSelectedRange(range, restrictToNode);
+ if (handledRange) {
+ documentFragment.appendChild(handledRange);
+ documentFragment.originalNodes.push(range.commonAncestorContainer);
+ }
}
if (documentFragment.textContent.length === 0) return null;