@univerjs/docs-drawing 1.0.0-alpha.3 → 1.0.0-alpha.5
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/cjs/facade.js +559 -0
- package/lib/cjs/index.js +500 -83
- package/lib/es/facade.js +558 -0
- package/lib/es/index.js +495 -85
- package/lib/facade.js +558 -0
- package/lib/index.js +495 -85
- package/lib/types/commands/commands/insert-doc-drawing.command.d.ts +26 -0
- package/lib/types/commands/commands/remove-doc-drawing.command.d.ts +28 -0
- package/lib/types/commands/commands/set-drawing-arrange.command.d.ts +25 -0
- package/lib/types/commands/commands/update-doc-drawing-transform.command.d.ts +27 -0
- package/lib/types/commands/commands/update-doc-drawing-wrapping-style.command.d.ts +36 -0
- package/lib/types/controllers/doc-drawing.controller.d.ts +4 -2
- package/lib/types/facade/f-document-image.d.ts +324 -0
- package/lib/types/facade/f-document.d.ts +120 -0
- package/lib/types/facade/f-enum.d.ts +84 -0
- package/lib/types/facade/index.d.ts +19 -0
- package/lib/types/index.d.ts +10 -0
- package/lib/umd/facade.js +1 -0
- package/lib/umd/index.js +1 -1
- package/package.json +16 -4
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let _univerjs_core = require("@univerjs/core");
|
|
3
|
+
let _univerjs_docs = require("@univerjs/docs");
|
|
4
|
+
let _univerjs_docs_drawing = require("@univerjs/docs-drawing");
|
|
5
|
+
let _univerjs_docs_facade = require("@univerjs/docs/facade");
|
|
6
|
+
let _univerjs_drawing = require("@univerjs/drawing");
|
|
7
|
+
let _univerjs_core_facade = require("@univerjs/core/facade");
|
|
8
|
+
|
|
9
|
+
//#region src/facade/f-document-image.ts
|
|
10
|
+
/**
|
|
11
|
+
* Facade API for an image in a document.
|
|
12
|
+
* @hideconstructor
|
|
13
|
+
*/
|
|
14
|
+
var FDocumentImage = class {
|
|
15
|
+
constructor(_document, _imageId, _injector) {
|
|
16
|
+
this._document = _document;
|
|
17
|
+
this._imageId = _imageId;
|
|
18
|
+
this._injector = _injector;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Gets the id of the document containing the image.
|
|
22
|
+
* @returns {string} The document unit id.
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
26
|
+
* const image = fDocument.getImages()[0];
|
|
27
|
+
*
|
|
28
|
+
* if (image) {
|
|
29
|
+
* console.log(image.getUnitId());
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
getUnitId() {
|
|
34
|
+
return this._document.getId();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Gets the drawing id of the image.
|
|
38
|
+
* @returns {string} The drawing id.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
42
|
+
* const image = fDocument.getImages()[0];
|
|
43
|
+
*
|
|
44
|
+
* if (image) {
|
|
45
|
+
* console.log(image.getId());
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
getId() {
|
|
50
|
+
return this._imageId;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets the image source supplied at insertion time.
|
|
54
|
+
* @returns {string | undefined} The image source, or `undefined` when the image no longer exists.
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
58
|
+
* const image = fDocument.getImages()[0];
|
|
59
|
+
*
|
|
60
|
+
* if (image) {
|
|
61
|
+
* console.log(image.getSource());
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
getSource() {
|
|
66
|
+
var _this$getImageData;
|
|
67
|
+
return (_this$getImageData = this.getImageData()) === null || _this$getImageData === void 0 ? void 0 : _this$getImageData.source;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Gets the image source type supplied at insertion time.
|
|
71
|
+
* @returns {ImageSourceType | undefined} The image source type, or `undefined` when the image no longer exists.
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
75
|
+
* const image = fDocument.getImages()[0];
|
|
76
|
+
*
|
|
77
|
+
* if (image) {
|
|
78
|
+
* console.log(image.getSourceType());
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
getSourceType() {
|
|
83
|
+
var _this$getImageData2;
|
|
84
|
+
return (_this$getImageData2 = this.getImageData()) === null || _this$getImageData2 === void 0 ? void 0 : _this$getImageData2.imageSourceType;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Gets the current image size.
|
|
88
|
+
* @returns {ISize | null} The width and height in pixels, or `null` when the image no longer exists.
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
92
|
+
* const image = fDocument.getImages()[0];
|
|
93
|
+
*
|
|
94
|
+
* if (image) {
|
|
95
|
+
* console.log(image.getSize());
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
getSize() {
|
|
100
|
+
var _this$getImageData$do, _this$getImageData3;
|
|
101
|
+
return (_this$getImageData$do = (_this$getImageData3 = this.getImageData()) === null || _this$getImageData3 === void 0 ? void 0 : _this$getImageData3.docTransform.size) !== null && _this$getImageData$do !== void 0 ? _this$getImageData$do : null;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Gets the current clockwise rotation angle.
|
|
105
|
+
* @returns {number | undefined} The rotation angle in degrees, or `undefined` when the image no longer exists.
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
109
|
+
* const image = fDocument.getImages()[0];
|
|
110
|
+
*
|
|
111
|
+
* if (image) {
|
|
112
|
+
* console.log(image.getAngle());
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
getAngle() {
|
|
117
|
+
var _this$getImageData4;
|
|
118
|
+
return (_this$getImageData4 = this.getImageData()) === null || _this$getImageData4 === void 0 ? void 0 : _this$getImageData4.docTransform.angle;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Gets the current horizontal position.
|
|
122
|
+
* @returns {IObjectPositionH | null} The horizontal position, or `null` when the image no longer exists.
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
126
|
+
* const image = fDocument.getImages()[0];
|
|
127
|
+
*
|
|
128
|
+
* if (image) {
|
|
129
|
+
* console.log(image.getPositionH());
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
getPositionH() {
|
|
134
|
+
var _this$getImageData$do2, _this$getImageData5;
|
|
135
|
+
return (_this$getImageData$do2 = (_this$getImageData5 = this.getImageData()) === null || _this$getImageData5 === void 0 ? void 0 : _this$getImageData5.docTransform.positionH) !== null && _this$getImageData$do2 !== void 0 ? _this$getImageData$do2 : null;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Gets the current vertical position.
|
|
139
|
+
* @returns {IObjectPositionV | null} The vertical position, or `null` when the image no longer exists.
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
143
|
+
* const image = fDocument.getImages()[0];
|
|
144
|
+
*
|
|
145
|
+
* if (image) {
|
|
146
|
+
* console.log(image.getPositionV());
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
getPositionV() {
|
|
151
|
+
var _this$getImageData$do3, _this$getImageData6;
|
|
152
|
+
return (_this$getImageData$do3 = (_this$getImageData6 = this.getImageData()) === null || _this$getImageData6 === void 0 ? void 0 : _this$getImageData6.docTransform.positionV) !== null && _this$getImageData$do3 !== void 0 ? _this$getImageData$do3 : null;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Gets the current raw document image data.
|
|
156
|
+
* @returns {IDocImage | null} The image data, or `null` when the image no longer exists.
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
160
|
+
* const image = fDocument.getImages()[0];
|
|
161
|
+
*
|
|
162
|
+
* if (image) {
|
|
163
|
+
* console.log(image.getImageData());
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
getImageData() {
|
|
168
|
+
var _this$_document$getDo;
|
|
169
|
+
const drawing = (_this$_document$getDo = this._document.getDocumentDataModel().getDrawings()) === null || _this$_document$getDo === void 0 ? void 0 : _this$_document$getDo[this._imageId];
|
|
170
|
+
if (!drawing || drawing.drawingType !== _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE) return null;
|
|
171
|
+
return drawing;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Sets the image size.
|
|
175
|
+
* @param {number} width The width in pixels.
|
|
176
|
+
* @param {number} height The height in pixels.
|
|
177
|
+
* @returns {boolean} `true` when the update command succeeds; otherwise, `false`.
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
181
|
+
* const image = fDocument.getImages()[0];
|
|
182
|
+
*
|
|
183
|
+
* if (image) {
|
|
184
|
+
* const success = image.setSize(400, 300);
|
|
185
|
+
* console.log(success);
|
|
186
|
+
* }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
setSize(width, height) {
|
|
190
|
+
return this._updateTransform("size", {
|
|
191
|
+
width,
|
|
192
|
+
height
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Sets the clockwise rotation angle.
|
|
197
|
+
* @param {number} angle The rotation angle in degrees.
|
|
198
|
+
* @returns {boolean} `true` when the update command succeeds; otherwise, `false`.
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
202
|
+
* const image = fDocument.getImages()[0];
|
|
203
|
+
*
|
|
204
|
+
* if (image) {
|
|
205
|
+
* const success = image.setRotate(45);
|
|
206
|
+
* console.log(success);
|
|
207
|
+
* }
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
setRotate(angle) {
|
|
211
|
+
return this._updateTransform("angle", angle);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Sets the horizontal position of the image.
|
|
215
|
+
* Inline images are positioned by their document placeholder, so this has a visible effect only when the image
|
|
216
|
+
* wrapping style is not `TextWrappingStyle.INLINE`.
|
|
217
|
+
* @param {IObjectPositionH} positionH The horizontal position relative to the document.
|
|
218
|
+
* @returns {boolean} `true` when the update command succeeds; otherwise, `false`.
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
222
|
+
* const image = fDocument.getImages()[0];
|
|
223
|
+
*
|
|
224
|
+
* if (image) {
|
|
225
|
+
* const success = image.setPositionH({
|
|
226
|
+
* relativeFrom: univerAPI.Enum.DocsImageRelativeFromH.MARGIN,
|
|
227
|
+
* posOffset: 100
|
|
228
|
+
* });
|
|
229
|
+
* console.log(success);
|
|
230
|
+
* }
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
setPositionH(positionH) {
|
|
234
|
+
return this._updateTransform("positionH", positionH);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Sets the vertical position of the image.
|
|
238
|
+
* Inline images are positioned by their document placeholder, so this has a visible effect only when the image
|
|
239
|
+
* wrapping style is not `TextWrappingStyle.INLINE`.
|
|
240
|
+
* @param {IObjectPositionV} positionV The vertical position relative to the document.
|
|
241
|
+
* @returns {boolean} `true` when the update command succeeds; otherwise, `false`.
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
245
|
+
* const image = fDocument.getImages()[0];
|
|
246
|
+
*
|
|
247
|
+
* if (image) {
|
|
248
|
+
* const success = image.setPositionV({
|
|
249
|
+
* relativeFrom: univerAPI.Enum.DocsImageRelativeFromV.MARGIN,
|
|
250
|
+
* posOffset: 100
|
|
251
|
+
* });
|
|
252
|
+
* console.log(success);
|
|
253
|
+
* }
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
setPositionV(positionV) {
|
|
257
|
+
return this._updateTransform("positionV", positionV);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Sets the image wrapping style.
|
|
261
|
+
* When switching from inline to a floating style in a UI environment, the current visual position is preserved.
|
|
262
|
+
* @param {TextWrappingStyle} wrappingStyle The wrapping style to apply.
|
|
263
|
+
* @returns {boolean} `true` when the update command succeeds; otherwise, `false`.
|
|
264
|
+
* @example
|
|
265
|
+
* ```ts
|
|
266
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
267
|
+
* const image = fDocument.getImages()[0];
|
|
268
|
+
*
|
|
269
|
+
* if (image) {
|
|
270
|
+
* const success = image.setWrappingStyle(univerAPI.Enum.DocsImageWrappingStyle.WRAP_SQUARE);
|
|
271
|
+
* console.log(success);
|
|
272
|
+
* }
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
setWrappingStyle(wrappingStyle) {
|
|
276
|
+
const image = this.getImageData();
|
|
277
|
+
if (!image) return false;
|
|
278
|
+
return this._injector.get(_univerjs_core.ICommandService).syncExecuteCommand(_univerjs_docs_drawing.UpdateDocDrawingWrappingStyleCommand.id, {
|
|
279
|
+
unitId: this.getUnitId(),
|
|
280
|
+
subUnitId: this.getUnitId(),
|
|
281
|
+
drawings: [image],
|
|
282
|
+
wrappingStyle
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Moves the image forward by one level in the drawing order.
|
|
287
|
+
* @returns {boolean} `true` when the arrange command succeeds; otherwise, `false`.
|
|
288
|
+
* @example
|
|
289
|
+
* ```ts
|
|
290
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
291
|
+
* const image = fDocument.getImages()[0];
|
|
292
|
+
*
|
|
293
|
+
* if (image) {
|
|
294
|
+
* const success = image.setForward();
|
|
295
|
+
* console.log(success);
|
|
296
|
+
* }
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
setForward() {
|
|
300
|
+
return this._arrange(_univerjs_core.ArrangeTypeEnum.forward);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Moves the image backward by one level in the drawing order.
|
|
304
|
+
* @returns {boolean} `true` when the arrange command succeeds; otherwise, `false`.
|
|
305
|
+
* @example
|
|
306
|
+
* ```ts
|
|
307
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
308
|
+
* const image = fDocument.getImages()[0];
|
|
309
|
+
*
|
|
310
|
+
* if (image) {
|
|
311
|
+
* const success = image.setBackward();
|
|
312
|
+
* console.log(success);
|
|
313
|
+
* }
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
setBackward() {
|
|
317
|
+
return this._arrange(_univerjs_core.ArrangeTypeEnum.backward);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Moves the image to the back of the drawing order.
|
|
321
|
+
* @returns {boolean} `true` when the arrange command succeeds; otherwise, `false`.
|
|
322
|
+
* @example
|
|
323
|
+
* ```ts
|
|
324
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
325
|
+
* const image = fDocument.getImages()[0];
|
|
326
|
+
*
|
|
327
|
+
* if (image) {
|
|
328
|
+
* const success = image.setBack();
|
|
329
|
+
* console.log(success);
|
|
330
|
+
* }
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
setBack() {
|
|
334
|
+
return this._arrange(_univerjs_core.ArrangeTypeEnum.back);
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Moves the image to the front of the drawing order.
|
|
338
|
+
* @returns {boolean} `true` when the arrange command succeeds; otherwise, `false`.
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
342
|
+
* const image = fDocument.getImages()[0];
|
|
343
|
+
*
|
|
344
|
+
* if (image) {
|
|
345
|
+
* const success = image.setFront();
|
|
346
|
+
* console.log(success);
|
|
347
|
+
* }
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
setFront() {
|
|
351
|
+
return this._arrange(_univerjs_core.ArrangeTypeEnum.front);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Removes the image and its document placeholder.
|
|
355
|
+
* @returns {boolean} `true` when the remove command succeeds; otherwise, `false`.
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* const fDocument = univerAPI.getActiveDocument();
|
|
359
|
+
* const image = fDocument.getImages()[0];
|
|
360
|
+
*
|
|
361
|
+
* if (image) {
|
|
362
|
+
* const success = image.remove();
|
|
363
|
+
* console.log(success);
|
|
364
|
+
* }
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
remove() {
|
|
368
|
+
const image = this.getImageData();
|
|
369
|
+
const textRange = this._getTextRange();
|
|
370
|
+
if (!image || !textRange) return false;
|
|
371
|
+
return this._injector.get(_univerjs_core.ICommandService).syncExecuteCommand(_univerjs_docs_drawing.RemoveDocDrawingCommand.id, {
|
|
372
|
+
unitId: this.getUnitId(),
|
|
373
|
+
drawings: [{
|
|
374
|
+
unitId: this.getUnitId(),
|
|
375
|
+
subUnitId: this.getUnitId(),
|
|
376
|
+
drawingId: this._imageId,
|
|
377
|
+
drawingType: _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE
|
|
378
|
+
}],
|
|
379
|
+
textRange
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
_updateTransform(key, value) {
|
|
383
|
+
return this._injector.get(_univerjs_core.ICommandService).syncExecuteCommand(_univerjs_docs_drawing.UpdateDrawingDocTransformCommand.id, {
|
|
384
|
+
unitId: this.getUnitId(),
|
|
385
|
+
subUnitId: this.getUnitId(),
|
|
386
|
+
drawings: [{
|
|
387
|
+
drawingId: this._imageId,
|
|
388
|
+
key,
|
|
389
|
+
value
|
|
390
|
+
}]
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
_arrange(arrangeType) {
|
|
394
|
+
return this._injector.get(_univerjs_core.ICommandService).syncExecuteCommand(_univerjs_docs_drawing.SetDocDrawingArrangeCommand.id, {
|
|
395
|
+
unitId: this.getUnitId(),
|
|
396
|
+
subUnitId: this.getUnitId(),
|
|
397
|
+
drawingIds: [this._imageId],
|
|
398
|
+
arrangeType
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
_getTextRange() {
|
|
402
|
+
const { body, headers = {}, footers = {} } = this._document.getDocumentDataModel().getSnapshot();
|
|
403
|
+
const segments = [
|
|
404
|
+
{
|
|
405
|
+
segmentId: "",
|
|
406
|
+
body
|
|
407
|
+
},
|
|
408
|
+
...Object.entries(headers).map(([segmentId, header]) => ({
|
|
409
|
+
segmentId,
|
|
410
|
+
body: header.body
|
|
411
|
+
})),
|
|
412
|
+
...Object.entries(footers).map(([segmentId, footer]) => ({
|
|
413
|
+
segmentId,
|
|
414
|
+
body: footer.body
|
|
415
|
+
}))
|
|
416
|
+
];
|
|
417
|
+
for (const { segmentId, body } of segments) {
|
|
418
|
+
var _body$customBlocks;
|
|
419
|
+
const customBlock = body === null || body === void 0 || (_body$customBlocks = body.customBlocks) === null || _body$customBlocks === void 0 ? void 0 : _body$customBlocks.find((block) => block.blockId === this._imageId);
|
|
420
|
+
if (customBlock) return {
|
|
421
|
+
startOffset: customBlock.startIndex,
|
|
422
|
+
endOffset: customBlock.startIndex,
|
|
423
|
+
collapsed: true,
|
|
424
|
+
segmentId
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
//#endregion
|
|
432
|
+
//#region src/facade/f-document.ts
|
|
433
|
+
var FDocumentImageMixin = class extends _univerjs_docs_facade.FDocument {
|
|
434
|
+
async insertImage(options) {
|
|
435
|
+
var _options$wrappingStyl, _options$angle, _options$positionH, _options$positionV;
|
|
436
|
+
const unitId = this.getId();
|
|
437
|
+
const imageId = (0, _univerjs_core.generateRandomId)(6);
|
|
438
|
+
const size = resolveImageSize(await this._getIntrinsicSize(options.source, options.imageSourceType), options);
|
|
439
|
+
const defaultTransform = (0, _univerjs_docs.buildDocTransform)(size.width, size.height);
|
|
440
|
+
const wrappingStyle = (_options$wrappingStyl = options.wrappingStyle) !== null && _options$wrappingStyl !== void 0 ? _options$wrappingStyl : _univerjs_docs_drawing.TextWrappingStyle.INLINE;
|
|
441
|
+
const docTransform = {
|
|
442
|
+
...defaultTransform,
|
|
443
|
+
angle: (_options$angle = options.angle) !== null && _options$angle !== void 0 ? _options$angle : defaultTransform.angle,
|
|
444
|
+
positionH: (_options$positionH = options.positionH) !== null && _options$positionH !== void 0 ? _options$positionH : defaultTransform.positionH,
|
|
445
|
+
positionV: (_options$positionV = options.positionV) !== null && _options$positionV !== void 0 ? _options$positionV : defaultTransform.positionV
|
|
446
|
+
};
|
|
447
|
+
const transform = (0, _univerjs_docs.docDrawingPositionToTransform)(docTransform);
|
|
448
|
+
const drawing = {
|
|
449
|
+
unitId,
|
|
450
|
+
subUnitId: unitId,
|
|
451
|
+
drawingId: imageId,
|
|
452
|
+
drawingType: _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE,
|
|
453
|
+
imageSourceType: options.imageSourceType,
|
|
454
|
+
source: options.source,
|
|
455
|
+
transform,
|
|
456
|
+
docTransform,
|
|
457
|
+
behindDoc: wrappingStyle === _univerjs_docs_drawing.TextWrappingStyle.BEHIND_TEXT ? _univerjs_core.BooleanNumber.TRUE : _univerjs_core.BooleanNumber.FALSE,
|
|
458
|
+
title: "",
|
|
459
|
+
description: "",
|
|
460
|
+
layoutType: _univerjs_docs_drawing.WRAPPING_STYLE_TO_LAYOUT_TYPE[wrappingStyle],
|
|
461
|
+
wrapText: _univerjs_core.WrapTextType.BOTH_SIDES,
|
|
462
|
+
distB: 0,
|
|
463
|
+
distL: 0,
|
|
464
|
+
distR: 0,
|
|
465
|
+
distT: 0
|
|
466
|
+
};
|
|
467
|
+
if (!this._injector.get(_univerjs_core.ICommandService).syncExecuteCommand(_univerjs_docs_drawing.InsertDocDrawingCommand.id, {
|
|
468
|
+
unitId,
|
|
469
|
+
drawings: [drawing],
|
|
470
|
+
textRange: options.textRange
|
|
471
|
+
})) return null;
|
|
472
|
+
return this._injector.createInstance(FDocumentImage, this, imageId, this._injector);
|
|
473
|
+
}
|
|
474
|
+
getImage(imageId) {
|
|
475
|
+
var _this$getDocumentData;
|
|
476
|
+
const drawing = (_this$getDocumentData = this.getDocumentDataModel().getDrawings()) === null || _this$getDocumentData === void 0 ? void 0 : _this$getDocumentData[imageId];
|
|
477
|
+
if (!drawing || drawing.drawingType !== _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE) return null;
|
|
478
|
+
return this._injector.createInstance(FDocumentImage, this, imageId, this._injector);
|
|
479
|
+
}
|
|
480
|
+
getImages() {
|
|
481
|
+
var _documentDataModel$ge, _documentDataModel$ge2;
|
|
482
|
+
const documentDataModel = this.getDocumentDataModel();
|
|
483
|
+
const drawings = (_documentDataModel$ge = documentDataModel.getDrawings()) !== null && _documentDataModel$ge !== void 0 ? _documentDataModel$ge : {};
|
|
484
|
+
return ((_documentDataModel$ge2 = documentDataModel.getDrawingsOrder()) !== null && _documentDataModel$ge2 !== void 0 ? _documentDataModel$ge2 : Object.keys(drawings)).filter((drawingId) => {
|
|
485
|
+
var _drawings$drawingId;
|
|
486
|
+
return ((_drawings$drawingId = drawings[drawingId]) === null || _drawings$drawingId === void 0 ? void 0 : _drawings$drawingId.drawingType) === _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE;
|
|
487
|
+
}).map((drawingId) => this._injector.createInstance(FDocumentImage, this, drawingId, this._injector));
|
|
488
|
+
}
|
|
489
|
+
async _getIntrinsicSize(source, imageSourceType) {
|
|
490
|
+
const imageIoService = this._injector.has(_univerjs_core.IImageIoService) ? this._injector.get(_univerjs_core.IImageIoService) : null;
|
|
491
|
+
let resolvedSource = source;
|
|
492
|
+
if (imageSourceType === _univerjs_core.ImageSourceType.UUID && imageIoService) resolvedSource = await imageIoService.getImage(source);
|
|
493
|
+
else if (imageSourceType === _univerjs_core.ImageSourceType.URL && this._injector.has(_univerjs_core.IURLImageService)) try {
|
|
494
|
+
resolvedSource = await this._injector.get(_univerjs_core.IURLImageService).getImage(source);
|
|
495
|
+
} catch {
|
|
496
|
+
resolvedSource = source;
|
|
497
|
+
}
|
|
498
|
+
const { width, height, image } = await (0, _univerjs_drawing.getImageSize)(resolvedSource);
|
|
499
|
+
imageIoService === null || imageIoService === void 0 || imageIoService.addImageSourceCache(source, imageSourceType, image);
|
|
500
|
+
return {
|
|
501
|
+
width,
|
|
502
|
+
height
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
function resolveImageSize(intrinsicSize, options) {
|
|
507
|
+
const { width: intrinsicWidth, height: intrinsicHeight } = intrinsicSize;
|
|
508
|
+
if (options.width != null && options.height != null) return {
|
|
509
|
+
width: options.width,
|
|
510
|
+
height: options.height
|
|
511
|
+
};
|
|
512
|
+
if (options.width != null) return {
|
|
513
|
+
width: options.width,
|
|
514
|
+
height: intrinsicHeight * options.width / intrinsicWidth
|
|
515
|
+
};
|
|
516
|
+
if (options.height != null) return {
|
|
517
|
+
width: intrinsicWidth * options.height / intrinsicHeight,
|
|
518
|
+
height: options.height
|
|
519
|
+
};
|
|
520
|
+
const scale = Math.min(1, _univerjs_drawing.DRAWING_IMAGE_WIDTH_LIMIT / intrinsicWidth, _univerjs_drawing.DRAWING_IMAGE_HEIGHT_LIMIT / intrinsicHeight);
|
|
521
|
+
return {
|
|
522
|
+
width: intrinsicWidth * scale,
|
|
523
|
+
height: intrinsicHeight * scale
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
_univerjs_docs_facade.FDocument.extend(FDocumentImageMixin);
|
|
527
|
+
|
|
528
|
+
//#endregion
|
|
529
|
+
//#region src/facade/f-enum.ts
|
|
530
|
+
/**
|
|
531
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
532
|
+
*
|
|
533
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
534
|
+
* you may not use this file except in compliance with the License.
|
|
535
|
+
* You may obtain a copy of the License at
|
|
536
|
+
*
|
|
537
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
538
|
+
*
|
|
539
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
540
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
541
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
542
|
+
* See the License for the specific language governing permissions and
|
|
543
|
+
* limitations under the License.
|
|
544
|
+
*/
|
|
545
|
+
var FDocumentImageEnumMixin = class extends _univerjs_core_facade.FEnum {
|
|
546
|
+
get DocsImageWrappingStyle() {
|
|
547
|
+
return _univerjs_docs_drawing.TextWrappingStyle;
|
|
548
|
+
}
|
|
549
|
+
get DocsImageRelativeFromH() {
|
|
550
|
+
return _univerjs_core.ObjectRelativeFromH;
|
|
551
|
+
}
|
|
552
|
+
get DocsImageRelativeFromV() {
|
|
553
|
+
return _univerjs_core.ObjectRelativeFromV;
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
_univerjs_core_facade.FEnum.extend(FDocumentImageEnumMixin);
|
|
557
|
+
|
|
558
|
+
//#endregion
|
|
559
|
+
exports.FDocumentImage = FDocumentImage;
|