scrapbox-cursor-position-4
Next version:
scrapboxのcursorの縦棒の位置を取得する関数
返り値
tstype Return = {
left?: CharObject; // cursorの左側のDOM
right?: CharObject; // cursorの右側のDOM
line: LineObject; // cursorのいる行
};
cursorの縦棒の位置が特定できなかったら line
以外 undefined
となる
dependencies
script.jsimport {char as c} from '/api/code/takker/scrapbox-char-accessor/script.js';
import {line as l} from '/api/code/takker/scrapbox-line-accessor/script.js';
import {getCharBorder} from '/api/code/takker/scrapbox-position/script.js';
import {scrapboxDOM} from '/api/code/takker/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);
2020-12-29
10:22:18 cursorが見えないときでも座標を取得できるようにした
script.js const {left, right, line, message} = getCharBorder({x, y});
if (message) return {line: l(line), message};
return {left: c(left), right: c(right), line: l(line)};
};
テストコード
test.jsimport {cursor} from '/api/code/takker/scrapbox-cursor-position-4/script.js';
document.addEventListener('keydown', () => {
const c = cursor();
if (!c.left && ! c.right) throw Error('[test:scrapbox-cursor-position-3] Both char DOMs are undefined.');
console.log('[test:scrapbox-cursor-position-3] %o', c);
});