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(facade): fix implementation of `getWrap` and remove redundant `getWraps` method (#2675)
白熱6e2ef32809fb
diff --git a/packages/core/src/sheets/range.ts b/packages/core/src/sheets/range.tsindex 5e6b66dac7..ffb0261a30 100644--- a/packages/core/src/sheets/range.ts+++ b/packages/core/src/sheets/range.ts@@ -17,8 +17,8 @@import type { IObjectMatrixPrimitiveType, Nullable } from '../shared';import { ObjectMatrix, Tools } from '../shared';import { DEFAULT_STYLES } from '../types/const';-import type { HorizontalAlign, VerticalAlign, WrapStrategy } from '../types/enum';-import { BooleanNumber, FontItalic, FontWeight } from '../types/enum';+import type { HorizontalAlign, VerticalAlign } from '../types/enum';+import { BooleanNumber, FontItalic, FontWeight, WrapStrategy } from '../types/enum';import {type IBorderData,type ICellData,@@ -707,14 +707,7 @@ export class Range {* Returns whether the text in the cell wraps.*/getWrap(): BooleanNumber {- return this.getWraps()[0][0];- }-- /**- * Returns whether the text in the cells wrap.- */- getWraps(): BooleanNumber[][] {- return this._getStyles('tb') as BooleanNumber[][];+ return this.getWrapStrategy() === WrapStrategy.WRAP ? BooleanNumber.TRUE : BooleanNumber.FALSE;}/**diff --git a/packages/facade/src/apis/facade.ts b/packages/facade/src/apis/facade.tsindex 93af956d6a..bfbb20c63a 100644--- a/packages/facade/src/apis/facade.ts+++ b/packages/facade/src/apis/facade.ts@@ -14,9 +14,15 @@* limitations under the License.*/-import type { CommandListener, DocumentDataModel, IDocumentData, IExecutionOptions, IWorkbookData,+import type {+ CommandListener,+ DocumentDataModel,+ IDocumentData,+ IExecutionOptions,+ IWorkbookData,Nullable,- Workbook } from '@univerjs/core';+ Workbook,+} from '@univerjs/core';import {BorderStyleTypes,debounce,@@ -28,6 +34,7 @@ import {UniverInstanceType,WrapStrategy,} from '@univerjs/core';+import type { ISocket } from '@univerjs/network';import { ISocketService, WebSocketService } from '@univerjs/network';import type { IRegisterFunctionParams } from '@univerjs/sheets-formula';import { IRegisterFunctionService, RegisterFunctionService } from '@univerjs/sheets-formula';@@ -40,9 +47,9 @@ import type {RenderComponentType,SheetComponent,SheetExtension, SpreadsheetColumnHeader,- SpreadsheetRowHeader } from '@univerjs/engine-render';-import {- IRenderManagerService } from '@univerjs/engine-render';+ SpreadsheetRowHeader,+} from '@univerjs/engine-render';+import { IRenderManagerService } from '@univerjs/engine-render';import { SHEET_VIEW_KEY } from '@univerjs/sheets-ui';import { SetFormulaCalculationStartMutation } from '@univerjs/engine-formula';import { FDocument } from './docs/f-document';@@ -51,11 +58,49 @@ import { FSheetHooks } from './sheets/f-sheet-hooks';import { FHooks } from './f-hooks';export class FUniver {+ static BorderStyle = BorderStyleTypes;++ static WrapStrategy = WrapStrategy;++ constructor(+ @Inject(Injector) protected readonly _injector: Injector,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,+ @ICommandService private readonly _commandService: ICommandService,+ @ISocketService private readonly _ws: ISocketService,+ @IRenderManagerService private readonly _renderManagerService: IRenderManagerService+ ) {+ this._initialize();+ }++ /**+ * Initialize the FUniver instance.+ *+ * @private+ */+ private _initialize(): void {+ this._debouncedFormulaCalculation = debounce(() => {+ this._commandService.executeCommand(+ SetFormulaCalculationStartMutation.id,+ {+ commands: [],+ forceCalculation: true,+ },+ {+ onlyLocal: true,+ }+ );+ }, 10);+ }+/*** Get dependencies for FUniver, you can override newAPI to add more dependencies.- * @param injector- * @param derivedDependencies- * @returns+ *+ * @static+ * @protected+ *+ * @param {Injector} injector - The injector instance used to retrieve dependencies.+ * @param {Dependency[]} [derivedDependencies] - Optional array of pre-derived dependencies.+ * @returns {Dependency[]} - An array of dependencies required by the service.*/protected static getDependencies(injector: Injector, derivedDependencies?: []): Dependency[] {const dependencies: Dependency[] = derivedDependencies || [];@@ -68,13 +113,12 @@ export class FUniver {}/**- * Create a FUniver instance, if the injector is not provided, it will create a new Univer instance.- * @param wrapped The Univer instance or injector.- * @returns FUniver instance.+ * Create an FUniver instance, if the injector is not provided, it will create a new Univer instance.+ *+ * @static*- * @zh 创建一个 FUniver 实例,如果未提供注入器,将创建一个新的 Univer 实例。- * @param_zh wrapped Univer 实例或注入器。- * @returns_zh FUniver 实例。+ * @param {Univer | Injector} wrapped - The Univer instance or injector instance.+ * @returns {FUniver} - The FUniver instance.*/static newAPI(wrapped: Univer | Injector): FUniver {const injector = wrapped instanceof Univer ? wrapped.__getInjector() : wrapped;@@ -83,28 +127,16 @@ export class FUniver {return injector.createInstance(FUniver);}- static BorderStyle = BorderStyleTypes;- static WrapStrategy = WrapStrategy;-/*** registerFunction may be executed multiple times, triggering multiple formula forced refreshes*/private _debouncedFormulaCalculation: () => void;- constructor(- @Inject(Injector) protected readonly _injector: Injector,- @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,- @ICommandService private readonly _commandService: ICommandService,- @ISocketService private readonly _ws: ISocketService,- @IRenderManagerService private readonly _renderManagerService: IRenderManagerService- ) {- this._initialize();- }-/*** Create a new spreadsheet and get the API handler of that spreadsheet.- * @param data The snapshot of the spreadsheet.- * @returns Spreadsheet API instance.+ *+ * @param {Partial<IWorkbookData>} data The snapshot of the spreadsheet.+ * @returns {FWorkbook} FWorkbook API instance.*/createUniverSheet(data: Partial<IWorkbookData>): FWorkbook {const workbook = this._univerInstanceService.createUnit<IWorkbookData, Workbook>(UniverInstanceType.UNIVER_SHEET, data);@@ -113,8 +145,9 @@ export class FUniver {/*** Create a new document and get the API handler of that document.- * @param data The snapshot of the document.- * @returns Document API instance.+ *+ * @param {Partial<IDocumentData>} data The snapshot of the document.+ * @returns {FDocument} FDocument API instance.*/createUniverDoc(data: Partial<IDocumentData>): FDocument {const document = this._univerInstanceService.createUnit<IDocumentData, DocumentDataModel>(UniverInstanceType.UNIVER_DOC, data);@@ -123,8 +156,9 @@ export class FUniver {/*** Dispose the UniverSheet by the `unitId`. The UniverSheet would be unload from the application.- * @param unitId The `unitId` of the UniverSheet.- * @returns If the UniverSheet is disposed successfully, return `true`, otherwise return `false`.+ *+ * @param unitId The unit id of the UniverSheet.+ * @returns Whether the Univer instance is disposed successfully.*/disposeUnit(unitId: string): boolean {return this._univerInstanceService.disposeUnit(unitId);@@ -132,8 +166,9 @@ export class FUniver {/*** Get the spreadsheet API handler by the spreadsheet id.- * @param id The spreadsheet id.- * @returns Spreadsheet API instance.+ *+ * @param {string} id The spreadsheet id.+ * @returns {FWorkbook | null} The spreadsheet API instance.*/getUniverSheet(id: string): FWorkbook | null {const workbook = this._univerInstanceService.getUniverSheetInstance(id);@@ -146,8 +181,9 @@ export class FUniver {/*** Get the document API handler by the document id.- * @param id The document id.- * @returns Document API instance.+ *+ * @param {string} id The document id.+ * @returns {FDocument | null} The document API instance.*/getUniverDoc(id: string): FDocument | null {const document = this._univerInstanceService.getUniverDocInstance(id);@@ -160,7 +196,8 @@ export class FUniver {/*** Get the currently focused Univer spreadsheet.- * @returns the currently focused Univer spreadsheet.+ *+ * @returns {FWorkbook | null} The currently focused Univer spreadsheet.*/getActiveWorkbook(): FWorkbook | null {const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;@@ -173,7 +210,8 @@ export class FUniver {/*** Get the currently focused Univer document.- * @returns the currently focused Univer document.+ *+ * @returns {FDocument | null} The currently focused Univer document.*/getActiveDocument(): FDocument | null {const document = this._univerInstanceService.getCurrentUniverDocInstance();@@ -186,7 +224,9 @@ export class FUniver {/*** Register a function to the spreadsheet.- * @param config+ *+ * @param {IRegisterFunctionParams} config The configuration of the function.+ * @returns {IDisposable} The disposable instance.*/registerFunction(config: IRegisterFunctionParams): IDisposable {let registerFunctionService = this._injector.get(IRegisterFunctionService);@@ -208,8 +248,10 @@ export class FUniver {/*** Register sheet row header render extensions.- * @param unitId- * @param extensions+ *+ * @param {string} unitId The unit id of the spreadsheet.+ * @param {SheetExtension[]} extensions The extensions to register.+ * @returns {IDisposable} The disposable instance.*/registerSheetRowHeaderExtension(unitId: string, ...extensions: SheetExtension[]): IDisposable {const sheetComponent = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.ROW) as SheetComponent;@@ -223,8 +265,10 @@ export class FUniver {/*** Register sheet column header render extensions.- * @param unitId- * @param extensions+ *+ * @param {string} unitId The unit id of the spreadsheet.+ * @param {SheetExtension[]} extensions The extensions to register.+ * @returns {IDisposable} The disposable instance.*/registerSheetColumnHeaderExtension(unitId: string, ...extensions: SheetExtension[]): IDisposable {const sheetComponent = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.COLUMN) as SheetComponent;@@ -238,8 +282,10 @@ export class FUniver {/*** Register sheet main render extensions.- * @param unitId- * @param uKeys+ *+ * @param {string} unitId The unit id of the spreadsheet.+ * @param {SheetExtension[]} extensions The extensions to register.+ * @returns {IDisposable} The disposable instance.*/registerSheetMainExtension(unitId: string, ...extensions: SheetExtension[]): IDisposable {const sheetComponent = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.MAIN) as SheetComponent;@@ -255,7 +301,8 @@ export class FUniver {/*** Undo an editing on the currently focused document.- * @returns redo result+ *+ * @returns {Promise<boolean>} undo result*/undo(): Promise<boolean> {return this._commandService.executeCommand(UndoCommand.id);@@ -263,7 +310,8 @@ export class FUniver {/*** Redo an editing on the currently focused document.- * @returns redo result+ *+ * @returns {Promise<boolean>} redo result*/redo(): Promise<boolean> {return this._commandService.executeCommand(UndoCommand.id);@@ -275,8 +323,9 @@ export class FUniver {/*** Register a callback that will be triggered before invoking a command.- * @param callback the callback.- * @returns A function to dispose the listening.+ *+ * @param {CommandListener} callback The callback.+ * @returns {IDisposable} The disposable instance.*/onBeforeCommandExecute(callback: CommandListener): IDisposable {return this._commandService.beforeCommandExecuted((command, options?: IExecutionOptions) => {@@ -286,8 +335,9 @@ export class FUniver {/*** Register a callback that will be triggered when a command is invoked.- * @param callback the callback.- * @returns A function to dispose the listening.+ *+ * @param {CommandListener} callback The callback.+ * @returns {IDisposable} The disposable instance.*/onCommandExecuted(callback: CommandListener): IDisposable {return this._commandService.onCommandExecuted((command, options?: IExecutionOptions) => {@@ -299,10 +349,11 @@ export class FUniver {/*** Execute command- * @param id Command id- * @param params Command params- * @param options Command options- * @returns Command Promise+ *+ * @param {string} id Command ID+ * @param {object} params Command parameters+ * @param {IExecutionOptions} options Command execution options+ * @returns {Promise<R>} Command execution result*/executeCommand<P extends object = object, R = boolean>(id: string,@@ -314,10 +365,11 @@ export class FUniver {/*** Set WebSocket URL for WebSocketService- * @param url WebSocketService URL- * @returns WebSocket info and callback+ *+ * @param {string} url WebSocket URL+ * @returns {ISocket} WebSocket instance*/- createSocket(url: string) {+ createSocket(url: string): ISocket {const ws = this._ws.createSocket(url);if (!ws) {@@ -329,23 +381,30 @@ export class FUniver {/*** Get sheet hooks- * @returns+ *+ * @returns {FSheetHooks} FSheetHooks instance*/- getSheetHooks() {+ getSheetHooks(): FSheetHooks {return this._injector.createInstance(FSheetHooks);}/*** Get hooks+ *+ * @returns {FHooks} FHooks instance*/- getHooks() {+ getHooks(): FHooks {return this._injector.createInstance(FHooks);}/*** Get sheet render component from render by unitId and view key.- * @param unitId- * @returns+ *+ * @private+ *+ * @param {string} unitId The unit id of the spreadsheet.+ * @param {SHEET_VIEW_KEY} viewKey The view key of the spreadsheet.+ * @returns {Nullable<RenderComponentType>} The render component.*/private _getSheetRenderComponent(unitId: string, viewKey: SHEET_VIEW_KEY): Nullable<RenderComponentType> {const render = this._renderManagerService.getRenderById(unitId);@@ -364,10 +423,14 @@ export class FUniver {}/**- * customizeColumnHeader- * @param cfg- * cfg example- ({ headerStyle:{backgroundColor: 'pink', fontSize: 9}, columnsCfg: ['MokaII', undefined, null, {text: 'Size', textAlign: 'left'}]})+ * Customize the column header of the spreadsheet.+ *+ * @param {IColumnsHeaderCfgParam} cfg The configuration of the column header.+ *+ * @example+ * ```typescript+ * customizeColumnHeader({ headerStyle: { backgroundColor: 'pink', fontSize: 9 }, columnsCfg: ['MokaII', undefined, null, { text: 'Size', textAlign: 'left' }] });+ * ```*/customizeColumnHeader(cfg: IColumnsHeaderCfgParam) {const wb = this.getActiveWorkbook();@@ -380,6 +443,16 @@ export class FUniver {sheetColumn.setCustomHeader(cfg);}+ /**+ * Customize the row header of the spreadsheet.+ *+ * @param {IRowsHeaderCfgParam} cfg The configuration of the row header.+ *+ * @example+ * ```typescript+ * customizeRowHeader({ headerStyle: { backgroundColor: 'pink', fontSize: 9 }, rowsCfg: ['MokaII', undefined, null, { text: 'Size', textAlign: 'left' }] });+ * ```+ */customizeRowHeader(cfg: IRowsHeaderCfgParam) {const wb = this.getActiveWorkbook();if (!wb) {@@ -390,19 +463,4 @@ export class FUniver {const sheetRow = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.ROW) as SpreadsheetRowHeader;sheetRow.setCustomHeader(cfg);}-- private _initialize(): void {- this._debouncedFormulaCalculation = debounce(() => {- this._commandService.executeCommand(- SetFormulaCalculationStartMutation.id,- {- commands: [],- forceCalculation: true,- },- {- onlyLocal: true,- }- );- }, 10);- }}diff --git a/packages/facade/src/apis/sheets/__tests__/f-range.spec.ts b/packages/facade/src/apis/sheets/__tests__/f-range.spec.tsindex 7529cc717d..01a0e27eda 100644--- a/packages/facade/src/apis/sheets/__tests__/f-range.spec.ts+++ b/packages/facade/src/apis/sheets/__tests__/f-range.spec.ts@@ -81,6 +81,34 @@ describe('Test FRange', () => {};});+ it('Range getRow', () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();+ const range = activeSheet?.getRange(0, 0, 1, 1);++ expect(range?.getRow()).toBe(0);+ });++ it('Range getColumn', () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();+ const range = activeSheet?.getRange(0, 0, 1, 1);++ expect(range?.getColumn()).toBe(0);+ });++ it('Range getWidth', () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();+ const range = activeSheet?.getRange(0, 0, 1, 1);++ expect(range?.getWidth()).toBe(1);+ });++ it('Range getHeight', () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();+ const range = activeSheet?.getRange(0, 0, 1, 1);++ expect(range?.getHeight()).toBe(1);+ });+it('Range setValue', () => {const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();@@ -241,6 +269,13 @@ describe('Test FRange', () => {]);});+ it('Range getWrap', async () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();+ const range = activeSheet?.getRange(0, 0);+ await range?.setWrap(true);+ expect(range?.getWrap()).toBe(true);+ });+it('Range setFontWeight', () => {const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();diff --git a/packages/sheets/src/commands/commands/__tests__/set-style.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-style.command.spec.tsindex bb2df4fa20..61526f9b39 100644--- a/packages/sheets/src/commands/commands/__tests__/set-style.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-style.command.spec.ts@@ -720,15 +720,15 @@ describe("Test commands used for updating cells' styles", () => {value: WrapStrategy.WRAP,})).toBeTruthy();- expect(getTextWrap()).toBe(WrapStrategy.WRAP);+ expect(getTextWrap()).toBe(BooleanNumber.TRUE);// undoexpect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();- expect(getTextWrap()).toBe(WrapStrategy.UNSPECIFIED);+ expect(getTextWrap()).toBe(BooleanNumber.FALSE);// redoexpect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();- expect(getTextWrap()).toBe(WrapStrategy.WRAP);+ expect(getTextWrap()).toBe(BooleanNumber.TRUE);});});