@univerjs/drawing 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { CommandType, Disposable, DrawingTypeEnum, ICommandService, IConfigService, IImageIoService, IURLImageService, ImageSourceType, ImageUploadStatusType, Inject, Injector, JSON1, Plugin, createIdentifier, generateRandomId, merge, mergeOverrideWithDependencies, normalizeDrawingOrderIndex, sortRules, sortRulesByDesc, toDisposable } from "@univerjs/core";
1
+ import { CommandType, Disposable, DrawingTypeEnum, ICommandService, IConfigService, IImageIoService, IResourceManagerService, IURLImageService, ImageSourceType, ImageUploadStatusType, Inject, Injector, JSON1, Plugin, UniverInstanceType, createIdentifier, generateRandomId, merge, mergeOverrideWithDependencies, normalizeDrawingOrderIndex, sortRules, sortRulesByDesc, toDisposable } from "@univerjs/core";
2
2
  import { Subject } from "rxjs";
3
3
 
4
- //#region src/basics/config.ts
4
+ // src/basics/config.ts
5
5
  /**
6
6
  * Copyright 2023-present DreamNum Co., Ltd.
7
7
  *
@@ -35,12 +35,10 @@ const DRAWING_IMAGE_ALLOW_IMAGE_LIST = [
35
35
  "image/bmp"
36
36
  ];
37
37
 
38
- //#endregion
39
- //#region src/services/drawing-manager.service.ts
38
+ // src/services/drawing-manager.service.ts
40
39
  const IDrawingManagerService = createIdentifier("univer.drawing-manager.service");
41
40
 
42
- //#endregion
43
- //#region src/commands/operations/set-drawing-selected.operation.ts
41
+ // src/commands/operations/set-drawing-selected.operation.ts
44
42
  const SetDrawingSelectedOperation = {
45
43
  id: "drawing.operation.set-drawing-selected",
46
44
  type: CommandType.OPERATION,
@@ -52,19 +50,123 @@ const SetDrawingSelectedOperation = {
52
50
  }
53
51
  };
54
52
 
55
- //#endregion
56
- //#region package.json
53
+ // src/models/ole-model.ts
54
+ const OLE_RESOURCE_NAME = "UNIVER_OLE_ATTACHMENT_PLUGIN";
55
+ const OLE_MAX_RESOURCE_BYTES = 52428800;
56
+ function getOleOfficeType(mimeType) {
57
+ switch (mimeType) {
58
+ case "application/vnd.ms-excel":
59
+ case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": return UniverInstanceType.UNIVER_SHEET;
60
+ case "application/msword":
61
+ case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": return UniverInstanceType.UNIVER_DOC;
62
+ case "application/vnd.ms-powerpoint":
63
+ case "application/vnd.openxmlformats-officedocument.presentationml.presentation": return UniverInstanceType.UNIVER_SLIDE;
64
+ default: return;
65
+ }
66
+ }
67
+ function parseOleResource(json) {
68
+ if (new TextEncoder().encode(json).byteLength > 52428800) throw new Error("OLE resource exceeds size limit");
69
+ const value = JSON.parse(json);
70
+ if (!value.files || !value.objects || Array.isArray(value.files) || Array.isArray(value.objects)) throw new Error("Invalid OLE resource");
71
+ let total = 0;
72
+ for (const [id, file] of Object.entries(value.files)) {
73
+ var _file$attachment;
74
+ if (!/^[a-f0-9]{64}$/.test(id) || id !== file.sha256 || !Number.isSafeInteger(file.size) || file.size < 0 || typeof file.name !== "string" || typeof file.mimeType !== "string") throw new Error("Invalid OLE file record");
75
+ total += file.size;
76
+ if (total > 52428800 || file.data != null && (typeof file.data !== "string" || file.data.length > Math.ceil(file.size / 3) * 4)) throw new Error("OLE files exceed size limit");
77
+ if (file.data == null && (!((_file$attachment = file.attachment) === null || _file$attachment === void 0 ? void 0 : _file$attachment.source) || file.attachment.sourceType !== "UUID")) throw new Error("Missing OLE attachment source");
78
+ }
79
+ for (const [id, object] of Object.entries(value.objects)) {
80
+ if (object.id !== id || typeof object.drawingId !== "string" || typeof object.subUnitId !== "string" || object.fileName != null && typeof object.fileName !== "string") throw new Error("Invalid OLE object identity");
81
+ for (const fileId of [
82
+ object.fileId,
83
+ object.rawFileId,
84
+ ...Object.values(object.parts)
85
+ ]) if (fileId != null && !Object.prototype.hasOwnProperty.call(value.files, fileId)) throw new Error("Missing OLE file record");
86
+ }
87
+ return {
88
+ files: value.files,
89
+ objects: value.objects
90
+ };
91
+ }
92
+ async function readOleFile(file, io, signal) {
93
+ var _globalThis$crypto;
94
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) throw new Error("OLE request aborted");
95
+ let bytes;
96
+ if (file.data != null) {
97
+ const binary = atob(file.data);
98
+ bytes = Uint8Array.from(binary, (character) => character.charCodeAt(0));
99
+ } else if (file.attachment && io) bytes = await io.read(file.attachment, signal);
100
+ else throw new Error("OLE attachment reader is unavailable");
101
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) throw new Error("OLE request aborted");
102
+ if (bytes.byteLength !== file.size || bytes.byteLength > 52428800) throw new Error("OLE attachment size mismatch");
103
+ const hash = ((_globalThis$crypto = globalThis.crypto) === null || _globalThis$crypto === void 0 ? void 0 : _globalThis$crypto.subtle) ? new Uint8Array(await globalThis.crypto.subtle.digest("SHA-256", bytes)) : (await import("@noble/hashes/sha256")).sha256(bytes);
104
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) throw new Error("OLE request aborted");
105
+ if (Array.from(hash, (byte) => byte.toString(16).padStart(2, "0")).join("") !== file.sha256) throw new Error("OLE attachment integrity mismatch");
106
+ return bytes;
107
+ }
108
+ /** Atomic model conversion: failures leave the caller's resource unchanged. */
109
+ async function prepareOleResource(resource, mode, targetUnitId, io) {
110
+ const candidate = parseOleResource(JSON.stringify(resource));
111
+ if (mode === "unit" && (!targetUnitId || !io)) throw new Error("OLE online import requires a target Unit and attachment IO");
112
+ for (const file of Object.values(candidate.files)) {
113
+ const bytes = await readOleFile(file, io);
114
+ if (mode === "unit") {
115
+ const attachment = await io.upload(new File([bytes], file.name, { type: file.mimeType }), targetUnitId);
116
+ if (!attachment.source || attachment.sourceType !== "UUID" || attachment.size !== file.size) throw new Error("Invalid persisted OLE attachment response");
117
+ file.attachment = attachment;
118
+ delete file.data;
119
+ } else {
120
+ let binary = "";
121
+ for (let offset = 0; offset < bytes.length; offset += 8192) binary += String.fromCharCode(...bytes.subarray(offset, offset + 8192));
122
+ file.data = btoa(binary);
123
+ delete file.attachment;
124
+ }
125
+ }
126
+ return parseOleResource(JSON.stringify(candidate));
127
+ }
128
+ /** Prepare a UnitData model or a native exchange JSON envelope without changing the caller's model. */
129
+ async function prepareOleSnapshot(snapshot, mode, targetUnitId, io) {
130
+ const clone = (value) => {
131
+ if (value instanceof Uint8Array) return Uint8Array.from(value);
132
+ if (Array.isArray(value)) return value.map(clone);
133
+ if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, clone(item)]));
134
+ return value;
135
+ };
136
+ const candidate = clone(snapshot);
137
+ const envelope = candidate.snapshot ?? candidate;
138
+ const host = envelope.workbook ?? envelope.doc ?? envelope.slide ?? envelope;
139
+ let model = host;
140
+ let encoded = false;
141
+ if (envelope.slide && host.originalMeta != null) {
142
+ const bytes = typeof host.originalMeta === "string" ? Uint8Array.from(atob(host.originalMeta), (character) => character.charCodeAt(0)) : host.originalMeta;
143
+ model = JSON.parse(new TextDecoder().decode(bytes));
144
+ encoded = true;
145
+ }
146
+ for (const entry of model.resources ?? []) if (entry.name === "UNIVER_OLE_ATTACHMENT_PLUGIN") {
147
+ const text = typeof entry.data === "string" ? entry.data : new TextDecoder().decode(entry.data);
148
+ const prepared = JSON.stringify(await prepareOleResource(parseOleResource(text), mode, targetUnitId, io));
149
+ entry.data = typeof entry.data === "string" ? prepared : new TextEncoder().encode(prepared);
150
+ }
151
+ if (encoded) {
152
+ const bytes = new TextEncoder().encode(JSON.stringify(model));
153
+ let binary = "";
154
+ for (let offset = 0; offset < bytes.length; offset += 8192) binary += String.fromCharCode(...bytes.subarray(offset, offset + 8192));
155
+ host.originalMeta = typeof host.originalMeta === "string" ? btoa(binary) : bytes;
156
+ }
157
+ return candidate;
158
+ }
159
+
160
+ // package.json
57
161
  var name = "@univerjs/drawing";
58
- var version = "1.3.0";
162
+ var version = "1.4.0";
59
163
 
60
- //#endregion
61
- //#region src/config/config.ts
164
+ // src/config/config.ts
62
165
  const DRAWING_PLUGIN_CONFIG_KEY = "drawing.config";
63
166
  const configSymbol = Symbol(DRAWING_PLUGIN_CONFIG_KEY);
64
167
  const defaultPluginConfig = {};
65
168
 
66
- //#endregion
67
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/typeof.js
169
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/typeof.js
68
170
  function _typeof(o) {
69
171
  "@babel/helpers - typeof";
70
172
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -74,8 +176,7 @@ function _typeof(o) {
74
176
  }, _typeof(o);
75
177
  }
76
178
 
77
- //#endregion
78
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPrimitive.js
179
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/toPrimitive.js
79
180
  function toPrimitive(t, r) {
80
181
  if ("object" != _typeof(t) || !t) return t;
81
182
  var e = t[Symbol.toPrimitive];
@@ -87,15 +188,13 @@ function toPrimitive(t, r) {
87
188
  return ("string" === r ? String : Number)(t);
88
189
  }
89
190
 
90
- //#endregion
91
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPropertyKey.js
191
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/toPropertyKey.js
92
192
  function toPropertyKey(t) {
93
193
  var i = toPrimitive(t, "string");
94
194
  return "symbol" == _typeof(i) ? i : i + "";
95
195
  }
96
196
 
97
- //#endregion
98
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/defineProperty.js
197
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/defineProperty.js
99
198
  function _defineProperty(e, r, t) {
100
199
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
101
200
  value: t,
@@ -105,8 +204,7 @@ function _defineProperty(e, r, t) {
105
204
  }) : e[r] = t, e;
106
205
  }
107
206
 
108
- //#endregion
109
- //#region src/services/drawing-manager-impl.service.ts
207
+ // src/services/drawing-manager-impl.service.ts
110
208
  function isNonEmptyOp(op) {
111
209
  return Array.isArray(op) && op.length > 0;
112
210
  }
@@ -1039,8 +1137,7 @@ var UnitDrawingService = class {
1039
1137
  };
1040
1138
  var DrawingManagerService = class extends UnitDrawingService {};
1041
1139
 
1042
- //#endregion
1043
- //#region src/services/image-io-impl.service.ts
1140
+ // src/services/image-io-impl.service.ts
1044
1141
  var ImageIoService = class {
1045
1142
  constructor() {
1046
1143
  _defineProperty(this, "_waitCount", 0);
@@ -1110,8 +1207,68 @@ var ImageIoService = class {
1110
1207
  }
1111
1208
  };
1112
1209
 
1113
- //#endregion
1114
- //#region src/services/url-image.service.ts
1210
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/decorateParam.js
1211
+ function __decorateParam(paramIndex, decorator) {
1212
+ return function(target, key) {
1213
+ decorator(target, key, paramIndex);
1214
+ };
1215
+ }
1216
+
1217
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/decorate.js
1218
+ function __decorate(decorators, target, key, desc) {
1219
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1220
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1221
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1222
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1223
+ }
1224
+
1225
+ // src/services/ole.service.ts
1226
+ /**
1227
+ * Copyright 2023-present DreamNum Co., Ltd.
1228
+ *
1229
+ * Licensed under the Apache License, Version 2.0 (the "License");
1230
+ * you may not use this file except in compliance with the License.
1231
+ * You may obtain a copy of the License at
1232
+ *
1233
+ * http://www.apache.org/licenses/LICENSE-2.0
1234
+ *
1235
+ * Unless required by applicable law or agreed to in writing, software
1236
+ * distributed under the License is distributed on an "AS IS" BASIS,
1237
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1238
+ * See the License for the specific language governing permissions and
1239
+ * limitations under the License.
1240
+ */
1241
+ let OleService = class OleService extends Disposable {
1242
+ constructor(resourceManager) {
1243
+ super();
1244
+ _defineProperty(this, "_resources", /* @__PURE__ */ new Map());
1245
+ this.disposeWithMe(resourceManager.registerPluginResource({
1246
+ pluginName: OLE_RESOURCE_NAME,
1247
+ businesses: [
1248
+ UniverInstanceType.UNIVER_SHEET,
1249
+ UniverInstanceType.UNIVER_DOC,
1250
+ UniverInstanceType.UNIVER_SLIDE
1251
+ ],
1252
+ onLoad: (unitId, resource) => this._resources.set(unitId, resource),
1253
+ onUnLoad: (unitId) => this._resources.delete(unitId),
1254
+ toJson: (unitId) => JSON.stringify(this._resources.get(unitId) ?? {
1255
+ files: {},
1256
+ objects: {}
1257
+ }),
1258
+ parseJson: parseOleResource
1259
+ }));
1260
+ }
1261
+ getResource(unitId) {
1262
+ return this._resources.get(unitId);
1263
+ }
1264
+ dispose() {
1265
+ this._resources.clear();
1266
+ super.dispose();
1267
+ }
1268
+ };
1269
+ OleService = __decorate([__decorateParam(0, IResourceManagerService)], OleService);
1270
+
1271
+ // src/services/url-image.service.ts
1115
1272
  var URLImageService = class extends Disposable {
1116
1273
  constructor(..._args) {
1117
1274
  super(..._args);
@@ -1142,25 +1299,7 @@ var URLImageService = class extends Disposable {
1142
1299
  }
1143
1300
  };
1144
1301
 
1145
- //#endregion
1146
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/decorateParam.js
1147
- function __decorateParam(paramIndex, decorator) {
1148
- return function(target, key) {
1149
- decorator(target, key, paramIndex);
1150
- };
1151
- }
1152
-
1153
- //#endregion
1154
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/decorate.js
1155
- function __decorate(decorators, target, key, desc) {
1156
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1157
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1158
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1159
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1160
- }
1161
-
1162
- //#endregion
1163
- //#region src/plugin.ts
1302
+ // src/plugin.ts
1164
1303
  let UniverDrawingPlugin = class UniverDrawingPlugin extends Plugin {
1165
1304
  constructor(_config = defaultPluginConfig, _injector, _configService, _commandService) {
1166
1305
  super();
@@ -1175,13 +1314,15 @@ let UniverDrawingPlugin = class UniverDrawingPlugin extends Plugin {
1175
1314
  onStarting() {
1176
1315
  this._initCommands();
1177
1316
  this._initDependencies();
1317
+ this._injector.get(OleService);
1178
1318
  }
1179
1319
  _initDependencies() {
1180
1320
  var _this$_config;
1181
1321
  mergeOverrideWithDependencies([
1182
1322
  [IImageIoService, { useClass: ImageIoService }],
1183
1323
  [IURLImageService, { useClass: URLImageService }],
1184
- [IDrawingManagerService, { useClass: DrawingManagerService }]
1324
+ [IDrawingManagerService, { useClass: DrawingManagerService }],
1325
+ [OleService]
1185
1326
  ], (_this$_config = this._config) === null || _this$_config === void 0 ? void 0 : _this$_config.override).forEach((d) => this._injector.add(d));
1186
1327
  }
1187
1328
  _initCommands() {
@@ -1197,8 +1338,7 @@ UniverDrawingPlugin = __decorate([
1197
1338
  __decorateParam(3, ICommandService)
1198
1339
  ], UniverDrawingPlugin);
1199
1340
 
1200
- //#endregion
1201
- //#region src/utils/drawing-copy-plan.ts
1341
+ // src/utils/drawing-copy-plan.ts
1202
1342
  /**
1203
1343
  * A command-scoped cache key shared by sheet-copy interceptors so every drawing model
1204
1344
  * resolves against the same copied drawing IDs.
@@ -1255,8 +1395,7 @@ function getOrCreateDrawingCopyPlan(copyContext, drawings, options) {
1255
1395
  return plan;
1256
1396
  }
1257
1397
 
1258
- //#endregion
1259
- //#region src/utils/drawing-group.ts
1398
+ // src/utils/drawing-group.ts
1260
1399
  const DRAWING_GROUPABLE_TYPES = [
1261
1400
  DrawingTypeEnum.DRAWING_IMAGE,
1262
1401
  DrawingTypeEnum.DRAWING_SHAPE,
@@ -1269,14 +1408,12 @@ function isGroupableDrawingType(type) {
1269
1408
  return DRAWING_GROUPABLE_TYPES.includes(type);
1270
1409
  }
1271
1410
 
1272
- //#endregion
1273
- //#region src/utils/get-image-shape-key.ts
1411
+ // src/utils/get-image-shape-key.ts
1274
1412
  function getDrawingShapeKeyByDrawingSearch({ unitId, subUnitId, drawingId }, index) {
1275
1413
  return typeof index === "number" ? `${unitId}#-#${subUnitId}#-#${drawingId}#-#${index}` : `${unitId}#-#${subUnitId}#-#${drawingId}`;
1276
1414
  }
1277
1415
 
1278
- //#endregion
1279
- //#region src/utils/get-image-size.ts
1416
+ // src/utils/get-image-size.ts
1280
1417
  const getImageSize = async (src) => {
1281
1418
  return new Promise((resolve, reject) => {
1282
1419
  const image = new Image();
@@ -1294,8 +1431,7 @@ const getImageSize = async (src) => {
1294
1431
  });
1295
1432
  };
1296
1433
 
1297
- //#endregion
1298
- //#region src/utils/image-opacity.ts
1434
+ // src/utils/image-opacity.ts
1299
1435
  /**
1300
1436
  * Copyright 2023-present DreamNum Co., Ltd.
1301
1437
  *
@@ -1316,13 +1452,13 @@ function normalizeImageOpacity(opacity) {
1316
1452
  return Math.min(Math.max(opacity, 0), 1);
1317
1453
  }
1318
1454
 
1319
- //#endregion
1320
- //#region src/utils/rotate-enabled.ts
1455
+ // src/utils/rotate-enabled.ts
1321
1456
  function getDrawingKey(drawing) {
1322
1457
  return `${drawing.unitId}\u0000${drawing.subUnitId}\u0000${drawing.drawingId}`;
1323
1458
  }
1324
1459
  function resolveDrawingRotateEnabled(drawing, options = {}, visiting = /* @__PURE__ */ new Set()) {
1325
1460
  var _drawing$transform, _options$getRenderObj, _options$isKnownNonRo;
1461
+ if (drawing.drawingType === DrawingTypeEnum.DRAWING_OLE) return false;
1326
1462
  const drawingKey = getDrawingKey(drawing);
1327
1463
  if (visiting.has(drawingKey)) return true;
1328
1464
  if (drawing.drawingType === DrawingTypeEnum.DRAWING_GROUP) {
@@ -1342,5 +1478,4 @@ function resolveDrawingRotateEnabled(drawing, options = {}, visiting = /* @__PUR
1342
1478
  return true;
1343
1479
  }
1344
1480
 
1345
- //#endregion
1346
- export { DRAWING_COPY_CONTEXT_KEY, DRAWING_GROUPABLE_TYPES, DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT, DrawingManagerService, IDrawingManagerService, ImageIoService, SetDrawingSelectedOperation, URLImageService, UnitDrawingService, UniverDrawingPlugin, createDrawingCopyPlan, getDrawingImageAllowSize, getDrawingShapeKeyByDrawingSearch, getImageSize, getOrCreateDrawingCopyPlan, isGroupableDrawingType, normalizeImageOpacity, resolveDrawingRotateEnabled };
1481
+ export { DRAWING_COPY_CONTEXT_KEY, DRAWING_GROUPABLE_TYPES, DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT, DrawingManagerService, IDrawingManagerService, ImageIoService, OLE_RESOURCE_NAME, OleService, SetDrawingSelectedOperation, URLImageService, UnitDrawingService, UniverDrawingPlugin, createDrawingCopyPlan, getDrawingImageAllowSize, getDrawingShapeKeyByDrawingSearch, getImageSize, getOleOfficeType, getOrCreateDrawingCopyPlan, isGroupableDrawingType, normalizeImageOpacity, prepareOleSnapshot, readOleFile, resolveDrawingRotateEnabled };