generated at
scrapbox-cursor-position-4
Next version:
hr
scrapboxのcursorの縦棒の位置を取得する関数
ScrapVimなどで使う
返り値
ts
type Return = { left?: CharObject; // cursorの左側のDOM right?: CharObject; // cursorの右側のDOM line: LineObject; // cursorのいる行 };
cursorの縦棒の位置が特定できなかったら line 以外 undefined となる
旧版scrapbox-cursor-position-3からの変更点

dependencies
script.js
import {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が見えないときでも座標を取得できるようにした
Element.getBoundingClientRect()ではなく、.cursorのinline styleに指定されている top left を使う
この値は#editorElement.getBoundingClientRect() top left からの相対座標になっている
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)}; };

テストコードtakker
test.js
import {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); });

#2021-02-20 02:38:48
#2021-01-17 15:46:42