Skip to content

Commit

Permalink
🎨 undo redo ir
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 authored and stevapple committed Apr 8, 2020
1 parent fceaf47 commit 980e41a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/ts/ir/processKeydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
return false;
}

// 添加第一次记录 undo 的光标
if (event.key.indexOf("Arrow") === -1) {
vditor.irUndo.recordFirstWbr(vditor, event);
}

// 仅处理以下快捷键操作
if (event.key !== "Enter" && event.key !== "Tab" && event.key !== "Backspace" &&
!isCtrl(event) && event.key !== "Escape") {
Expand Down
7 changes: 0 additions & 7 deletions src/ts/undo/IRUndo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import DiffMatchPatch, {diff_match_patch, patch_obj} from "diff-match-patch";
import {processAfterRender} from "../ir/process";
import {disableToolbar} from "../toolbar/setToolbar";
import {enableToolbar} from "../toolbar/setToolbar";
import {isFirefox, isSafari} from "../util/compatibility";
import {scrollCenter} from "../util/editorCommenEvent";
import {setSelectionFocus} from "../util/selection";
Expand Down Expand Up @@ -102,11 +100,6 @@ class IRUndo {
if (this.hasUndo) {
this.redoStack = [];
this.hasUndo = false;
disableToolbar(vditor.toolbar.elements, ["redo"]);
}

if (this.undoStack.length > 1) {
enableToolbar(vditor.toolbar.elements, ["undo"]);
}
}

Expand Down
28 changes: 18 additions & 10 deletions src/ts/util/editorCommenEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,37 @@ export const hotkeyEvent = (vditor: IVditor, editorElement: HTMLElement) => {
}

// undo
if (!vditor.toolbar.elements.undo && matchHotKey("⌘-Z", event)) {
if (vditor.currentMode === "sv") {
if (matchHotKey("⌘-Z", event)) {
if (vditor.currentMode === "sv" && !vditor.toolbar.elements.undo) {
vditor.undo.undo(vditor);
} else if (vditor.currentMode === "wysiwyg") {
event.preventDefault();
return;
} else if (vditor.currentMode === "wysiwyg" && !vditor.toolbar.elements.undo) {
vditor.wysiwygUndo.undo(vditor);
event.preventDefault();
return;
} else if (vditor.currentMode === "ir") {
vditor.irUndo.undo(vditor);
event.preventDefault();
return;
}
event.preventDefault();
return;
}

// redo
if (!vditor.toolbar.elements.redo && matchHotKey("⌘-Y", event)) {
if (vditor.currentMode === "sv") {
if (matchHotKey("⌘-Y", event)) {
if (vditor.currentMode === "sv" && !vditor.toolbar.elements.redo) {
vditor.undo.redo(vditor);
} else if (vditor.currentMode === "wysiwyg") {
event.preventDefault();
return;
} else if (vditor.currentMode === "wysiwyg" && !vditor.toolbar.elements.redo) {
vditor.wysiwygUndo.redo(vditor);
event.preventDefault();
return;
} else if (vditor.currentMode === "ir") {
vditor.irUndo.redo(vditor);
event.preventDefault();
return;
}
event.preventDefault();
return;
}

// esc
Expand Down

0 comments on commit 980e41a

Please sign in to comment.