dom-to-code
Version:
navigate to the code through the dom
35 lines (32 loc) • 1.07 kB
JavaScript
import { O as OPEN_CODE_API, D as DOM_ATTR } from './shared/dom-to-code.2c4d4e6b.mjs';
const requestService = (filePath) => {
const { origin } = window.location;
fetch(`${origin}${OPEN_CODE_API}?filePath=${filePath}`).catch((error) => {
console.error("dom-to-code: ", error);
});
};
function getFilePath(element) {
if (!element || !element.getAttribute)
return null;
if (element.getAttribute(DOM_ATTR))
return element.getAttribute(DOM_ATTR);
return getFilePath(element.parentNode);
}
function initDomToCode() {
let keyCode = "";
document.addEventListener("keydown", (e) => {
keyCode = e.key;
});
document.addEventListener("mousedown", (e) => {
if ((e.button === 1 || e.button === 2) && (keyCode === "Control" || keyCode === "Meta")) {
e.stopImmediatePropagation();
e.preventDefault();
e.stopPropagation();
console.log("dom-to-code: open editor.");
const filePath = getFilePath(e.target);
filePath && requestService(filePath);
keyCode = "";
}
}, false);
}
export { initDomToCode };