1
0
Van 6 жил өмнө
parent
commit
9f83a85485
2 өөрчлөгдсөн 43 нэмэгдсэн , 9 устгасан
  1. 2 1
      CHANGELOG.md
  2. 41 8
      src/ts/hotkey/index.ts

+ 2 - 1
CHANGELOG.md

@@ -7,14 +7,15 @@
 
 ### TODO
 
-* [98](https://github.com/b3log/vditor/issues/98) 支持 shift + tab `feature`
 * [86](https://github.com/b3log/vditor/issues/86) 代码分包优化 `feature`
 
 ### v1.7.11 / 2019-08-22
 
 * [103](https://github.com/b3log/vditor/issues/103) 光标位置应在正中间 `feature`
+* [102](https://github.com/b3log/vditor/issues/102) 安装依赖后自动删除已有的依赖 `question`
 * [101](https://github.com/b3log/vditor/issues/101) video 标签移动端溢出 `enhancement`
 * [100](https://github.com/b3log/vditor/issues/100) esc/选中工具栏中的表情或标题后输入框中的 at 及 emoji 的提示应消失 `bug`
+* [98](https://github.com/b3log/vditor/issues/98) 支持 shift + tab `feature`
 * [99](https://github.com/b3log/vditor/issues/99) esc/选中工具栏中的表情或标题后输入框中的 at 及 emoji 的提示应消失 `bug`
 * [97](https://github.com/b3log/vditor/issues/97) 添加五线谱支持 `feature`
 * [96](https://github.com/b3log/vditor/issues/96) 工具栏没有配置 preview, both, redo, undo 在其他操作时会报错 `bug`

+ 41 - 8
src/ts/hotkey/index.ts

@@ -64,16 +64,49 @@ export class Hotkey {
             }
 
             if (this.vditor.options.tab && event.key.toLowerCase() === "tab") {
-                const selectionValue = getSelectText(this.vditor.editor.element);
-                const selectionResult = selectionValue.split("\n").map((value) => {
-                    return this.vditor.options.tab + value;
-                }).join("\n");
-                const position = getSelectPosition(this.vditor.editor.element);
-                insertText(this.vditor, selectionResult, "", true);
-                setSelectionByPosition(position.start,
-                    position.start + selectionResult.length, this.vditor.editor.element);
                 event.preventDefault();
                 event.stopPropagation();
+
+                const position = getSelectPosition(this.vditor.editor.element);
+                const text = getText(this.vditor.editor.element)
+                const selectLinePosition = getCurrentLinePosition(position, text)
+                const selectLineList = text.substring(selectLinePosition.start, selectLinePosition.end - 1).split("\n");
+
+                if (event.shiftKey) {
+                    let shiftCount = 0
+                    let startIsShift = false
+                    const selectionShiftResult = selectLineList.map((value, index) => {
+                        let shiftLineValue = value
+                        if (value.indexOf(this.vditor.options.tab) === 0) {
+                            if (index === 0) {
+                                startIsShift = true
+                            }
+                            shiftCount++
+                            shiftLineValue = value.replace(this.vditor.options.tab, '')
+                        }
+                        return shiftLineValue;
+                    }).join("\n");
+
+                    formatRender(this.vditor, text.substring(0, selectLinePosition.start) + selectionShiftResult + text.substring(selectLinePosition.end - 1),
+                        {
+                            end: position.end - shiftCount * this.vditor.options.tab.length,
+                            start: position.start - (startIsShift ? this.vditor.options.tab.length : 0),
+                        })
+                    return
+                }
+
+                if (position.start === position.end) {
+                    insertText(this.vditor, this.vditor.options.tab, "");
+                    return
+                }
+                const selectionResult = selectLineList.map((value) => {
+                    return this.vditor.options.tab + value;
+                }).join("\n");
+                formatRender(this.vditor, text.substring(0, selectLinePosition.start) + selectionResult + text.substring(selectLinePosition.end - 1),
+                    {
+                        end: position.end + selectLineList.length * this.vditor.options.tab.length,
+                        start: position.start + this.vditor.options.tab.length,
+                    })
                 return;
             }