@leapfrog/interactive-canvas 1.14.4 → 1.14.6

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.
Files changed (68) hide show
  1. package/package.json +1 -1
  2. package/readme.md +11 -4
  3. package/dist/abstract-interactive-canvas.d.ts +0 -372
  4. package/dist/dimensions/millimeter-dimensions.d.ts +0 -15
  5. package/dist/dimensions/pixel-canvas-dimensions.d.ts +0 -13
  6. package/dist/dimensions/print-6-col-canvas-dimensions.d.ts +0 -18
  7. package/dist/dimensions/print-8-col-canvas-dimensions.d.ts +0 -18
  8. package/dist/font/font-library.d.ts +0 -27
  9. package/dist/font/font-library.interface.d.ts +0 -24
  10. package/dist/font/font.interface.d.ts +0 -20
  11. package/dist/font/unknown-font.d.ts +0 -6
  12. package/dist/headless-interactive-canvas.d.ts +0 -10
  13. package/dist/index.d.ts +0 -54
  14. package/dist/interactive-canvas.d.ts +0 -25
  15. package/dist/interactive-canvas.es.js +0 -3984
  16. package/dist/interactive-canvas.interface.d.ts +0 -297
  17. package/dist/interactive-canvas.js +0 -4017
  18. package/dist/layout/abstract-layout-manager.d.ts +0 -17
  19. package/dist/layout/advanced-layout-manager.d.ts +0 -16
  20. package/dist/layout/function/pin-to-bottom.layout-function.d.ts +0 -4
  21. package/dist/layout/function/redraw-all.layout-function.d.ts +0 -4
  22. package/dist/layout/function/reposition-backgrounds.layout-function.d.ts +0 -4
  23. package/dist/layout/function/reposition-borders.layout-function.d.ts +0 -4
  24. package/dist/layout/function/reposition-cutoffs.layout-function.d.ts +0 -4
  25. package/dist/layout/function/resize-canvas.layout-function.d.ts +0 -4
  26. package/dist/layout/function/stack-objects.layout-function.d.ts +0 -11
  27. package/dist/layout/function/update-object-dimensions.layout-function.d.ts +0 -4
  28. package/dist/layout/layout-manager.enum.d.ts +0 -5
  29. package/dist/layout/layout-manager.interface.d.ts +0 -13
  30. package/dist/layout/standard-layout-manager.d.ts +0 -40
  31. package/dist/mutator/mutable-background-color.interface.d.ts +0 -15
  32. package/dist/mutator/mutable-color.interface.d.ts +0 -6
  33. package/dist/mutator/mutable-image.interface.d.ts +0 -28
  34. package/dist/mutator/mutable-position.interface.d.ts +0 -63
  35. package/dist/mutator/mutable-stroke.interface.d.ts +0 -8
  36. package/dist/mutator/mutable-text-style.interface.d.ts +0 -163
  37. package/dist/mutator/mutable-text.interface.d.ts +0 -80
  38. package/dist/object/abstract-canvas-object.d.ts +0 -207
  39. package/dist/object/canvas-object-data.d.ts +0 -24
  40. package/dist/object/canvas-object.interface.d.ts +0 -109
  41. package/dist/object/impl/background-image.d.ts +0 -38
  42. package/dist/object/impl/border.d.ts +0 -70
  43. package/dist/object/impl/cutoff.d.ts +0 -59
  44. package/dist/object/impl/extended-fabric-object.interface.d.ts +0 -7
  45. package/dist/object/impl/extended-fabric-textbox.interface.d.ts +0 -7
  46. package/dist/object/impl/horizontal-rule.d.ts +0 -83
  47. package/dist/object/impl/image.d.ts +0 -76
  48. package/dist/object/impl/rectangle.d.ts +0 -78
  49. package/dist/object/impl/rich-text.d.ts +0 -39
  50. package/dist/object/impl/text.d.ts +0 -314
  51. package/dist/object/impl/vertical-rule.d.ts +0 -83
  52. package/dist/profile/canvas-profile.interface.d.ts +0 -22
  53. package/dist/profile/default-canvas-profile.d.ts +0 -23
  54. package/dist/types/canvas-dimension-restrictions.interface.d.ts +0 -15
  55. package/dist/types/canvas-dimensions.interface.d.ts +0 -21
  56. package/dist/types/canvas-state.interface.d.ts +0 -11
  57. package/dist/types/dimension-restrictions.interface.d.ts +0 -6
  58. package/dist/types/image-type.enum.d.ts +0 -6
  59. package/dist/types/margin.interface.d.ts +0 -6
  60. package/dist/types/object-dimensions.interface.d.ts +0 -23
  61. package/dist/types/object-type.enum.d.ts +0 -12
  62. package/dist/types/overflow.interface.d.ts +0 -4
  63. package/dist/types/position.interface.d.ts +0 -9
  64. package/dist/types/rect.interface.d.ts +0 -6
  65. package/dist/types/selection-data.interface.d.ts +0 -19
  66. package/dist/types/text-alignment.interface.d.ts +0 -1
  67. package/dist/undo-stack.d.ts +0 -27
  68. package/dist/undo-stack.interface.d.ts +0 -6
@@ -1,3984 +0,0 @@
1
- import * as _ from 'lodash';
2
- import { round, each, values, reduce, clone, replace, find, isEqual } from 'lodash';
3
- import { oc } from 'ts-optchain';
4
- import { BehaviorSubject, Subject } from 'rxjs';
5
- import { fabric } from 'fabric';
6
-
7
- var PIXELS_PER_MM = 3.937;
8
- var MillimeterDimensions = /** @class */ (function () {
9
- function MillimeterDimensions(width, height) {
10
- this.widthUnit = "mm";
11
- this.heightUnit = "mm";
12
- this.width = width;
13
- this.widthMm = Math.max(0, width);
14
- this.widthPx = round(this.widthMm * PIXELS_PER_MM);
15
- this.height = height;
16
- this.heightMm = Math.max(0, height);
17
- this.heightPx = round(this.heightMm * PIXELS_PER_MM);
18
- }
19
- MillimeterDimensions.prototype.withSize = function (width, height) {
20
- return new MillimeterDimensions(width, height);
21
- };
22
- MillimeterDimensions.prototype.widthPxToMm = function (px) {
23
- return px / PIXELS_PER_MM;
24
- };
25
- MillimeterDimensions.prototype.heightPxToMm = function (px) {
26
- return px / PIXELS_PER_MM;
27
- };
28
- return MillimeterDimensions;
29
- }());
30
-
31
- var PixelCanvasDimensions = /** @class */ (function () {
32
- function PixelCanvasDimensions(width, height) {
33
- this.widthUnit = "px";
34
- this.heightUnit = "px";
35
- this.width = width;
36
- this.widthPx = width;
37
- this.height = height;
38
- this.heightPx = height;
39
- }
40
- PixelCanvasDimensions.prototype.withSize = function (width, height) {
41
- return new PixelCanvasDimensions(width, height);
42
- };
43
- PixelCanvasDimensions.prototype.widthPxToMm = function (px) {
44
- return void 0;
45
- };
46
- PixelCanvasDimensions.prototype.heightPxToMm = function (px) {
47
- return void 0;
48
- };
49
- return PixelCanvasDimensions;
50
- }());
51
-
52
- var COLUMN_WIDTH_MM = 41;
53
- var GUTTER_WIDTH_MM = 3;
54
- var PIXELS_PER_MM$1 = 3.937;
55
- var Print6ColCanvasDimensions = /** @class */ (function () {
56
- function Print6ColCanvasDimensions(width, height) {
57
- this.widthUnit = "column(s)";
58
- this.heightUnit = "cm";
59
- this.width = width;
60
- this.gutterCount = Math.max(0, width - 1);
61
- this.gutterTotalMm = this.gutterCount * GUTTER_WIDTH_MM;
62
- this.columnsTotalMm = width * COLUMN_WIDTH_MM;
63
- this.widthMm = Math.max(0, this.columnsTotalMm + this.gutterTotalMm);
64
- this.widthPx = round(this.widthMm * PIXELS_PER_MM$1);
65
- this.height = height;
66
- this.heightMm = Math.max(0, height * 10);
67
- this.heightPx = round(this.heightMm * PIXELS_PER_MM$1);
68
- }
69
- Print6ColCanvasDimensions.prototype.withSize = function (width, height) {
70
- return new Print6ColCanvasDimensions(width, height);
71
- };
72
- Print6ColCanvasDimensions.prototype.widthPxToMm = function (px) {
73
- return px / PIXELS_PER_MM$1;
74
- };
75
- Print6ColCanvasDimensions.prototype.heightPxToMm = function (px) {
76
- return px / PIXELS_PER_MM$1;
77
- };
78
- return Print6ColCanvasDimensions;
79
- }());
80
-
81
- var COLUMN_WIDTH_MM$1 = 30.8772;
82
- var GUTTER_WIDTH_MM$1 = 2.1053;
83
- var PIXELS_PER_MM$2 = 3.937;
84
- var Print8ColCanvasDimensions = /** @class */ (function () {
85
- function Print8ColCanvasDimensions(width, height) {
86
- this.widthUnit = "column(s)";
87
- this.heightUnit = "cm";
88
- this.width = width;
89
- this.gutterCount = Math.max(0, width - 1);
90
- this.gutterTotalMm = this.gutterCount * GUTTER_WIDTH_MM$1;
91
- this.columnsTotalMm = width * COLUMN_WIDTH_MM$1;
92
- this.widthMm = Math.max(0, this.columnsTotalMm + this.gutterTotalMm);
93
- this.widthPx = round(this.widthMm * PIXELS_PER_MM$2);
94
- this.height = height;
95
- this.heightMm = Math.max(0, height * 10);
96
- this.heightPx = round(this.heightMm * PIXELS_PER_MM$2);
97
- }
98
- Print8ColCanvasDimensions.prototype.withSize = function (width, height) {
99
- return new Print8ColCanvasDimensions(width, height);
100
- };
101
- Print8ColCanvasDimensions.prototype.widthPxToMm = function (px) {
102
- return px / PIXELS_PER_MM$2;
103
- };
104
- Print8ColCanvasDimensions.prototype.heightPxToMm = function (px) {
105
- return px / PIXELS_PER_MM$2;
106
- };
107
- return Print8ColCanvasDimensions;
108
- }());
109
-
110
- /*! *****************************************************************************
111
- Copyright (c) Microsoft Corporation.
112
-
113
- Permission to use, copy, modify, and/or distribute this software for any
114
- purpose with or without fee is hereby granted.
115
-
116
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
117
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
118
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
119
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
120
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
121
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
122
- PERFORMANCE OF THIS SOFTWARE.
123
- ***************************************************************************** */
124
- /* global Reflect, Promise */
125
-
126
- var extendStatics = function(d, b) {
127
- extendStatics = Object.setPrototypeOf ||
128
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
129
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
130
- return extendStatics(d, b);
131
- };
132
-
133
- function __extends(d, b) {
134
- extendStatics(d, b);
135
- function __() { this.constructor = d; }
136
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
137
- }
138
-
139
- function __awaiter(thisArg, _arguments, P, generator) {
140
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
141
- return new (P || (P = Promise))(function (resolve, reject) {
142
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
143
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
144
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
145
- step((generator = generator.apply(thisArg, _arguments || [])).next());
146
- });
147
- }
148
-
149
- function __generator(thisArg, body) {
150
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
151
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
152
- function verb(n) { return function (v) { return step([n, v]); }; }
153
- function step(op) {
154
- if (f) throw new TypeError("Generator is already executing.");
155
- while (_) try {
156
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
157
- if (y = 0, t) op = [op[0] & 2, t.value];
158
- switch (op[0]) {
159
- case 0: case 1: t = op; break;
160
- case 4: _.label++; return { value: op[1], done: false };
161
- case 5: _.label++; y = op[1]; op = [0]; continue;
162
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
163
- default:
164
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
165
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
166
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
167
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
168
- if (t[2]) _.ops.pop();
169
- _.trys.pop(); continue;
170
- }
171
- op = body.call(thisArg, _);
172
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
173
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
174
- }
175
- }
176
-
177
- function __spreadArrays() {
178
- for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
179
- for (var r = Array(s), k = 0, i = 0; i < il; i++)
180
- for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
181
- r[k] = a[j];
182
- return r;
183
- }
184
-
185
- var Font = /** @class */ (function () {
186
- function Font() {
187
- }
188
- /**
189
- * @override
190
- * @inheritDoc
191
- */
192
- Font.prototype.isBold = function (fontWeight) {
193
- return fontWeight === this.boldWeight;
194
- };
195
- return Font;
196
- }());
197
-
198
- var UnknownFont = /** @class */ (function (_super) {
199
- __extends(UnknownFont, _super);
200
- function UnknownFont() {
201
- var _this = _super !== null && _super.apply(this, arguments) || this;
202
- _this.family = "_Unknown_";
203
- _this.normalWeight = 400;
204
- _this.boldWeight = 700;
205
- return _this;
206
- }
207
- return UnknownFont;
208
- }(Font));
209
-
210
- var UNKNOWN_FONT = new UnknownFont();
211
- var FontLibrary = /** @class */ (function () {
212
- function FontLibrary() {
213
- this._fonts = {};
214
- }
215
- /**
216
- * @override
217
- * @inheritDoc
218
- */
219
- FontLibrary.prototype.registerFont = function (font) {
220
- this._fonts[font.family] = font;
221
- };
222
- /**
223
- * @override
224
- * @inheritDoc
225
- */
226
- FontLibrary.prototype.registerFonts = function (fonts) {
227
- var _this = this;
228
- each(fonts, function (font) { return _this._fonts[font.family] = font; });
229
- };
230
- /**
231
- * @override
232
- * @inheritDoc
233
- */
234
- FontLibrary.prototype.getFonts = function () {
235
- return values(this._fonts);
236
- };
237
- /**
238
- * @override
239
- * @inheritDoc
240
- */
241
- FontLibrary.prototype.getFont = function (family) {
242
- return this._fonts[family] || UNKNOWN_FONT;
243
- };
244
- return FontLibrary;
245
- }());
246
-
247
- var AbstractLayoutManager = /** @class */ (function () {
248
- function AbstractLayoutManager(canvas) {
249
- this.isEnforcedPositioning = false;
250
- this.enterTextEditImmediatelyOnSelect = false;
251
- this.canvas = canvas;
252
- }
253
- AbstractLayoutManager.prototype.getCanvas = function () {
254
- return this.canvas;
255
- };
256
- AbstractLayoutManager.prototype.onInit = function () {
257
- //noop
258
- };
259
- AbstractLayoutManager.prototype.onProfileChange = function (profile) {
260
- //noop
261
- };
262
- AbstractLayoutManager.prototype.onCanvasDimensionsChange = function () {
263
- //noop
264
- };
265
- AbstractLayoutManager.prototype.onTextChange = function () {
266
- //noop
267
- };
268
- AbstractLayoutManager.prototype.onObjectListChange = function () {
269
- //noop
270
- };
271
- AbstractLayoutManager.prototype.onMarginChange = function () {
272
- //noop
273
- };
274
- return AbstractLayoutManager;
275
- }());
276
-
277
- var RedrawAllLayoutFunction = /** @class */ (function () {
278
- function RedrawAllLayoutFunction() {
279
- }
280
- RedrawAllLayoutFunction.prototype.execute = function (canvas) {
281
- canvas.getObjectsInternal().forEach(function (object) { return object.setCoords(); });
282
- canvas.redraw();
283
- };
284
- return RedrawAllLayoutFunction;
285
- }());
286
-
287
- var RepositionCutoffsLayoutFunction = /** @class */ (function () {
288
- function RepositionCutoffsLayoutFunction() {
289
- }
290
- RepositionCutoffsLayoutFunction.prototype.execute = function (canvas) {
291
- canvas.getObjectsByTypeInternal(ObjectType.CUTOFF)
292
- .forEach(function (cutoff) { return cutoff.updatePosition(); });
293
- };
294
- return RepositionCutoffsLayoutFunction;
295
- }());
296
-
297
- var RepositionBordersLayoutFunction = /** @class */ (function () {
298
- function RepositionBordersLayoutFunction() {
299
- }
300
- RepositionBordersLayoutFunction.prototype.execute = function (canvas) {
301
- canvas.getObjectsByTypeInternal(ObjectType.BORDER)
302
- .forEach(function (border) { return border.updatePosition(); });
303
- };
304
- return RepositionBordersLayoutFunction;
305
- }());
306
-
307
- var PinToBottomLayoutFunction = /** @class */ (function () {
308
- function PinToBottomLayoutFunction() {
309
- }
310
- PinToBottomLayoutFunction.prototype.execute = function (canvas) {
311
- canvas.getObjectsInternal()
312
- .forEach(function (object) { return object.moveToPinnedPosition(); });
313
- };
314
- return PinToBottomLayoutFunction;
315
- }());
316
-
317
- var ResizeCanvasLayoutFunction = /** @class */ (function () {
318
- function ResizeCanvasLayoutFunction() {
319
- }
320
- ResizeCanvasLayoutFunction.prototype.execute = function (canvas) {
321
- if (canvas.getLayoutManager().isEnforcedPositioning)
322
- return;
323
- if (oc(canvas.getDimensionsRestrictions()).fixed(false))
324
- return;
325
- var currentDimensions = canvas.getDimensions();
326
- var baseDimensions = canvas.getBaseDimensions();
327
- var textHeightIncreasePx = _(canvas.getObjectsByTypeInternal(ObjectType.TEXT))
328
- .filter(function (text) { return text.isResizeCanvas(); })
329
- .reduce(function (x, text) { return x + text.getSizeIncreaseSinceCreation(); }, 0);
330
- var increase = baseDimensions.withSize(currentDimensions.width, 0);
331
- while (increase.heightPx < textHeightIncreasePx) {
332
- increase = increase.withSize(increase.width, increase.height + 1);
333
- }
334
- var targetHeight = increase.withSize(increase.width, baseDimensions.height + increase.height);
335
- if (currentDimensions.height !== targetHeight.height) {
336
- canvas.setDimensions(targetHeight);
337
- }
338
- canvas.getObjectsInternal().forEach(function (object) { return object.setCoords(); });
339
- canvas.redraw();
340
- };
341
- return ResizeCanvasLayoutFunction;
342
- }());
343
-
344
- var LayoutManager;
345
- (function (LayoutManager) {
346
- LayoutManager["LEGACY"] = "LEGACY";
347
- LayoutManager["STANDARD"] = "STANDARD";
348
- LayoutManager["ADVANCED"] = "ADVANCED";
349
- })(LayoutManager || (LayoutManager = {}));
350
-
351
- var RepositionBackgroundsLayoutFunction = /** @class */ (function () {
352
- function RepositionBackgroundsLayoutFunction() {
353
- }
354
- RepositionBackgroundsLayoutFunction.prototype.execute = function (canvas) {
355
- canvas.getObjectsByTypeInternal(ObjectType.BACKGROUND_IMAGE)
356
- .forEach(function (border) { return border.updatePosition(); });
357
- };
358
- return RepositionBackgroundsLayoutFunction;
359
- }());
360
-
361
- var AdvancedLayoutManager = /** @class */ (function (_super) {
362
- __extends(AdvancedLayoutManager, _super);
363
- function AdvancedLayoutManager(canvas) {
364
- var _this = _super.call(this, canvas) || this;
365
- _this.key = LayoutManager.ADVANCED;
366
- _this.pinToBottom = new PinToBottomLayoutFunction();
367
- _this.redrawAll = new RedrawAllLayoutFunction();
368
- _this.repositionBorders = new RepositionBordersLayoutFunction();
369
- _this.repositionCutoffs = new RepositionCutoffsLayoutFunction();
370
- _this.repositionBackgrounds = new RepositionBackgroundsLayoutFunction();
371
- _this.resizeCanvas = new ResizeCanvasLayoutFunction();
372
- return _this;
373
- }
374
- AdvancedLayoutManager.prototype.onCanvasDimensionsChange = function () {
375
- this.pinToBottom.execute(this.canvas);
376
- this.repositionCutoffs.execute(this.canvas);
377
- this.repositionBorders.execute(this.canvas);
378
- this.repositionBackgrounds.execute(this.canvas);
379
- this.redrawAll.execute(this.canvas);
380
- };
381
- AdvancedLayoutManager.prototype.onTextChange = function () {
382
- this.resizeCanvas.execute(this.canvas);
383
- };
384
- AdvancedLayoutManager.prototype.onObjectListChange = function () {
385
- this.redrawAll.execute(this.canvas);
386
- };
387
- return AdvancedLayoutManager;
388
- }(AbstractLayoutManager));
389
-
390
- var UpdateObjectDimensionsLayoutFunction = /** @class */ (function () {
391
- function UpdateObjectDimensionsLayoutFunction() {
392
- }
393
- UpdateObjectDimensionsLayoutFunction.prototype.execute = function (canvas) {
394
- canvas.getObjectsInternal().forEach(function (object) { return object.updateDimensions(); });
395
- };
396
- return UpdateObjectDimensionsLayoutFunction;
397
- }());
398
-
399
- var MIN_MAGNITUDE = 1;
400
- var StackObjectsLayoutFunction = /** @class */ (function () {
401
- function StackObjectsLayoutFunction() {
402
- }
403
- StackObjectsLayoutFunction.prototype.execute = function (canvas) {
404
- var canvasMargin = canvas.getMargin();
405
- var canvasDimensions = canvas.getDimensions();
406
- var allObjects = canvas.getObjectsByTypesInternal(ObjectType.TEXT, ObjectType.RICH_TEXT, ObjectType.IMAGE, ObjectType.RECTANGLE, ObjectType.VERTICAL_RULE, ObjectType.HORIZONTAL_RULE);
407
- var objects = _(allObjects)
408
- .partition(function (object) { return object.isPinToBottom(); })
409
- .value();
410
- var footerObjects = objects[0];
411
- var bodyObjects = objects[1];
412
- var bodyWidth = canvasDimensions.widthPx - canvasMargin.right - canvasMargin.left;
413
- canvas.redraw();
414
- //Make one pass and set the widths of all elements. This will force objects with dynamic heights (like text) to take on their necessary height before trying to lay them out.
415
- this.applyWidths(allObjects, bodyWidth);
416
- var bodyRows = this.getRows(bodyObjects);
417
- var footerRows = this.getRows(footerObjects);
418
- var minimumHeight = canvasMargin.top + bodyRows.totalHeight + footerRows.totalHeight + canvasMargin.bottom;
419
- this.adjustCanvasSize(canvas, minimumHeight);
420
- this.layoutFooter(footerRows, canvas);
421
- this.layoutBody(bodyRows, canvas, footerRows.totalHeight);
422
- };
423
- StackObjectsLayoutFunction.prototype.layoutFooter = function (footerRows, canvas) {
424
- var canvasDimensions = canvas.getDimensions();
425
- var canvasMargin = canvas.getMargin();
426
- var layoutAreaWidth = canvasDimensions.widthPx - canvasMargin.left - canvasMargin.right;
427
- var nextObjectTop = canvasDimensions.heightPx - canvasMargin.bottom - footerRows.totalHeight;
428
- var currentColumnWidthPercent = 0;
429
- each(footerRows.rows, function (row) {
430
- each(row.objects, function (object) {
431
- var columnOffset = currentColumnWidthPercent / 100 * layoutAreaWidth;
432
- var objectMargin = oc(object.getMargin()).left(0);
433
- var left = canvasMargin.left + columnOffset + objectMargin;
434
- if (object.getType() === ObjectType.VERTICAL_RULE)
435
- object.setPosition({ height: row.height });
436
- object.setPosition({
437
- top: nextObjectTop + oc(object.getMargin()).top(0),
438
- left: left
439
- });
440
- currentColumnWidthPercent += object.getColumnWidthPercent();
441
- });
442
- currentColumnWidthPercent = 0;
443
- nextObjectTop += row.height;
444
- });
445
- };
446
- StackObjectsLayoutFunction.prototype.layoutBody = function (bodyRows, canvas, footerHeight) {
447
- var canvasDimensions = canvas.getDimensions();
448
- var canvasMargin = canvas.getMargin();
449
- var workableWidth = canvasDimensions.widthPx - canvasMargin.left - canvasMargin.right;
450
- var workableHeight = canvasDimensions.heightPx - canvasMargin.top - canvasMargin.bottom - footerHeight;
451
- var leftoverSpace = workableHeight - bodyRows.totalHeight;
452
- var spacesRequired = this.countSpaces(bodyRows);
453
- var spaceSize = leftoverSpace / spacesRequired;
454
- var nextObjectTop = canvasMargin.top;
455
- var currentColumnWidthPercent = 0;
456
- for (var i = 0; i < bodyRows.rows.length; i++) {
457
- var row = bodyRows.rows[i];
458
- var nextRow = bodyRows.rows[i + 1];
459
- for (var _i = 0, _a = row.objects; _i < _a.length; _i++) {
460
- var object = _a[_i];
461
- var columnOffset = currentColumnWidthPercent / 100 * workableWidth;
462
- var objectMargin = oc(object.getMargin()).left(0);
463
- var objectLeft = canvasMargin.left + columnOffset + objectMargin;
464
- if (object.getType() === ObjectType.TEXT && object.isBulleted())
465
- objectLeft += object.getBulletMargin();
466
- if (object.getType() === ObjectType.TEXT && object.isOrderedList())
467
- objectLeft += object.getOrderedListMargin();
468
- if (object.getType() === ObjectType.VERTICAL_RULE)
469
- object.setPosition({ height: row.height });
470
- object.setPosition({
471
- top: nextObjectTop + oc(object.getMargin()).top(0),
472
- left: objectLeft
473
- });
474
- currentColumnWidthPercent += object.getColumnWidthPercent();
475
- }
476
- currentColumnWidthPercent = 0;
477
- nextObjectTop += row.height;
478
- var thisRowIsHR = row.objects.length === 1 && row.objects[0].getType() === ObjectType.HORIZONTAL_RULE;
479
- var nextRowIsHR = oc(nextRow).objects.length() === 1 && oc(nextRow).objects()[0].getType() === ObjectType.HORIZONTAL_RULE;
480
- if (!thisRowIsHR && !nextRowIsHR) {
481
- nextObjectTop += spaceSize;
482
- }
483
- }
484
- };
485
- StackObjectsLayoutFunction.prototype.applyWidths = function (objects, layoutWidth) {
486
- _(objects).forEach(function (object) {
487
- var targetWidth = object.getColumnWidthPercent() / 100 * layoutWidth;
488
- var actualWidth = targetWidth - oc(object.getMargin()).left(0) - oc(object.getMargin()).right(0);
489
- if (object.getType() === ObjectType.VERTICAL_RULE)
490
- return;
491
- if (object.getType() === ObjectType.TEXT && object.isBulleted())
492
- actualWidth -= object.getBulletMargin();
493
- if (object.getType() === ObjectType.TEXT && object.isOrderedList())
494
- actualWidth -= object.getOrderedListMargin();
495
- object.setPosition({
496
- width: actualWidth
497
- });
498
- object.updateDimensions();
499
- });
500
- };
501
- StackObjectsLayoutFunction.prototype.adjustCanvasSize = function (canvas, requiredHeight) {
502
- if (oc(canvas.getDimensionsRestrictions()).fixed(false))
503
- return;
504
- var dimensions = canvas.getDimensions();
505
- var minHeight = oc(canvas.getDimensionsRestrictions()).height.min(MIN_MAGNITUDE);
506
- var targetDimensions = canvas.getDimensions().withSize(dimensions.width, minHeight);
507
- while (targetDimensions.heightPx < requiredHeight) {
508
- targetDimensions = targetDimensions.withSize(dimensions.width, targetDimensions.height + 1);
509
- }
510
- if (dimensions.height !== targetDimensions.height) {
511
- canvas.setDimensions(targetDimensions);
512
- }
513
- };
514
- StackObjectsLayoutFunction.prototype.getRows = function (objectsToLayout) {
515
- var _this = this;
516
- var result = { totalHeight: 0, rows: [] };
517
- var currentWidth = 0;
518
- var row = { objects: [], height: 1 };
519
- each(objectsToLayout, function (object) {
520
- var nextWidth = object.getColumnWidthPercent();
521
- var objectHeight = _this.getObjectHeight(object);
522
- if (currentWidth + nextWidth > 100) {
523
- result.rows.push(row);
524
- row = { objects: [], height: 1 };
525
- currentWidth = 0;
526
- }
527
- row.objects.push(object);
528
- if (object.getType() !== ObjectType.VERTICAL_RULE)
529
- row.height = Math.max(row.height, objectHeight);
530
- currentWidth += nextWidth;
531
- });
532
- if (row.objects.length)
533
- result.rows.push(row);
534
- result.totalHeight = reduce(result.rows, function (x, y) { return x + y.height; }, 0);
535
- return result;
536
- };
537
- StackObjectsLayoutFunction.prototype.getObjectHeight = function (element) {
538
- return element.getBoundingBox().height
539
- + oc(element.getMargin()).top(0)
540
- + oc(element.getMargin()).bottom(0);
541
- };
542
- StackObjectsLayoutFunction.prototype.countSpaces = function (rows) {
543
- var count = 0;
544
- for (var i = 0; i < rows.rows.length - 1; i++) {
545
- var row = rows.rows[i];
546
- var nextRow = rows.rows[i + 1];
547
- var thisRowIsHR = row.objects.length === 1 && row.objects[0].getType() === ObjectType.HORIZONTAL_RULE;
548
- var nextRowIsHR = oc(nextRow).objects.length() === 1 && oc(nextRow).objects()[0].getType() === ObjectType.HORIZONTAL_RULE;
549
- if (!thisRowIsHR && !nextRowIsHR) {
550
- count++;
551
- }
552
- }
553
- return count;
554
- };
555
- return StackObjectsLayoutFunction;
556
- }());
557
-
558
- var StandardLayoutManager = /** @class */ (function (_super) {
559
- __extends(StandardLayoutManager, _super);
560
- function StandardLayoutManager(canvas) {
561
- var _this = _super.call(this, canvas) || this;
562
- _this.key = LayoutManager.STANDARD;
563
- _this.isEnforcedPositioning = true;
564
- _this.enterTextEditImmediatelyOnSelect = true;
565
- _this.redrawAll = new RedrawAllLayoutFunction();
566
- _this.repositionCutoffs = new RepositionCutoffsLayoutFunction();
567
- _this.repositionBorders = new RepositionBordersLayoutFunction();
568
- _this.repositionBackgrounds = new RepositionBackgroundsLayoutFunction();
569
- _this.stackObjects = new StackObjectsLayoutFunction();
570
- _this.updateObjectDimensions = new UpdateObjectDimensionsLayoutFunction();
571
- return _this;
572
- }
573
- /**
574
- * @override
575
- */
576
- StandardLayoutManager.prototype.onInit = function () {
577
- this.stackAndRedraw();
578
- };
579
- /**
580
- * @override
581
- */
582
- StandardLayoutManager.prototype.onProfileChange = function (profile) {
583
- this.canvas.setMultiSelect(false);
584
- this.redrawAll.execute(this.canvas);
585
- };
586
- /**
587
- * @override
588
- */
589
- StandardLayoutManager.prototype.onCanvasDimensionsChange = function () {
590
- this.repositionCutoffs.execute(this.canvas);
591
- this.repositionBorders.execute(this.canvas);
592
- this.repositionBackgrounds.execute(this.canvas);
593
- };
594
- /**
595
- * @override
596
- */
597
- StandardLayoutManager.prototype.onTextChange = function () {
598
- this.stackAndRedraw();
599
- };
600
- /**
601
- * @override
602
- */
603
- StandardLayoutManager.prototype.onObjectListChange = function () {
604
- this.stackAndRedraw();
605
- };
606
- /**
607
- * @override
608
- */
609
- StandardLayoutManager.prototype.onMarginChange = function () {
610
- this.stackAndRedraw();
611
- };
612
- StandardLayoutManager.prototype.stackAndRedraw = function () {
613
- this.stackObjects.execute(this.canvas);
614
- this.repositionCutoffs.execute(this.canvas);
615
- this.repositionBorders.execute(this.canvas);
616
- this.repositionBackgrounds.execute(this.canvas);
617
- this.updateObjectDimensions.execute(this.canvas);
618
- this.redrawAll.execute(this.canvas);
619
- };
620
- return StandardLayoutManager;
621
- }(AbstractLayoutManager));
622
-
623
- function isMutableBackgroundColor(object) {
624
- return "setBackgroundColor" in object;
625
- }
626
-
627
- function isMutableColor(object) {
628
- return "setColor" in object;
629
- }
630
-
631
- function isMutableImage(object) {
632
- return "setImageUrl" in object;
633
- }
634
-
635
- function isMutablePosition(object) {
636
- return "centerHorizontally" in object;
637
- }
638
-
639
- function isMutableStroke(object) {
640
- return "setStrokeColor" in object;
641
- }
642
-
643
- function isMutableTextStyle(object) {
644
- return "getTextSelection" in object;
645
- }
646
-
647
- function isMutableText(object) {
648
- return "setText" in object;
649
- }
650
-
651
- /**
652
- * Data for a persisted canvas object.
653
- */
654
- var CanvasObjectData = /** @class */ (function () {
655
- function CanvasObjectData(type, id) {
656
- this.id = id;
657
- this.type = type;
658
- }
659
- return CanvasObjectData;
660
- }());
661
-
662
- var ROTATION_SNAP_ANGLE = 45;
663
- var AbstractCanvasObject = /** @class */ (function () {
664
- /**
665
- * Create a new instance.
666
- * @param type Object type.
667
- * @param canvas Interactive canvas that the field will be rendered on.
668
- * @param fabricObject The fabric object to bind this field to.
669
- * @param data Existing persisted field data to load from, if applicable.
670
- */
671
- function AbstractCanvasObject(type, canvas, fabricObject, data) {
672
- var _this = this;
673
- this.canvas = canvas;
674
- this.fabricObject = fabricObject;
675
- this.data = data ? data : new CanvasObjectData(type, this.fabricObject.id);
676
- this.dimensions$ = new BehaviorSubject(new FieldDimensions(this.fabricObject, this.canvas));
677
- this.opacity$ = new BehaviorSubject(this.fabricObject.opacity);
678
- this.updatePinToBottomDistance();
679
- this.canvas.getProfile$().subscribe(function (profile) { return _this.onProfileChange(profile); });
680
- this.canvas.getLayoutManager$().subscribe(function (layoutManager) { return _this.onLayoutManagerChange(layoutManager); });
681
- this.fabricObject.on("moving", function (e) { return _this.onMove(e); });
682
- this.fabricObject.on("rotating", function (e) { return _this.onRotate(e); });
683
- this.fabricObject.on("scaling", function (e) { return _this.onScale(e); });
684
- this.fabricObject.on("scaled", function (e) { return _this.afterScale(e); });
685
- }
686
- /**
687
- * @override
688
- * @inheritDoc
689
- */
690
- AbstractCanvasObject.prototype.getId = function () {
691
- return this.data.id;
692
- };
693
- /**
694
- * @override
695
- * @inheritDoc
696
- */
697
- AbstractCanvasObject.prototype.getType = function () {
698
- return this.data.type;
699
- };
700
- /**
701
- * @override
702
- * @inheritDoc
703
- */
704
- AbstractCanvasObject.prototype.getDescription = function () {
705
- switch (this.data.type) {
706
- case ObjectType.BORDER:
707
- return "Border";
708
- case ObjectType.CUTOFF:
709
- return "Bottom Border";
710
- case ObjectType.IMAGE:
711
- return "Image";
712
- case ObjectType.LIBRARY_IMAGE:
713
- return "Library Image";
714
- case ObjectType.RECTANGLE:
715
- return "Rectangle";
716
- case ObjectType.TEXT:
717
- return "Text";
718
- default:
719
- return "Field";
720
- }
721
- };
722
- /**
723
- * @override
724
- * @inheritDoc
725
- */
726
- AbstractCanvasObject.prototype.setSortCaption = function (value) {
727
- this.data.sortCaption = value;
728
- };
729
- /**
730
- * @override
731
- * @inheritDoc
732
- */
733
- AbstractCanvasObject.prototype.isSortCaption = function () {
734
- return this.data.sortCaption;
735
- };
736
- /**
737
- * @override
738
- * @inheritDoc
739
- */
740
- AbstractCanvasObject.prototype.getDimensions = function () {
741
- return this.dimensions$.value;
742
- };
743
- /**
744
- * @override
745
- * @inheritDoc
746
- */
747
- AbstractCanvasObject.prototype.getDimensions$ = function () {
748
- return this.dimensions$;
749
- };
750
- /**
751
- * @override
752
- * @inheritDoc
753
- */
754
- AbstractCanvasObject.prototype.getOpacity = function () {
755
- return this.opacity$.value;
756
- };
757
- /**
758
- * @override
759
- * @inheritDoc
760
- */
761
- AbstractCanvasObject.prototype.getOpacity$ = function () {
762
- return this.opacity$;
763
- };
764
- /**
765
- * @override
766
- * @inheritDoc
767
- */
768
- AbstractCanvasObject.prototype.setOpacity = function (value) {
769
- this.fabricObject.set({ opacity: value });
770
- this.opacity$.next(value);
771
- this.redraw();
772
- };
773
- /**
774
- * @override
775
- * @inheritDoc
776
- */
777
- AbstractCanvasObject.prototype.getMargin = function () {
778
- return this.data.margin || {
779
- top: 0,
780
- right: 0,
781
- bottom: 0,
782
- left: 0
783
- };
784
- };
785
- /**
786
- * @override
787
- * @inheritDoc
788
- */
789
- AbstractCanvasObject.prototype.setMargin = function (margin) {
790
- if (!margin)
791
- return;
792
- var newMargin = oc(this.data).margin({
793
- top: 0,
794
- right: 0,
795
- bottom: 0,
796
- left: 0
797
- });
798
- if (margin.top != undefined)
799
- newMargin.top = margin.top || 0;
800
- if (margin.right != undefined)
801
- newMargin.right = margin.right || 0;
802
- if (margin.bottom != undefined)
803
- newMargin.bottom = margin.bottom || 0;
804
- if (margin.left != undefined)
805
- newMargin.left = margin.left || 0;
806
- this.data.margin = newMargin;
807
- this.canvas.notifyObjectListChanged();
808
- };
809
- /**
810
- * @override
811
- * @inheritDoc
812
- */
813
- AbstractCanvasObject.prototype.getColumnWidthPercent = function () {
814
- return Math.max(Math.min(oc(this.data).columnWidthPercent(100), 100), 0);
815
- };
816
- /**
817
- * @override
818
- * @inheritDoc
819
- */
820
- AbstractCanvasObject.prototype.setColumnWidthPercent = function (percent) {
821
- this.data.columnWidthPercent = percent;
822
- this.canvas.notifyObjectListChanged();
823
- };
824
- /**
825
- * @override
826
- * @inheritDoc
827
- */
828
- AbstractCanvasObject.prototype.bringForward = function () {
829
- this.fabricObject.bringForward();
830
- this.canvas.syncObjectList();
831
- };
832
- /**
833
- * @override
834
- * @inheritDoc
835
- */
836
- AbstractCanvasObject.prototype.bringToFront = function () {
837
- this.fabricObject.bringToFront();
838
- this.canvas.syncObjectList();
839
- };
840
- /**
841
- * @override
842
- * @inheritDoc
843
- */
844
- AbstractCanvasObject.prototype.sendBackward = function () {
845
- this.fabricObject.sendBackwards();
846
- this.canvas.syncObjectList();
847
- };
848
- /**
849
- * @override
850
- * @inheritDoc
851
- */
852
- AbstractCanvasObject.prototype.sendToBack = function () {
853
- this.fabricObject.sendToBack();
854
- this.canvas.syncObjectList();
855
- };
856
- /**
857
- * @override
858
- * @inheritDoc
859
- */
860
- AbstractCanvasObject.prototype.moveUp = function () {
861
- this.sendBackward();
862
- };
863
- /**
864
- * @override
865
- * @inheritDoc
866
- */
867
- AbstractCanvasObject.prototype.moveDown = function () {
868
- this.bringForward();
869
- };
870
- /**
871
- * @override
872
- * @inheritDoc
873
- */
874
- AbstractCanvasObject.prototype.delete = function () {
875
- this.canvas.removeObject(this);
876
- this.onDelete();
877
- };
878
- /**
879
- * @override
880
- * @inheritDoc
881
- */
882
- AbstractCanvasObject.prototype.persist = function () {
883
- return clone(this.data);
884
- };
885
- /**
886
- * @override
887
- * @inheritDoc
888
- */
889
- AbstractCanvasObject.prototype.isPinToBottom = function () {
890
- return this.data.pinToBottom;
891
- };
892
- /**
893
- * @override
894
- * @inheritDoc
895
- */
896
- AbstractCanvasObject.prototype.setPinToBottom = function (value) {
897
- if (value === void 0) { value = true; }
898
- this.updatePinToBottomDistance();
899
- this.data.pinToBottom = value;
900
- this.canvas.notifyObjectListChanged();
901
- this.updateFabricObjectBehavior();
902
- };
903
- /**
904
- * @override
905
- * @inheritDoc
906
- */
907
- AbstractCanvasObject.prototype.setLeft = function (left) {
908
- this.setPosition({ left: left });
909
- };
910
- /**
911
- * @override
912
- * @inheritDoc
913
- */
914
- AbstractCanvasObject.prototype.setTop = function (top) {
915
- this.setPosition({ top: top });
916
- };
917
- /**
918
- * @override
919
- * @inheritDoc
920
- */
921
- AbstractCanvasObject.prototype.setHeight = function (height) {
922
- this.setPosition({ height: height });
923
- };
924
- /**
925
- * @override
926
- * @inheritDoc
927
- */
928
- AbstractCanvasObject.prototype.setWidth = function (width) {
929
- this.setPosition({ width: width });
930
- };
931
- /**
932
- * @override
933
- * @inheritDoc
934
- */
935
- AbstractCanvasObject.prototype.setAngle = function (angle) {
936
- this.setPosition({ angle: angle });
937
- };
938
- /**
939
- * @override
940
- * @inheritDoc
941
- */
942
- AbstractCanvasObject.prototype.setScaleX = function (scale) {
943
- this.setPosition({ scaleX: scale });
944
- };
945
- /**
946
- * @override
947
- * @inheritDoc
948
- */
949
- AbstractCanvasObject.prototype.setScaleY = function (scale) {
950
- this.setPosition({ scaleY: scale });
951
- };
952
- /**
953
- * Get the underlying fabric object.
954
- * (Internal use only, do not expose this outside the canvas module).
955
- */
956
- AbstractCanvasObject.prototype.getFabricObjectInternal = function () {
957
- return this.fabricObject;
958
- };
959
- AbstractCanvasObject.prototype.setPosition = function (position) {
960
- var scale = this.getDimensions().scale;
961
- if (position.top != undefined)
962
- this.fabricObject.set({ top: position.top });
963
- if (position.left != undefined)
964
- this.fabricObject.set({ left: position.left });
965
- if (position.width != undefined)
966
- this.fabricObject.set({ width: position.width / scale.x });
967
- if (position.height != undefined)
968
- this.fabricObject.set({ height: position.height / scale.y });
969
- if (position.angle != undefined)
970
- this.fabricObject.set({ angle: position.angle });
971
- if (position.scaleX != undefined)
972
- this.fabricObject.set({ scaleX: position.scaleX });
973
- if (position.scaleY != undefined)
974
- this.fabricObject.set({ scaleY: position.scaleY });
975
- this.setCoords();
976
- this.redraw();
977
- };
978
- AbstractCanvasObject.prototype.setCoords = function () {
979
- this.fabricObject.setCoords();
980
- };
981
- AbstractCanvasObject.prototype.updateDimensions = function () {
982
- this.setCoords();
983
- var dimensions = new FieldDimensions(this.fabricObject, this.canvas);
984
- this.dimensions$.next(dimensions);
985
- };
986
- AbstractCanvasObject.prototype.getBoundingBox = function () {
987
- var absolute = true;
988
- var calculate = true;
989
- return this.fabricObject.getBoundingRect(absolute, calculate);
990
- };
991
- AbstractCanvasObject.prototype.moveToPinnedPosition = function () {
992
- if (this.data.pinToBottom) {
993
- this.setPosition({
994
- top: this.canvas.getDimensions().heightPx - this.pinToBottomDistance
995
- });
996
- }
997
- };
998
- AbstractCanvasObject.prototype.updatePinToBottomDistance = function () {
999
- this.pinToBottomDistance = this.canvas.getDimensions().heightPx - this.getDimensions().top.px;
1000
- };
1001
- AbstractCanvasObject.prototype.redraw = function () {
1002
- this.canvas.redraw();
1003
- };
1004
- // noinspection JSUnusedLocalSymbols
1005
- AbstractCanvasObject.prototype.onProfileChange = function (profile) {
1006
- this.updateFabricObjectBehavior();
1007
- this.redraw();
1008
- };
1009
- // noinspection JSUnusedLocalSymbols
1010
- AbstractCanvasObject.prototype.onLayoutManagerChange = function (layoutManager) {
1011
- this.updateFabricObjectBehavior();
1012
- this.redraw();
1013
- };
1014
- // noinspection JSUnusedLocalSymbols
1015
- AbstractCanvasObject.prototype.onMove = function (event) {
1016
- if (!this.canvas.getProfile().allowObjectsToLeaveCanvas) {
1017
- this.preventLeavingCanvas();
1018
- }
1019
- this.updateDimensions();
1020
- };
1021
- AbstractCanvasObject.prototype.onRotate = function (event) {
1022
- if (event.e.shiftKey) {
1023
- this.fabricObject.set({ snapAngle: 0 });
1024
- }
1025
- else {
1026
- this.fabricObject.set({ snapAngle: ROTATION_SNAP_ANGLE });
1027
- }
1028
- this.updateDimensions();
1029
- };
1030
- // noinspection JSUnusedLocalSymbols
1031
- AbstractCanvasObject.prototype.onScale = function (event) {
1032
- this.updateDimensions();
1033
- };
1034
- // noinspection JSUnusedLocalSymbols
1035
- AbstractCanvasObject.prototype.afterScale = function (event) {
1036
- this.updateDimensions();
1037
- };
1038
- AbstractCanvasObject.prototype.onDelete = function () {
1039
- //no-op
1040
- };
1041
- AbstractCanvasObject.prototype.preventLeavingCanvas = function () {
1042
- var overflow = this.getCanvasOverflow();
1043
- if (overflow.hasOverflow) {
1044
- var adjustX = overflow.left || -overflow.right;
1045
- var adjustY = overflow.top || -overflow.bottom;
1046
- this.fabricObject.set({
1047
- top: this.fabricObject.top + adjustY,
1048
- left: this.fabricObject.left + adjustX
1049
- });
1050
- this.redraw();
1051
- }
1052
- };
1053
- AbstractCanvasObject.prototype.getCanvasOverflow = function () {
1054
- var bound = this.getBoundingBox();
1055
- var canvasDimensions = this.canvas.getDimensions();
1056
- var top = Math.max(0, 0 - bound.top);
1057
- var right = Math.max(0, bound.left + bound.width - canvasDimensions.widthPx);
1058
- var bottom = Math.max(0, bound.top + bound.height - canvasDimensions.heightPx);
1059
- var left = Math.max(0, 0 - bound.left);
1060
- return {
1061
- top: top,
1062
- right: right,
1063
- bottom: bottom,
1064
- left: left,
1065
- hasOverflow: top > 0 || right > 0 || bottom > 0 || left > 0
1066
- };
1067
- };
1068
- AbstractCanvasObject.prototype.centerHorizontallyInternal = function () {
1069
- var canvasWidth = this.canvas.getDimensions().widthPx;
1070
- var objectWidth = this.getDimensions().width.px;
1071
- this.fabricObject.set({ left: ((canvasWidth - objectWidth) / 2) - 0.5 });
1072
- this.updateDimensions();
1073
- this.redraw();
1074
- };
1075
- AbstractCanvasObject.prototype.centerVerticallyInternal = function () {
1076
- var canvasHeight = this.canvas.getDimensions().heightPx;
1077
- var objectHeight = this.getDimensions().height.px;
1078
- this.fabricObject.set({ top: ((canvasHeight - objectHeight) / 2) - 0.5 });
1079
- this.redraw();
1080
- this.updateDimensions();
1081
- };
1082
- AbstractCanvasObject.prototype.nudgeInternal = function (x, y) {
1083
- if (x === void 0) { x = 0; }
1084
- if (y === void 0) { y = 0; }
1085
- this.fabricObject.set({
1086
- top: this.getDimensions().top.px + y,
1087
- left: this.getDimensions().left.px + x
1088
- });
1089
- this.redraw();
1090
- this.updateDimensions();
1091
- };
1092
- AbstractCanvasObject.prototype.updateFabricObjectBehavior = function () {
1093
- var allowManualPositioning = !this.canvas.getLayoutManager().isEnforcedPositioning;
1094
- var profile = this.canvas.getProfile();
1095
- var options = {
1096
- allowMove: allowManualPositioning && profile.allowMove && !this.isPinToBottom(),
1097
- allowRotate: allowManualPositioning && profile.allowRotate && !this.isPinToBottom(),
1098
- allowScale: allowManualPositioning && profile.allowScale && !this.isPinToBottom()
1099
- };
1100
- this.fabricObject.set({
1101
- lockMovementX: !options.allowMove,
1102
- lockMovementY: !options.allowMove,
1103
- lockScalingX: !options.allowScale,
1104
- lockScalingY: !options.allowScale,
1105
- lockScalingFlip: true,
1106
- lockRotation: !options.allowRotate
1107
- });
1108
- this.fabricObject.setControlsVisibility({
1109
- tl: options.allowScale,
1110
- tr: options.allowScale,
1111
- br: options.allowScale,
1112
- bl: options.allowScale,
1113
- ml: options.allowScale,
1114
- mt: options.allowScale,
1115
- mr: options.allowScale,
1116
- mb: options.allowScale,
1117
- mtr: options.allowRotate
1118
- });
1119
- };
1120
- return AbstractCanvasObject;
1121
- }());
1122
- var FieldDimensions = /** @class */ (function () {
1123
- function FieldDimensions(fabricObject, canvas) {
1124
- var widthPxToMmFunction = canvas.getDimensions().widthPxToMm;
1125
- var heightPxToMmFunction = canvas.getDimensions().heightPxToMm;
1126
- this.top = { px: fabricObject.top, mm: heightPxToMmFunction(fabricObject.top) };
1127
- this.left = { px: fabricObject.left, mm: widthPxToMmFunction(fabricObject.left) };
1128
- var widthPx = fabricObject.width * fabricObject.scaleX;
1129
- var heightPx = fabricObject.height * fabricObject.scaleY;
1130
- this.width = { px: widthPx, mm: widthPxToMmFunction(widthPx) };
1131
- this.height = { px: heightPx, mm: heightPxToMmFunction(heightPx) };
1132
- this.scale = { x: fabricObject.scaleX, y: fabricObject.scaleY };
1133
- this.angle = fabricObject.angle;
1134
- }
1135
- return FieldDimensions;
1136
- }());
1137
-
1138
- var BackgroundImage = /** @class */ (function (_super) {
1139
- __extends(BackgroundImage, _super);
1140
- function BackgroundImage(canvas, fabricObject, persistedField) {
1141
- var _this = _super.call(this, ObjectType.BACKGROUND_IMAGE, canvas, fabricObject, persistedField) || this;
1142
- _this.imageUrl$ = new BehaviorSubject(_this.fabricObject.getSrc());
1143
- _this.data.imagePath = _this.imageUrl$.value;
1144
- _this.updatePosition();
1145
- return _this;
1146
- }
1147
- BackgroundImage.prototype.updatePosition = function () {
1148
- var _a = this.canvas.getDimensions(), widthPx = _a.widthPx, heightPx = _a.heightPx;
1149
- this.fabricObject.set({
1150
- top: 0,
1151
- left: 0,
1152
- scaleX: widthPx / this.fabricObject.width,
1153
- scaleY: heightPx / this.fabricObject.height
1154
- });
1155
- this.fabricObject.sendToBack();
1156
- this.updateDimensions();
1157
- };
1158
- /**
1159
- * @override
1160
- * @inheritDoc
1161
- */
1162
- BackgroundImage.prototype.updateFabricObjectBehavior = function () {
1163
- _super.prototype.updateFabricObjectBehavior.call(this);
1164
- this.fabricObject.set({
1165
- lockMovementX: true,
1166
- lockMovementY: true,
1167
- lockScalingX: true,
1168
- lockScalingY: true,
1169
- lockScalingFlip: true
1170
- });
1171
- this.fabricObject.setControlsVisibility({
1172
- tl: false,
1173
- tr: false,
1174
- br: false,
1175
- bl: false,
1176
- ml: false,
1177
- mt: false,
1178
- mr: false,
1179
- mb: false,
1180
- mtr: false
1181
- });
1182
- this.redraw();
1183
- };
1184
- /**
1185
- * @override
1186
- * @inheritDoc
1187
- */
1188
- BackgroundImage.prototype.getImageUrl = function () {
1189
- return this.imageUrl$.value;
1190
- };
1191
- /**
1192
- * @override
1193
- * @inheritDoc
1194
- */
1195
- BackgroundImage.prototype.getImageUrl$ = function () {
1196
- return this.imageUrl$;
1197
- };
1198
- /**
1199
- * @override
1200
- * @inheritDoc
1201
- */
1202
- BackgroundImage.prototype.setImageUrl = function (url) {
1203
- return __awaiter(this, void 0, void 0, function () {
1204
- return __generator(this, function (_a) {
1205
- switch (_a.label) {
1206
- case 0: return [4 /*yield*/, this.setCanvasImage(url)];
1207
- case 1:
1208
- _a.sent();
1209
- this.data.imagePath = url;
1210
- this.imageUrl$.next(url);
1211
- this.canvas.notifyObjectListChanged();
1212
- return [2 /*return*/];
1213
- }
1214
- });
1215
- });
1216
- };
1217
- /**
1218
- * @override
1219
- * @inheritDoc
1220
- */
1221
- BackgroundImage.prototype.setImageWidth = function (px, sizeRestrictions) {
1222
- return 0; //no-op
1223
- };
1224
- BackgroundImage.prototype.setCanvasImage = function (url) {
1225
- return __awaiter(this, void 0, void 0, function () {
1226
- var imageOptions, _a, widthPx, heightPx;
1227
- var _this = this;
1228
- return __generator(this, function (_b) {
1229
- switch (_b.label) {
1230
- case 0:
1231
- imageOptions = { crossOrigin: "anonymous" };
1232
- _a = this.canvas.getDimensions(), widthPx = _a.widthPx, heightPx = _a.heightPx;
1233
- return [4 /*yield*/, new Promise(function (resolve) {
1234
- _this.fabricObject.setSrc(url, function (img) {
1235
- img.scaleToWidth(widthPx);
1236
- img.scaleToHeight(heightPx);
1237
- img.setCoords();
1238
- _this.redraw();
1239
- resolve();
1240
- }, imageOptions);
1241
- })];
1242
- case 1: return [2 /*return*/, _b.sent()];
1243
- }
1244
- });
1245
- });
1246
- };
1247
- return BackgroundImage;
1248
- }(AbstractCanvasObject));
1249
-
1250
- var Border = /** @class */ (function (_super) {
1251
- __extends(Border, _super);
1252
- function Border(canvas, fabricObject, persistedField) {
1253
- var _this = _super.call(this, ObjectType.BORDER, canvas, fabricObject, persistedField) || this;
1254
- _this.strokeWidth$ = new BehaviorSubject(0);
1255
- _this.canvas = canvas;
1256
- _this.strokeWidth$.next(_this.fabricObject.strokeWidth);
1257
- _this.fabricObject.set({ "fill": null });
1258
- _this.updatePosition();
1259
- return _this;
1260
- }
1261
- /**
1262
- * @override
1263
- * @inheritDoc
1264
- */
1265
- Border.prototype.getColor = function () {
1266
- return this.fabricObject.stroke;
1267
- };
1268
- /**
1269
- * @override
1270
- * @inheritDoc
1271
- */
1272
- Border.prototype.setColor = function (color) {
1273
- this.fabricObject.set({ stroke: color });
1274
- this.data.color = color;
1275
- this.redraw();
1276
- };
1277
- /**
1278
- * @override
1279
- * @inheritDoc
1280
- */
1281
- Border.prototype.getStrokeWidth = function () {
1282
- return this.fabricObject.strokeWidth;
1283
- };
1284
- /**
1285
- * @override
1286
- * @inheritDoc
1287
- */
1288
- Border.prototype.getStrokeWidth$ = function () {
1289
- return this.strokeWidth$;
1290
- };
1291
- /**
1292
- * @override
1293
- * @inheritDoc
1294
- */
1295
- Border.prototype.setStrokeWidth = function (width) {
1296
- this.strokeWidth$.next(width);
1297
- this.data.strokeWidth = width;
1298
- this.updatePosition();
1299
- this.canvas.notifyMarginChanged();
1300
- this.redraw();
1301
- };
1302
- Border.prototype.getPadding = function () {
1303
- return this.data.padding || {
1304
- top: 0,
1305
- right: 0,
1306
- bottom: 0,
1307
- left: 0
1308
- };
1309
- };
1310
- Border.prototype.setPadding = function (padding) {
1311
- if (!padding)
1312
- return;
1313
- this.data.padding = {
1314
- top: padding.top || 0,
1315
- right: padding.right || 0,
1316
- bottom: padding.bottom || 0,
1317
- left: padding.left || 0
1318
- };
1319
- this.canvas.notifyMarginChanged();
1320
- this.redraw();
1321
- };
1322
- Border.prototype.updatePosition = function () {
1323
- this.fabricObject.set({
1324
- strokeWidth: this.strokeWidth$.value,
1325
- top: 0,
1326
- left: 0,
1327
- width: this.canvas.getDimensions().widthPx - this.strokeWidth$.value,
1328
- height: this.canvas.getDimensions().heightPx - this.strokeWidth$.value
1329
- });
1330
- };
1331
- /**
1332
- * @override
1333
- * @inheritDoc
1334
- */
1335
- Border.prototype.updateFabricObjectBehavior = function () {
1336
- _super.prototype.updateFabricObjectBehavior.call(this);
1337
- this.fabricObject.set({
1338
- lockMovementX: true,
1339
- lockMovementY: true,
1340
- lockScalingX: true,
1341
- lockScalingY: true,
1342
- lockScalingFlip: true
1343
- });
1344
- this.fabricObject.setControlsVisibility({
1345
- tl: false,
1346
- tr: false,
1347
- br: false,
1348
- bl: false,
1349
- ml: false,
1350
- mt: false,
1351
- mr: false,
1352
- mb: false,
1353
- mtr: false
1354
- });
1355
- this.redraw();
1356
- };
1357
- return Border;
1358
- }(AbstractCanvasObject));
1359
-
1360
- var Cutoff = /** @class */ (function (_super) {
1361
- __extends(Cutoff, _super);
1362
- function Cutoff(canvas, fabricObject, persistedField) {
1363
- var _this = _super.call(this, ObjectType.CUTOFF, canvas, fabricObject, persistedField) || this;
1364
- _this.strokeWidth$ = new BehaviorSubject(0);
1365
- _this.canvas = canvas;
1366
- _this.strokeWidth$.next(_this.fabricObject.height);
1367
- _this.updatePosition();
1368
- _this.fabricObject.set({ strokeWidth: 0 });
1369
- return _this;
1370
- }
1371
- /**
1372
- * @override
1373
- * @inheritDoc
1374
- */
1375
- Cutoff.prototype.getColor = function () {
1376
- return this.fabricObject.fill;
1377
- };
1378
- /**
1379
- * @override
1380
- * @inheritDoc
1381
- */
1382
- Cutoff.prototype.setColor = function (color) {
1383
- this.fabricObject.set({ fill: color, stroke: color });
1384
- this.data.color = color;
1385
- this.redraw();
1386
- };
1387
- /**
1388
- * @override
1389
- * @inheritDoc
1390
- */
1391
- Cutoff.prototype.getStrokeWidth = function () {
1392
- // noinspection JSSuspiciousNameCombination
1393
- return this.fabricObject.height;
1394
- };
1395
- /**
1396
- * @override
1397
- * @inheritDoc
1398
- */
1399
- Cutoff.prototype.getStrokeWidth$ = function () {
1400
- return this.strokeWidth$;
1401
- };
1402
- /**
1403
- * @override
1404
- * @inheritDoc
1405
- */
1406
- Cutoff.prototype.setStrokeWidth = function (width) {
1407
- this.strokeWidth$.next(width);
1408
- this.data.strokeWidth = width;
1409
- this.updatePosition();
1410
- this.canvas.notifyMarginChanged();
1411
- this.redraw();
1412
- };
1413
- Cutoff.prototype.updatePosition = function () {
1414
- this.fabricObject.set({
1415
- top: this.canvas.getDimensions().heightPx - this.strokeWidth$.value,
1416
- left: 0,
1417
- width: this.canvas.getDimensions().widthPx,
1418
- height: this.strokeWidth$.value
1419
- });
1420
- };
1421
- /**
1422
- * @override
1423
- * @inheritDoc
1424
- */
1425
- Cutoff.prototype.updateFabricObjectBehavior = function () {
1426
- this.fabricObject.set({
1427
- lockMovementX: true,
1428
- lockMovementY: true,
1429
- lockScalingX: true,
1430
- lockScalingY: true,
1431
- lockScalingFlip: true
1432
- });
1433
- this.fabricObject.setControlsVisibility({
1434
- tl: false,
1435
- tr: false,
1436
- br: false,
1437
- bl: false,
1438
- ml: false,
1439
- mt: false,
1440
- mr: false,
1441
- mb: false,
1442
- mtr: false
1443
- });
1444
- this.redraw();
1445
- };
1446
- return Cutoff;
1447
- }(AbstractCanvasObject));
1448
-
1449
- var HorizontalRule = /** @class */ (function (_super) {
1450
- __extends(HorizontalRule, _super);
1451
- function HorizontalRule(canvas, fabricObject, persistedField) {
1452
- var _this = _super.call(this, ObjectType.HORIZONTAL_RULE, canvas, fabricObject, persistedField) || this;
1453
- _this.strokeWidth$ = new BehaviorSubject(2);
1454
- _this.canvas = canvas;
1455
- _this.strokeWidth$.next(_this.getDimensions().height.px);
1456
- _this.fabricObject.set({ strokeWidth: 0 });
1457
- return _this;
1458
- }
1459
- /**
1460
- * @override
1461
- * @inheritDoc
1462
- */
1463
- HorizontalRule.prototype.getStrokeWidth = function () {
1464
- // noinspection JSSuspiciousNameCombination
1465
- return this.fabricObject.height;
1466
- };
1467
- /**
1468
- * @override
1469
- * @inheritDoc
1470
- */
1471
- HorizontalRule.prototype.getStrokeWidth$ = function () {
1472
- return this.strokeWidth$;
1473
- };
1474
- /**
1475
- * @override
1476
- * @inheritDoc
1477
- */
1478
- HorizontalRule.prototype.setStrokeWidth = function (value) {
1479
- this.strokeWidth$.next(value);
1480
- this.data.strokeWidth = value;
1481
- this.setPosition({ height: value });
1482
- this.canvas.notifyObjectListChanged();
1483
- this.redraw();
1484
- };
1485
- /**
1486
- * @override
1487
- * @inheritDoc
1488
- */
1489
- HorizontalRule.prototype.getColor = function () {
1490
- return this.fabricObject.fill;
1491
- };
1492
- /**
1493
- * @override
1494
- * @inheritDoc
1495
- */
1496
- HorizontalRule.prototype.setColor = function (color) {
1497
- this.fabricObject.set({ fill: color, stroke: color });
1498
- this.data.color = color;
1499
- this.redraw();
1500
- };
1501
- /**
1502
- * @override
1503
- * @inheritDoc
1504
- */
1505
- HorizontalRule.prototype.centerHorizontally = function () {
1506
- _super.prototype.centerHorizontallyInternal.call(this);
1507
- };
1508
- /**
1509
- * @override
1510
- * @inheritDoc
1511
- */
1512
- HorizontalRule.prototype.centerVertically = function () {
1513
- _super.prototype.centerVerticallyInternal.call(this);
1514
- };
1515
- /**
1516
- * @override
1517
- * @inheritDoc
1518
- */
1519
- HorizontalRule.prototype.nudgeUp = function () {
1520
- _super.prototype.nudgeInternal.call(this, 0, -1);
1521
- };
1522
- /**
1523
- * @override
1524
- * @inheritDoc
1525
- */
1526
- HorizontalRule.prototype.nudgeDown = function () {
1527
- _super.prototype.nudgeInternal.call(this, 0, +1);
1528
- };
1529
- /**
1530
- * @override
1531
- * @inheritDoc
1532
- */
1533
- HorizontalRule.prototype.nudgeLeft = function () {
1534
- _super.prototype.nudgeInternal.call(this, -1, 0);
1535
- };
1536
- /**
1537
- * @override
1538
- * @inheritDoc
1539
- */
1540
- HorizontalRule.prototype.nudgeRight = function () {
1541
- _super.prototype.nudgeInternal.call(this, +1, 0);
1542
- };
1543
- return HorizontalRule;
1544
- }(AbstractCanvasObject));
1545
-
1546
- var Image = /** @class */ (function (_super) {
1547
- __extends(Image, _super);
1548
- function Image(canvas, fabricObject, persistedField) {
1549
- var _this = _super.call(this, ObjectType.IMAGE, canvas, fabricObject, persistedField) || this;
1550
- _this.DEFAULT_RESTRICTIONS = {
1551
- minWidth: 1,
1552
- minHeight: 1
1553
- };
1554
- _this.canvas = canvas;
1555
- _this.imageUrl$ = new BehaviorSubject(_this.fabricObject.getSrc());
1556
- _this.data.imagePath = _this.imageUrl$.value;
1557
- _this.layoutWidth = _this.getBoundingBox().width;
1558
- return _this;
1559
- }
1560
- /**
1561
- * @override
1562
- * @inheritDoc
1563
- */
1564
- Image.prototype.getImageUrl = function () {
1565
- return this.imageUrl$.value;
1566
- };
1567
- /**
1568
- * @override
1569
- * @inheritDoc
1570
- */
1571
- Image.prototype.getImageUrl$ = function () {
1572
- return this.imageUrl$;
1573
- };
1574
- /**
1575
- * @override
1576
- * @inheritDoc
1577
- */
1578
- Image.prototype.setImageUrl = function (url) {
1579
- return __awaiter(this, void 0, void 0, function () {
1580
- return __generator(this, function (_a) {
1581
- switch (_a.label) {
1582
- case 0: return [4 /*yield*/, this.setCanvasImage(url)];
1583
- case 1:
1584
- _a.sent();
1585
- this.data.imagePath = url;
1586
- this.imageUrl$.next(url);
1587
- this.canvas.notifyObjectListChanged();
1588
- return [2 /*return*/];
1589
- }
1590
- });
1591
- });
1592
- };
1593
- /**
1594
- * @override
1595
- * @inheritDoc
1596
- */
1597
- Image.prototype.setImageWidth = function (px, sizeRestrictions) {
1598
- if (sizeRestrictions === void 0) { sizeRestrictions = this.DEFAULT_RESTRICTIONS; }
1599
- var minWidth = Math.max(sizeRestrictions.minWidth, this.getMinWidthBasedOnMinHeight(sizeRestrictions.minHeight));
1600
- var maxWidth = Math.min(sizeRestrictions.maxWidth, this.getMaxWidthBasedOnMaxHeight(sizeRestrictions.maxHeight));
1601
- if (px < minWidth) {
1602
- px = minWidth;
1603
- }
1604
- else if (px > maxWidth) {
1605
- px = maxWidth;
1606
- }
1607
- var premultipliedWidth = this.fabricObject.width;
1608
- var newScale = (premultipliedWidth > 0) ? px / premultipliedWidth : 1;
1609
- this.fabricObject.set({ scaleX: newScale, scaleY: newScale });
1610
- this.setCoords();
1611
- this.canvas.notifyObjectListChanged();
1612
- return px;
1613
- };
1614
- /**
1615
- * @override
1616
- * @inheritDoc
1617
- */
1618
- Image.prototype.centerHorizontally = function () {
1619
- _super.prototype.centerHorizontallyInternal.call(this);
1620
- };
1621
- /**
1622
- * @override
1623
- * @inheritDoc
1624
- */
1625
- Image.prototype.centerVertically = function () {
1626
- _super.prototype.centerVerticallyInternal.call(this);
1627
- };
1628
- /**
1629
- * @override
1630
- * @inheritDoc
1631
- */
1632
- Image.prototype.nudgeUp = function () {
1633
- _super.prototype.nudgeInternal.call(this, 0, -1);
1634
- };
1635
- /**
1636
- * @override
1637
- * @inheritDoc
1638
- */
1639
- Image.prototype.nudgeDown = function () {
1640
- _super.prototype.nudgeInternal.call(this, 0, +1);
1641
- };
1642
- /**
1643
- * @override
1644
- * @inheritDoc
1645
- */
1646
- Image.prototype.nudgeLeft = function () {
1647
- _super.prototype.nudgeInternal.call(this, -1, 0);
1648
- };
1649
- /**
1650
- * @override
1651
- * @inheritDoc
1652
- */
1653
- Image.prototype.nudgeRight = function () {
1654
- _super.prototype.nudgeInternal.call(this, +1, 0);
1655
- };
1656
- /**
1657
- * @override
1658
- * @inheritDoc
1659
- */
1660
- Image.prototype.setPosition = function (position) {
1661
- if (this.canvas.getLayoutManager().isEnforcedPositioning) {
1662
- if (position.top != undefined)
1663
- this.fabricObject.set({ top: position.top });
1664
- if (position.width != undefined) {
1665
- this.layoutWidth = position.width;
1666
- }
1667
- if (position.height != undefined) {
1668
- var premultipliedHeight = this.fabricObject.height;
1669
- var newScale = position.height / premultipliedHeight;
1670
- this.fabricObject.set({ scaleX: newScale, scaleY: newScale });
1671
- this.setCoords();
1672
- }
1673
- if (position.left != undefined || position.width != undefined || position.height != undefined) {
1674
- this.centerHorizontallyInLayoutWidth(position.left || 0);
1675
- }
1676
- }
1677
- else {
1678
- _super.prototype.setPosition.call(this, position);
1679
- }
1680
- };
1681
- Image.prototype.centerHorizontallyInLayoutWidth = function (left) {
1682
- var imageWidth = this.getBoundingBox().width;
1683
- this.fabricObject.set({ left: left + (this.layoutWidth / 2) - (imageWidth / 2) });
1684
- };
1685
- Image.prototype.setCanvasImage = function (url) {
1686
- return __awaiter(this, void 0, void 0, function () {
1687
- var imageOptions, maxSizeScale, maxWidth;
1688
- var _this = this;
1689
- return __generator(this, function (_a) {
1690
- switch (_a.label) {
1691
- case 0:
1692
- imageOptions = { crossOrigin: "anonymous" };
1693
- maxSizeScale = 2;
1694
- maxWidth = this.canvas.getDimensions().widthPx / maxSizeScale;
1695
- return [4 /*yield*/, new Promise(function (resolve) {
1696
- _this.fabricObject.setSrc(url, function (img) {
1697
- var newScale = 1;
1698
- var curWidth = newScale * img.width;
1699
- if (curWidth > maxWidth) {
1700
- newScale = maxWidth / curWidth;
1701
- }
1702
- img.set({
1703
- scaleX: newScale,
1704
- scaleY: newScale
1705
- });
1706
- img.setCoords();
1707
- _this.redraw();
1708
- resolve();
1709
- }, imageOptions);
1710
- })];
1711
- case 1: return [2 /*return*/, _a.sent()];
1712
- }
1713
- });
1714
- });
1715
- };
1716
- Image.prototype.getMinWidthBasedOnMinHeight = function (minHeight) {
1717
- if (!minHeight) {
1718
- return 0;
1719
- }
1720
- var premultipliedHeight = this.fabricObject.height;
1721
- var newScale = minHeight / premultipliedHeight;
1722
- return this.getBoundingBox().width * newScale;
1723
- };
1724
- Image.prototype.getMaxWidthBasedOnMaxHeight = function (maxHeight) {
1725
- if (!maxHeight) {
1726
- return Infinity;
1727
- }
1728
- var premultipliedHeight = this.fabricObject.height;
1729
- var newScale = maxHeight / premultipliedHeight;
1730
- return this.getBoundingBox().width * newScale;
1731
- };
1732
- return Image;
1733
- }(AbstractCanvasObject));
1734
-
1735
- var Rectangle = /** @class */ (function (_super) {
1736
- __extends(Rectangle, _super);
1737
- function Rectangle(canvas, fabricObject, persistedField) {
1738
- var _this = _super.call(this, ObjectType.RECTANGLE, canvas, fabricObject, persistedField) || this;
1739
- _this.canvas = canvas;
1740
- return _this;
1741
- }
1742
- /**
1743
- * @override
1744
- * @inheritDoc
1745
- */
1746
- Rectangle.prototype.getColor = function () {
1747
- return this.fabricObject.fill;
1748
- };
1749
- /**
1750
- * @override
1751
- * @inheritDoc
1752
- */
1753
- Rectangle.prototype.setColor = function (color) {
1754
- this.fabricObject.set({ fill: color });
1755
- this.data.color = color;
1756
- this.redraw();
1757
- };
1758
- /**
1759
- * @override
1760
- * @inheritDoc
1761
- */
1762
- Rectangle.prototype.getStrokeColor = function () {
1763
- return this.fabricObject.stroke;
1764
- };
1765
- /**
1766
- * @override
1767
- * @inheritDoc
1768
- */
1769
- Rectangle.prototype.setStrokeColor = function (color) {
1770
- this.fabricObject.set({ stroke: color });
1771
- this.data.color = color;
1772
- this.redraw();
1773
- };
1774
- /**
1775
- * @override
1776
- * @inheritDoc
1777
- */
1778
- Rectangle.prototype.getStrokeWidth = function () {
1779
- return this.fabricObject.strokeWidth;
1780
- };
1781
- /**
1782
- * @override
1783
- * @inheritDoc
1784
- */
1785
- Rectangle.prototype.setStrokeWidth = function (width) {
1786
- this.fabricObject.set({ strokeWidth: width });
1787
- this.data.strokeWidth = width;
1788
- this.redraw();
1789
- };
1790
- /**
1791
- * @override
1792
- * @inheritDoc
1793
- */
1794
- Rectangle.prototype.centerHorizontally = function () {
1795
- _super.prototype.centerHorizontallyInternal.call(this);
1796
- };
1797
- /**
1798
- * @override
1799
- * @inheritDoc
1800
- */
1801
- Rectangle.prototype.centerVertically = function () {
1802
- _super.prototype.centerVerticallyInternal.call(this);
1803
- };
1804
- /**
1805
- * @override
1806
- * @inheritDoc
1807
- */
1808
- Rectangle.prototype.nudgeUp = function () {
1809
- _super.prototype.nudgeInternal.call(this, 0, -1);
1810
- };
1811
- /**
1812
- * @override
1813
- * @inheritDoc
1814
- */
1815
- Rectangle.prototype.nudgeDown = function () {
1816
- _super.prototype.nudgeInternal.call(this, 0, +1);
1817
- };
1818
- /**
1819
- * @override
1820
- * @inheritDoc
1821
- */
1822
- Rectangle.prototype.nudgeLeft = function () {
1823
- _super.prototype.nudgeInternal.call(this, -1, 0);
1824
- };
1825
- /**
1826
- * @override
1827
- * @inheritDoc
1828
- */
1829
- Rectangle.prototype.nudgeRight = function () {
1830
- _super.prototype.nudgeInternal.call(this, +1, 0);
1831
- };
1832
- /**
1833
- * @override
1834
- * @inheritDoc
1835
- */
1836
- Rectangle.prototype.afterScale = function (event) {
1837
- this.updateDimensions();
1838
- var _a = this.getDimensions(), width = _a.width, height = _a.height;
1839
- this.fabricObject.set({ scaleX: 1, scaleY: 1, width: width.px, height: height.px });
1840
- this.updateDimensions();
1841
- };
1842
- return Rectangle;
1843
- }(AbstractCanvasObject));
1844
-
1845
- var RichText = /** @class */ (function (_super) {
1846
- __extends(RichText, _super);
1847
- function RichText(canvas, fabricObject, persistedField) {
1848
- var _this = _super.call(this, canvas, fabricObject, persistedField) || this;
1849
- _this.data.type = ObjectType.RICH_TEXT;
1850
- return _this;
1851
- }
1852
- Object.defineProperty(RichText.prototype, "htmlContent", {
1853
- /**
1854
- * @override
1855
- * @inheritDoc
1856
- */
1857
- get: function () {
1858
- return this.data.userText;
1859
- },
1860
- /**
1861
- * @override
1862
- * @inheritDoc
1863
- */
1864
- set: function (text) {
1865
- this.data.userText = text;
1866
- },
1867
- enumerable: false,
1868
- configurable: true
1869
- });
1870
- /**
1871
- * @override
1872
- * @inheritDoc
1873
- */
1874
- RichText.prototype.setRichTextContent = function (renderedContentUrl, htmlContent) {
1875
- return __awaiter(this, void 0, void 0, function () {
1876
- var canvasWidthPx;
1877
- return __generator(this, function (_a) {
1878
- switch (_a.label) {
1879
- case 0:
1880
- canvasWidthPx = this.canvas.getDimensions().widthPx;
1881
- if (!renderedContentUrl)
1882
- return [2 /*return*/];
1883
- this.htmlContent = htmlContent;
1884
- return [4 /*yield*/, this.setImageUrl(renderedContentUrl)];
1885
- case 1:
1886
- _a.sent();
1887
- this.setImageWidth(canvasWidthPx);
1888
- return [2 /*return*/];
1889
- }
1890
- });
1891
- });
1892
- };
1893
- return RichText;
1894
- }(Image));
1895
-
1896
- var Text = /** @class */ (function (_super) {
1897
- __extends(Text, _super);
1898
- function Text(canvas, fabricObject, persistedField) {
1899
- var _this_1 = _super.call(this, ObjectType.TEXT, canvas, fabricObject, persistedField) || this;
1900
- _this_1.canvas = canvas;
1901
- _this_1.fontLibrary = canvas.getFontLibrary();
1902
- _this_1.baseComponentHeight = _this_1.fabricObject.height;
1903
- _this_1.text$ = new BehaviorSubject(_this_1.fabricObject.text);
1904
- _this_1.data.userText = _this_1.fabricObject.text;
1905
- _this_1.textSelection$ = new BehaviorSubject(new TextSelection(_this_1.fabricObject, _this_1.fontLibrary));
1906
- _this_1.fabricObject.set({ strokeWidth: 0 });
1907
- _this_1.fabricObject.on("changed", function () { return _this_1.onTextChange(); });
1908
- _this_1.fabricObject.on("selection:changed", function () { return _this_1.onSelectionChange(); });
1909
- _this_1.applyBulletListMonkeyPatches();
1910
- return _this_1;
1911
- }
1912
- /**
1913
- * @override
1914
- * @inheritDoc
1915
- */
1916
- Text.prototype.getText = function () {
1917
- return this.text$.value;
1918
- };
1919
- /**
1920
- * @override
1921
- * @inheritDoc
1922
- */
1923
- Text.prototype.getText$ = function () {
1924
- return this.text$;
1925
- };
1926
- /**
1927
- * @override
1928
- * @inheritDoc
1929
- */
1930
- Text.prototype.setText = function (text) {
1931
- var style = this.fabricObject.getStyleAtPosition(0, true);
1932
- this.fabricObject.set({ text: text });
1933
- this.data.userText = this.fabricObject.text;
1934
- this.text$.next(text);
1935
- this.fabricObject.setSelectionStyles(style, 0, text.length);
1936
- this.redraw();
1937
- };
1938
- /**
1939
- * @override
1940
- * @inheritDoc
1941
- */
1942
- Text.prototype.replaceText = function (target, replacement) {
1943
- var start = this.getText().indexOf(target);
1944
- this.fabricObject.insertChars(replacement, null, start, start + target.length);
1945
- this.redraw();
1946
- this.data.userText = this.fabricObject.text;
1947
- this.text$.next(this.fabricObject.text);
1948
- };
1949
- /**
1950
- * @override
1951
- * @inheritDoc
1952
- */
1953
- Text.prototype.insertText = function (text, style, start, end) {
1954
- this.fabricObject.insertChars(text, style, start, end);
1955
- var newCursorLocation = start + text.length;
1956
- this.fabricObject.selectionStart = newCursorLocation;
1957
- this.fabricObject.selectionEnd = newCursorLocation;
1958
- this.redraw();
1959
- this.data.userText = this.fabricObject.text;
1960
- this.text$.next(this.fabricObject.text);
1961
- };
1962
- /**
1963
- * @override
1964
- * @inheritDoc
1965
- */
1966
- Text.prototype.insertTextAtSelection = function (text, style) {
1967
- var selection = this.getTextSelection();
1968
- this.insertText(text, style, selection.start, selection.end);
1969
- };
1970
- /**
1971
- * @override
1972
- * @inheritDoc
1973
- */
1974
- Text.prototype.getColor = function () {
1975
- return this.textSelection$.value.fill;
1976
- };
1977
- /**
1978
- * @override
1979
- * @inheritDoc
1980
- */
1981
- Text.prototype.setColor = function (color) {
1982
- if (this.hasTextSelection()) {
1983
- this.fabricObject.setSelectionStyles({ fill: color });
1984
- }
1985
- else {
1986
- this.fabricObject.set({ fill: color });
1987
- }
1988
- this.data.color = color;
1989
- this.updateTextSelection();
1990
- this.redraw();
1991
- };
1992
- /**
1993
- * @override
1994
- * @inheritDoc
1995
- */
1996
- Text.prototype.getTextSelection = function () {
1997
- return this.textSelection$.value;
1998
- };
1999
- /**
2000
- * @override
2001
- * @inheritDoc
2002
- */
2003
- Text.prototype.getTextSelection$ = function () {
2004
- return this.textSelection$;
2005
- };
2006
- /**
2007
- * @override
2008
- * @inheritDoc
2009
- */
2010
- Text.prototype.setTextAlign = function (alignment) {
2011
- this.fabricObject.set({ textAlign: alignment });
2012
- this.redraw();
2013
- this.updateTextSelection();
2014
- };
2015
- /**
2016
- * @override
2017
- * @inheritDoc
2018
- */
2019
- Text.prototype.setCharSpacing = function (spacing) {
2020
- this.fabricObject.set({ charSpacing: spacing });
2021
- this.redraw();
2022
- this.updateTextSelection();
2023
- };
2024
- /**
2025
- * @override
2026
- * @inheritDoc
2027
- */
2028
- Text.prototype.setLineHeight = function (height) {
2029
- this.fabricObject.set({ lineHeight: height });
2030
- this.redraw();
2031
- this.updateTextSelection();
2032
- };
2033
- /**
2034
- * @override
2035
- * @inheritDoc
2036
- */
2037
- Text.prototype.setBold = function (bold) {
2038
- if (bold === void 0) { bold = true; }
2039
- if (this.hasTextSelection()) {
2040
- var start = this.textSelection$.value.start;
2041
- for (var i = 0; i < this.textSelection$.value.length; i++) {
2042
- var style = this.fabricObject.getStyleAtPosition(start + i, true);
2043
- var font = this.fontLibrary.getFont(style.fontFamily);
2044
- var fontWeight = bold ? font.boldWeight : font.normalWeight;
2045
- this.fabricObject.setSelectionStyles({ fontWeight: fontWeight }, start + i, start + i + 1);
2046
- }
2047
- }
2048
- else {
2049
- var font = this.fontLibrary.getFont(this.fabricObject.fontFamily);
2050
- var fontWeight = bold ? font.boldWeight : font.normalWeight;
2051
- this.fabricObject.set({ fontWeight: fontWeight });
2052
- }
2053
- this.redraw();
2054
- this.updateTextSelection();
2055
- };
2056
- /**
2057
- * @override
2058
- * @inheritDoc
2059
- */
2060
- Text.prototype.setBoldRegion = function (start, end, bold) {
2061
- if (bold === void 0) { bold = true; }
2062
- if (start > end)
2063
- throw Error("Bad region specified: [" + start + " - " + end + "]");
2064
- for (var i = start; i < end; i++) {
2065
- var style = this.fabricObject.getStyleAtPosition(i, true);
2066
- var font = this.fontLibrary.getFont(style.fontFamily);
2067
- style.fontWeight = bold ? font.boldWeight : font.normalWeight;
2068
- this.fabricObject.setSelectionStyles(style, i, i + 1);
2069
- }
2070
- this.redraw();
2071
- };
2072
- /**
2073
- * @override
2074
- * @inheritDoc
2075
- */
2076
- Text.prototype.setItalic = function (italic) {
2077
- if (italic === void 0) { italic = true; }
2078
- if (this.hasTextSelection()) {
2079
- this.fabricObject.setSelectionStyles({ fontStyle: italic ? "italic" : "" });
2080
- }
2081
- else {
2082
- this.fabricObject.set({ fontStyle: italic ? "italic" : "" });
2083
- }
2084
- this.redraw();
2085
- this.updateTextSelection();
2086
- };
2087
- /**
2088
- * @override
2089
- * @inheritDoc
2090
- */
2091
- Text.prototype.setItalicRegion = function (start, end, italic) {
2092
- if (italic === void 0) { italic = true; }
2093
- if (start > end)
2094
- throw Error("Bad region specified: [" + start + " - " + end + "]");
2095
- this.fabricObject.setSelectionStyles({ fontStyle: italic ? "italic" : "" }, start, end);
2096
- this.redraw();
2097
- };
2098
- /**
2099
- * @override
2100
- * @inheritDoc
2101
- */
2102
- Text.prototype.setStrikethrough = function (strikethrough) {
2103
- if (strikethrough === void 0) { strikethrough = true; }
2104
- if (this.hasTextSelection()) {
2105
- this.fabricObject.setSelectionStyles({ linethrough: strikethrough });
2106
- }
2107
- else {
2108
- this.fabricObject.set({ linethrough: strikethrough });
2109
- }
2110
- this.redraw();
2111
- this.updateTextSelection();
2112
- };
2113
- /**
2114
- * @override
2115
- * @inheritDoc
2116
- */
2117
- Text.prototype.setUnderline = function (underline) {
2118
- if (underline === void 0) { underline = true; }
2119
- this.fabricObject.setSelectionStyles({ underline: underline });
2120
- this.redraw();
2121
- this.updateTextSelection();
2122
- };
2123
- /**
2124
- * @override
2125
- * @inheritDoc
2126
- */
2127
- Text.prototype.setFont = function (font) {
2128
- if (this.hasTextSelection()) {
2129
- this.fabricObject.setSelectionStyles({ fontFamily: font.family, fontWeight: font.normalWeight });
2130
- }
2131
- else {
2132
- this.fabricObject.set({ fontFamily: font.family, fontWeight: font.normalWeight });
2133
- }
2134
- this.redraw();
2135
- this.updateTextSelection();
2136
- };
2137
- /**
2138
- * @override
2139
- * @inheritDoc
2140
- */
2141
- Text.prototype.setFontSize = function (fontSize) {
2142
- if (this.hasTextSelection()) {
2143
- this.fabricObject.setSelectionStyles({ fontSize: fontSize });
2144
- }
2145
- else {
2146
- this.fabricObject.set({ fontSize: fontSize });
2147
- }
2148
- this.redraw();
2149
- this.updateTextSelection();
2150
- };
2151
- /**
2152
- * @override
2153
- * @inheritDoc
2154
- */
2155
- Text.prototype.setTextBackgroundColor = function (color) {
2156
- if (this.hasTextSelection()) {
2157
- this.fabricObject.setSelectionStyles({ textBackgroundColor: color });
2158
- }
2159
- else {
2160
- this.fabricObject.set({ textBackgroundColor: color });
2161
- }
2162
- this.redraw();
2163
- this.updateTextSelection();
2164
- };
2165
- /**
2166
- * @override
2167
- * @inheritDoc
2168
- */
2169
- Text.prototype.centerHorizontally = function () {
2170
- _super.prototype.centerHorizontallyInternal.call(this);
2171
- };
2172
- /**
2173
- * @override
2174
- * @inheritDoc
2175
- */
2176
- Text.prototype.centerVertically = function () {
2177
- _super.prototype.centerVerticallyInternal.call(this);
2178
- };
2179
- /**
2180
- * @override
2181
- * @inheritDoc
2182
- */
2183
- Text.prototype.nudgeUp = function () {
2184
- _super.prototype.nudgeInternal.call(this, 0, -1);
2185
- };
2186
- /**
2187
- * @override
2188
- * @inheritDoc
2189
- */
2190
- Text.prototype.nudgeDown = function () {
2191
- _super.prototype.nudgeInternal.call(this, 0, +1);
2192
- };
2193
- /**
2194
- * @override
2195
- * @inheritDoc
2196
- */
2197
- Text.prototype.nudgeLeft = function () {
2198
- _super.prototype.nudgeInternal.call(this, -1, 0);
2199
- };
2200
- /**
2201
- * @override
2202
- * @inheritDoc
2203
- */
2204
- Text.prototype.nudgeRight = function () {
2205
- _super.prototype.nudgeInternal.call(this, +1, 0);
2206
- };
2207
- /**
2208
- * @override
2209
- * @inheritDoc
2210
- */
2211
- Text.prototype.getBackgroundColor = function () {
2212
- return this.fabricObject.backgroundColor;
2213
- };
2214
- /**
2215
- * @override
2216
- * @inheritDoc
2217
- */
2218
- Text.prototype.setBackgroundColor = function (color) {
2219
- this.fabricObject.set({ backgroundColor: color });
2220
- this.redraw();
2221
- };
2222
- /**
2223
- * @override
2224
- * @inheritDoc
2225
- */
2226
- Text.prototype.setPrefillType = function (prefillType) {
2227
- this.data.prefillType = prefillType;
2228
- };
2229
- /**
2230
- * @override
2231
- * @inheritDoc
2232
- */
2233
- Text.prototype.getPrefillType = function () {
2234
- return this.data.prefillType;
2235
- };
2236
- /**
2237
- * @override
2238
- * @inheritDoc
2239
- */
2240
- Text.prototype.getBulletMargin = function () {
2241
- var _this_1 = this;
2242
- if (!this.isBulleted())
2243
- return 0;
2244
- var fontSize = 0;
2245
- each(this.fabricObject._textLines, function (textLine, lineIndex) {
2246
- fontSize = Math.max(fontSize, _this_1.fabricObject.getValueOfPropertyAt(lineIndex, 0, "fontSize"));
2247
- });
2248
- return fontSize * 0.6;
2249
- };
2250
- /**
2251
- * @override
2252
- * @inheritDoc
2253
- */
2254
- Text.prototype.getOrderedListMargin = function () {
2255
- if (!this.isOrderedList())
2256
- return 0;
2257
- var orderedListMarkerEntities = this.fabricObject._orderedListMarkerEntities;
2258
- var max = _(orderedListMarkerEntities)
2259
- .filter(function (x) { return !!x; })
2260
- .map(function (x) { return x.getBoundingRect(true, true).width; })
2261
- .max();
2262
- return (max * 1.2) || 0;
2263
- };
2264
- /**
2265
- * @override
2266
- * @inheritDoc
2267
- */
2268
- Text.prototype.isResizeCanvas = function () {
2269
- return this.data.resizeCanvas;
2270
- };
2271
- /**
2272
- * @override
2273
- * @inheritDoc
2274
- */
2275
- Text.prototype.setResizeCanvas = function (value) {
2276
- this.baseComponentHeight = this.fabricObject.height;
2277
- this.data.resizeCanvas = value;
2278
- };
2279
- /**
2280
- * @override
2281
- * @inheritDoc
2282
- */
2283
- Text.prototype.focus = function () {
2284
- this.fabricObject.hiddenTextarea.focus();
2285
- };
2286
- /**
2287
- * @override
2288
- * @inheritDoc
2289
- */
2290
- Text.prototype.isEditing = function () {
2291
- return this.fabricObject.isEditing;
2292
- };
2293
- /**
2294
- * @override
2295
- * @inheritDoc
2296
- */
2297
- Text.prototype.enterEditing = function () {
2298
- if (!this.isEditing()) {
2299
- this.fabricObject.enterEditing();
2300
- this.focus();
2301
- this.selectAllText();
2302
- this.redraw();
2303
- }
2304
- };
2305
- /**
2306
- * @override
2307
- * @inheritDoc
2308
- */
2309
- Text.prototype.selectAllText = function () {
2310
- this.fabricObject.selectAll();
2311
- };
2312
- /**
2313
- * @override
2314
- * @inheritDoc
2315
- */
2316
- Text.prototype.isBulleted = function () {
2317
- return this.data.bulleted || false;
2318
- };
2319
- /**
2320
- * @override
2321
- * @inheritDoc
2322
- */
2323
- Text.prototype.enableBullets = function () {
2324
- this.disableOrderedList();
2325
- this.fabricObject["objectCaching"] = false;
2326
- this.fabricObject["_bullets"] = this.fabricObject["_bullets"] || {};
2327
- this.data.bulleted = true;
2328
- this.notifyCanvasOfTextChanges();
2329
- this.redraw();
2330
- };
2331
- /**
2332
- * @override
2333
- * @inheritDoc
2334
- */
2335
- Text.prototype.disableBullets = function () {
2336
- this.removeBullets();
2337
- this.data.bulleted = false;
2338
- this.notifyCanvasOfTextChanges();
2339
- this.redraw();
2340
- };
2341
- /**
2342
- * @override
2343
- * @inheritDoc
2344
- */
2345
- Text.prototype.isOrderedList = function () {
2346
- return this.fabricObject._isOrderedList || false;
2347
- };
2348
- /**
2349
- * @override
2350
- * @inheritDoc
2351
- */
2352
- Text.prototype.enableOrderedList = function () {
2353
- this.disableBullets();
2354
- this.fabricObject.objectCaching = false;
2355
- this.fabricObject._isOrderedList = true;
2356
- this.notifyCanvasOfTextChanges();
2357
- this.redraw();
2358
- };
2359
- /**
2360
- * @override
2361
- * @inheritDoc
2362
- */
2363
- Text.prototype.disableOrderedList = function () {
2364
- this.removeOrderedListMarkers();
2365
- delete this.fabricObject._isOrderedList;
2366
- this.notifyCanvasOfTextChanges();
2367
- this.redraw();
2368
- };
2369
- /**
2370
- * @override
2371
- * @inheritDoc
2372
- */
2373
- Text.prototype.setOrderedListStart = function (start) {
2374
- this.fabricObject._orderedListStart = start;
2375
- this.notifyCanvasOfTextChanges();
2376
- this.redraw();
2377
- };
2378
- /**
2379
- * @override
2380
- * @inheritDoc
2381
- */
2382
- Text.prototype.getOrderedListStart = function () {
2383
- return this.fabricObject._orderedListStart || 1;
2384
- };
2385
- Text.prototype.getSizeIncreaseSinceCreation = function () {
2386
- return Math.max(0, this.fabricObject.height - this.baseComponentHeight);
2387
- };
2388
- Text.prototype.removeBullets = function () {
2389
- var _this_1 = this;
2390
- if (this.data.bulleted) {
2391
- var bullets = this.fabricObject["_bullets"];
2392
- delete this.fabricObject["_bullets"];
2393
- each(bullets, function (bullet) { return _this_1.canvas.removeFabricObject(bullet); });
2394
- this.fabricObject["objectCaching"] = true;
2395
- }
2396
- };
2397
- Text.prototype.removeOrderedListMarkers = function () {
2398
- var _this_1 = this;
2399
- if (this.fabricObject._isOrderedList) {
2400
- var markerEntities = this.fabricObject._orderedListMarkerEntities;
2401
- delete this.fabricObject._orderedListMarkerEntities;
2402
- each(markerEntities, function (marker) { return _this_1.canvas.removeFabricObject(marker); });
2403
- this.fabricObject["objectCaching"] = true;
2404
- }
2405
- };
2406
- Text.prototype.applyBulletListMonkeyPatches = function () {
2407
- var _this = this;
2408
- if (this.data.bulleted)
2409
- this.enableBullets();
2410
- if (this.fabricObject._isOrderedList)
2411
- this.enableOrderedList();
2412
- var originalRender = this.fabricObject["_render"];
2413
- var fabric = this.canvas.fabric;
2414
- this.fabricObject["_render"] = function (ctx) {
2415
- var fabricText = this;
2416
- if (fabricText["_bullets"]) {
2417
- // Determine list of active bullets
2418
- var currentBullets_1 = fabricText["_bullets"];
2419
- var activeBullets_1 = {};
2420
- each(fabricText._textLines, function (textLine, lineIndex) {
2421
- if (textLine.length) {
2422
- // Start of real lines will have offset 0 in styleMap
2423
- var styleMap = fabricText._styleMap[lineIndex];
2424
- if (styleMap.offset === 0) {
2425
- if (currentBullets_1[lineIndex]) {
2426
- // Reuse existing bullet for this line
2427
- activeBullets_1[lineIndex] = currentBullets_1[lineIndex];
2428
- }
2429
- else {
2430
- // Create new bullet for this line
2431
- var options = {
2432
- left: 0,
2433
- top: 0,
2434
- radius: 2,
2435
- fill: fabricText.fill,
2436
- scaleX: fabricText.scaleX,
2437
- scaleY: fabricText.scaleY,
2438
- strokeWidth: 0,
2439
- lockMovementX: true,
2440
- lockMovementY: true,
2441
- hasControls: false,
2442
- selectable: false,
2443
- excludeFromJson: true
2444
- };
2445
- activeBullets_1[lineIndex] = new fabric.Circle(options);
2446
- }
2447
- }
2448
- }
2449
- });
2450
- // Add/Remove bullets from canvas
2451
- var canvas_1 = fabricText.canvas;
2452
- var canvasDirty_1 = false;
2453
- canvas_1.renderOnAddRemove = false;
2454
- each(activeBullets_1, function (bullet) {
2455
- if (!canvas_1.contains(bullet)) {
2456
- canvas_1.add(bullet);
2457
- canvasDirty_1 = true;
2458
- }
2459
- });
2460
- each(currentBullets_1, function (bullet, key) {
2461
- if (!activeBullets_1[key]) {
2462
- canvas_1.remove(bullet);
2463
- canvasDirty_1 = true;
2464
- }
2465
- });
2466
- canvas_1.renderOnAddRemove = true;
2467
- fabricText["_bullets"] = activeBullets_1;
2468
- if (canvasDirty_1) {
2469
- // Fabric doesn't like it when you add new things within render so use a sneaky timeout to delay render
2470
- setTimeout(canvas_1.renderAll.bind(canvas_1));
2471
- }
2472
- }
2473
- if (fabricText._isOrderedList) {
2474
- var existingMarkers_1 = fabricText._orderedListMarkerEntities || [];
2475
- var activeMarkers_1 = [];
2476
- each(fabricText._textLines, function (textLine, lineIndex) {
2477
- if (textLine.length) {
2478
- // Start of real lines will have offset 0 in styleMap
2479
- var styleMap = fabricText._styleMap[lineIndex];
2480
- if (styleMap.offset === 0) {
2481
- if (existingMarkers_1[lineIndex]) {
2482
- // Reuse existing marker for this line
2483
- activeMarkers_1[lineIndex] = existingMarkers_1[lineIndex];
2484
- }
2485
- else {
2486
- // Create new marker for this line
2487
- var textbox = new fabric.Textbox(lineIndex + ".", {
2488
- left: 0,
2489
- top: 0,
2490
- fill: fabricText.fill,
2491
- scaleX: fabricText.scaleX,
2492
- scaleY: fabricText.scaleY,
2493
- strokeWidth: 0,
2494
- lockMovementX: true,
2495
- lockMovementY: true,
2496
- hasControls: false,
2497
- selectable: false
2498
- });
2499
- textbox._isSlaveObject = true;
2500
- activeMarkers_1[lineIndex] = textbox;
2501
- }
2502
- }
2503
- }
2504
- });
2505
- // Add/Remove markers from canvas
2506
- var canvas_2 = fabricText.canvas;
2507
- var canvasDirty_2 = false;
2508
- canvas_2.renderOnAddRemove = false;
2509
- each(activeMarkers_1, function (marker) {
2510
- if (!marker)
2511
- return;
2512
- if (!canvas_2.contains(marker)) {
2513
- canvas_2.add(marker);
2514
- canvasDirty_2 = true;
2515
- }
2516
- });
2517
- each(existingMarkers_1, function (marker, key) {
2518
- if (!marker)
2519
- return;
2520
- if (!activeMarkers_1[key]) {
2521
- canvas_2.remove(marker);
2522
- canvasDirty_2 = true;
2523
- }
2524
- });
2525
- canvas_2.renderOnAddRemove = true;
2526
- fabricText._orderedListMarkerEntities = activeMarkers_1;
2527
- if (canvasDirty_2) {
2528
- // Fabric doesn't like it when you add new things within render so use a sneaky timeout to delay render
2529
- setTimeout(canvas_2.renderAll.bind(canvas_2));
2530
- }
2531
- }
2532
- originalRender.apply(fabricText, [ctx]);
2533
- };
2534
- var originalRenderTextLine = this.fabricObject["_renderTextLine"];
2535
- this.fabricObject["_renderTextLine"] = function (method, ctx, line, left, top, lineIndex) {
2536
- var fabricText = this;
2537
- var bullets = fabricText._bullets;
2538
- if (bullets && bullets[lineIndex]) {
2539
- var bullet = bullets[lineIndex];
2540
- var fontSize = this.getValueOfPropertyAt(lineIndex, 0, "fontSize");
2541
- var color = this.getValueOfPropertyAt(lineIndex, 0, "fill");
2542
- var radius = fontSize / 5;
2543
- var angle = fabricText.angle;
2544
- var theta = angle * Math.PI / 180;
2545
- var x = -radius * 2;
2546
- var y = (fabricText.height / 2) + top - (fontSize / 1.5);
2547
- var xOffset = (x * Math.cos(theta)) - (y * Math.sin(theta));
2548
- var yOffset = (x * Math.sin(theta)) + (y * Math.cos(theta));
2549
- var bulletCenterOffset = [-radius * Math.sqrt(2) / 2, -radius * Math.sqrt(2) / 2];
2550
- bullet.set({
2551
- radius: radius,
2552
- fill: color,
2553
- left: fabricText.left + xOffset + bulletCenterOffset[0],
2554
- top: fabricText.top + yOffset + bulletCenterOffset[1]
2555
- });
2556
- }
2557
- var orderedListMarkerEntities = fabricText._orderedListMarkerEntities;
2558
- var xShift = -_this.getOrderedListMargin() + 1;
2559
- if (orderedListMarkerEntities && orderedListMarkerEntities[lineIndex]) {
2560
- var entry = orderedListMarkerEntities[lineIndex];
2561
- var fontSize = this.getValueOfPropertyAt(lineIndex, 0, "fontSize");
2562
- var fontFamily = this.getValueOfPropertyAt(lineIndex, 0, "fontFamily");
2563
- var color = this.getValueOfPropertyAt(lineIndex, 0, "fill");
2564
- var angle = fabricText.angle;
2565
- var theta = angle * Math.PI / 180;
2566
- var yShift = (fabricText.height / 2) + top - fontSize - 1;
2567
- var xOffset = (xShift * Math.cos(theta)) - (yShift * Math.sin(theta));
2568
- var yOffset = (xShift * Math.sin(theta)) + (yShift * Math.cos(theta));
2569
- var lineNumber = _this.getOrderedListStart();
2570
- for (var i = 0; i < lineIndex; i++) {
2571
- if (orderedListMarkerEntities[i])
2572
- lineNumber++;
2573
- }
2574
- entry.set({
2575
- width: 1,
2576
- fontSize: fontSize,
2577
- fontFamily: fontFamily,
2578
- fill: color,
2579
- left: fabricText.left + xOffset,
2580
- top: fabricText.top + yOffset,
2581
- angle: fabricText.angle,
2582
- text: lineNumber + "."
2583
- });
2584
- }
2585
- originalRenderTextLine.apply(fabricText, [method, ctx, line, left, top, lineIndex]);
2586
- };
2587
- };
2588
- /**
2589
- * @override
2590
- * @inheritDoc
2591
- */
2592
- Text.prototype.updateFabricObjectBehavior = function () {
2593
- _super.prototype.updateFabricObjectBehavior.call(this);
2594
- var profile = this.canvas.getProfile();
2595
- var allowManualPositioning = !this.canvas.getLayoutManager().isEnforcedPositioning;
2596
- this.fabricObject.set({
2597
- lockScalingY: true,
2598
- lockScalingX: this.isPinToBottom(),
2599
- lockScalingFlip: true
2600
- });
2601
- this.fabricObject.setControlsVisibility({
2602
- tl: false,
2603
- tr: false,
2604
- br: false,
2605
- bl: false,
2606
- ml: allowManualPositioning && profile.allowScale && !this.isPinToBottom(),
2607
- mt: false,
2608
- mr: allowManualPositioning && profile.allowScale && !this.isPinToBottom(),
2609
- mb: false
2610
- });
2611
- this.redraw();
2612
- };
2613
- Text.prototype.onTextChange = function () {
2614
- this.data.userText = this.fabricObject.text;
2615
- this.text$.next(this.fabricObject.text);
2616
- if (this.getBoundingBox().width > this.canvas.getDimensions().widthPx) {
2617
- this.fabricObject.set({ left: 0, width: this.canvas.getDimensions().widthPx });
2618
- this.redraw();
2619
- }
2620
- };
2621
- Text.prototype.onDelete = function () {
2622
- this.removeBullets();
2623
- };
2624
- Text.prototype.onSelectionChange = function () {
2625
- this.updateTextSelection();
2626
- };
2627
- Text.prototype.hasTextSelection = function () {
2628
- return this.canvas.getSelection().isText && this.textSelection$.value.length > 0;
2629
- };
2630
- Text.prototype.updateTextSelection = function () {
2631
- if (this.canvas.headless)
2632
- return;
2633
- this.textSelection$.next(new TextSelection(this.fabricObject, this.fontLibrary));
2634
- this.notifyCanvasOfTextChanges();
2635
- };
2636
- Text.prototype.notifyCanvasOfTextChanges = function () {
2637
- this.canvas.notifyTextChanged();
2638
- };
2639
- return Text;
2640
- }(AbstractCanvasObject));
2641
- var TextSelection = /** @class */ (function () {
2642
- function TextSelection(fabricObject, fontLibrary) {
2643
- this.fabricObject = fabricObject;
2644
- this.fontLibrary = fontLibrary;
2645
- this.start = this.fabricObject.selectionStart;
2646
- this.end = this.fabricObject.selectionEnd;
2647
- this.length = this.end - this.start;
2648
- var cursorLoc = this.fabricObject.get2DCursorLocation();
2649
- this.lineIndex = cursorLoc.lineIndex;
2650
- this.charIndex = cursorLoc.charIndex;
2651
- var style = this.fabricObject.getCompleteStyleDeclaration(this.lineIndex, this.charIndex);
2652
- var hasSelection = this.length > 0;
2653
- this.font = hasSelection ? this.fontLibrary.getFont(style.fontFamily) : this.fontLibrary.getFont(this.fabricObject.fontFamily);
2654
- this.fontSize = hasSelection ? style.fontSize : this.fabricObject.fontSize;
2655
- this.fontWeight = hasSelection ? style.fontWeight : this.fabricObject.fontWeight;
2656
- this.bold = this.font.isBold(this.fontWeight);
2657
- this.italic = hasSelection ? style.fontStyle === "italic" : this.fabricObject.fontStyle === "italic";
2658
- this.underline = hasSelection ? style.underline : this.fabricObject.underline;
2659
- this.strikethrough = hasSelection ? style.linethrough : this.fabricObject.linethrough;
2660
- this.overline = hasSelection ? style.overline : this.fabricObject.overline;
2661
- this.textBackgroundColor = hasSelection ? style.textBackgroundColor : this.fabricObject.textBackgroundColor;
2662
- this.textAlign = this.fabricObject.textAlign;
2663
- this.charSpacing = this.fabricObject.charSpacing;
2664
- this.lineHeight = this.fabricObject.lineHeight;
2665
- this.fill = style.fill;
2666
- this.stroke = style.stroke;
2667
- this.strokeWidth = style.strokeWidth;
2668
- }
2669
- return TextSelection;
2670
- }());
2671
-
2672
- var VerticalRule = /** @class */ (function (_super) {
2673
- __extends(VerticalRule, _super);
2674
- function VerticalRule(canvas, fabricObject, persistedField) {
2675
- var _this = _super.call(this, ObjectType.VERTICAL_RULE, canvas, fabricObject, persistedField) || this;
2676
- _this.strokeWidth$ = new BehaviorSubject(2);
2677
- _this.canvas = canvas;
2678
- _this.strokeWidth$.next(_this.getDimensions().width.px);
2679
- _this.fabricObject.set({ strokeWidth: 0 });
2680
- return _this;
2681
- }
2682
- /**
2683
- * @override
2684
- * @inheritDoc
2685
- */
2686
- VerticalRule.prototype.getStrokeWidth = function () {
2687
- return this.fabricObject.width;
2688
- };
2689
- /**
2690
- * @override
2691
- * @inheritDoc
2692
- */
2693
- VerticalRule.prototype.getStrokeWidth$ = function () {
2694
- return this.strokeWidth$;
2695
- };
2696
- /**
2697
- * @override
2698
- * @inheritDoc
2699
- */
2700
- VerticalRule.prototype.setStrokeWidth = function (width) {
2701
- this.strokeWidth$.next(width);
2702
- this.data.strokeWidth = width;
2703
- this.setPosition({ width: width });
2704
- this.canvas.notifyObjectListChanged();
2705
- this.redraw();
2706
- };
2707
- /**
2708
- * @override
2709
- * @inheritDoc
2710
- */
2711
- VerticalRule.prototype.getColor = function () {
2712
- return this.fabricObject.fill;
2713
- };
2714
- /**
2715
- * @override
2716
- * @inheritDoc
2717
- */
2718
- VerticalRule.prototype.setColor = function (color) {
2719
- this.fabricObject.set({ fill: color, stroke: color });
2720
- this.data.color = color;
2721
- this.redraw();
2722
- };
2723
- /**
2724
- * @override
2725
- * @inheritDoc
2726
- */
2727
- VerticalRule.prototype.centerHorizontally = function () {
2728
- _super.prototype.centerHorizontallyInternal.call(this);
2729
- };
2730
- /**
2731
- * @override
2732
- * @inheritDoc
2733
- */
2734
- VerticalRule.prototype.centerVertically = function () {
2735
- _super.prototype.centerVerticallyInternal.call(this);
2736
- };
2737
- /**
2738
- * @override
2739
- * @inheritDoc
2740
- */
2741
- VerticalRule.prototype.nudgeUp = function () {
2742
- _super.prototype.nudgeInternal.call(this, 0, -1);
2743
- };
2744
- /**
2745
- * @override
2746
- * @inheritDoc
2747
- */
2748
- VerticalRule.prototype.nudgeDown = function () {
2749
- _super.prototype.nudgeInternal.call(this, 0, +1);
2750
- };
2751
- /**
2752
- * @override
2753
- * @inheritDoc
2754
- */
2755
- VerticalRule.prototype.nudgeLeft = function () {
2756
- _super.prototype.nudgeInternal.call(this, -1, 0);
2757
- };
2758
- /**
2759
- * @override
2760
- * @inheritDoc
2761
- */
2762
- VerticalRule.prototype.nudgeRight = function () {
2763
- _super.prototype.nudgeInternal.call(this, +1, 0);
2764
- };
2765
- return VerticalRule;
2766
- }(AbstractCanvasObject));
2767
-
2768
- /**
2769
- * Profile with as many features disabled as possible.
2770
- */
2771
- var DefaultCanvasProfile = /** @class */ (function () {
2772
- function DefaultCanvasProfile() {
2773
- this.allowMultiSelect = false;
2774
- this.enterTextEditImmediatelyOnSelect = true;
2775
- this.allowObjectsToLeaveCanvas = false;
2776
- this.allowMove = false;
2777
- this.allowRotate = false;
2778
- this.allowScale = false;
2779
- this.defaultFont = "Lato";
2780
- this.defaultFontSize = 9;
2781
- this.defaultFontColor = "#000000";
2782
- this.defaultLineHeight = 1.8;
2783
- this.defaultCharacterSpacing = 0;
2784
- this.defaultTextAlignment = "left";
2785
- this.defaultBorderColor = "#000000";
2786
- this.defaultBorderWidth = 1;
2787
- this.defaultCutoffColor = "#000000";
2788
- this.defaultCutoffWidth = 1;
2789
- }
2790
- return DefaultCanvasProfile;
2791
- }());
2792
-
2793
- var ImageType;
2794
- (function (ImageType) {
2795
- ImageType["DEFAULT"] = "DEFAULT";
2796
- ImageType["BACKGROUND"] = "BACKGROUND";
2797
- ImageType["LOGO"] = "LOGO";
2798
- ImageType["PHOTO"] = "PHOTO";
2799
- })(ImageType || (ImageType = {}));
2800
-
2801
- var ObjectType;
2802
- (function (ObjectType) {
2803
- ObjectType["TEXT"] = "TEXT";
2804
- ObjectType["BORDER"] = "BORDER";
2805
- ObjectType["IMAGE"] = "IMAGE";
2806
- ObjectType["RECTANGLE"] = "RECTANGLE";
2807
- ObjectType["LIBRARY_IMAGE"] = "LIBRARY_IMAGE";
2808
- ObjectType["CUTOFF"] = "CUTOFF";
2809
- ObjectType["VERTICAL_RULE"] = "VERTICAL_RULE";
2810
- ObjectType["HORIZONTAL_RULE"] = "HORIZONTAL_RULE";
2811
- ObjectType["RICH_TEXT"] = "RICH_TEXT";
2812
- ObjectType["BACKGROUND_IMAGE"] = "BACKGROUND_IMAGE";
2813
- })(ObjectType || (ObjectType = {}));
2814
-
2815
- var AbstractInteractiveCanvas = /** @class */ (function () {
2816
- function AbstractInteractiveCanvas(fabric, fabricCanvas, devicePixelRatio) {
2817
- this.fabric = fabric;
2818
- this.fabricCanvas = fabricCanvas;
2819
- this.devicePixelRatio = devicePixelRatio;
2820
- this.fontLibrary = new FontLibrary();
2821
- this.dimensionChangeRejections$ = new Subject();
2822
- this.IMAGE_CAPTURE_COEFFICIENT = this.calculateImageCaptureCoefficient();
2823
- /**
2824
- * Fabric defaults to only 2 decimal places when saving, which causes havoc on the scaling of large images.
2825
- */
2826
- fabric.Object.NUM_FRACTION_DIGITS = 10;
2827
- this.baseDimensions = new PixelCanvasDimensions(this.fabricCanvas.width, this.fabricCanvas.height);
2828
- var baseRestrictions = { width: { min: 1 }, height: { min: 1 } };
2829
- //Initialize properties
2830
- this.dimensions$ = new BehaviorSubject(this.baseDimensions);
2831
- this.dimensionRestrictions$ = new BehaviorSubject(baseRestrictions);
2832
- this.zoom$ = new BehaviorSubject(1);
2833
- this.profile$ = new BehaviorSubject(new DefaultCanvasProfile());
2834
- this.layoutManager$ = new BehaviorSubject(new AdvancedLayoutManager(this));
2835
- this.backgroundColor$ = new BehaviorSubject(this.fabricCanvas.backgroundColor);
2836
- this.objects$ = new BehaviorSubject([]);
2837
- this.selection$ = new BehaviorSubject(new SelectionData([]));
2838
- //Configure canvas behaviour
2839
- this.fabricCanvas.stateful = true;
2840
- this.fabricCanvas.preserveObjectStacking = true;
2841
- this.fabricCanvas.perPixelTargetFind = true;
2842
- this.fabricCanvas.targetFindTolerance = 10;
2843
- this.setBackgroundColor("#FFFFFF");
2844
- }
2845
- /**
2846
- * @override
2847
- * @inheritDoc
2848
- */
2849
- AbstractInteractiveCanvas.prototype.getFontLibrary = function () {
2850
- return this.fontLibrary;
2851
- };
2852
- /**
2853
- * @override
2854
- * @inheritDoc
2855
- */
2856
- AbstractInteractiveCanvas.prototype.getProfile = function () {
2857
- return this.profile$.value;
2858
- };
2859
- /**
2860
- * @override
2861
- * @inheritDoc
2862
- */
2863
- AbstractInteractiveCanvas.prototype.getProfile$ = function () {
2864
- return this.profile$;
2865
- };
2866
- /**
2867
- * @override
2868
- * @inheritDoc
2869
- */
2870
- AbstractInteractiveCanvas.prototype.setProfile = function (profile) {
2871
- this.profile$.next(profile);
2872
- this.onProfileChanged(profile);
2873
- this.redraw();
2874
- };
2875
- /**
2876
- * @override
2877
- * @inheritDoc
2878
- */
2879
- AbstractInteractiveCanvas.prototype.getLayoutManager = function () {
2880
- return this.layoutManager$.value;
2881
- };
2882
- /**
2883
- * @override
2884
- * @inheritDoc
2885
- */
2886
- AbstractInteractiveCanvas.prototype.getLayoutManager$ = function () {
2887
- return this.layoutManager$;
2888
- };
2889
- /**
2890
- * @override
2891
- * @inheritDoc
2892
- */
2893
- AbstractInteractiveCanvas.prototype.setLayoutManager = function (layoutManager) {
2894
- if (layoutManager === LayoutManager.STANDARD) {
2895
- this.layoutManager$.next(new StandardLayoutManager(this));
2896
- }
2897
- else {
2898
- this.layoutManager$.next(new AdvancedLayoutManager(this));
2899
- }
2900
- this.layoutManager$.value.onInit();
2901
- };
2902
- /**
2903
- * @override
2904
- * @inheritDoc
2905
- */
2906
- AbstractInteractiveCanvas.prototype.getBackgroundColor = function () {
2907
- return this.backgroundColor$.value;
2908
- };
2909
- /**
2910
- * @override
2911
- * @inheritDoc
2912
- */
2913
- AbstractInteractiveCanvas.prototype.getBackgroundColor$ = function () {
2914
- return this.backgroundColor$;
2915
- };
2916
- /**
2917
- * @override
2918
- * @inheritDoc
2919
- */
2920
- AbstractInteractiveCanvas.prototype.setBackgroundColor = function (color) {
2921
- if (color === void 0) { color = "White"; }
2922
- this.fabricCanvas.setBackgroundColor(color, function () {
2923
- });
2924
- this.fabricCanvas.renderAll();
2925
- this.backgroundColor$.next(color);
2926
- };
2927
- /**
2928
- * @override
2929
- * @inheritDoc
2930
- */
2931
- AbstractInteractiveCanvas.prototype.getDimensions = function () {
2932
- return this.dimensions$.value;
2933
- };
2934
- /**
2935
- * @override
2936
- * @inheritDoc
2937
- */
2938
- AbstractInteractiveCanvas.prototype.getBaseDimensions = function () {
2939
- return this.baseDimensions;
2940
- };
2941
- /**
2942
- * @override
2943
- * @inheritDoc
2944
- */
2945
- AbstractInteractiveCanvas.prototype.getDimensions$ = function () {
2946
- return this.dimensions$;
2947
- };
2948
- /**
2949
- * @override
2950
- * @inheritDoc
2951
- */
2952
- AbstractInteractiveCanvas.prototype.setDimensions = function (dimensions) {
2953
- if (this.dimensionsExceedRestrictions(dimensions)) {
2954
- console.info("Requested canvas dimensions exceed current restrictions", dimensions, this.dimensionRestrictions$.value);
2955
- this.dimensionChangeRejections$.next(dimensions);
2956
- return;
2957
- }
2958
- this.updateCanvasDimensions(dimensions, this.zoom$.value);
2959
- this.dimensions$.next(dimensions);
2960
- this.onDimensionsChange();
2961
- };
2962
- /**
2963
- * @override
2964
- * @inheritDoc
2965
- */
2966
- AbstractInteractiveCanvas.prototype.setBaseDimensions = function (dimensions) {
2967
- this.baseDimensions = dimensions;
2968
- this.setDimensions(dimensions);
2969
- };
2970
- /**
2971
- * @override
2972
- * @inheritDoc
2973
- */
2974
- AbstractInteractiveCanvas.prototype.getDimensionChangeRejections$ = function () {
2975
- return this.dimensionChangeRejections$;
2976
- };
2977
- /**
2978
- * @override
2979
- * @inheritDoc
2980
- */
2981
- AbstractInteractiveCanvas.prototype.getDimensionsRestrictions = function () {
2982
- return this.dimensionRestrictions$.value;
2983
- };
2984
- /**
2985
- * @override
2986
- * @inheritDoc
2987
- */
2988
- AbstractInteractiveCanvas.prototype.getDimensionsRestrictions$ = function () {
2989
- return this.dimensionRestrictions$;
2990
- };
2991
- /**
2992
- * @override
2993
- * @inheritDoc
2994
- */
2995
- AbstractInteractiveCanvas.prototype.setDimensionRestrictions = function (restrictions) {
2996
- this.dimensionRestrictions$.next(restrictions);
2997
- var dimensions = this.getDimensions();
2998
- if (this.dimensionsExceedRestrictions(dimensions)) {
2999
- var newDimensions = dimensions
3000
- .withSize(Math.min(Math.max(oc(restrictions).width.min(dimensions.width), dimensions.width), Math.max(oc(restrictions).width.max(dimensions.width))), Math.min(Math.max(oc(restrictions).height.min(dimensions.height), dimensions.height), Math.max(oc(restrictions).height.max(dimensions.height))));
3001
- this.setBaseDimensions(newDimensions);
3002
- }
3003
- };
3004
- AbstractInteractiveCanvas.prototype.getMargin = function () {
3005
- var borderWidth = this.getObjectsByTypeInternal(ObjectType.BORDER)
3006
- .map(function (border) { return border.getStrokeWidth(); })
3007
- .reduce(function (current, next) { return Math.max(current, next); }, 0);
3008
- var borderPadding = this.getObjectsByTypeInternal(ObjectType.BORDER)
3009
- .map(function (border) { return border.getPadding(); })
3010
- .reduce(function (current, next) {
3011
- return {
3012
- top: Math.max(current.top, next.top),
3013
- right: Math.max(current.right, next.right),
3014
- bottom: Math.max(current.bottom, next.bottom),
3015
- left: Math.max(current.left, next.left)
3016
- };
3017
- }, { top: 0, right: 0, bottom: 0, left: 0 });
3018
- var cutoffWidth = this.getObjectsByTypeInternal(ObjectType.CUTOFF)
3019
- .map(function (border) { return border.getStrokeWidth(); })
3020
- .reduce(function (current, next) { return Math.max(current, next); }, 0);
3021
- // noinspection JSSuspiciousNameCombination
3022
- return {
3023
- top: borderWidth + borderPadding.top,
3024
- right: borderWidth + borderPadding.right,
3025
- bottom: Math.max(cutoffWidth, borderWidth) + borderPadding.bottom,
3026
- left: borderWidth + borderPadding.left
3027
- };
3028
- };
3029
- /**
3030
- * @override
3031
- * @inheritDoc
3032
- */
3033
- AbstractInteractiveCanvas.prototype.getZoom = function () {
3034
- return this.zoom$.value;
3035
- };
3036
- /**
3037
- * @override
3038
- * @inheritDoc
3039
- */
3040
- AbstractInteractiveCanvas.prototype.getZoom$ = function () {
3041
- return this.zoom$;
3042
- };
3043
- /**
3044
- * @override
3045
- * @inheritDoc
3046
- */
3047
- AbstractInteractiveCanvas.prototype.setZoom = function (zoom) {
3048
- this.updateCanvasDimensions(this.dimensions$.value, zoom);
3049
- this.zoom$.next(zoom);
3050
- };
3051
- /**
3052
- * @override
3053
- * @inheritDoc
3054
- */
3055
- AbstractInteractiveCanvas.prototype.getObjects = function () {
3056
- return this.objects$.value;
3057
- };
3058
- /**
3059
- * Get the list of objects directly.
3060
- */
3061
- AbstractInteractiveCanvas.prototype.getObjectsInternal = function () {
3062
- return this.objects$.value;
3063
- };
3064
- /**
3065
- * @override
3066
- * @inheritDoc
3067
- */
3068
- AbstractInteractiveCanvas.prototype.getObjectsByTypeInternal = function (type) {
3069
- return _(this.getObjectsInternal())
3070
- .filter(function (object) { return object.getType() === type; })
3071
- .value();
3072
- };
3073
- /**
3074
- * @override
3075
- * @inheritDoc
3076
- */
3077
- AbstractInteractiveCanvas.prototype.getObjectsByTypesInternal = function () {
3078
- var types = [];
3079
- for (var _i = 0; _i < arguments.length; _i++) {
3080
- types[_i] = arguments[_i];
3081
- }
3082
- return _(this.getObjectsInternal())
3083
- .filter(function (object) { return _(types).includes(object.getType()); })
3084
- .value();
3085
- };
3086
- /**
3087
- * @override
3088
- * @inheritDoc
3089
- */
3090
- AbstractInteractiveCanvas.prototype.getObjects$ = function () {
3091
- return this.objects$;
3092
- };
3093
- /**
3094
- * @override
3095
- * @inheritDoc
3096
- */
3097
- AbstractInteractiveCanvas.prototype.removeObject = function (target) {
3098
- var concreteObject = this.findFabricObject(target.getId());
3099
- this.fabricCanvas.remove(concreteObject);
3100
- this.objects$.next(_(this.objects$.value)
3101
- .filter(function (object) { return object.getId() !== target.getId(); })
3102
- .value());
3103
- this.onObjectListChange();
3104
- };
3105
- /**
3106
- * @override
3107
- * @inheritDoc
3108
- */
3109
- AbstractInteractiveCanvas.prototype.addImageObject = function (imageUrl) {
3110
- return __awaiter(this, void 0, void 0, function () {
3111
- var maxSizeScale, maxWidth, options;
3112
- var _this = this;
3113
- return __generator(this, function (_a) {
3114
- switch (_a.label) {
3115
- case 0:
3116
- maxSizeScale = 2;
3117
- maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3118
- options = { crossOrigin: "anonymous" };
3119
- return [4 /*yield*/, new Promise(function (resolve) {
3120
- _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
3121
- var id = _this.randomId();
3122
- var newScale = 1;
3123
- var curWidth = newScale * fabricImage.width;
3124
- if (curWidth > maxWidth) {
3125
- newScale = maxWidth / curWidth;
3126
- }
3127
- fabricImage.set({
3128
- id: id,
3129
- scaleX: newScale,
3130
- scaleY: newScale
3131
- });
3132
- _this.fabricCanvas.add(fabricImage);
3133
- var object = new Image(_this, fabricImage);
3134
- _this.trackNewObject(object);
3135
- _this.selectFabricObject(fabricImage);
3136
- _this.syncObjectList();
3137
- resolve(object);
3138
- }, options);
3139
- })];
3140
- case 1: return [2 /*return*/, _a.sent()];
3141
- }
3142
- });
3143
- });
3144
- };
3145
- /**
3146
- * @override
3147
- * @inheritDoc
3148
- */
3149
- AbstractInteractiveCanvas.prototype.addTextObject = function () {
3150
- return __awaiter(this, void 0, void 0, function () {
3151
- var profile, id, fontSizePx, canvasWidthPx, lowestTextPointPx, topPx, textOptions, fabricTextbox, object;
3152
- return __generator(this, function (_a) {
3153
- profile = this.profile$.value;
3154
- id = this.randomId();
3155
- fontSizePx = profile.defaultFontSize;
3156
- canvasWidthPx = this.getDimensions().widthPx;
3157
- lowestTextPointPx = this.getLowestTextPoint();
3158
- topPx = Math.min(lowestTextPointPx, this.getDimensions().heightPx - fontSizePx);
3159
- textOptions = {
3160
- id: id,
3161
- top: topPx,
3162
- left: 5,
3163
- width: canvasWidthPx - 10,
3164
- fill: profile.defaultFontColor,
3165
- fontSize: fontSizePx,
3166
- fontFamily: profile.defaultFont,
3167
- textAlign: profile.defaultTextAlignment,
3168
- multiLine: true,
3169
- lineHeight: profile.defaultLineHeight,
3170
- charSpacing: profile.defaultCharacterSpacing
3171
- };
3172
- fabricTextbox = new this.fabric.Textbox("Text goes here", textOptions);
3173
- this.fabricCanvas.add(fabricTextbox);
3174
- object = new Text(this, fabricTextbox);
3175
- this.trackNewObject(object);
3176
- this.selectFabricObject(fabricTextbox);
3177
- this.syncObjectList();
3178
- return [2 /*return*/, object];
3179
- });
3180
- });
3181
- };
3182
- /**
3183
- * @override
3184
- * @inheritDoc
3185
- */
3186
- AbstractInteractiveCanvas.prototype.addRichTextObject = function (imageUrl, htmlContent) {
3187
- return __awaiter(this, void 0, void 0, function () {
3188
- var maxSizeScale, maxWidth, options;
3189
- var _this = this;
3190
- return __generator(this, function (_a) {
3191
- switch (_a.label) {
3192
- case 0:
3193
- maxSizeScale = 2;
3194
- maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3195
- options = { crossOrigin: "anonymous" };
3196
- return [4 /*yield*/, new Promise(function (resolve) {
3197
- _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
3198
- var id = _this.randomId();
3199
- var newScale = 1;
3200
- var curWidth = newScale * fabricImage.width;
3201
- if (curWidth > maxWidth) {
3202
- newScale = maxWidth / curWidth;
3203
- }
3204
- fabricImage.set({
3205
- id: id,
3206
- scaleX: newScale,
3207
- scaleY: newScale
3208
- });
3209
- _this.fabricCanvas.add(fabricImage);
3210
- var object = new RichText(_this, fabricImage);
3211
- object.htmlContent = htmlContent;
3212
- _this.trackNewObject(object);
3213
- var margin = _this.getMargin();
3214
- object.setImageWidth(_this.dimensions$.value.widthPx - margin.left - margin.right);
3215
- _this.selectFabricObject(fabricImage);
3216
- _this.syncObjectList();
3217
- resolve(object);
3218
- }, options);
3219
- })];
3220
- case 1: return [2 /*return*/, _a.sent()];
3221
- }
3222
- });
3223
- });
3224
- };
3225
- /**
3226
- * @override
3227
- * @inheritDoc
3228
- */
3229
- AbstractInteractiveCanvas.prototype.addRectangleObject = function () {
3230
- return __awaiter(this, void 0, void 0, function () {
3231
- var canvasWidth, canvasHeight, rectWidth, rectHeight, rectangleOptions, fabricRect, object;
3232
- return __generator(this, function (_a) {
3233
- canvasWidth = this.getDimensions().widthPx;
3234
- canvasHeight = this.getDimensions().heightPx;
3235
- rectWidth = canvasWidth / 2;
3236
- rectHeight = canvasHeight / 2;
3237
- rectangleOptions = {
3238
- id: this.randomId(),
3239
- width: canvasWidth / 2,
3240
- height: 2,
3241
- top: canvasHeight / 2 - rectHeight / 2,
3242
- left: canvasWidth / 2 - rectWidth / 2,
3243
- fill: "#000000"
3244
- };
3245
- fabricRect = new this.fabric.Rect(rectangleOptions);
3246
- this.fabricCanvas.add(fabricRect);
3247
- object = new Rectangle(this, fabricRect);
3248
- this.trackNewObject(object);
3249
- this.selectFabricObject(fabricRect);
3250
- this.syncObjectList();
3251
- return [2 /*return*/, object];
3252
- });
3253
- });
3254
- };
3255
- /**
3256
- * @override
3257
- * @inheritDoc
3258
- */
3259
- AbstractInteractiveCanvas.prototype.addCutoffObject = function () {
3260
- return __awaiter(this, void 0, void 0, function () {
3261
- var profile, initialThickness, canvasWidth, canvasHeight, rectangleOptions, fabricRect, object;
3262
- return __generator(this, function (_a) {
3263
- profile = this.profile$.value;
3264
- initialThickness = profile.defaultCutoffWidth;
3265
- canvasWidth = this.getDimensions().widthPx;
3266
- canvasHeight = this.getDimensions().heightPx;
3267
- rectangleOptions = {
3268
- id: this.randomId(),
3269
- width: canvasWidth,
3270
- height: initialThickness,
3271
- top: canvasHeight - initialThickness,
3272
- left: 0,
3273
- fill: profile.defaultCutoffColor
3274
- };
3275
- fabricRect = new this.fabric.Rect(rectangleOptions);
3276
- this.fabricCanvas.add(fabricRect);
3277
- object = new Cutoff(this, fabricRect);
3278
- this.trackNewObject(object);
3279
- this.selectFabricObject(fabricRect);
3280
- this.syncObjectList();
3281
- return [2 /*return*/, object];
3282
- });
3283
- });
3284
- };
3285
- /**
3286
- * @override
3287
- * @inheritDoc
3288
- */
3289
- AbstractInteractiveCanvas.prototype.addBorderObject = function () {
3290
- return __awaiter(this, void 0, void 0, function () {
3291
- var profile, initialThickness, rectangleOptions, fabricRect, object;
3292
- return __generator(this, function (_a) {
3293
- profile = this.profile$.value;
3294
- initialThickness = profile.defaultBorderWidth;
3295
- rectangleOptions = {
3296
- id: this.randomId(),
3297
- fill: null,
3298
- stroke: profile.defaultBorderColor,
3299
- strokeWidth: initialThickness
3300
- };
3301
- fabricRect = new this.fabric.Rect(rectangleOptions);
3302
- this.fabricCanvas.add(fabricRect);
3303
- object = new Border(this, fabricRect);
3304
- this.trackNewObject(object);
3305
- this.selectFabricObject(fabricRect);
3306
- this.syncObjectList();
3307
- return [2 /*return*/, object];
3308
- });
3309
- });
3310
- };
3311
- /**
3312
- * @override
3313
- * @inheritDoc
3314
- */
3315
- AbstractInteractiveCanvas.prototype.addVerticalRuleObject = function () {
3316
- return __awaiter(this, void 0, void 0, function () {
3317
- var rectOptions, fabricRect, object;
3318
- return __generator(this, function (_a) {
3319
- rectOptions = {
3320
- id: this.randomId(),
3321
- width: 1,
3322
- height: 10,
3323
- top: 0,
3324
- left: 0,
3325
- fill: "#000000"
3326
- };
3327
- fabricRect = new this.fabric.Rect(rectOptions);
3328
- this.fabricCanvas.add(fabricRect);
3329
- object = new VerticalRule(this, fabricRect);
3330
- this.trackNewObject(object);
3331
- this.selectFabricObject(fabricRect);
3332
- this.syncObjectList();
3333
- return [2 /*return*/, object];
3334
- });
3335
- });
3336
- };
3337
- /**
3338
- * @override
3339
- * @inheritDoc
3340
- */
3341
- AbstractInteractiveCanvas.prototype.addHorizontalRuleObject = function () {
3342
- return __awaiter(this, void 0, void 0, function () {
3343
- var rectOptions, fabricRect, object;
3344
- return __generator(this, function (_a) {
3345
- rectOptions = {
3346
- id: this.randomId(),
3347
- width: 10,
3348
- height: 1,
3349
- top: 0,
3350
- left: 0,
3351
- fill: "#000000"
3352
- };
3353
- fabricRect = new this.fabric.Rect(rectOptions);
3354
- this.fabricCanvas.add(fabricRect);
3355
- object = new HorizontalRule(this, fabricRect);
3356
- this.trackNewObject(object);
3357
- this.selectFabricObject(fabricRect);
3358
- this.syncObjectList();
3359
- return [2 /*return*/, object];
3360
- });
3361
- });
3362
- };
3363
- /**
3364
- * @override
3365
- * @inheritDoc
3366
- */
3367
- AbstractInteractiveCanvas.prototype.addBackgroundImageObject = function (imageUrl) {
3368
- return __awaiter(this, void 0, void 0, function () {
3369
- var options;
3370
- var _this = this;
3371
- return __generator(this, function (_a) {
3372
- switch (_a.label) {
3373
- case 0:
3374
- options = { crossOrigin: "anonymous" };
3375
- return [4 /*yield*/, new Promise(function (resolve) {
3376
- _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
3377
- var id = _this.randomId();
3378
- fabricImage.set({
3379
- id: id,
3380
- });
3381
- _this.fabricCanvas.add(fabricImage);
3382
- var object = new BackgroundImage(_this, fabricImage);
3383
- _this.trackNewObject(object);
3384
- _this.selectFabricObject(fabricImage);
3385
- _this.syncObjectList();
3386
- resolve(object);
3387
- }, options);
3388
- })];
3389
- case 1: return [2 /*return*/, _a.sent()];
3390
- }
3391
- });
3392
- });
3393
- };
3394
- /**
3395
- * @override
3396
- * @inheritDoc
3397
- */
3398
- AbstractInteractiveCanvas.prototype.setPrefillData = function (data) {
3399
- _(this.getObjectsByTypeInternal(ObjectType.TEXT)).each(function (object) {
3400
- var prefillType = object.getPrefillType();
3401
- if (prefillType !== undefined) {
3402
- object.setText(data[prefillType] || "");
3403
- }
3404
- });
3405
- };
3406
- /**
3407
- * @override
3408
- * @inheritDoc
3409
- */
3410
- AbstractInteractiveCanvas.prototype.distributeObjectsVertically = function () {
3411
- var margin = this.getMargin();
3412
- var canvasHeight = this.getDimensions().heightPx;
3413
- var objectsToLayout = _(this.getObjectsByTypesInternal(ObjectType.TEXT, ObjectType.IMAGE))
3414
- .sortBy(function (x) { return x.getBoundingBox().top; })
3415
- .value();
3416
- var objectsHeight = reduce(objectsToLayout, function (total, element) { return total + element.getBoundingBox().height; }, 0);
3417
- var workableHeight = canvasHeight - margin.top - margin.bottom;
3418
- var requiredPadding = workableHeight - objectsHeight;
3419
- var spaceBetween = requiredPadding / (objectsToLayout.length - 1);
3420
- var nextElementTop = margin.top;
3421
- for (var _i = 0, objectsToLayout_1 = objectsToLayout; _i < objectsToLayout_1.length; _i++) {
3422
- var element = objectsToLayout_1[_i];
3423
- var elementHeight = element.getBoundingBox().height;
3424
- element.setPosition({
3425
- top: nextElementTop
3426
- });
3427
- element.setCoords();
3428
- nextElementTop += elementHeight + spaceBetween;
3429
- }
3430
- this.redraw();
3431
- };
3432
- /**
3433
- * @override
3434
- * @inheritDoc
3435
- */
3436
- AbstractInteractiveCanvas.prototype.getSelection = function () {
3437
- return this.selection$.value;
3438
- };
3439
- /**
3440
- * @override
3441
- * @inheritDoc
3442
- */
3443
- AbstractInteractiveCanvas.prototype.getSelection$ = function () {
3444
- return this.selection$;
3445
- };
3446
- /**
3447
- * @override
3448
- * @inheritDoc
3449
- */
3450
- AbstractInteractiveCanvas.prototype.select = function (object) {
3451
- var concreteField = this.findObject(object.getId());
3452
- if (!concreteField) {
3453
- console.error("Attempted to select unknown object", object);
3454
- return;
3455
- }
3456
- this.selectFabricObject(concreteField.getFabricObjectInternal());
3457
- this.fabricCanvas.renderAll();
3458
- };
3459
- /**
3460
- * @override
3461
- * @inheritDoc
3462
- */
3463
- AbstractInteractiveCanvas.prototype.clearSelection = function () {
3464
- this.fabricCanvas.discardActiveObject();
3465
- this.fabricCanvas.renderAll();
3466
- };
3467
- AbstractInteractiveCanvas.prototype.setMultiSelect = function (value) {
3468
- this.fabricCanvas.selection = value;
3469
- this.fabricCanvas.selectionKey = value ? "shiftKey" : null;
3470
- };
3471
- /**
3472
- * @override
3473
- * @inheritDoc
3474
- */
3475
- AbstractInteractiveCanvas.prototype.clear = function () {
3476
- this.fabricCanvas.clear();
3477
- this.setBackgroundColor("#FFFFFF");
3478
- this.objects$.next([]);
3479
- };
3480
- /**
3481
- * @override
3482
- * @inheritDoc
3483
- */
3484
- AbstractInteractiveCanvas.prototype.save = function () {
3485
- var objects = _(this.objects$.value)
3486
- .map(function (x) { return x.persist(); })
3487
- .value();
3488
- var includedProperties = ["id", "_isSlaveObject", "_isOrderedList", "_orderedListStart"];
3489
- var json = this.fabricCanvas.toJSON(includedProperties);
3490
- return {
3491
- dimensions: this.dimensions$.value,
3492
- canvasJson: JSON.stringify(json),
3493
- objects: objects,
3494
- backgroundColor: this.backgroundColor$.value
3495
- };
3496
- };
3497
- /**
3498
- * @override
3499
- * @inheritDoc
3500
- */
3501
- AbstractInteractiveCanvas.prototype.load = function (canvasState) {
3502
- return __awaiter(this, void 0, void 0, function () {
3503
- var json_1;
3504
- var _this = this;
3505
- return __generator(this, function (_a) {
3506
- switch (_a.label) {
3507
- case 0:
3508
- this.setBaseDimensions(canvasState.dimensions);
3509
- if (!(canvasState.canvasJson != undefined)) return [3 /*break*/, 2];
3510
- json_1 = JSON.parse(canvasState.canvasJson);
3511
- return [4 /*yield*/, new Promise(function (resolve) {
3512
- _this.fabricCanvas.loadFromJSON(json_1, function () { return resolve(); });
3513
- })];
3514
- case 1:
3515
- _a.sent();
3516
- _a.label = 2;
3517
- case 2:
3518
- if (canvasState.backgroundColor)
3519
- this.setBackgroundColor(canvasState.backgroundColor);
3520
- this.backgroundColor$.next(this.fabricCanvas.backgroundColor);
3521
- this.bindFields(canvasState.objects);
3522
- this.removePersistedBullets();
3523
- this.removeSlaveObjects();
3524
- this.layoutManager$.value.onInit();
3525
- return [2 /*return*/];
3526
- }
3527
- });
3528
- });
3529
- };
3530
- /**
3531
- * Another hack for bullet points... because they use canvas objects to render the bullets, they get saved when the canvas is persisted... and then loaded into a new canvas, but there's
3532
- * nothing linking them to the field that needs bullets, so they end up duplicating every time the canvas is saved/loaded. Since we don't use circles for anything else, this just
3533
- * deletes all circles on canvas load.
3534
- */
3535
- AbstractInteractiveCanvas.prototype.removePersistedBullets = function () {
3536
- var _this = this;
3537
- var objects = this.fabricCanvas.getObjects();
3538
- each(objects, function (object) {
3539
- if (object.type === "circle")
3540
- _this.removeFabricObject(object);
3541
- });
3542
- };
3543
- /**
3544
- * Remove any slave objects persisted to the canvas; their master objects will recreate them.
3545
- */
3546
- AbstractInteractiveCanvas.prototype.removeSlaveObjects = function () {
3547
- var _this = this;
3548
- var objects = this.fabricCanvas.getObjects();
3549
- each(objects, function (object) {
3550
- if (object._isSlaveObject)
3551
- _this.removeFabricObject(object);
3552
- });
3553
- };
3554
- /**
3555
- * @override
3556
- * @inheritDoc
3557
- */
3558
- AbstractInteractiveCanvas.prototype.toSvg = function () {
3559
- var _this = this;
3560
- var widthInMm = round(this.dimensions$.value.widthMm, 1) + "mm";
3561
- var heightInMm = round(this.dimensions$.value.heightMm, 1) + "mm";
3562
- var options = {
3563
- width: widthInMm,
3564
- height: heightInMm,
3565
- suppressPreamble: true,
3566
- encoding: null
3567
- };
3568
- var svg = this.executeWithNormalizedCanvas(function () { return _this.fabricCanvas.toSVG(options); });
3569
- this.redraw();
3570
- return svg;
3571
- };
3572
- /**
3573
- * @override
3574
- * @inheritDoc
3575
- */
3576
- AbstractInteractiveCanvas.prototype.toImage = function (multiplier) {
3577
- var _this = this;
3578
- var options = {
3579
- format: "png",
3580
- multiplier: multiplier || this.IMAGE_CAPTURE_COEFFICIENT,
3581
- enableRetinaScaling: true
3582
- };
3583
- var base64 = this.executeWithNormalizedCanvas(function () { return _this.fabricCanvas.toDataURL(options); });
3584
- return replace(base64, /^data:.*;base64,/, "");
3585
- };
3586
- /**
3587
- * @override
3588
- * @inheritDoc
3589
- */
3590
- AbstractInteractiveCanvas.prototype.createPNGStream = function () {
3591
- return this.fabricCanvas.createPNGStream();
3592
- };
3593
- /**
3594
- * @override
3595
- * @inheritDoc
3596
- */
3597
- AbstractInteractiveCanvas.prototype.registerUndoState = function () {
3598
- throw Error("Undo not supported on this canvas type");
3599
- };
3600
- /**
3601
- * @override
3602
- * @inheritDoc
3603
- */
3604
- AbstractInteractiveCanvas.prototype.undo = function () {
3605
- throw Error("Undo not supported on this canvas type");
3606
- };
3607
- /**
3608
- * @override
3609
- * @inheritDoc
3610
- */
3611
- AbstractInteractiveCanvas.prototype.redo = function () {
3612
- throw Error("Redo not supported on this canvas type");
3613
- };
3614
- /**
3615
- * Redraw the canvas.
3616
- */
3617
- AbstractInteractiveCanvas.prototype.redraw = function () {
3618
- this.fabricCanvas.renderAll();
3619
- };
3620
- AbstractInteractiveCanvas.prototype.syncObjectList = function () {
3621
- var _this = this;
3622
- var newObjectList = _(this.fabricCanvas.getObjects())
3623
- .map(function (obj) { return _this.findObject(obj.id); })
3624
- .filter(function (x) { return x !== undefined; })
3625
- .value();
3626
- this.objects$.next(newObjectList);
3627
- this.onObjectListChange();
3628
- };
3629
- /**
3630
- * @override
3631
- * @inheritDoc
3632
- */
3633
- AbstractInteractiveCanvas.prototype.notifyObjectListChanged = function () {
3634
- this.onObjectListChange();
3635
- };
3636
- /**
3637
- * @override
3638
- * @inheritDoc
3639
- */
3640
- AbstractInteractiveCanvas.prototype.notifyTextChanged = function () {
3641
- this.onTextChange();
3642
- };
3643
- /**
3644
- * @override
3645
- * @inheritDoc
3646
- */
3647
- AbstractInteractiveCanvas.prototype.notifyMarginChanged = function () {
3648
- this.onMarginChange();
3649
- };
3650
- AbstractInteractiveCanvas.prototype.removeFabricObject = function (obj) {
3651
- this.fabricCanvas.remove(obj);
3652
- };
3653
- AbstractInteractiveCanvas.prototype.selectFabricObject = function (object) {
3654
- this.fabricCanvas.setActiveObject(object);
3655
- };
3656
- AbstractInteractiveCanvas.prototype.onTextChange = function () {
3657
- this.layoutManager$.value.onTextChange();
3658
- };
3659
- AbstractInteractiveCanvas.prototype.onObjectListChange = function () {
3660
- this.layoutManager$.value.onObjectListChange();
3661
- };
3662
- AbstractInteractiveCanvas.prototype.onDimensionsChange = function () {
3663
- this.layoutManager$.value.onCanvasDimensionsChange();
3664
- };
3665
- AbstractInteractiveCanvas.prototype.onMarginChange = function () {
3666
- this.layoutManager$.value.onMarginChange();
3667
- };
3668
- AbstractInteractiveCanvas.prototype.bindFields = function (persistedFields) {
3669
- var _this = this;
3670
- var bound = _(persistedFields)
3671
- .flatMap(function (field) {
3672
- try {
3673
- return [_this.bindField(field)];
3674
- }
3675
- catch (e) {
3676
- console.error("Could not bind field", e, field);
3677
- return []; //skip
3678
- }
3679
- })
3680
- .value();
3681
- this.objects$.next(bound);
3682
- };
3683
- AbstractInteractiveCanvas.prototype.bindField = function (persistedField) {
3684
- var fabricObject = this.findFabricObject(persistedField.id);
3685
- switch (persistedField.type) {
3686
- case ObjectType.TEXT:
3687
- return new Text(this, fabricObject, persistedField);
3688
- case ObjectType.RECTANGLE:
3689
- return new Rectangle(this, fabricObject, persistedField);
3690
- case ObjectType.BORDER:
3691
- return new Border(this, fabricObject, persistedField);
3692
- case ObjectType.CUTOFF:
3693
- return new Cutoff(this, fabricObject, persistedField);
3694
- case ObjectType.IMAGE:
3695
- case ObjectType.LIBRARY_IMAGE:
3696
- return new Image(this, fabricObject, persistedField);
3697
- case ObjectType.HORIZONTAL_RULE:
3698
- return new HorizontalRule(this, fabricObject, persistedField);
3699
- case ObjectType.VERTICAL_RULE:
3700
- return new VerticalRule(this, fabricObject, persistedField);
3701
- case ObjectType.RICH_TEXT:
3702
- return new RichText(this, fabricObject, persistedField);
3703
- case ObjectType.BACKGROUND_IMAGE:
3704
- return new BackgroundImage(this, fabricObject, persistedField);
3705
- default:
3706
- throw new Error("Unknown field type: [" + persistedField.type + "]");
3707
- }
3708
- };
3709
- AbstractInteractiveCanvas.prototype.findFabricObject = function (id) {
3710
- var objects = this.fabricCanvas.getObjects();
3711
- var fabricObject = find(objects, function (o) { return o.id == id; });
3712
- if (!fabricObject) {
3713
- throw new Error("Cannot find fabric object with id [" + id + "]");
3714
- }
3715
- return fabricObject;
3716
- };
3717
- AbstractInteractiveCanvas.prototype.findObject = function (id) {
3718
- //Don't use strict compare here. fabric ids are numbers while ad builder ids are strings... mostly.
3719
- return find(this.objects$.value, function (object) { return object.getId() == id; });
3720
- };
3721
- /**
3722
- * Reset zoom and canvas dimensions to 1:1 scale, execute a function, then return the canvas to
3723
- * the previous values.
3724
- * @param func Function to execute.
3725
- */
3726
- AbstractInteractiveCanvas.prototype.executeWithNormalizedCanvas = function (func) {
3727
- this.updateCanvasDimensions(this.dimensions$.value, 1);
3728
- var response;
3729
- try {
3730
- response = func();
3731
- }
3732
- finally {
3733
- this.updateCanvasDimensions(this.dimensions$.value, this.zoom$.value);
3734
- }
3735
- return response;
3736
- };
3737
- /**
3738
- * Update the actual dimensions of the canvas by multiplying the requested dimensions by the current zoom factor.
3739
- */
3740
- AbstractInteractiveCanvas.prototype.updateCanvasDimensions = function (dims, zoom) {
3741
- var width = dims.widthPx * zoom;
3742
- var height = dims.heightPx * zoom;
3743
- this.fabricCanvas.setZoom(zoom);
3744
- this.fabricCanvas.setDimensions({
3745
- width: width,
3746
- height: height
3747
- });
3748
- };
3749
- AbstractInteractiveCanvas.prototype.onProfileChanged = function (profile) {
3750
- this.setMultiSelect(profile.allowMultiSelect);
3751
- this.layoutManager$.value.onProfileChange(profile);
3752
- };
3753
- AbstractInteractiveCanvas.prototype.getFieldByCanvasObject = function (object) {
3754
- if (!object) {
3755
- return null;
3756
- }
3757
- return find(this.objects$.value, function (field) { return field.getId() == object.id; });
3758
- };
3759
- AbstractInteractiveCanvas.prototype.getFieldsByCanvasObject = function (objects) {
3760
- var _this = this;
3761
- return _(objects)
3762
- .map(function (obj) { return _this.getFieldByCanvasObject(obj); })
3763
- .reject(function (x) { return !x; })
3764
- .value();
3765
- };
3766
- AbstractInteractiveCanvas.prototype.calculateImageCaptureCoefficient = function () {
3767
- var precision = 3;
3768
- var devicePixelRatio = this.devicePixelRatio || 1;
3769
- return round(2 / devicePixelRatio, precision);
3770
- };
3771
- AbstractInteractiveCanvas.prototype.randomId = function () {
3772
- var min = 0;
3773
- var max = 999;
3774
- var id = Math.floor(Math.random() * (max - min)) + min;
3775
- return (new Date()).getTime() + "" + id;
3776
- };
3777
- AbstractInteractiveCanvas.prototype.getLowestTextPoint = function () {
3778
- return _(this.objects$.value)
3779
- .filter(function (object) { return object.getType() === ObjectType.TEXT; })
3780
- .map(function (text) { return text.getDimensions().top.px + text.getDimensions().height.px; })
3781
- .reduce(function (x, y) { return Math.max(x, y); }, 0);
3782
- };
3783
- AbstractInteractiveCanvas.prototype.trackNewObject = function (object) {
3784
- this.objects$.next(__spreadArrays(this.objects$.value, [object]));
3785
- };
3786
- AbstractInteractiveCanvas.prototype.dimensionsExceedRestrictions = function (dimensions) {
3787
- var restrictions = this.dimensionRestrictions$.value;
3788
- return oc(restrictions).height.min() != undefined && dimensions.height < oc(restrictions).height.min()
3789
- || oc(restrictions).height.max() != undefined && dimensions.height > oc(restrictions).height.max()
3790
- || oc(restrictions).width.min() != undefined && dimensions.width < oc(restrictions).width.min()
3791
- || oc(restrictions).width.max() != undefined && dimensions.width > oc(restrictions).width.max()
3792
- || oc(restrictions).volume.min() != undefined && dimensions.height * dimensions.width < oc(restrictions).volume.min()
3793
- || oc(restrictions).volume.max() != undefined && dimensions.height * dimensions.width > oc(restrictions).volume.max();
3794
- };
3795
- return AbstractInteractiveCanvas;
3796
- }());
3797
- var SelectionData = /** @class */ (function () {
3798
- function SelectionData(objects) {
3799
- this.objects = objects;
3800
- this.empty = this.objects.length < 1;
3801
- var selectedObject = this.objects[0];
3802
- this.isText = selectedObject
3803
- && selectedObject.getType() === ObjectType.TEXT
3804
- && selectedObject.isEditing();
3805
- }
3806
- return SelectionData;
3807
- }());
3808
-
3809
- var HeadlessInteractiveCanvas = /** @class */ (function (_super) {
3810
- __extends(HeadlessInteractiveCanvas, _super);
3811
- function HeadlessInteractiveCanvas() {
3812
- var _this = _super.call(this, fabric, new fabric.StaticCanvas(null, { width: 640, height: 480 }), null) || this;
3813
- _this.headless = true;
3814
- return _this;
3815
- }
3816
- /**
3817
- * @override
3818
- */
3819
- HeadlessInteractiveCanvas.prototype.selectFabricObject = function (object) {
3820
- //no-op
3821
- };
3822
- return HeadlessInteractiveCanvas;
3823
- }(AbstractInteractiveCanvas));
3824
-
3825
- var UndoStack = /** @class */ (function () {
3826
- function UndoStack(canvas, size) {
3827
- if (size === void 0) { size = 100; }
3828
- this.canvas = canvas;
3829
- this.undoStates = [];
3830
- this.redoStates = [];
3831
- this.lastState = null;
3832
- this.size = size;
3833
- }
3834
- /**
3835
- * @override
3836
- * @inheritDoc
3837
- */
3838
- UndoStack.prototype.save = function (state) {
3839
- var currentState = this.canvas.save();
3840
- var undoState = this.undoStates[this.undoStates.length - 1];
3841
- if (isEqual(undoState, state))
3842
- return;
3843
- this.undoStates.push(state);
3844
- if (this.undoStates.length > this.size)
3845
- this.undoStates.splice(0, 1);
3846
- this.clearRedoStackIfCanvasChanged(currentState);
3847
- };
3848
- /**
3849
- * @override
3850
- * @inheritDoc
3851
- */
3852
- UndoStack.prototype.undo = function () {
3853
- return __awaiter(this, void 0, void 0, function () {
3854
- var undoState, currentState;
3855
- return __generator(this, function (_a) {
3856
- switch (_a.label) {
3857
- case 0:
3858
- undoState = this.undoStates.pop();
3859
- if (!undoState)
3860
- return [2 /*return*/];
3861
- currentState = this.canvas.save();
3862
- if (isEqual(undoState, currentState)) {
3863
- undoState = this.undoStates.pop();
3864
- if (!undoState)
3865
- return [2 /*return*/];
3866
- }
3867
- this.lastState = undoState;
3868
- this.redoStates.push(currentState);
3869
- return [4 /*yield*/, this.canvas.load(undoState)];
3870
- case 1:
3871
- _a.sent();
3872
- return [2 /*return*/];
3873
- }
3874
- });
3875
- });
3876
- };
3877
- /**
3878
- * @override
3879
- * @inheritDoc
3880
- */
3881
- UndoStack.prototype.redo = function () {
3882
- return __awaiter(this, void 0, void 0, function () {
3883
- var redoState, currentState;
3884
- return __generator(this, function (_a) {
3885
- switch (_a.label) {
3886
- case 0:
3887
- redoState = this.redoStates.pop();
3888
- if (!redoState)
3889
- return [2 /*return*/];
3890
- currentState = this.canvas.save();
3891
- this.lastState = redoState;
3892
- this.undoStates.push(currentState);
3893
- return [4 /*yield*/, this.canvas.load(redoState)];
3894
- case 1:
3895
- _a.sent();
3896
- return [2 /*return*/];
3897
- }
3898
- });
3899
- });
3900
- };
3901
- UndoStack.prototype.clearRedoStackIfCanvasChanged = function (currentState) {
3902
- if (isEqual(this.lastState, currentState))
3903
- return;
3904
- this.redoStates = [];
3905
- };
3906
- return UndoStack;
3907
- }());
3908
-
3909
- var InteractiveCanvas = /** @class */ (function (_super) {
3910
- __extends(InteractiveCanvas, _super);
3911
- function InteractiveCanvas(elementId, devicePixelRatio) {
3912
- var _this = _super.call(this, window['fabric'], new window['fabric'].Canvas(elementId), devicePixelRatio) || this;
3913
- _this.devicePixelRatio = devicePixelRatio;
3914
- _this.headless = false;
3915
- _this.undoStack = new UndoStack(_this);
3916
- //Initialize watchers
3917
- _this.fabricCanvas.on("selection:updated", function () { return _this.onFabricSelectionChanged(); });
3918
- _this.fabricCanvas.on("selection:created", function () { return _this.onFabricSelectionChanged(); });
3919
- _this.fabricCanvas.on("selection:cleared", function () { return _this.onFabricSelectionChanged(); });
3920
- _this.fabricCanvas.on("text:editing:entered", function () { return _this.onTextEditing(); });
3921
- _this.fabricCanvas.on("text:editing:exited", function () { return _this.onTextEditing(); });
3922
- _this.fabricCanvas.on("text:changed", function () { return _this.onTextChange(); });
3923
- return _this;
3924
- }
3925
- /**
3926
- * @override
3927
- * @inheritDoc
3928
- */
3929
- InteractiveCanvas.prototype.registerUndoState = function () {
3930
- this.undoStack.save(this.save());
3931
- };
3932
- /**
3933
- * @override
3934
- * @inheritDoc
3935
- */
3936
- InteractiveCanvas.prototype.undo = function () {
3937
- return __awaiter(this, void 0, void 0, function () {
3938
- return __generator(this, function (_a) {
3939
- switch (_a.label) {
3940
- case 0: return [4 /*yield*/, this.undoStack.undo()];
3941
- case 1:
3942
- _a.sent();
3943
- return [2 /*return*/];
3944
- }
3945
- });
3946
- });
3947
- };
3948
- /**
3949
- * @override
3950
- * @inheritDoc
3951
- */
3952
- InteractiveCanvas.prototype.redo = function () {
3953
- return __awaiter(this, void 0, void 0, function () {
3954
- return __generator(this, function (_a) {
3955
- switch (_a.label) {
3956
- case 0: return [4 /*yield*/, this.undoStack.redo()];
3957
- case 1:
3958
- _a.sent();
3959
- return [2 /*return*/];
3960
- }
3961
- });
3962
- });
3963
- };
3964
- InteractiveCanvas.prototype.onFabricSelectionChanged = function () {
3965
- var selected = this.getFieldsByCanvasObject(this.fabricCanvas.getActiveObjects());
3966
- this.selection$.next(new SelectionData(selected));
3967
- this.enterTextSelectionModeIfRequired();
3968
- };
3969
- InteractiveCanvas.prototype.enterTextSelectionModeIfRequired = function () {
3970
- if (!this.profile$.value.enterTextEditImmediatelyOnSelect && !this.layoutManager$.value.enterTextEditImmediatelyOnSelect)
3971
- return;
3972
- if (!this.selection$.value || !this.selection$.value.objects[0] || this.selection$.value.objects[0].getType() !== ObjectType.TEXT)
3973
- return;
3974
- var text = this.selection$.value.objects[0];
3975
- text.enterEditing();
3976
- };
3977
- InteractiveCanvas.prototype.onTextEditing = function () {
3978
- var selected = this.getFieldsByCanvasObject(this.fabricCanvas.getActiveObjects());
3979
- this.selection$.next(new SelectionData(selected));
3980
- };
3981
- return InteractiveCanvas;
3982
- }(AbstractInteractiveCanvas));
3983
-
3984
- export { AbstractCanvasObject, AbstractInteractiveCanvas, AbstractLayoutManager, AdvancedLayoutManager, BackgroundImage, Border, CanvasObjectData, Cutoff, DefaultCanvasProfile, Font, FontLibrary, HeadlessInteractiveCanvas, HorizontalRule, Image, ImageType, InteractiveCanvas, LayoutManager, MillimeterDimensions, ObjectType, PixelCanvasDimensions, Print6ColCanvasDimensions, Print8ColCanvasDimensions, Rectangle, RichText, SelectionData, StandardLayoutManager, Text, UNKNOWN_FONT, UnknownFont, VerticalRule, isMutableBackgroundColor, isMutableColor, isMutableImage, isMutablePosition, isMutableStroke, isMutableText, isMutableTextStyle };