Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(docs): doc select all content and remove when has tables (#3800)
Ran Luo2ad811d324d9
diff --git a/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts b/packages/docs-ui/src/commands/commands/clipboard.inner.command.tsindex b13844bc9e..745f57bacf 100644--- a/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts+++ b/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts@@ -17,6 +17,7 @@import type {DocumentDataModel,ICommand,+ ICustomTable,IDocumentBody,IDocumentData,IMutationInfo,@@ -240,6 +241,19 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {},};+// TODO: WORKAROUND to fix https://github.com/dream-num/univer-pro/issues/2560.+function adjustSelectionByTable(selection: ITextRange, tables: ICustomTable[]): ITextRange {+ const { startOffset, endOffset } = selection;+ const endsWithTable = tables.some((t) => t.startIndex === endOffset);+ const newEndOffset = Math.max(startOffset, endsWithTable ? endOffset - 1 : endOffset);++ return {+ ...selection,+ endOffset: newEndOffset,+ collapsed: startOffset === newEndOffset,+ };+}+function getCutActionsFromTextRanges(selections: ITextRange[],docDataModel: DocumentDataModel,@@ -255,17 +269,14 @@ function getCutActionsFromTextRanges(return rawActions;}+ const { tables = [] } = originBody;+const memoryCursor = new MemoryCursor();memoryCursor.reset();for (let i = 0; i < selections.length; i++) {- const selection = selections[i];+ const selection = adjustSelectionByTable(selections[i], tables);const { startOffset, endOffset, collapsed } = selection;-- if (startOffset == null || endOffset == null) {- continue;- }-const len = startOffset - memoryCursor.cursor;if (collapsed) {@@ -274,7 +285,7 @@ function getCutActionsFromTextRanges(len,});} else {- textX.push(...BuildTextUtils.selection.delete([selection], originBody, memoryCursor.cursor, null, selections.length === 1));+ textX.push(...BuildTextUtils.selection.delete([selection], originBody, memoryCursor.cursor, null, false));}memoryCursor.reset();@@ -465,10 +476,9 @@ export interface IInnerCutCommandParams {selections?: ITextRange[];}-const INNER_CUT_COMMAND_ID = 'doc.command.inner-cut';-export const CutContentCommand: ICommand<IInnerCutCommandParams> = {- id: INNER_CUT_COMMAND_ID,+ id: 'doc.command.inner-cut',+type: CommandType.COMMAND,handler: async (accessor, params: IInnerCutCommandParams) => {@@ -477,6 +487,7 @@ export const CutContentCommand: ICommand<IInnerCutCommandParams> = {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.tsnew file mode 100644index 0000000000..cd1a0533d1--- /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<ISelectAllCommandParams> = {+ 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<DocumentDataModel>(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 - 5, // 4 is CELL_END, ROW_END, TABLE_END AND \n.+ rangeType: DOC_RANGE_TYPE.RECT,+ });++ offset = endIndex;+ }++ 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.tsdeleted file mode 100644index 7b5bef1592..0000000000--- 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<ISelectAllOperationParams> = {- 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.tsindex 6e5ec405b5..00c8e5a06a 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/commandsimport { DeleteCustomBlockCommand, DeleteLeftCommand, DeleteRightCommand, MergeTwoParagraphCommand } from './commands/commands/doc-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, DocTableIimport { 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.tsindex 465b9dc8e6..fe6c5e1795 100644--- a/packages/docs-ui/src/index.ts+++ b/packages/docs-ui/src/index.ts@@ -145,7 +145,7 @@ export { genTableSource, getEmptyTableCell, getEmptyTableRow, getTableColumn } fexport { 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';// #endregiondiff --git a/packages/docs-ui/src/shortcuts/cursor.shortcut.ts b/packages/docs-ui/src/shortcuts/cursor.shortcut.tsindex 0a5fd974da..f381e2407e 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.tsindex 8e1ea710d3..a498bcb34d 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';