From 1f638f3f977f9ee3e4a0506dc86614ab590c64fd Mon Sep 17 00:00:00 2001 From: jocs Date: Sat, 19 Oct 2024 11:54:42 +0800 Subject: [PATCH 1/2] fix: doc select all content and remove when has tables --- .../commands/doc-select-all.command.ts | 82 +++++++++++++++++++ .../operations/select-all.operation.ts | 51 ------------ packages/docs-ui/src/docs-ui-plugin.ts | 4 +- packages/docs-ui/src/index.ts | 2 +- .../src/shortcuts/core-editing.shortcut.ts | 2 +- .../docs-ui/src/shortcuts/cursor.shortcut.ts | 6 +- .../docs-ui/src/shortcuts/toolbar.shortcut.ts | 2 +- 7 files changed, 90 insertions(+), 59 deletions(-) create mode 100644 packages/docs-ui/src/commands/commands/doc-select-all.command.ts delete mode 100644 packages/docs-ui/src/commands/operations/select-all.operation.ts diff --git a/packages/docs-ui/src/commands/commands/doc-select-all.command.ts b/packages/docs-ui/src/commands/commands/doc-select-all.command.ts new file mode 100644 index 00000000000..f50bc489583 --- /dev/null +++ b/packages/docs-ui/src/commands/commands/doc-select-all.command.ts @@ -0,0 +1,82 @@ +/** + * Copyright 2023-present DreamNum Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { DocumentDataModel, ICommand } from '@univerjs/core'; +import type { ISuccinctDocRangeParam } from '@univerjs/engine-render'; +import { CommandType, DOC_RANGE_TYPE, IUniverInstanceService, UniverInstanceType } from '@univerjs/core'; +import { DocSelectionManagerService } from '@univerjs/docs'; + +interface ISelectAllCommandParams { } + +export const DocSelectAllCommand: ICommand = { + id: 'doc.command.select-all', + type: CommandType.COMMAND, + handler: async (accessor) => { + const univerInstanceService = accessor.get(IUniverInstanceService); + const docSelectionManagerService = accessor.get(DocSelectionManagerService); + const docDataModel = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC); + const activeTextRange = docSelectionManagerService.getActiveTextRange(); + if (docDataModel == null || activeTextRange == null) { + return false; + } + + const { segmentId } = activeTextRange; + const unitId = docDataModel.getUnitId(); + const prevBody = docDataModel.getSelfOrHeaderFooterModel(segmentId).getSnapshot().body; + if (prevBody == null) { + return false; + } + + const { tables = [] } = prevBody; + const textRanges: ISuccinctDocRangeParam[] = []; + let offset = 0; + + for (const table of tables) { + const { startIndex, endIndex } = table; + if (offset !== startIndex) { + textRanges.push({ + startOffset: offset, + endOffset: startIndex - 1, + rangeType: DOC_RANGE_TYPE.TEXT, + }); + } + + // Push the rect range. + textRanges.push({ + startOffset: startIndex + 3, // 3 is TABLE_START, ROW_START, CELL_START. + endOffset: endIndex - 4, // 4 is CELL_END, ROW_END, TABLE_END AND \n. + rangeType: DOC_RANGE_TYPE.RECT, + }); + + offset = endIndex + 1; + } + + if (offset !== prevBody.dataStream.length - 2) { + textRanges.push({ + startOffset: offset, + endOffset: prevBody.dataStream.length - 2, + rangeType: DOC_RANGE_TYPE.TEXT, + }); + } + + docSelectionManagerService.replaceDocRanges(textRanges, { + unitId, + subUnitId: unitId, + }, false); + + return true; + }, +}; diff --git a/packages/docs-ui/src/commands/operations/select-all.operation.ts b/packages/docs-ui/src/commands/operations/select-all.operation.ts deleted file mode 100644 index 7b5bef1592e..00000000000 --- a/packages/docs-ui/src/commands/operations/select-all.operation.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright 2023-present DreamNum Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { CommandType, IUniverInstanceService } from '@univerjs/core'; -import { DocSelectionManagerService } from '@univerjs/docs'; -import type { ICommand } from '@univerjs/core'; - -interface ISelectAllOperationParams { } - -export const SelectAllOperation: ICommand = { - id: 'doc.operation.select-all', - type: CommandType.COMMAND, - handler: async (accessor) => { - const univerInstanceService = accessor.get(IUniverInstanceService); - const docSelectionManagerService = accessor.get(DocSelectionManagerService); - const docDataModel = univerInstanceService.getCurrentUniverDocInstance(); - const activeTextRange = docSelectionManagerService.getActiveTextRange(); - if (docDataModel == null || activeTextRange == null) { - return false; - } - - const { segmentId } = activeTextRange; - const prevBody = docDataModel.getSelfOrHeaderFooterModel(segmentId).getSnapshot().body; - if (prevBody == null) { - return false; - } - - const textRanges = [ - { - startOffset: 0, - endOffset: prevBody.dataStream.length - 2, - }, - ]; - - docSelectionManagerService.replaceTextRanges(textRanges, false); - return true; - }, -}; diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.ts index a56be9ea7af..e30558245a4 100644 --- a/packages/docs-ui/src/docs-ui-plugin.ts +++ b/packages/docs-ui/src/docs-ui-plugin.ts @@ -38,6 +38,7 @@ import { DeleteCommand, InsertCommand, UpdateCommand } from './commands/commands import { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, MergeTwoParagraphCommand } from './commands/commands/delete.command'; import { CloseHeaderFooterCommand } from './commands/commands/doc-header-footer.command'; import { DocParagraphSettingCommand } from './commands/commands/doc-paragraph-setting.command'; +import { DocSelectAllCommand } from './commands/commands/doc-select-all.command'; import { IMEInputCommand } from './commands/commands/ime-input.command'; import { ResetInlineFormatTextBackgroundColorCommand, SetInlineFormatBoldCommand, SetInlineFormatCommand, SetInlineFormatFontFamilyCommand, SetInlineFormatFontSizeCommand, SetInlineFormatItalicCommand, SetInlineFormatStrikethroughCommand, SetInlineFormatSubscriptCommand, SetInlineFormatSuperscriptCommand, SetInlineFormatTextBackgroundColorCommand, SetInlineFormatTextColorCommand, SetInlineFormatUnderlineCommand } from './commands/commands/inline-format.command'; import { BulletListCommand, ChangeListNestingLevelCommand, ChangeListTypeCommand, CheckListCommand, ListOperationCommand, OrderListCommand, QuickListCommand, ToggleCheckListCommand } from './commands/commands/list.command'; @@ -51,7 +52,6 @@ import { DocTableInsertColumnCommand, DocTableInsertColumnLeftCommand, DocTableI import { DocTableTabCommand } from './commands/commands/table/doc-table-tab.command'; import { MoveCursorOperation, MoveSelectionOperation } from './commands/operations/doc-cursor.operation'; import { DocParagraphSettingPanelOperation } from './commands/operations/doc-paragraph-setting-panel.operation'; -import { SelectAllOperation } from './commands/operations/select-all.operation'; import { SetDocZoomRatioOperation } from './commands/operations/set-doc-zoom-ratio.operation'; import { AppUIController } from './controllers'; import { defaultPluginConfig, PLUGIN_CONFIG_KEY } from './controllers/config.schema'; @@ -211,7 +211,7 @@ export class UniverDocsUIPlugin extends Plugin { ReplaceSnapshotCommand, CoverContentCommand, SetDocZoomRatioCommand, - SelectAllOperation, + DocSelectAllCommand, DocParagraphSettingPanelOperation, MoveCursorOperation, MoveSelectionOperation, diff --git a/packages/docs-ui/src/index.ts b/packages/docs-ui/src/index.ts index 0568e12cd72..c33cc9cae69 100644 --- a/packages/docs-ui/src/index.ts +++ b/packages/docs-ui/src/index.ts @@ -147,7 +147,7 @@ export { genTableSource, getEmptyTableCell, getEmptyTableRow, getTableColumn } f export { DocCreateTableOperation } from './commands/operations/doc-create-table.operation'; export { MoveSelectionOperation } from './commands/operations/doc-cursor.operation'; export { MoveCursorOperation } from './commands/operations/doc-cursor.operation'; -export { SelectAllOperation } from './commands/operations/select-all.operation'; +export { DocSelectAllCommand } from './commands/commands/doc-select-all.command'; export { type ISetDocZoomRatioOperationParams, SetDocZoomRatioOperation } from './commands/operations/set-doc-zoom-ratio.operation'; // #endregion diff --git a/packages/docs-ui/src/shortcuts/core-editing.shortcut.ts b/packages/docs-ui/src/shortcuts/core-editing.shortcut.ts index 202fa87cc4b..79efddc84c4 100644 --- a/packages/docs-ui/src/shortcuts/core-editing.shortcut.ts +++ b/packages/docs-ui/src/shortcuts/core-editing.shortcut.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import { KeyCode } from '@univerjs/ui'; import type { IShortcutItem } from '@univerjs/ui'; +import { KeyCode } from '@univerjs/ui'; import { EnterCommand } from '../commands/commands/auto-format.command'; import { DeleteLeftCommand, DeleteRightCommand } from '../commands/commands/delete.command'; import { whenDocAndEditorFocused, whenDocAndEditorFocusedWithBreakLine } from './utils'; diff --git a/packages/docs-ui/src/shortcuts/cursor.shortcut.ts b/packages/docs-ui/src/shortcuts/cursor.shortcut.ts index 0a5fd974da2..f381e2407e9 100644 --- a/packages/docs-ui/src/shortcuts/cursor.shortcut.ts +++ b/packages/docs-ui/src/shortcuts/cursor.shortcut.ts @@ -14,11 +14,11 @@ * limitations under the License. */ +import type { IShortcutItem } from '@univerjs/ui'; import { Direction, EDITOR_ACTIVATED, FOCUSING_DOC, FOCUSING_UNIVER_EDITOR } from '@univerjs/core'; import { KeyCode, MetaKeys } from '@univerjs/ui'; -import type { IShortcutItem } from '@univerjs/ui'; +import { DocSelectAllCommand } from '../commands/commands/doc-select-all.command'; import { MoveCursorOperation, MoveSelectionOperation } from '../commands/operations/doc-cursor.operation'; -import { SelectAllOperation } from '../commands/operations/select-all.operation'; import { whenDocAndEditorFocused } from './utils'; export const MoveCursorUpShortcut: IShortcutItem = { @@ -94,7 +94,7 @@ export const MoveSelectionRightShortcut: IShortcutItem = { }; export const SelectAllShortcut: IShortcutItem = { - id: SelectAllOperation.id, + id: DocSelectAllCommand.id, binding: KeyCode.A | MetaKeys.CTRL_COMMAND, preconditions: (contextService) => contextService.getContextValue(FOCUSING_UNIVER_EDITOR) && (contextService.getContextValue(FOCUSING_DOC) || contextService.getContextValue(EDITOR_ACTIVATED)), diff --git a/packages/docs-ui/src/shortcuts/toolbar.shortcut.ts b/packages/docs-ui/src/shortcuts/toolbar.shortcut.ts index 8e1ea710d37..a498bcb34d3 100644 --- a/packages/docs-ui/src/shortcuts/toolbar.shortcut.ts +++ b/packages/docs-ui/src/shortcuts/toolbar.shortcut.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import { KeyCode, MetaKeys } from '@univerjs/ui'; import type { IShortcutItem } from '@univerjs/ui'; +import { KeyCode, MetaKeys } from '@univerjs/ui'; import { SetInlineFormatBoldCommand, SetInlineFormatItalicCommand, SetInlineFormatStrikethroughCommand, SetInlineFormatSubscriptCommand, SetInlineFormatSuperscriptCommand, SetInlineFormatUnderlineCommand } from '../commands/commands/inline-format.command'; import { BulletListCommand, OrderListCommand } from '../commands/commands/list.command'; import { AlignCenterCommand, AlignJustifyCommand, AlignLeftCommand, AlignRightCommand } from '../commands/commands/paragraph-align.command'; From 447b33dd016edd3e5dfb0478b1c48454a756b702 Mon Sep 17 00:00:00 2001 From: jocs Date: Fri, 25 Oct 2024 12:35:31 +0800 Subject: [PATCH 2/2] fix: select all --- .../src/commands/commands/clipboard.inner.command.ts | 6 +++--- .../docs-ui/src/commands/commands/doc-select-all.command.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts b/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts index 7c04c8270cc..4a31d323b70 100644 --- a/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts +++ b/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts @@ -459,10 +459,9 @@ export interface IInnerCutCommandParams { selections?: ITextRange[]; } -const INNER_CUT_COMMAND_ID = 'doc.command.inner-cut'; - export const CutContentCommand: ICommand = { - id: INNER_CUT_COMMAND_ID, + id: 'doc.command.inner-cut', + type: CommandType.COMMAND, handler: async (accessor, params: IInnerCutCommandParams) => { @@ -471,6 +470,7 @@ export const CutContentCommand: ICommand = { const docSelectionManagerService = accessor.get(DocSelectionManagerService); const univerInstanceService = accessor.get(IUniverInstanceService); const selections = params.selections ?? docSelectionManagerService.getTextRanges(); + const rectRanges = docSelectionManagerService.getRectRanges(); if ( diff --git a/packages/docs-ui/src/commands/commands/doc-select-all.command.ts b/packages/docs-ui/src/commands/commands/doc-select-all.command.ts index f50bc489583..cd1a0533d18 100644 --- a/packages/docs-ui/src/commands/commands/doc-select-all.command.ts +++ b/packages/docs-ui/src/commands/commands/doc-select-all.command.ts @@ -57,11 +57,11 @@ export const DocSelectAllCommand: ICommand = { // Push the rect range. textRanges.push({ startOffset: startIndex + 3, // 3 is TABLE_START, ROW_START, CELL_START. - endOffset: endIndex - 4, // 4 is CELL_END, ROW_END, TABLE_END AND \n. + endOffset: endIndex - 5, // 4 is CELL_END, ROW_END, TABLE_END AND \n. rangeType: DOC_RANGE_TYPE.RECT, }); - offset = endIndex + 1; + offset = endIndex; } if (offset !== prevBody.dataStream.length - 2) {