Skip to content

Commit f72ff5f

Browse files
authored
Merge pull request #204 from elyscape/improve-link-wrapping
Improve link anchor wrapping
2 parents f63c6aa + 352e72e commit f72ff5f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

extension/src/json-viewer/highlighter.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,26 @@ Highlighter.prototype = {
8989
if (text.match(URL_PATTERN) && self.clickableUrls()) {
9090
var decodedText = self.decodeText(text);
9191
elements.forEach(function(node) {
92-
node.classList.add("cm-string-link");
93-
node.setAttribute("data-url", decodedText);
9492
if (self.wrapLinkWithAnchorTag()) {
9593
var linkTag = document.createElement("a");
9694
linkTag.href = decodedText;
9795
linkTag.setAttribute('target', '_blank')
98-
linkTag.innerHTML = decodedText;
99-
node.innerHTML = "";
96+
linkTag.classList.add("cm-string");
97+
98+
// reparent the child nodes to preserve the cursor when editing
99+
node.childNodes.forEach(function(child) {
100+
linkTag.appendChild(child);
101+
});
102+
103+
// block CodeMirror's contextmenu handler
104+
linkTag.addEventListener("contextmenu", function(e) {
105+
if (e.bubbles) e.cancelBubble = true;
106+
});
107+
100108
node.appendChild(linkTag);
109+
} else {
110+
node.classList.add("cm-string-link");
111+
node.setAttribute("data-url", decodedText);
101112
}
102113
});
103114
}

0 commit comments

Comments
 (0)