V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wxaxiaoyao
V2EX  ›  程序员

codemirror TAB 缩进问题记录

  •  
  •   wxaxiaoyao · 2019-07-31 16:57:29 +08:00 · 987 次点击
    这是一个创建于 1728 天前的主题,其中的信息可能已经有所发展或是发生改变。

    codemirror 默认 TAB 键是支持缩进的, 但在没有选择文本时, 缩进是一个 Tab 键而不是 indexUnit 对应的空格数. 默认也不支持 Shift - Tab 往回缩进, 这个在排版文本是非常麻烦的. 为了解决这两个问题我们需要重新定义这两个快捷键的实现, 具体伪代码如下:

    { // 此对象为 codemirror 配置选项对象, 下面仅对选项的快捷键相关部分做说明
        extraKeys: {
            Tab: (cm) => {
                if (cm.somethingSelected()) {      // 存在文本选择
                    cm.indentSelection('add');    // 正向缩进文本
                } else {                    // 无文本选择  
                    //cm.indentLine(cm.getCursor().line, "add");  // 整行缩进 不符合预期
                    cm.replaceSelection(Array(cm.getOption("indentUnit") + 1).join(" "), "end", "+input");  // 光标处插入 indentUnit 个空格
                }   
            },  
            "Shift-Tab": (cm) => {              // 反向缩进   
                if (cm.somethingSelected()) {
                    cm.indentSelection('subtract');  // 反向缩进
                } else {
                    // cm.indentLine(cm.getCursor().line, "subtract");  // 直接缩进整行
                    const cursor = cm.getCursor();
                    cm.setCursor({line: cursor.line, ch: cursor.ch - 4});  // 光标回退 indexUnit 字符
                }   
                return ;
            },  
        }
    }
    

    原文链接 wxaxiaoyao.cn/article/53

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5793 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 02:55 · PVG 10:55 · LAX 19:55 · JFK 22:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.