Skip to content

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.

feat(docs-ui): add custom-range insert command (#4810)

ZhangWeic47d5254f88a
diff --git a/packages/docs-ui/src/commands/commands/auto-format.command.ts b/packages/docs-ui/src/commands/commands/auto-format.command.ts
index d45b81ef90..036df1e642 100644
--- a/packages/docs-ui/src/commands/commands/auto-format.command.ts
+++ b/packages/docs-ui/src/commands/commands/auto-format.command.ts
@@ -14,7 +14,8 @@
* limitations under the License.
*/
-import { CommandType, type ICommand, ICommandService, sequenceExecuteAsync } from '@univerjs/core';
+import type { ICommand } from '@univerjs/core';
+import { CommandType, ICommandService, sequenceExecuteAsync } from '@univerjs/core';
import { DocAutoFormatService } from '../../services/doc-auto-format.service';
const TabCommandId = 'doc.command.tab';
diff --git a/packages/docs-ui/src/commands/commands/insert-custom-range.command.ts b/packages/docs-ui/src/commands/commands/insert-custom-range.command.ts
new file mode 100644
index 0000000000..4169de2d3e
--- /dev/null
+++ b/packages/docs-ui/src/commands/commands/insert-custom-range.command.ts
@@ -0,0 +1,56 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 { ICommand } from '@univerjs/core';
+import type { ITextRangeWithStyle } from '@univerjs/engine-render';
+import type { IReplaceSelectionCommandParams } from './replace-content.command';
+import { CommandType, CustomRangeType, generateRandomId, ICommandService } from '@univerjs/core';
+import { ReplaceSelectionCommand } from './replace-content.command';
+
+export interface IInsertCustomRangeCommandParams {
+ unitId: string;
+ rangeId?: string;
+ textRanges?: ITextRangeWithStyle[];
+ properties?: Record<string, any>;
+ text: string;
+
+}
+
+export const InsertCustomRangeCommand: ICommand<IInsertCustomRangeCommandParams> = {
+ id: 'doc.command.insert-custom-range',
+ type: CommandType.COMMAND,
+ handler: (accessor, params) => {
+ if (!params) return false;
+ const commandService = accessor.get(ICommandService);
+ const { unitId, rangeId = generateRandomId(), textRanges, properties, text } = params;
+ const replaceSelectionParams: IReplaceSelectionCommandParams = {
+ unitId,
+ textRanges,
+ body: {
+ dataStream: text,
+ customRanges: [{
+ startIndex: 0,
+ endIndex: text.length - 1,
+ rangeId,
+ rangeType: CustomRangeType.CUSTOM,
+ properties,
+ }],
+ },
+ };
+
+ return commandService.syncExecuteCommand(ReplaceSelectionCommand.id, replaceSelectionParams);
+ },
+};
diff --git a/packages/docs-ui/src/commands/commands/replace-content.command.ts b/packages/docs-ui/src/commands/commands/replace-content.command.ts
index 4c37f70439..1255ccc0c7 100644
--- a/packages/docs-ui/src/commands/commands/replace-content.command.ts
+++ b/packages/docs-ui/src/commands/commands/replace-content.command.ts
@@ -307,7 +307,6 @@ export interface IReplaceSelectionCommandParams {
textRanges?: ITextRangeWithStyle[];
}
-// TODO: implement
export const ReplaceSelectionCommand: ICommand<IReplaceSelectionCommandParams> = {
id: 'doc.command.replace-selection',
type: CommandType.COMMAND,
diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.ts
index b7dad856fc..0c5ef5754b 100644
--- a/packages/docs-ui/src/docs-ui-plugin.ts
+++ b/packages/docs-ui/src/docs-ui-plugin.ts
@@ -59,6 +59,7 @@ import {
SetInlineFormatTextColorCommand,
SetInlineFormatUnderlineCommand,
} from './commands/commands/inline-format.command';
+import { InsertCustomRangeCommand } from './commands/commands/insert-custom-range.command';
import {
BulletListCommand,
ChangeListNestingLevelCommand,
@@ -70,7 +71,7 @@ import {
ToggleCheckListCommand,
} from './commands/commands/list.command';
import { AlignCenterCommand, AlignJustifyCommand, AlignLeftCommand, AlignOperationCommand, AlignRightCommand } from './commands/commands/paragraph-align.command';
-import { CoverContentCommand, ReplaceContentCommand, ReplaceSnapshotCommand, ReplaceTextRunsCommand } from './commands/commands/replace-content.command';
+import { CoverContentCommand, ReplaceContentCommand, ReplaceSelectionCommand, ReplaceSnapshotCommand, ReplaceTextRunsCommand } from './commands/commands/replace-content.command';
import { SetDocZoomRatioCommand } from './commands/commands/set-doc-zoom-ratio.command';
import { SwitchDocModeCommand } from './commands/commands/switch-doc-mode.command';
import { CreateDocTableCommand } from './commands/commands/table/doc-table-create.command';
@@ -259,6 +260,8 @@ export class UniverDocsUIPlugin extends Plugin {
MoveCursorOperation,
MoveSelectionOperation,
ReplaceTextRunsCommand,
+ ReplaceSelectionCommand,
+ InsertCustomRangeCommand,
].forEach((e) => {
this._commandService.registerCommand(e);
});
diff --git a/packages/docs-ui/src/index.ts b/packages/docs-ui/src/index.ts
index 1222b25d8e..85b1fb2ff7 100644
--- a/packages/docs-ui/src/index.ts
+++ b/packages/docs-ui/src/index.ts
@@ -115,11 +115,11 @@ export {
AlignRightCommand,
} from './commands/commands/paragraph-align.command';
export { ReplaceTextRunsCommand } from './commands/commands/replace-content.command';
-export { CoverContentCommand, type IReplaceSelectionCommandParams, type IReplaceSnapshotCommandParams, ReplaceContentCommand, ReplaceSnapshotCommand } from './commands/commands/replace-content.command';
+export { CoverContentCommand, type IReplaceSelectionCommandParams, type IReplaceSnapshotCommandParams, ReplaceContentCommand, ReplaceSelectionCommand, ReplaceSnapshotCommand } from './commands/commands/replace-content.command';
export { SetDocZoomRatioCommand } from './commands/commands/set-doc-zoom-ratio.command';
export { CreateDocTableCommand, type ICreateDocTableCommandParams } from './commands/commands/table/doc-table-create.command';
export { DocTableDeleteColumnsCommand, DocTableDeleteRowsCommand, DocTableDeleteTableCommand } from './commands/commands/table/doc-table-delete.command';
-
+export { type IInsertCustomRangeCommandParams, InsertCustomRangeCommand } from './commands/commands/insert-custom-range.command';
export type {
IDocTableDeleteColumnsCommandParams,
IDocTableDeleteRowsCommandParams,