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
feat(docs): tab in table (#3064)
Ran Luo39ce1556efe4
diff --git a/packages/docs-ui/src/commands/commands/doc-tab.command.ts b/packages/docs-ui/src/commands/commands/doc-tab.command.tsdeleted file mode 100644index c21b014b63..0000000000--- a/packages/docs-ui/src/commands/commands/doc-tab.command.ts+++ /dev/null@@ -1,69 +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, type ICommand, ICommandService } from '@univerjs/core';-import { DocTableShiftTabCommand, DocTableTabCommand, TextSelectionManagerService } from '@univerjs/docs';-import { isInSameTableCell } from '@univerjs/engine-render';--export interface IDocTabCommandParams { }--export const DocTabCommand: ICommand<IDocTabCommandParams> = {- id: 'doc.tab.tab-in-doc',- type: CommandType.COMMAND,- handler: async (accessor) => {- const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const commandService = accessor.get(ICommandService);-- const activeTextRange = textSelectionManagerService.getActiveTextRangeWithStyle();-- if (activeTextRange == null) {- return false;- }-- const { startNodePosition, endNodePosition } = activeTextRange;-- if (startNodePosition && endNodePosition && isInSameTableCell(startNodePosition, endNodePosition)) {- return commandService.executeCommand(DocTableTabCommand.id);- }-- return false;- },-};--export interface IDocShiftTabCommandParams { }--export const DocShiftTabCommand: ICommand<IDocShiftTabCommandParams> = {- id: 'doc.tab.shift-tab-in-doc',- type: CommandType.COMMAND,- handler: async (accessor) => {- const textSelectionManagerService = accessor.get(TextSelectionManagerService);- const commandService = accessor.get(ICommandService);-- const activeTextRange = textSelectionManagerService.getActiveTextRangeWithStyle();-- if (activeTextRange == null) {- return false;- }-- const { startNodePosition, endNodePosition } = activeTextRange;-- if (startNodePosition && endNodePosition && isInSameTableCell(startNodePosition, endNodePosition)) {- return commandService.executeCommand(DocTableShiftTabCommand.id);- }-- return false;- },-};diff --git a/packages/docs-ui/src/controllers/doc-auto-format.controller.ts b/packages/docs-ui/src/controllers/doc-auto-format.controller.tsindex d79f3648da..babbda3012 100644--- a/packages/docs-ui/src/controllers/doc-auto-format.controller.ts+++ b/packages/docs-ui/src/controllers/doc-auto-format.controller.ts@@ -16,7 +16,8 @@import { Disposable, Inject, LifecycleStages, OnLifecycle, QuickListTypeMap } from '@univerjs/core';import type { ITabCommandParams } from '@univerjs/docs';-import { AfterSpaceCommand, BreakLineCommand, ChangeListNestingLevelCommand, ChangeListNestingLevelType, DocAutoFormatService, EnterCommand, ListOperationCommand, QuickListCommand, TabCommand } from '@univerjs/docs';+import { AfterSpaceCommand, BreakLineCommand, ChangeListNestingLevelCommand, ChangeListNestingLevelType, DocAutoFormatService, DocTableTabCommand, EnterCommand, ListOperationCommand, QuickListCommand, TabCommand } from '@univerjs/docs';+import { isInSameTableCell } from '@univerjs/engine-render';import type { Nullable } from 'vitest';@OnLifecycle(LifecycleStages.Rendered, DocAutoFormatController)@@ -49,7 +50,7 @@ export class DocAutoFormatController extends Disposable {return false;}return true;- } else if (paragraphs.length > 1) {+ } else if (paragraphs.length > 1 && paragraphs.some((p) => p.bullet)) {return true;}return false;@@ -64,6 +65,39 @@ export class DocAutoFormatController extends Disposable {},}];},+ priority: 100,+ })+ );++ this.disposeWithMe(+ this._docAutoFormatService.registerAutoFormat({+ id: TabCommand.id,+ match: (context) => {+ const { selection } = context;++ const { startNodePosition, endNodePosition } = selection;++ if (startNodePosition && endNodePosition && isInSameTableCell(startNodePosition, endNodePosition)) {+ return true;+ }++ if (startNodePosition && !endNodePosition && startNodePosition.path.indexOf('cells') > -1) {+ return true;+ }++ return false;+ },++ getMutations(context) {+ const params = context.commandParams as Nullable<ITabCommandParams>;+ return [{+ id: DocTableTabCommand.id,+ params: {+ shift: !!params?.shift,+ },+ }];+ },+ priority: 99,}));}diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.tsindex caa0329137..35666fed25 100644--- a/packages/docs-ui/src/docs-ui-plugin.ts+++ b/packages/docs-ui/src/docs-ui-plugin.ts@@ -68,7 +68,6 @@ import { DocParagraphSettingController } from './controllers/doc-paragraph-settiimport { DocParagraphSettingPanelOperation } from './commands/operations/doc-paragraph-setting-panel.operation';import { DocParagraphSettingCommand } from './commands/commands/doc-paragraph-setting.command';import { DocTableController } from './controllers/doc-table.controller';-import { DocShiftTabCommand, DocTabCommand } from './commands/commands/doc-tab.command';export class UniverDocsUIPlugin extends Plugin {static override pluginName = DOC_UI_PLUGIN_NAME;@@ -87,7 +86,6 @@ export class UniverDocsUIPlugin extends Plugin {this._initDependencies(_injector);this._initializeShortcut();this._initCommand();- this._registerCommands();}override onReady(): void {@@ -128,13 +126,6 @@ export class UniverDocsUIPlugin extends Plugin {});}- private _registerCommands() {- [- DocTabCommand,- DocShiftTabCommand,- ].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));- }-private _initDependencies(injector: Injector) {const dependencies: Dependency[] = [[DocUIController, { useFactory: () => this._injector.createInstance(DocUIController, this._config) }],diff --git a/packages/docs/src/basics/table.ts b/packages/docs/src/basics/table.tsindex 0051988f2f..317c350305 100644--- a/packages/docs/src/basics/table.ts+++ b/packages/docs/src/basics/table.ts@@ -118,10 +118,6 @@ export function findAboveCell(cell: IDocumentSkeletonPage) {return aboveRow.cells[col];}-export function findNextCell(cell: IDocumentSkeletonPage) {}--export function findPrevCell(cell: IDocumentSkeletonPage) {}-export function findTableBeforeLine(line: IDocumentSkeletonLine, page: IDocumentSkeletonPage) {const { st } = line;const { skeTables } = page;diff --git a/packages/docs/src/commands/commands/auto-format.command.ts b/packages/docs/src/commands/commands/auto-format.command.tsindex 8753ab9fa8..45b683e34d 100644--- a/packages/docs/src/commands/commands/auto-format.command.ts+++ b/packages/docs/src/commands/commands/auto-format.command.ts@@ -29,6 +29,7 @@ export const TabCommand: ICommand<ITabCommandParams> = {async handler(accessor, params) {const autoFormatService = accessor.get(DocAutoFormatService);const mutations = autoFormatService.onAutoFormat(TabCommand.id, params);+return (await sequenceExecuteAsync(mutations, accessor.get(ICommandService))).result;},};diff --git a/packages/docs/src/commands/commands/table/doc-table-tab.command.ts b/packages/docs/src/commands/commands/table/doc-table-tab.command.tsindex 96a7dbf6e5..fde716ac2d 100644--- a/packages/docs/src/commands/commands/table/doc-table-tab.command.ts+++ b/packages/docs/src/commands/commands/table/doc-table-tab.command.ts@@ -14,29 +14,75 @@* limitations under the License.*/-import type { ICommand } from '@univerjs/core';-import { CommandType } from '@univerjs/core';+import type { ICommand, Nullable } from '@univerjs/core';+import { CommandType, ICommandService, IUniverInstanceService } from '@univerjs/core';+import { TextSelectionManagerService } from '../../../services/text-selection-manager.service';+import { getCommandSkeleton } from '../../util';+import type { IOffsets } from './table';+import { CellPosition, getCellOffsets, INSERT_ROW_POSITION } from './table';+import { DocTableInsertRowCommand } from './doc-table-insert.command';-export interface IDocTableTabCommandParams {}+export interface IDocTableTabCommandParams {+ shift: boolean;+}export const DocTableTabCommand: ICommand<IDocTableTabCommandParams> = {id: 'doc.table.tab-in-table',type: CommandType.COMMAND,+ handler: async (accessor, params: IDocTableTabCommandParams) => {+ const { shift } = params;+ const textSelectionManager = accessor.get(TextSelectionManagerService);+ const activeTextRange = textSelectionManager.getActiveTextRangeWithStyle();+ const commandService = accessor.get(ICommandService);+ const univerInstanceService = accessor.get(IUniverInstanceService);- handler: async () => {- // TODO: implement tab in table- return true;- },-};+ const docDataModel = univerInstanceService.getCurrentUniverDocInstance();+ if (!docDataModel) {+ return false;+ }-export interface IDocTableShiftTabCommandParams {}+ const unitId = docDataModel.getUnitId();+ const docSkeletonManagerService = getCommandSkeleton(accessor, unitId);+ const skeleton = docSkeletonManagerService?.getSkeleton();+ const viewModel = skeleton?.getViewModel().getSelfOrHeaderFooterViewModel(activeTextRange?.segmentId);-export const DocTableShiftTabCommand: ICommand<IDocTableShiftTabCommandParams> = {- id: 'doc.table.shift-tab-in-table',- type: CommandType.COMMAND,+ if (viewModel == null) {+ return false;+ }++ if (activeTextRange == null) {+ return false;+ }++ let offsets: Nullable<IOffsets> = null;++ if (shift) {+ offsets = getCellOffsets(viewModel, activeTextRange, CellPosition.PREV);+ } else {+ offsets = getCellOffsets(viewModel, activeTextRange, CellPosition.NEXT);+ }++ if (offsets) {+ const { startOffset, endOffset } = offsets;++ const textRanges = [{+ startOffset,+ endOffset,+ }];++ textSelectionManager.replaceTextRanges(textRanges);++ return true;+ }++ if (shift === false) {+ const result = await commandService.executeCommand(DocTableInsertRowCommand.id, {+ position: INSERT_ROW_POSITION.BELLOW,+ });++ return result;+ }- handler: async () => {- // TODO: implement tab in tablereturn true;},};diff --git a/packages/docs/src/commands/commands/table/table.ts b/packages/docs/src/commands/commands/table/table.tsindex 40cb49b762..e59c82853d 100644--- a/packages/docs/src/commands/commands/table/table.ts+++ b/packages/docs/src/commands/commands/table/table.ts@@ -17,6 +17,7 @@import type { IParagraph, ISectionBreak, ITable, ITableCell, ITableColumn, ITableRow, Nullable } from '@univerjs/core';import { DataStreamTreeTokenType, generateRandomId, ObjectRelativeFromH, ObjectRelativeFromV, TableAlignmentType, TableCellHeightRule, TableSizeType, TableTextWrapType, Tools } from '@univerjs/core';import type { DataStreamTreeNode, DocumentViewModel, RectRange, TextRange } from '@univerjs/engine-render';+import type { ITextActiveRange } from '../../../services/text-selection-manager.service';export enum INSERT_ROW_POSITION {ABOVE,@@ -689,3 +690,96 @@ export function getDeleteRowContentActionParams(rangeInfo: IRangeInfo, viewModelrowCount: table.children.length,};}++export interface IOffsets {+ startOffset: number;+ endOffset: number;+}++export enum CellPosition {+ NEXT,+ PREV,+}++// eslint-disable-next-line complexity, max-lines-per-function+export function getCellOffsets(viewModel: DocumentViewModel, range: ITextActiveRange, position: CellPosition): Nullable<IOffsets> {+ const { startOffset } = range;++ let targetTable = null;++ for (const section of viewModel.children) {+ for (const paragraph of section.children) {+ const table = paragraph.children[0];+ if (table) {+ if (startOffset > table.startIndex && startOffset < table.endIndex) {+ targetTable = table;+ break;+ }+ }+ }++ if (targetTable) {+ break;+ }+ }++ if (targetTable == null) {+ return null;+ }++ let cellIndex = -1;+ let rowIndex = -1;+ let targetRow = null;++ for (const row of targetTable.children) {+ for (const cell of row.children) {+ if (startOffset > cell.startIndex && startOffset < cell.endIndex) {+ cellIndex = row.children.indexOf(cell);+ rowIndex = targetTable.children.indexOf(row);+ targetRow = row;+ break;+ }+ }++ if (cellIndex > -1) {+ break;+ }+ }++ if (cellIndex === -1 || rowIndex === -1 || targetRow == null) {+ return null;+ }++ let newCell = null;++ if (position === CellPosition.NEXT) {+ newCell = targetRow.children[cellIndex + 1];++ if (!newCell) {+ const nextRow = targetTable.children[rowIndex + 1];++ if (nextRow) {+ newCell = nextRow.children[0];+ }+ }+ } else {+ newCell = targetRow.children[cellIndex - 1];++ if (!newCell) {+ const prevRow = targetTable.children[rowIndex - 1];++ if (prevRow) {+ newCell = prevRow.children[prevRow.children.length - 1];+ }+ }+ }++ if (newCell) {+ const { startIndex, endIndex } = newCell;++ return {+ startOffset: startIndex + 1,+ endOffset: endIndex - 2,+ };+ }+}diff --git a/packages/docs/src/doc-plugin.ts b/packages/docs/src/doc-plugin.tsindex a9b8b4fff2..774467fe39 100644--- a/packages/docs/src/doc-plugin.ts+++ b/packages/docs/src/doc-plugin.ts@@ -64,7 +64,7 @@ import { AfterSpaceCommand, EnterCommand, TabCommand } from './commands/commandsimport { CreateDocTableCommand } from './commands/commands/table/doc-table-create.command';import { DocTableDeleteColumnsCommand, DocTableDeleteRowsCommand, DocTableDeleteTableCommand } from './commands/commands/table/doc-table-delete.command';import { DocTableInsertColumnCommand, DocTableInsertColumnLeftCommand, DocTableInsertColumnRightCommand, DocTableInsertRowAboveCommand, DocTableInsertRowBellowCommand, DocTableInsertRowCommand } from './commands/commands/table/doc-table-insert.command';-import { DocTableShiftTabCommand, DocTableTabCommand } from './commands/commands/table/doc-table-tab.command';+import { DocTableTabCommand } from './commands/commands/table/doc-table-tab.command';export interface IUniverDocsConfig {hasScroll?: boolean;@@ -138,7 +138,6 @@ export class UniverDocsPlugin extends Plugin {DocTableDeleteColumnsCommand,DocTableDeleteTableCommand,DocTableTabCommand,- DocTableShiftTabCommand,DocsRenameMutation,TabCommand,AfterSpaceCommand,diff --git a/packages/docs/src/index.ts b/packages/docs/src/index.tsindex 7bd3cbc96c..21eadcefca 100644--- a/packages/docs/src/index.ts+++ b/packages/docs/src/index.ts@@ -109,8 +109,8 @@ export { DocTableDeleteRowsCommand, DocTableDeleteColumnsCommand, DocTableDeleteexport type { IDocTableDeleteRowsCommandParams, IDocTableDeleteColumnsCommandParams, IDocTableDeleteTableCommandParams } from './commands/commands/table/doc-table-delete.command';export type { IDocTableInsertColumnCommandParams, IDocTableInsertRowCommandParams, IDocTableInsertColumnRightCommandParams, IDocTableInsertRowAboveCommandParams, IDocTableInsertRowBellowCommandParams, IDocTableInsertColumnLeftCommandParams } from './commands/commands/table/doc-table-insert.command';export { DocTableInsertColumnCommand, DocTableInsertRowCommand, DocTableInsertColumnRightCommand, DocTableInsertRowAboveCommand, DocTableInsertRowBellowCommand, DocTableInsertColumnLeftCommand } from './commands/commands/table/doc-table-insert.command';-export type { IDocTableShiftTabCommandParams, IDocTableTabCommandParams } from './commands/commands/table/doc-table-tab.command';-export { DocTableTabCommand, DocTableShiftTabCommand } from './commands/commands/table/doc-table-tab.command';+export type { IDocTableTabCommandParams } from './commands/commands/table/doc-table-tab.command';+export { DocTableTabCommand } from './commands/commands/table/doc-table-tab.command';export { genTableSource, getEmptyTableRow, getEmptyTableCell, getTableColumn } from './commands/commands/table/table';export { getCursorWhenDelete } from './commands/commands/delete.command';diff --git a/packages/docs/src/services/doc-auto-format.service.ts b/packages/docs/src/services/doc-auto-format.service.tsindex b2ffdd85d9..51368cc9a4 100644--- a/packages/docs/src/services/doc-auto-format.service.ts+++ b/packages/docs/src/services/doc-auto-format.service.ts@@ -17,11 +17,12 @@import type { DocumentDataModel, ICommandInfo, ICustomRange, IDisposable, IParagraph, IParagraphRange, ITextRange, Nullable } from '@univerjs/core';import { Disposable, Inject, IUniverInstanceService, toDisposable, UniverInstanceType } from '@univerjs/core';import { getCustomRangesIntesetsWithRange } from '../basics/custom-range';+import type { ITextActiveRange } from './text-selection-manager.service';import { TextSelectionManagerService } from './text-selection-manager.service';export interface IAutoFormatContext {unit: DocumentDataModel;- selection: ITextRange;+ selection: ITextActiveRange;/*** is selection at doc body*/@@ -78,7 +79,7 @@ function getParagraphsInRange(activeRange: ITextRange, paragraphs: IParagraph[])}/**- * service for auto-formating, handle shortcut like `Space` or `Tab`+ * service for auto-formatting, handle shortcut like `Space` or `Tab`.*/export class DocAutoFormatService extends Disposable {private _matches: Map<string, IAutoFormat[]> = new Map();@@ -110,7 +111,7 @@ export class DocAutoFormatService extends Disposable {}onAutoFormat(id: string, params: Nullable<object>): ICommandInfo[] {- const autoForamts = this._matches.get(id) ?? [];+ const autoFormats = this._matches.get(id) ?? [];const unit = this._univerInstanceService.getCurrentUnitForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);const selection = this._textSelectionManagerService.getActiveTextRangeWithStyle();@@ -126,7 +127,7 @@ export class DocAutoFormatService extends Disposable {commandParams: params,};- const matched = autoForamts.find((i) => i.match(context));+ const matched = autoFormats.find((i) => i.match(context));return matched?.getMutations(context) ?? [];}