Skip to content

Commit 352e72e

Browse files
committed
Improve link anchor wrapping
When URLs are wrapped in anchor tags, we reparent the text nodes from the DOM to the new anchor node in order to prevent CodeMirror from getting confused and hiding the cursor when it's inside the URL. Additionally, we apply the cm-string class to the anchor tag to keep the style the same. We also only apply the cm-string-link class to the surrounding span tag when links are not wrapped in anchor tags in order to avoid triggering the custom link behavior by accident. Finally, we put an event listener on the anchor tags that prevents the contextmenu event from bubbling to CodeMirror's handler in order to restore the expected behavior of context menus for links.
1 parent f63c6aa commit 352e72e

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)