Skip to content

Build your personal knowledge base with Trilium Notes

feat(spreadsheet): add image support via Univer drawing preset (#9851)

Elian Doran110272c33578
diff --git a/apps/client/package.json b/apps/client/package.json
index 21b0759b93..67240572c5 100644
--- a/apps/client/package.json
+++ b/apps/client/package.json
@@ -38,6 +38,7 @@
"@univerjs/preset-sheets-conditional-formatting": "0.24.0",
"@univerjs/preset-sheets-core": "0.24.0",
"@univerjs/preset-sheets-data-validation": "0.24.0",
+ "@univerjs/preset-sheets-drawing": "0.24.0",
"@univerjs/preset-sheets-filter": "0.24.0",
"@univerjs/preset-sheets-find-replace": "0.24.0",
"@univerjs/preset-sheets-note": "0.24.0",
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
index 5287024cd5..82347e3481 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
@@ -1,5 +1,6 @@
import "./Spreadsheet.css";
import "@univerjs/preset-sheets-core/lib/index.css";
+import "@univerjs/preset-sheets-drawing/lib/index.css";
import "@univerjs/preset-sheets-sort/lib/index.css";
import "@univerjs/preset-sheets-conditional-formatting/lib/index.css";
import "@univerjs/preset-sheets-find-replace/lib/index.css";
@@ -12,6 +13,7 @@ import { DEFAULT_STYLES } from '@univerjs/core';
import { UniverSheetsConditionalFormattingPreset } from '@univerjs/preset-sheets-conditional-formatting';
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core';
import { UniverSheetsDataValidationPreset } from '@univerjs/preset-sheets-data-validation';
+import { UniverSheetsDrawingPreset } from '@univerjs/preset-sheets-drawing';
import { UniverSheetsFilterPreset } from '@univerjs/preset-sheets-filter';
import { UniverSheetsFindReplacePreset } from '@univerjs/preset-sheets-find-replace';
import { UniverSheetsHyperLinkPreset } from '@univerjs/preset-sheets-hyper-link';
@@ -243,6 +245,11 @@ function useInitializeSpreadsheet(containerRef: MutableRef<HTMLDivElement | null
"sheet.command.set-range-font-family": { hidden: true },
},
}),
+ // Floating images stored inline as base64 in the workbook's SHEET_DRAWING_PLUGIN
+ // resource (Univer's default ImageIoService), so they persist through the normal
+ // content save path. The insert-image toolbar button is hidden in read-only mode
+ // (toolbar is disabled above); the preset stays registered so existing images render.
+ UniverSheetsDrawingPreset(),
UniverSheetsFindReplacePreset(),
UniverSheetsNotePreset(),
UniverSheetsFilterPreset(),
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/locales.ts b/apps/client/src/widgets/type_widgets/spreadsheet/locales.ts
index d37ff398ce..0f323c1e95 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/locales.ts
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/locales.ts
@@ -24,6 +24,7 @@ interface UniverLocaleSource {
*/
export const SPREADSHEET_PRESET_PACKAGES = [
"@univerjs/preset-sheets-core",
+ "@univerjs/preset-sheets-drawing",
"@univerjs/preset-sheets-find-replace",
"@univerjs/preset-sheets-note",
"@univerjs/preset-sheets-filter",
@@ -42,6 +43,7 @@ const ENGLISH_LOCALE: UniverLocaleSource = {
type: LocaleType.EN_US,
load: () => Promise.all([
import('@univerjs/preset-sheets-core/locales/en-US'),
+ import('@univerjs/preset-sheets-drawing/locales/en-US'),
import('@univerjs/preset-sheets-find-replace/locales/en-US'),
import('@univerjs/preset-sheets-note/locales/en-US'),
import('@univerjs/preset-sheets-filter/locales/en-US'),
@@ -70,6 +72,7 @@ export const UNIVER_LOCALES: Record<DISPLAYABLE_LOCALE_IDS, UniverLocaleSource |
type: LocaleType.ZH_CN,
load: () => Promise.all([
import('@univerjs/preset-sheets-core/locales/zh-CN'),
+ import('@univerjs/preset-sheets-drawing/locales/zh-CN'),
import('@univerjs/preset-sheets-find-replace/locales/zh-CN'),
import('@univerjs/preset-sheets-note/locales/zh-CN'),
import('@univerjs/preset-sheets-filter/locales/zh-CN'),
@@ -89,6 +92,7 @@ export const UNIVER_LOCALES: Record<DISPLAYABLE_LOCALE_IDS, UniverLocaleSource |
type: LocaleType.ES_ES,
load: () => Promise.all([
import('@univerjs/preset-sheets-core/locales/es-ES'),
+ import('@univerjs/preset-sheets-drawing/locales/es-ES'),
import('@univerjs/preset-sheets-find-replace/locales/es-ES'),
import('@univerjs/preset-sheets-note/locales/es-ES'),
import('@univerjs/preset-sheets-filter/locales/es-ES'),
@@ -102,6 +106,7 @@ export const UNIVER_LOCALES: Record<DISPLAYABLE_LOCALE_IDS, UniverLocaleSource |
type: LocaleType.FR_FR,
load: () => Promise.all([
import('@univerjs/preset-sheets-core/locales/fr-FR'),
+ import('@univerjs/preset-sheets-drawing/locales/fr-FR'),
import('@univerjs/preset-sheets-find-replace/locales/fr-FR'),
import('@univerjs/preset-sheets-note/locales/fr-FR'),
import('@univerjs/preset-sheets-filter/locales/fr-FR'),
@@ -119,6 +124,7 @@ export const UNIVER_LOCALES: Record<DISPLAYABLE_LOCALE_IDS, UniverLocaleSource |
type: LocaleType.JA_JP,
load: () => Promise.all([
import('@univerjs/preset-sheets-core/locales/ja-JP'),
+ import('@univerjs/preset-sheets-drawing/locales/ja-JP'),
import('@univerjs/preset-sheets-find-replace/locales/ja-JP'),
import('@univerjs/preset-sheets-note/locales/ja-JP'),
import('@univerjs/preset-sheets-filter/locales/ja-JP'),
@@ -136,6 +142,7 @@ export const UNIVER_LOCALES: Record<DISPLAYABLE_LOCALE_IDS, UniverLocaleSource |
type: LocaleType.RU_RU,
load: () => Promise.all([
import('@univerjs/preset-sheets-core/locales/ru-RU'),
+ import('@univerjs/preset-sheets-drawing/locales/ru-RU'),
import('@univerjs/preset-sheets-find-replace/locales/ru-RU'),
import('@univerjs/preset-sheets-note/locales/ru-RU'),
import('@univerjs/preset-sheets-filter/locales/ru-RU'),
@@ -149,6 +156,7 @@ export const UNIVER_LOCALES: Record<DISPLAYABLE_LOCALE_IDS, UniverLocaleSource |
type: LocaleType.ZH_TW,
load: () => Promise.all([
import('@univerjs/preset-sheets-core/locales/zh-TW'),
+ import('@univerjs/preset-sheets-drawing/locales/zh-TW'),
import('@univerjs/preset-sheets-find-replace/locales/zh-TW'),
import('@univerjs/preset-sheets-note/locales/zh-TW'),
import('@univerjs/preset-sheets-filter/locales/zh-TW'),
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts
index a2903683d4..ff0be9a1cc 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts
@@ -33,6 +33,23 @@ describe("slimWorkbookData", () => {
]);
});
+ it("keeps the drawing resource carrying an inline base64 image, but drops it when empty", () => {
+ // Images are stored as base64 inside the SHEET_DRAWING_PLUGIN resource, so slimming must
+ // preserve a populated drawing resource (otherwise inserted images would vanish on save)
+ // while still discarding the empty "{}" the plugin emits for a sheet with no drawings.
+ const drawingData = "{\"sheet1\":{\"data\":{\"img1\":{\"source\":\"data:image/png;base64,iVBORw0KGgo=\"}}}}";
+ const result = slimWorkbookData(makeWorkbook({
+ resources: [
+ { name: "SHEET_DRAWING_PLUGIN", data: drawingData },
+ { name: "SHEET_NOTE_PLUGIN", data: "{}" }
+ ]
+ }));
+
+ expect(result.resources).toEqual([
+ { name: "SHEET_DRAWING_PLUGIN", data: drawingData }
+ ]);
+ });
+
it("leaves an all-empty resource list as an empty array and tolerates absent resources", () => {
expect(slimWorkbookData(makeWorkbook({
resources: [
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index fe88f65593..3ef9980d6c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -266,6 +266,9 @@ importers:
'@univerjs/preset-sheets-data-validation':
specifier: 0.24.0
+ '@univerjs/preset-sheets-drawing':
+ specifier: 0.24.0
'@univerjs/preset-sheets-filter':
specifier: 0.24.0