@leapfrog/interactive-canvas 1.0.2 → 1.0.4

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