Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Slate.js将光标移动到当前行的末尾?_Javascript_Slatejs - Fatal编程技术网

Javascript 如何使用Slate.js将光标移动到当前行的末尾?

Javascript 如何使用Slate.js将光标移动到当前行的末尾?,javascript,slatejs,Javascript,Slatejs,我想模仿macOS中的行尾快捷方式行为。我有这个: onKeyDown (event, change, next) { if (event.key === 'ArrowRight') { if (event.metaKey) { event.preventDefault(); change.moveTo(5); return true; } } return nex

我想模仿macOS中的行尾快捷方式行为。我有这个:

onKeyDown (event, change, next) {

    if (event.key === 'ArrowRight') {
        if (event.metaKey) {
            event.preventDefault();
            change.moveTo(5);
            return true;
        }
    }

    return next();
}
问题是,现在偏移索引在
change.moveTo(5)
上是固定的
5


如何找到当前行的最后一个字符的索引?

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;
}
getIndexLastCharOfLine(){
const selection=window.getSelection()
const range=selection.getRangeAt(0);
const caretIndex=range.startOffset;
const rect=range.getBoundingClientRect();
常量容器=range.startContainer;
const lastIndex=container.length;
for(设i=caretIndex;i
你能告诉我怎么走到这一行的开头吗?