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): add document name setter (#7447)
Univer0b9c6f1738d9
diff --git a/packages/docs/src/commands/commands/__tests__/set-document-name.command.spec.ts b/packages/docs/src/commands/commands/__tests__/set-document-name.command.spec.tsnew file mode 100644index 0000000000..e472ca2a7a--- /dev/null+++ b/packages/docs/src/commands/commands/__tests__/set-document-name.command.spec.ts@@ -0,0 +1,47 @@+/**+ * 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 { ICommandService, RedoCommand, UndoCommand } from '@univerjs/core';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { createDocumentData, createTestBed } from '../../../facade/__tests__/create-test-bed';+import { SetDocumentNameCommand } from '../set-document-name.command';++describe('SetDocumentNameCommand', () => {+ let testBed: ReturnType<typeof createTestBed>;+ let commandService: ICommandService;++ beforeEach(() => {+ testBed = createTestBed(createDocumentData('test', { dataStream: '\r\n' }));+ commandService = testBed.get(ICommandService);+ });++ afterEach(() => testBed.univer.dispose());++ it('renames a document and supports undo and redo', () => {+ expect(+ commandService.syncExecuteCommand(SetDocumentNameCommand.id, {+ unitId: 'test',+ name: 'New name',+ })+ ).toBe(true);+ expect(testBed.doc.getTitle()).toBe('New name');++ expect(commandService.syncExecuteCommand(UndoCommand.id)).toBe(true);+ expect(testBed.doc.getTitle()).toBe('');+ expect(commandService.syncExecuteCommand(RedoCommand.id)).toBe(true);+ expect(testBed.doc.getTitle()).toBe('New name');+ });+});diff --git a/packages/docs/src/commands/commands/set-document-name.command.ts b/packages/docs/src/commands/commands/set-document-name.command.tsnew file mode 100644index 0000000000..9ddcef1750--- /dev/null+++ b/packages/docs/src/commands/commands/set-document-name.command.ts@@ -0,0 +1,63 @@+/**+ * 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 { DocumentDataModel, ICommand } from '@univerjs/core';+import {+ CommandType,+ ICommandService,+ IUndoRedoService,+ IUniverInstanceService,+ UniverInstanceType,+} from '@univerjs/core';+import { DocsRenameMutation } from '../mutations/docs-rename.mutation';++export interface ISetDocumentNameCommandParams {+ unitId: string;+ name: string;+}++export const SetDocumentNameCommand: ICommand<ISetDocumentNameCommandParams> = {+ id: 'doc.command.set-name',+ type: CommandType.COMMAND,+ handler: (accessor, params) => {+ if (!params) {+ return false;+ }++ const document = accessor+ .get(IUniverInstanceService)+ .getUnit<DocumentDataModel>(params.unitId, UniverInstanceType.UNIVER_DOC);+ if (!document) {+ return false;+ }++ const commandService = accessor.get(ICommandService);+ const redoParams = { unitId: params.unitId, name: params.name };+ const undoParams = { unitId: params.unitId, name: document.getTitle() || '' };+ const result = commandService.syncExecuteCommand(DocsRenameMutation.id, redoParams);++ if (!result) {+ return false;+ }++ accessor.get(IUndoRedoService).pushUndoRedo({+ unitID: params.unitId,+ redoMutations: [{ id: DocsRenameMutation.id, params: redoParams }],+ undoMutations: [{ id: DocsRenameMutation.id, params: undoParams }],+ });+ return true;+ },+};diff --git a/packages/docs/src/commands/mutations/docs-rename.mutation.ts b/packages/docs/src/commands/mutations/docs-rename.mutation.tsindex 559728d474..14ce1952fa 100644--- a/packages/docs/src/commands/mutations/docs-rename.mutation.ts+++ b/packages/docs/src/commands/mutations/docs-rename.mutation.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { DocumentDataModel, ICommand } from '@univerjs/core';+import type { DocumentDataModel, IMutation } from '@univerjs/core';import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';export interface IDocsRenameMutationParams {@@ -22,10 +22,10 @@ export interface IDocsRenameMutationParams {unitId: string;}-export const DocsRenameMutation: ICommand = {+export const DocsRenameMutation: IMutation<IDocsRenameMutationParams> = {id: 'doc.mutation.rename-doc',type: CommandType.MUTATION,- handler: (accessor, params: IDocsRenameMutationParams) => {+ handler: (accessor, params) => {const univerInstanceService = accessor.get(IUniverInstanceService);const doc = univerInstanceService.getUnit<DocumentDataModel>(params.unitId, UniverInstanceType.UNIVER_DOC);if (!doc) {diff --git a/packages/docs/src/facade/__tests__/f-document.spec.ts b/packages/docs/src/facade/__tests__/f-document.spec.tsindex 21a2f1a911..3c034b6aa0 100644--- a/packages/docs/src/facade/__tests__/f-document.spec.ts+++ b/packages/docs/src/facade/__tests__/f-document.spec.ts@@ -114,6 +114,17 @@ describe('FDocument', () => {expect(document.save().body?.dataStream).toBe('Hello,\r\n');});+ it('sets the document name and supports undo and redo', () => {+ expect(document.setName('Renamed document')).toBe(document);+ expect(document.getName()).toBe('Renamed document');++ expect(document.undo()).toBe(true);+ expect(document.getName()).toBe('');++ expect(document.redo()).toBe(true);+ expect(document.getName()).toBe('Renamed document');+ });+it('runs undo and redo against the active document', () => {get(IUndoRedoService);diff --git a/packages/docs/src/facade/f-document.ts b/packages/docs/src/facade/f-document.tsindex 2cb3655316..a90d21925b 100644--- a/packages/docs/src/facade/f-document.ts+++ b/packages/docs/src/facade/f-document.ts@@ -34,7 +34,7 @@ import {UndoCommand,} from '@univerjs/core';import { FBaseInitialable } from '@univerjs/core/facade';-import { CreateHeaderFooterCommand, generateParagraphs, getTopLevelSectionBreaks, HeaderFooterType, InsertDocumentColumnBreakCommand, InsertDocumentSectionBreakCommand } from '@univerjs/docs';+import { CreateHeaderFooterCommand, generateParagraphs, getTopLevelSectionBreaks, HeaderFooterType, InsertDocumentColumnBreakCommand, InsertDocumentSectionBreakCommand, SetDocumentNameCommand } from '@univerjs/docs';import { FDocumentParagraph } from './f-document-paragraph';import { DocsSectionUnsupportedDocumentFlavorError, FDocumentSection } from './f-document-section';import { FDocumentTextRange } from './f-document-text-range';@@ -195,6 +195,25 @@ export class FDocument extends FBaseInitialable {return this._documentDataModel.getTitle() || '';}+ /**+ * Set the document name.+ * @param {string} name The new document name.+ * @returns {FDocument} The current document for chaining.+ *+ * @example+ * ```ts+ * const document = univerAPI.getActiveDocument();+ * document?.setName('Quarterly Report');+ * ```+ */+ setName(name: string): this {+ this._commandService.syncExecuteCommand(SetDocumentNameCommand.id, {+ unitId: this.id,+ name,+ });+ return this;+ }+/*** Returns the document's explicit layout flavor.*diff --git a/packages/docs/src/index.ts b/packages/docs/src/index.tsindex 1ebc2e46f6..da5e7732e1 100644--- a/packages/docs/src/index.ts+++ b/packages/docs/src/index.ts@@ -31,6 +31,8 @@ export type {IDocumentDefaultParagraphStylePatch,ISetDocumentDefaultParagraphStyleCommandParams,} from './commands/commands/set-document-default-paragraph-style.command';+export { SetDocumentNameCommand } from './commands/commands/set-document-name.command';+export type { ISetDocumentNameCommandParams } from './commands/commands/set-document-name.command';export { SetSectionHeaderFooterLinkCommand } from './commands/commands/set-section-header-footer-link.command';export type { ISetSectionHeaderFooterLinkCommandParams } from './commands/commands/set-section-header-footer-link.command';export { UpdateDocumentParagraphStyleCommand } from './commands/commands/update-document-paragraph-style.command';diff --git a/packages/docs/src/plugin.ts b/packages/docs/src/plugin.tsindex ed0628ace9..3354e2fa15 100644--- a/packages/docs/src/plugin.ts+++ b/packages/docs/src/plugin.ts@@ -28,6 +28,7 @@ import pkg from '../package.json';import { DeleteTextCommand, InsertTextCommand, UpdateTextCommand } from './commands/commands/core-editing.command';import { CreateHeaderFooterCommand } from './commands/commands/create-header-footer.command';import { SetDocumentDefaultParagraphStyleCommand } from './commands/commands/set-document-default-paragraph-style.command';+import { SetDocumentNameCommand } from './commands/commands/set-document-name.command';import { SetSectionHeaderFooterLinkCommand } from './commands/commands/set-section-header-footer-link.command';import { UpdateDocumentParagraphStyleCommand } from './commands/commands/update-document-paragraph-style.command';import { DeleteDocumentSectionBreakCommand, InsertDocumentColumnBreakCommand, InsertDocumentSectionBreakCommand, UpdateDocumentSectionCommand } from './commands/commands/update-document-section.command';@@ -78,6 +79,7 @@ export class UniverDocsPlugin extends Plugin {UpdateTextCommand,CreateHeaderFooterCommand,SetDocumentDefaultParagraphStyleCommand,+ SetDocumentNameCommand,SetSectionHeaderFooterLinkCommand,UpdateDocumentParagraphStyleCommand,UpdateDocumentSectionCommand,