levi

levi0001
关注
65354 号成员,2021-02-15 14:45:37 加入
309
个人主页 浏览
74h22m
在线时长
  • 请问定位光标到上一次的位置呢?

    2021-04-10 02:13

    我把源码中的 getEditorRange, setSelectionFocus 扒下来实现了

    const getEditorRange = (vditor):Range => {
        let range: Range;
        const element = vditor[vditor.currentMode].element;
        if (getSelection().rangeCount > 0) {
            range = getSelection().getRangeAt(0);
            if (element.isEqualNode(range.startContainer) || element.contains(range.startContainer)) {
                return range;
            }
        }
        if (vditor[vditor.currentMode].range) {
            return vditor[vditor.currentMode].range;
        }
        element.focus();
        range = element.ownerDocument.createRange();
        range.setStart(element, 0);
        range.collapse(true);
    
        return range;
    };
    
    const setSelectionFocus = (range: Range) => {
        const selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(range);
    }
    

    我为什么要扒下来?一开始我是采用导入的方式的

    import { getEditorRange } from 'vditor/src/ts/util/selection';
    

    引用 getEditorRange 后提示我找不到 VDITOR_VERSION,然后我引用如下

    import { VDITOR_VERSION } from 'vditor/src/ts/constants';
    import { getEditorRange } from 'vditor/src/ts/util/selection';
    

    还是报错。。。。无奈只好 copy 到方法中来了

    求更好的实现方法