Scrapboxcursor
2020-12-24 04:56:17 Element.closest()使

import {cursor} from '/api/code/takker/scrapbox-cursor-position-2/script.js'
export function getLinkIncludingCursor(){
return cursor().charDOM?.closest('a.page-link') ?? undefined;
}

LinkObserver使

export function getLinkIncludingCursor(){
const cursor = document.getElementById('text-input');
const editor = document.getElementById('editor');
const cursorLine = editor.getElementsByClassName('cursor-line')[0];
if(!cursor || !editor || !cursorLine) return undefined;
//
const links = cursorLine.getElementsByClassName('page-link');
// cursorlink
for (const link of links) {
const startLeft = getLeftPosition(link.firstElementChild, cursorLine);// '['
const endLeft = getLeftPosition(link.lastElementChild, cursorLine);// ']'
const cursorLeft = parseInt(cursor.style.left);// cursor
if (startLeft <= cursorLeft && cursorLeft <= endLeft) return link;
}
return undefined;
}

firefox
//
function getLeftPosition(targetElement, cursorLine){
return targetElement.getBoundingClientRect().left
- cursorLine.getBoundingClientRect().left;
}

#2020-11-12 00:37:08