我想在macOS中模拟行结束快捷方式的行为。我有这个:
onKeyDown (event, change, next) {
if (event.key === 'ArrowRight') {
if (event.metaKey) {
event.preventDefault();
change.moveTo(5);
return true;
}
}
return next();
}问题是,现在偏移索引是固定的5在change.moveTo(5)上。
如何找到当前行的最后一个字符的索引?
发布于 2018-10-17 00:23:29
Slate并不真正了解线条之类的东西,因此解决方案是获得一个本地范围,并检查其边界框的坐标。
我创建了一个函数,返回行中最后一个可能的位置的索引,或者返回当前文本块中的最后一个位置的索引,如果插入符位于最后一行的话。
getIndexLastCharOfLine () {
const selection = window.getSelection()
const range = selection.getRangeAt(0);
const caretIndex = range.startOffset;
const rect = range.getBoundingClientRect();
const container = range.startContainer;
const lastIndex = container.length;
for (let i = caretIndex; i < lastIndex; i++) {
const rangeTest = document.createRange();
rangeTest.setStart(container, i);
const rectTest = rangeTest.getBoundingClientRect();
// if the y is different it means the test range is in a different line
if (rectTest.y !== rect.y) return i - 1;
}
return lastIndex;
}https://stackoverflow.com/questions/52842172
复制相似问题