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(facade): add API to generate HTML content (#2219)

Wenzhao Hu3a9afd963aa1
diff --git a/packages/facade/src/apis/sheets/f-range.ts b/packages/facade/src/apis/sheets/f-range.ts
index 8b18617089..6d8b597b26 100644
--- a/packages/facade/src/apis/sheets/f-range.ts
+++ b/packages/facade/src/apis/sheets/f-range.ts
@@ -44,7 +44,8 @@ import type { ISetNumfmtCommandParams } from '@univerjs/sheets-numfmt';
import { SetNumfmtCommand } from '@univerjs/sheets-numfmt';
import { FormulaDataModel } from '@univerjs/engine-formula';
-import { Inject } from '@wendellhu/redi';
+import { Inject, Injector } from '@wendellhu/redi';
+import { ISheetClipboardService } from '@univerjs/sheets-ui';
import type { FHorizontalAlignment, FVerticalAlignment } from './utils';
import {
covertCellValue,
@@ -64,9 +65,12 @@ export class FRange {
private readonly _workbook: Workbook,
private readonly _worksheet: Worksheet,
private readonly _range: IRange,
+ @Inject(Injector) private readonly _injector: Injector,
@ICommandService private readonly _commandService: ICommandService,
@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel
- ) { }
+ ) {
+ // empty
+ }
getRow(): number {
return this._range.startRow;
@@ -484,4 +488,17 @@ export class FRange {
callback(row, col, value);
});
}
+
+ /**
+ * Generate HTML content for the range.
+ */
+ generateHTML(): string {
+ const copyContent = this._injector.get(ISheetClipboardService).generateCopyContent(
+ this._workbook.getUnitId(),
+ this._worksheet.getSheetId(),
+ this._range
+ );
+
+ return copyContent?.html ?? '';
+ }
}
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 4abe340d82..5846aa2453 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICellData, IMutationInfo, IRange, Workbook, Worksheet } from '@univerjs/core';
+import type { ICellData, IMutationInfo, IRange, Nullable, Workbook, Worksheet } from '@univerjs/core';
import {
Disposable,
ErrorService,
@@ -70,6 +70,14 @@ export const PREDEFINED_HOOK_NAME = {
SPECIAL_PASTE_BESIDES_BORDER: 'special-paste-besides-border',
};
+interface ICopyContent {
+ copyId: string;
+ plain: string;
+ html: string;
+ matrixFragment: ObjectMatrix<ICellDataWithSpanInfo>;
+ discreteRange: IDiscreteRange;
+}
+
/**
* This service provide hooks for sheet features to supplement content or modify behavior of clipboard.
*/
@@ -77,12 +85,14 @@ export const PREDEFINED_HOOK_NAME = {
HtmlToUSMService.use(PastePluginWord);
HtmlToUSMService.use(PastePluginLark);
HtmlToUSMService.use(PastePluginUniver);
+
export interface ISheetClipboardService {
copy(): Promise<boolean>;
cut(): Promise<boolean>;
paste(item: ClipboardItem, pasteType?: string): Promise<boolean>; // get content from a ClipboardItem and paste it.
legacyPaste(html?: string, text?: string): Promise<boolean>; // paste a HTML string or plain text directly.
+ generateCopyContent(workbookId: string, worksheetId: string, range: IRange): Nullable<ICopyContent>;
copyContentCache(): CopyContentCache; // return the cache content for inner copy/cut/paste.
addClipboardHook(hook: ISheetClipboardHook): IDisposable; // add a hook to the clipboard service
getClipboardHooks(): ISheetClipboardHook[]; // get all hooks
@@ -131,6 +141,14 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
return this._copyContentCache;
}
+ generateCopyContent(workbookId: string, worksheetId: string, range: IRange): Nullable<ICopyContent> {
+ const hooks = this._clipboardHooks;
+ hooks.forEach((h) => h.onBeforeCopy?.(workbookId, worksheetId, range));
+ const copyContent = this._generateCopyContent(workbookId, worksheetId, range, hooks);
+ hooks.forEach((h) => h.onAfterCopy?.());
+ return copyContent;
+ }
+
async copy(copyType = COPY_TYPE.COPY): Promise<boolean> {
const selection = this._selectionManagerService.getLast();
if (!selection) {
@@ -139,12 +157,8 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const worksheet = workbook.getActiveSheet();
- const hooks = this._clipboardHooks;
-
- // 1. tell hooks to get ready for copying
- hooks.forEach((h) => h.onBeforeCopy?.(workbook.getUnitId(), worksheet.getSheetId(), selection.range));
- const copyContent = this._generateCopyContent(workbook.getUnitId(), worksheet.getSheetId(), selection.range, hooks);
+ const copyContent = this.generateCopyContent(workbook.getUnitId(), worksheet.getSheetId(), selection.range);
if (!copyContent) {
return false;
}
@@ -170,9 +184,6 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const style = this._selectionManagerService.createCopyPasteSelection();
this._copyMarkId = this._markSelectionService.addShape({ ...selection, style });
- // 6. tell hooks to clean up
- hooks.forEach((h) => h.onAfterCopy?.());
-
return true;
}
@@ -227,7 +238,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
addClipboardHook(hook: ISheetClipboardHook): IDisposable {
if (this._clipboardHooks.findIndex((h) => h.id === hook.id) !== -1) {
this._logService.error('[SheetClipboardService]', 'hook already exists', hook.id);
- return { dispose: () => { } };
+ return { dispose: () => { /* empty */ } };
}
// hook added should be ordered at meaning while
const insertIndex = this._clipboardHooks.findIndex((existingHook) => {
@@ -252,7 +263,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
return this._clipboardHooks;
}
- private _generateCopyContent(unitId: string, subUnitId: string, range: IRange, hooks: ISheetClipboardHook[]) {
+ private _generateCopyContent(unitId: string, subUnitId: string, range: IRange, hooks: ISheetClipboardHook[]): Nullable<ICopyContent> {
const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);
const worksheet = workbook?.getSheetBySheetId(subUnitId);