Skip to content

Commit

Permalink
feat(mind): allow undo once insert_node when first enter mind node ed…
Browse files Browse the repository at this point in the history
…iting
  • Loading branch information
pubuzhixing8 committed Aug 8, 2024
1 parent 11804a7 commit 423d5b7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/famous-parrots-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@plait/common': patch
'@plait/mind': patch
---

allow undo once insert_node when first enter mind node editing
29 changes: 18 additions & 11 deletions packages/common/src/text/text-manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class TextManage {
MERGING.set(this.board, true);
}, 0);

exitCallback?: () => void;

constructor(
private board: PlaitBoard,
private options: {
Expand Down Expand Up @@ -155,18 +157,22 @@ export class TextManage {
}
});
const exitCallback = () => {
this.updateRectangle();
mousedown$.unsubscribe();
keydown$.unsubscribe();
IS_TEXT_EDITABLE.set(this.board, false);
MERGING.set(this.board, false);
callback && callback();
const props = {
readonly: true
};
this.textComponentRef.update(props);
this.isEditing = false;
if (this.isEditing) {
this.updateRectangle();
mousedown$.unsubscribe();
keydown$.unsubscribe();
IS_TEXT_EDITABLE.set(this.board, false);
MERGING.set(this.board, false);
callback && callback();
const props = {
readonly: true
};
this.textComponentRef.update(props);
this.isEditing = false;
this.exitCallback = undefined;
}
};
this.exitCallback = exitCallback;
return exitCallback;
}

Expand All @@ -192,6 +198,7 @@ export class TextManage {
destroy() {
this.g?.remove();
this.textComponentRef?.destroy();
this.exitCallback && this.exitCallback();
}
}

Expand Down
21 changes: 18 additions & 3 deletions packages/mind/src/plugins/with-mind-hotkey.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { PlaitBoard, Transforms, getSelectedElements } from '@plait/core';
import { PlaitBoard, PlaitOperation, Transforms, getSelectedElements } from '@plait/core';
import { MindElement, PlaitMind } from '../interfaces';
import { AbstractNode } from '@plait/layouts';
import { MindTransforms } from '../transforms';
import { editTopic } from '../utils/node/common';
import { PlaitMindBoard } from './with-mind.board';
import { isSpaceHotkey, isExpandHotkey, isTabHotkey, isEnterHotkey, isVirtualKey, isDelete } from '@plait/common';
import { isSpaceHotkey, isExpandHotkey, isTabHotkey, isEnterHotkey, isVirtualKey, isDelete, getFirstTextManage } from '@plait/common';
import isHotkey from 'is-hotkey';

export const withMindHotkey = (baseBoard: PlaitBoard) => {
const board = baseBoard as PlaitBoard & PlaitMindBoard;
const { keyDown } = board;
const { keyDown, globalKeyDown } = board;

board.keyDown = (event: KeyboardEvent) => {
const selectedElements = getSelectedElements(board);
Expand Down Expand Up @@ -60,5 +61,19 @@ export const withMindHotkey = (baseBoard: PlaitBoard) => {
keyDown(event);
};

board.globalKeyDown = (event: KeyboardEvent) => {
if (PlaitBoard.isFocus(board) && PlaitBoard.hasBeenTextEditing(board)) {
if (isHotkey('mod+z', event)) {
const { history } = board;
const { undos } = history;
const previousOp = undos.length > 0 ? undos[undos.length - 1][0] : undefined;
if (previousOp && previousOp.type === 'insert_node' && MindElement.isMindElement(board, previousOp.node) && getFirstTextManage(previousOp.node).isEditing) {
board.undo();
}
}
}
globalKeyDown(event);
};

return board;
};

0 comments on commit 423d5b7

Please sign in to comment.