Skip to content

Commit

Permalink
feat(common): handle enter event when editing text #WIK-15872 (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
huanhuanwa authored Jul 2, 2024
1 parent 21da2a0 commit 716e8f2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/late-jobs-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@plait/common': patch
'@plait/draw': patch
'@plait/mind': patch
---

handle enter event when editing text
4 changes: 2 additions & 2 deletions packages/common/src/text/text-manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class TextManage {
this.textComponentRef.update(props);
}

edit(callback?: () => void) {
edit(callback?: () => void, exitEdit?: (event: Event) => boolean) {
this.isEditing = true;
IS_TEXT_EDITABLE.set(this.board, true);
const props: Partial<TextProps> = {
Expand All @@ -127,7 +127,7 @@ export class TextManage {
if (event.isComposing) {
return;
}
if (event.key === 'Escape' || (event.key === 'Enter' && !event.shiftKey) || event.key === 'Tab') {
if (event.key === 'Escape' || event.key === 'Tab' || (exitEdit ? exitEdit(event) : false)) {
event.preventDefault();
event.stopPropagation();
exitCallback();
Expand Down
21 changes: 16 additions & 5 deletions packages/draw/src/plugins/with-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { editCell, getHitCell } from '../utils/table';
import { withTableResize } from './with-table-resize';
import { isVirtualKey, isDelete, isSpaceHotkey } from '@plait/common';
import { PlaitDrawElement } from '../interfaces';
import { getSelectedTableElements, isSingleSelectTable, setSelectedCells } from '../utils';
import { getSelectedCells, getSelectedTableElements, isSingleSelectTable, setSelectedCells } from '../utils';

export const withTable = (board: PlaitBoard) => {
const tableBoard = board as PlaitTableBoard;
Expand Down Expand Up @@ -68,12 +68,23 @@ export const withTable = (board: PlaitBoard) => {
const selectedElements = getSelectedElements(board);
const isSingleSelection = selectedElements.length === 1;
const targetElement = selectedElements[0];
if (!PlaitBoard.isReadonly(board) && !isVirtualKey(event) && !isDelete(event) && !isSpaceHotkey(event) && isSingleSelection) {
if (
!PlaitBoard.isReadonly(board) &&
!PlaitBoard.hasBeenTextEditing(tableBoard) &&
!isVirtualKey(event) &&
!isDelete(event) &&
!isSpaceHotkey(event) &&
isSingleSelection
) {
event.preventDefault();
if (PlaitDrawElement.isElementByTable(targetElement)) {
const firstTextCell = targetElement.cells.find(item => item.text && item.textHeight);
if (firstTextCell) {
editCell(firstTextCell);
const cells = getSelectedCells(targetElement);
let cell = targetElement.cells.find(item => item.text && item.textHeight);
if (cells?.length) {
cell = cells.find(item => item.text && item.textHeight);
}
if (cell) {
editCell(cell);
return;
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/mind/src/utils/node/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { getFirstTextManage } from '@plait/common';

export function editTopic(element: MindElement) {
const textManage = getFirstTextManage(element);
textManage?.edit(() => {});
textManage?.edit(
() => {},
event => {
const keyboardEvent = event as KeyboardEvent;
return keyboardEvent.key === 'Enter' && !keyboardEvent.shiftKey;
}
);
}

export const getSelectedMindElements = (board: PlaitBoard, elements?: PlaitElement[]) => {
Expand Down

0 comments on commit 716e8f2

Please sign in to comment.