scrapbox-cursor-position-6
position()
で {char, line}
を取得する
char
: cursorの左側の文字のDOM
ないときは undefined
になる
line
: cursorがいる行のDOM
known issue
座標値が無限になってしまう
dependencies
script.jsimport {scrapboxDOM} from '../scrapbox-dom-accessor/script.js';
export function position({pos = 'right'} = {}) {
const {top, left} = scrapboxDOM.editor.getBoundingClientRect(); // 基準座標
const style = scrapboxDOM.cursor.style;
const cursor = {
top: parseInt(style.top),
left: parseInt(style.left),
height: parseInt(style.height),
};
const position = {
x: cursor.left + left + 1,
y: (cursor.top + top) + cursor.height / 2,
};
const targets = document.elementsFromPoint(position.x, position.y);
const char = targets.find(target => target.classList.contains('char-index'));
const line = targets.find(target => target.classList.contains('line'));
return {char, line};
};
test code
jsimport('/api/code/takker/scrapbox-cursor-position-6/test.js');
test.jsimport {scrapboxDOM} from '../scrapbox-dom-accessor/script.js';
import {position} from './script.js';
const observer = new MutationObserver(() => console.log(position()));
observer.observe(scrapboxDOM.cursor, {attributes: true});