From 716e8f23fbfd6394b917b1e35ff3cb34d4d848dc Mon Sep 17 00:00:00 2001 From: huanhuanwa <44698191+huanhuanwa@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:42:39 +0800 Subject: [PATCH] feat(common): handle enter event when editing text #WIK-15872 (#935) --- .changeset/late-jobs-hide.md | 7 +++++++ packages/common/src/text/text-manage.ts | 4 ++-- packages/draw/src/plugins/with-table.ts | 21 ++++++++++++++++----- packages/mind/src/utils/node/common.ts | 8 +++++++- 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 .changeset/late-jobs-hide.md diff --git a/.changeset/late-jobs-hide.md b/.changeset/late-jobs-hide.md new file mode 100644 index 000000000..e18ed1485 --- /dev/null +++ b/.changeset/late-jobs-hide.md @@ -0,0 +1,7 @@ +--- +'@plait/common': patch +'@plait/draw': patch +'@plait/mind': patch +--- + +handle enter event when editing text diff --git a/packages/common/src/text/text-manage.ts b/packages/common/src/text/text-manage.ts index 937677bfb..d1ab6a2cc 100644 --- a/packages/common/src/text/text-manage.ts +++ b/packages/common/src/text/text-manage.ts @@ -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 = { @@ -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(); diff --git a/packages/draw/src/plugins/with-table.ts b/packages/draw/src/plugins/with-table.ts index 95d3b2263..eaffcc553 100644 --- a/packages/draw/src/plugins/with-table.ts +++ b/packages/draw/src/plugins/with-table.ts @@ -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; @@ -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; } } diff --git a/packages/mind/src/utils/node/common.ts b/packages/mind/src/utils/node/common.ts index d6e2044b6..5858f72cf 100644 --- a/packages/mind/src/utils/node/common.ts +++ b/packages/mind/src/utils/node/common.ts @@ -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[]) => {