美文网首页
CodeMirror 6将光标移动到行尾 2024-07-14

CodeMirror 6将光标移动到行尾 2024-07-14

作者: 齐格Insight | 来源:发表于2024-07-13 22:11 被阅读0次

背景

在Windows系统上有Ctrl-e这个快捷键,如果我们想要它的行为保持与Mac系统上一样,即:移动光标到行尾。

监测快捷键被按下事件

第一步,我们需要监测Ctrl-e这个快捷键被按下的事件,代码如下:

document.addEventListener('keydown', e => {
            // windows: Ctrl + e
            if (e.ctrlKey && e.key === 'e') {
                e.preventDefault()   // 阻止默认事件触发
                this.editor.focus()    // cm选种
                //  current select position from to, if not selected, use cursor
                const curPos = this.editor.state.selection;  // 当前选种文本的位置,如果没选种任何文本即光标位置
                const from = curPos.ranges && curPos.ranges[0].from
                const to = curPos.ranges && curPos.ranges[0].to
                moveCursorToEndOfLine(this.editor)
            } 
        });

移动光标处理

将光标移动到行尾,实现代码如下:

// codemirror 6, move cursor to end of current line
export function moveCursorToEndOfLine(editor: EditorView) {
    const lineAt = editor.state.doc.lineAt(editor.state.selection.main.head)
    editor.dispatch({ selection: { anchor: lineAt.to, head: lineAt.to } })
}

其中lineAt为当前行的信息,里面有三个字段,如下:

{
   from: 0 
   number:1
   to:75
   text: ''
}

from表示这行开始的位置,to表示这行结束的位置, number表示当前行号, text 表示当前行内容。

image.png

相关文章

  • 终端命令

    1、将光标移动到行首:ctrl + a 2、将光标移动到行尾:ctrl + e 3、清除屏幕: ...

  • 苹果电脑使用指南

    终端快捷键: 将光标移动到行首:control + a 将光标移动到行尾:control + e 清除屏幕:con...

  • Mac使用整理

    VIM 将光标移动到行首:ctrl + a 光标移动到行尾:ctrl + e 清除屏幕:ctrl + l 搜索以前...

  • linux

    输入 Ctrl + a:将光标移动到命令行首Ctrl + e:将光标移动到命令行尾 iterm2 command ...

  • 每天一个Linux 命令 - shell 快捷键

    shell 下快捷键 光标移动命令 Ctrl + a移动到行首 Ctrl + e移动到行尾 Ctrl + f向前移...

  • vim三种模式

    1、命令模式 光标移动光标移动到首行:shift+6 (T字母上的6,不要按小键盘的6)光标移动到行尾:shift...

  • Vim配置技巧以及编辑技巧

    配置vim 操作指令 光标移动行首和行尾^(Shift + 6)和$(Shift + 4)。 光标移动到行首0 插...

  • Linux 命令行常用快捷键

    将光标移动到行首 将光标移动到行尾 向左移动一个单词 向右移动一个单词 剪切命令行中光标所在处之前的所有字符(不包...

  • Bash

    快捷键 光标移动到行首ctrl+a 光标移动到行尾ctrl+e 光标向左移动一个单词alt+b 光标向右移动一个单...

  • VsCode快捷键

    光标类 选中多行alt + shift + 鼠标左键选中多行行尾选中要移动到行尾的行,alt + shift + i

网友评论

      本文标题:CodeMirror 6将光标移动到行尾 2024-07-14

      本文链接:https://www.haomeiwen.com/subject/ocvqhjtx.html