Skip to content

Commit

Permalink
fix: doc select all content and remove when has tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Oct 19, 2024
1 parent 177c0cb commit 5c49f15
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 59 deletions.
82 changes: 82 additions & 0 deletions packages/docs-ui/src/commands/commands/doc-select-all.command.ts
Original file line number Diff line number Diff line change
@@ -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 - 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;
},
};
51 changes: 0 additions & 51 deletions packages/docs-ui/src/commands/operations/select-all.operation.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/docs-ui/src/docs-ui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -210,7 +210,7 @@ export class UniverDocsUIPlugin extends Plugin {
ReplaceSnapshotCommand,
CoverContentCommand,
SetDocZoomRatioCommand,
SelectAllOperation,
DocSelectAllCommand,
DocParagraphSettingPanelOperation,
MoveCursorOperation,
MoveSelectionOperation,
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion packages/docs-ui/src/shortcuts/core-editing.shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
6 changes: 3 additions & 3 deletions packages/docs-ui/src/shortcuts/cursor.shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)),
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/src/shortcuts/toolbar.shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 5c49f15

Please sign in to comment.