scrapbox-cursor-position-5
scrapboxのcursorの縦棒の位置を取得する関数
返り値
code:ts
type Return = {
left?: CharObject; // cursorの左側のDOM
right?: CharObject; // cursorの右側のDOM
line: LineObject; // cursorのいる行
};
cursorの縦棒の位置が特定できなかったらline以外undefinedとなる
dependencies
code:script.js
import {char as c} from '../scrapbox-char-accessor/script.js';
import {line as l} from '../scrapbox-line-accessor/script.js';
import {getBorder} from '../scrapbox-position-2/script.js';
import {scrapboxDOM} from '../scrapbox-dom-accessor/script.js';
export const cursor = () => {
const origin = scrapboxDOM.editor.getBoundingClientRect();
const x = origin.left + parseInt(scrapboxDOM.cursor.style.left);
const y = origin.top + parseInt(scrapboxDOM.cursor.style.top);
return getBorder({x, y});
};
test code
code:js
import('/api/code/takker/scrapbox-cursor-position-5/test1.js');
code:test1.js
import {cursor} from './script.js';
document.addEventListener('keydown', () => {
const c = cursor();
if (!c.left && !c.right) throw Error([test:scrapbox-cursor-position] Both char DOMs are undefined: ${c.message});
});