@leapfrog/interactive-canvas 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/{interactive-canvas.impl.d.ts → abstract-interactive-canvas.d.ts} +340 -305
  2. package/dist/dimensions/pixel-canvas-dimensions.d.ts +13 -13
  3. package/dist/dimensions/print-6-col-canvas-dimensions.d.ts +18 -18
  4. package/dist/dimensions/print-8-col-canvas-dimensions.d.ts +18 -18
  5. package/dist/font/font-library.d.ts +27 -22
  6. package/dist/font/font-library.interface.d.ts +24 -20
  7. package/dist/font/font.interface.d.ts +20 -20
  8. package/dist/font/unknown-font.d.ts +6 -6
  9. package/dist/headless-interactive-canvas.d.ts +9 -0
  10. package/dist/index.d.ts +49 -47
  11. package/dist/interactive-canvas.d.ts +8 -0
  12. package/dist/interactive-canvas.es.js +3193 -0
  13. package/dist/interactive-canvas.interface.d.ts +267 -221
  14. package/dist/interactive-canvas.js +3222 -34
  15. package/dist/layout/abstract-layout-manager.d.ts +17 -18
  16. package/dist/layout/advanced-layout-manager.d.ts +15 -15
  17. package/dist/layout/function/pin-to-bottom.layout-function.d.ts +4 -4
  18. package/dist/layout/function/redraw-all.layout-function.d.ts +4 -4
  19. package/dist/layout/function/reposition-borders.layout-function.d.ts +4 -4
  20. package/dist/layout/function/reposition-cutoffs.layout-function.d.ts +4 -4
  21. package/dist/layout/function/resize-canvas.layout-function.d.ts +4 -4
  22. package/dist/layout/function/stack-objects.layout-function.d.ts +11 -11
  23. package/dist/layout/function/update-object-dimensions.layout-function.d.ts +4 -4
  24. package/dist/layout/layout-manager.enum.d.ts +5 -5
  25. package/dist/layout/layout-manager.interface.d.ts +13 -13
  26. package/dist/layout/standard-layout-manager.d.ts +35 -36
  27. package/dist/mutator/mutable-background-color.interface.d.ts +15 -15
  28. package/dist/mutator/mutable-color.interface.d.ts +6 -6
  29. package/dist/mutator/mutable-image.interface.d.ts +28 -28
  30. package/dist/mutator/mutable-position.interface.d.ts +33 -33
  31. package/dist/mutator/mutable-text-style.interface.d.ts +163 -149
  32. package/dist/mutator/mutable-text.interface.d.ts +40 -40
  33. package/dist/object/abstract-canvas-object.d.ts +155 -155
  34. package/dist/object/canvas-object-data.d.ts +23 -23
  35. package/dist/object/canvas-object.interface.d.ts +96 -96
  36. package/dist/object/impl/border.d.ts +59 -60
  37. package/dist/object/impl/cutoff.d.ts +59 -60
  38. package/dist/object/impl/horizontal-rule.d.ts +88 -89
  39. package/dist/object/impl/image.d.ts +81 -82
  40. package/dist/object/impl/rectangle.d.ts +58 -59
  41. package/dist/object/impl/text.d.ts +264 -255
  42. package/dist/object/impl/vertical-rule.d.ts +88 -89
  43. package/dist/profile/canvas-profile.interface.d.ts +22 -22
  44. package/dist/profile/default-canvas-profile.d.ts +23 -23
  45. package/dist/types/canvas-dimension-restrictions.interface.d.ts +14 -14
  46. package/dist/types/canvas-dimensions.interface.d.ts +21 -21
  47. package/dist/types/canvas-state.interface.d.ts +11 -11
  48. package/dist/types/dimension-restrictions.interface.d.ts +6 -6
  49. package/dist/types/image-type.enum.d.ts +6 -6
  50. package/dist/types/margin.interface.d.ts +6 -6
  51. package/dist/types/object-dimensions.interface.d.ts +23 -23
  52. package/dist/types/object-type.enum.d.ts +10 -10
  53. package/dist/types/overflow.interface.d.ts +4 -4
  54. package/dist/types/position.interface.d.ts +6 -6
  55. package/dist/types/rect.interface.d.ts +6 -6
  56. package/dist/types/selection-data.interface.d.ts +19 -19
  57. package/dist/types/text-alignment.interface.d.ts +1 -1
  58. package/package.json +7 -9
  59. package/readme.md +11 -0
  60. package/dist/interactive-canvas.js.map +0 -1
  61. package/dist/interactive-canvas.nomin.js +0 -57494
@@ -0,0 +1,3193 @@
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.updatePosition = function () {
1045
+ this.fabricObject.set({
1046
+ strokeWidth: this.strokeWidth$.value,
1047
+ top: 0,
1048
+ left: 0,
1049
+ width: this.canvas.getDimensions().widthPx - this.strokeWidth$.value,
1050
+ height: this.canvas.getDimensions().heightPx - this.strokeWidth$.value
1051
+ });
1052
+ };
1053
+ /**
1054
+ * @override
1055
+ * @inheritDoc
1056
+ */
1057
+ Border.prototype.updateFabricObjectBehavior = function () {
1058
+ _super.prototype.updateFabricObjectBehavior.call(this);
1059
+ this.fabricObject.set({
1060
+ lockMovementX: true,
1061
+ lockMovementY: true,
1062
+ lockScalingX: true,
1063
+ lockScalingY: true,
1064
+ lockScalingFlip: true
1065
+ });
1066
+ this.fabricObject.setControlsVisibility({
1067
+ tl: false,
1068
+ tr: false,
1069
+ br: false,
1070
+ bl: false,
1071
+ ml: false,
1072
+ mt: false,
1073
+ mr: false,
1074
+ mb: false,
1075
+ mtr: false
1076
+ });
1077
+ this.redraw();
1078
+ };
1079
+ return Border;
1080
+ }(AbstractCanvasObject));
1081
+
1082
+ var Cutoff = /** @class */ (function (_super) {
1083
+ __extends(Cutoff, _super);
1084
+ function Cutoff(canvas, fabricObject, persistedField) {
1085
+ var _this = _super.call(this, ObjectType.CUTOFF, canvas, fabricObject, persistedField) || this;
1086
+ _this.strokeWidth$ = new BehaviorSubject(0);
1087
+ _this.canvas = canvas;
1088
+ _this.strokeWidth$.next(_this.fabricObject.height);
1089
+ _this.updatePosition();
1090
+ _this.fabricObject.set({ strokeWidth: 0 });
1091
+ return _this;
1092
+ }
1093
+ /**
1094
+ * @override
1095
+ * @inheritDoc
1096
+ */
1097
+ Cutoff.prototype.getColor = function () {
1098
+ return this.fabricObject.fill;
1099
+ };
1100
+ /**
1101
+ * @override
1102
+ * @inheritDoc
1103
+ */
1104
+ Cutoff.prototype.setColor = function (color) {
1105
+ this.fabricObject.set({ fill: color, stroke: color });
1106
+ this.data.color = color;
1107
+ this.redraw();
1108
+ };
1109
+ /**
1110
+ * @override
1111
+ * @inheritDoc
1112
+ */
1113
+ Cutoff.prototype.getStrokeWidth = function () {
1114
+ // noinspection JSSuspiciousNameCombination
1115
+ return this.fabricObject.height;
1116
+ };
1117
+ /**
1118
+ * @override
1119
+ * @inheritDoc
1120
+ */
1121
+ Cutoff.prototype.getStrokeWidth$ = function () {
1122
+ return this.strokeWidth$;
1123
+ };
1124
+ /**
1125
+ * @override
1126
+ * @inheritDoc
1127
+ */
1128
+ Cutoff.prototype.setStrokeWidth = function (width) {
1129
+ this.strokeWidth$.next(width);
1130
+ this.data.strokeWidth = width;
1131
+ this.updatePosition();
1132
+ this.canvas.notifyMarginChanged();
1133
+ this.redraw();
1134
+ };
1135
+ Cutoff.prototype.updatePosition = function () {
1136
+ this.fabricObject.set({
1137
+ top: this.canvas.getDimensions().heightPx - this.strokeWidth$.value,
1138
+ left: 0,
1139
+ width: this.canvas.getDimensions().widthPx,
1140
+ height: this.strokeWidth$.value
1141
+ });
1142
+ };
1143
+ /**
1144
+ * @override
1145
+ * @inheritDoc
1146
+ */
1147
+ Cutoff.prototype.updateFabricObjectBehavior = function () {
1148
+ this.fabricObject.set({
1149
+ lockMovementX: true,
1150
+ lockMovementY: true,
1151
+ lockScalingX: true,
1152
+ lockScalingY: true,
1153
+ lockScalingFlip: true
1154
+ });
1155
+ this.fabricObject.setControlsVisibility({
1156
+ tl: false,
1157
+ tr: false,
1158
+ br: false,
1159
+ bl: false,
1160
+ ml: false,
1161
+ mt: false,
1162
+ mr: false,
1163
+ mb: false,
1164
+ mtr: false
1165
+ });
1166
+ this.redraw();
1167
+ };
1168
+ return Cutoff;
1169
+ }(AbstractCanvasObject));
1170
+
1171
+ var HorizontalRule = /** @class */ (function (_super) {
1172
+ __extends(HorizontalRule, _super);
1173
+ function HorizontalRule(canvas, fabricObject, persistedField) {
1174
+ var _this = _super.call(this, ObjectType.HORIZONTAL_RULE, canvas, fabricObject, persistedField) || this;
1175
+ _this.strokeWidth$ = new BehaviorSubject(2);
1176
+ _this.canvas = canvas;
1177
+ _this.strokeWidth$.next(_this.getDimensions().height.px);
1178
+ _this.fabricObject.set({ strokeWidth: 0 });
1179
+ return _this;
1180
+ }
1181
+ /**
1182
+ * @override
1183
+ * @inheritDoc
1184
+ */
1185
+ HorizontalRule.prototype.getStrokeWidth = function () {
1186
+ // noinspection JSSuspiciousNameCombination
1187
+ return this.fabricObject.height;
1188
+ };
1189
+ /**
1190
+ * @override
1191
+ * @inheritDoc
1192
+ */
1193
+ HorizontalRule.prototype.getStrokeWidth$ = function () {
1194
+ return this.strokeWidth$;
1195
+ };
1196
+ /**
1197
+ * @override
1198
+ * @inheritDoc
1199
+ */
1200
+ HorizontalRule.prototype.setStrokeWidth = function (value) {
1201
+ this.strokeWidth$.next(value);
1202
+ this.data.strokeWidth = value;
1203
+ this.setPosition({ height: value });
1204
+ this.canvas.notifyObjectListChanged();
1205
+ this.redraw();
1206
+ };
1207
+ /**
1208
+ * @override
1209
+ * @inheritDoc
1210
+ */
1211
+ HorizontalRule.prototype.getColor = function () {
1212
+ return this.fabricObject.fill;
1213
+ };
1214
+ /**
1215
+ * @override
1216
+ * @inheritDoc
1217
+ */
1218
+ HorizontalRule.prototype.setColor = function (color) {
1219
+ this.fabricObject.set({ fill: color, stroke: color });
1220
+ this.data.color = color;
1221
+ this.redraw();
1222
+ };
1223
+ /**
1224
+ * @override
1225
+ * @inheritDoc
1226
+ */
1227
+ HorizontalRule.prototype.centerHorizontally = function () {
1228
+ _super.prototype.centerHorizontallyInternal.call(this);
1229
+ };
1230
+ /**
1231
+ * @override
1232
+ * @inheritDoc
1233
+ */
1234
+ HorizontalRule.prototype.centerVertically = function () {
1235
+ _super.prototype.centerVerticallyInternal.call(this);
1236
+ };
1237
+ /**
1238
+ * @override
1239
+ * @inheritDoc
1240
+ */
1241
+ HorizontalRule.prototype.setHeight = function (height) {
1242
+ this.setPosition({ height: height });
1243
+ this.canvas.notifyObjectListChanged();
1244
+ };
1245
+ /**
1246
+ * @override
1247
+ * @inheritDoc
1248
+ */
1249
+ HorizontalRule.prototype.nudgeUp = function () {
1250
+ _super.prototype.nudgeInternal.call(this, 0, -1);
1251
+ };
1252
+ /**
1253
+ * @override
1254
+ * @inheritDoc
1255
+ */
1256
+ HorizontalRule.prototype.nudgeDown = function () {
1257
+ _super.prototype.nudgeInternal.call(this, 0, +1);
1258
+ };
1259
+ /**
1260
+ * @override
1261
+ * @inheritDoc
1262
+ */
1263
+ HorizontalRule.prototype.nudgeLeft = function () {
1264
+ _super.prototype.nudgeInternal.call(this, -1, 0);
1265
+ };
1266
+ /**
1267
+ * @override
1268
+ * @inheritDoc
1269
+ */
1270
+ HorizontalRule.prototype.nudgeRight = function () {
1271
+ _super.prototype.nudgeInternal.call(this, +1, 0);
1272
+ };
1273
+ return HorizontalRule;
1274
+ }(AbstractCanvasObject));
1275
+
1276
+ var Image = /** @class */ (function (_super) {
1277
+ __extends(Image, _super);
1278
+ function Image(canvas, fabricObject, persistedField) {
1279
+ var _this = _super.call(this, ObjectType.IMAGE, canvas, fabricObject, persistedField) || this;
1280
+ _this.DEFAULT_RESTRICTIONS = {
1281
+ minWidth: 1,
1282
+ minHeight: 1
1283
+ };
1284
+ _this.canvas = canvas;
1285
+ _this.imageUrl$ = new BehaviorSubject(_this.fabricObject.getSrc());
1286
+ _this.data.imagePath = _this.imageUrl$.value;
1287
+ _this.layoutWidth = _this.getBoundingBox().width;
1288
+ return _this;
1289
+ }
1290
+ /**
1291
+ * @override
1292
+ * @inheritDoc
1293
+ */
1294
+ Image.prototype.getImageUrl = function () {
1295
+ return this.imageUrl$.value;
1296
+ };
1297
+ /**
1298
+ * @override
1299
+ * @inheritDoc
1300
+ */
1301
+ Image.prototype.getImageUrl$ = function () {
1302
+ return this.imageUrl$;
1303
+ };
1304
+ /**
1305
+ * @override
1306
+ * @inheritDoc
1307
+ */
1308
+ Image.prototype.setImageUrl = function (url) {
1309
+ this.setCanvasImage(url);
1310
+ this.data.imagePath = url;
1311
+ this.imageUrl$.next(url);
1312
+ this.canvas.notifyObjectListChanged();
1313
+ };
1314
+ /**
1315
+ * @override
1316
+ * @inheritDoc
1317
+ */
1318
+ Image.prototype.setImageWidth = function (px, sizeRestrictions) {
1319
+ if (sizeRestrictions === void 0) { sizeRestrictions = this.DEFAULT_RESTRICTIONS; }
1320
+ var minWidth = Math.max(sizeRestrictions.minWidth, this.getMinWidthBasedOnMinHeight(sizeRestrictions.minHeight));
1321
+ var maxWidth = Math.min(sizeRestrictions.maxWidth, this.getMaxWidthBasedOnMaxHeight(sizeRestrictions.maxHeight));
1322
+ if (px < minWidth) {
1323
+ px = minWidth;
1324
+ }
1325
+ else if (px > maxWidth) {
1326
+ px = maxWidth;
1327
+ }
1328
+ var premultipliedWidth = this.fabricObject.width;
1329
+ var newScale = px / premultipliedWidth;
1330
+ this.fabricObject.set({ scaleX: newScale, scaleY: newScale });
1331
+ this.setCoords();
1332
+ this.canvas.notifyObjectListChanged();
1333
+ return px;
1334
+ };
1335
+ /**
1336
+ * @override
1337
+ * @inheritDoc
1338
+ */
1339
+ Image.prototype.centerHorizontally = function () {
1340
+ _super.prototype.centerHorizontallyInternal.call(this);
1341
+ };
1342
+ /**
1343
+ * @override
1344
+ * @inheritDoc
1345
+ */
1346
+ Image.prototype.centerVertically = function () {
1347
+ _super.prototype.centerVerticallyInternal.call(this);
1348
+ };
1349
+ /**
1350
+ * @override
1351
+ * @inheritDoc
1352
+ */
1353
+ Image.prototype.setHeight = function (height) {
1354
+ this.setPosition({ height: height });
1355
+ this.canvas.notifyObjectListChanged();
1356
+ };
1357
+ /**
1358
+ * @override
1359
+ * @inheritDoc
1360
+ */
1361
+ Image.prototype.nudgeUp = function () {
1362
+ _super.prototype.nudgeInternal.call(this, 0, -1);
1363
+ };
1364
+ /**
1365
+ * @override
1366
+ * @inheritDoc
1367
+ */
1368
+ Image.prototype.nudgeDown = function () {
1369
+ _super.prototype.nudgeInternal.call(this, 0, +1);
1370
+ };
1371
+ /**
1372
+ * @override
1373
+ * @inheritDoc
1374
+ */
1375
+ Image.prototype.nudgeLeft = function () {
1376
+ _super.prototype.nudgeInternal.call(this, -1, 0);
1377
+ };
1378
+ /**
1379
+ * @override
1380
+ * @inheritDoc
1381
+ */
1382
+ Image.prototype.nudgeRight = function () {
1383
+ _super.prototype.nudgeInternal.call(this, +1, 0);
1384
+ };
1385
+ /**
1386
+ * @override
1387
+ * @inheritDoc
1388
+ */
1389
+ Image.prototype.setPosition = function (position) {
1390
+ if (this.canvas.getLayoutManager().isEnforcedPositioning) {
1391
+ if (position.top != undefined)
1392
+ this.fabricObject.set({ top: position.top });
1393
+ if (position.width != undefined) {
1394
+ this.layoutWidth = position.width;
1395
+ }
1396
+ if (position.height != undefined) {
1397
+ var premultipliedHeight = this.fabricObject.height;
1398
+ var newScale = position.height / premultipliedHeight;
1399
+ this.fabricObject.set({ scaleX: newScale, scaleY: newScale });
1400
+ this.setCoords();
1401
+ }
1402
+ if (position.left != undefined || position.width != undefined || position.height != undefined) {
1403
+ this.centerHorizontallyInLayoutWidth(position.left || 0);
1404
+ }
1405
+ }
1406
+ else {
1407
+ _super.prototype.setPosition.call(this, position);
1408
+ }
1409
+ };
1410
+ Image.prototype.centerHorizontallyInLayoutWidth = function (left) {
1411
+ var imageWidth = this.getBoundingBox().width;
1412
+ this.fabricObject.set({ left: left + (this.layoutWidth / 2) - (imageWidth / 2) });
1413
+ };
1414
+ Image.prototype.setCanvasImage = function (url) {
1415
+ var _this = this;
1416
+ var imageOptions = { crossOrigin: "anonymous" };
1417
+ //Don't let the image be more than half the canvas in size.
1418
+ var maxSizeScale = 2;
1419
+ var maxWidth = this.canvas.getDimensions().widthPx / maxSizeScale;
1420
+ this.fabricObject.setSrc(url, function (img) {
1421
+ var newScale = 1;
1422
+ var curWidth = newScale * img.width;
1423
+ if (curWidth > maxWidth) {
1424
+ newScale = maxWidth / curWidth;
1425
+ }
1426
+ img.set({
1427
+ scaleX: newScale,
1428
+ scaleY: newScale
1429
+ });
1430
+ img.setCoords();
1431
+ _this.redraw();
1432
+ }, imageOptions);
1433
+ };
1434
+ Image.prototype.getMinWidthBasedOnMinHeight = function (minHeight) {
1435
+ if (!minHeight) {
1436
+ return 0;
1437
+ }
1438
+ var premultipliedHeight = this.fabricObject.height;
1439
+ var newScale = minHeight / premultipliedHeight;
1440
+ return this.getBoundingBox().width * newScale;
1441
+ };
1442
+ Image.prototype.getMaxWidthBasedOnMaxHeight = function (maxHeight) {
1443
+ if (!maxHeight) {
1444
+ return Infinity;
1445
+ }
1446
+ var premultipliedHeight = this.fabricObject.height;
1447
+ var newScale = maxHeight / premultipliedHeight;
1448
+ return this.getBoundingBox().width * newScale;
1449
+ };
1450
+ return Image;
1451
+ }(AbstractCanvasObject));
1452
+
1453
+ var Rectangle = /** @class */ (function (_super) {
1454
+ __extends(Rectangle, _super);
1455
+ function Rectangle(canvas, fabricObject, persistedField) {
1456
+ var _this = _super.call(this, ObjectType.RECTANGLE, canvas, fabricObject, persistedField) || this;
1457
+ _this.canvas = canvas;
1458
+ _this.fabricObject.set({ strokeWidth: 0 });
1459
+ return _this;
1460
+ }
1461
+ /**
1462
+ * @override
1463
+ * @inheritDoc
1464
+ */
1465
+ Rectangle.prototype.getColor = function () {
1466
+ return this.fabricObject.fill;
1467
+ };
1468
+ /**
1469
+ * @override
1470
+ * @inheritDoc
1471
+ */
1472
+ Rectangle.prototype.setColor = function (color) {
1473
+ this.fabricObject.set({ fill: color, stroke: color });
1474
+ this.data.color = color;
1475
+ this.redraw();
1476
+ };
1477
+ /**
1478
+ * @override
1479
+ * @inheritDoc
1480
+ */
1481
+ Rectangle.prototype.centerHorizontally = function () {
1482
+ _super.prototype.centerHorizontallyInternal.call(this);
1483
+ };
1484
+ /**
1485
+ * @override
1486
+ * @inheritDoc
1487
+ */
1488
+ Rectangle.prototype.centerVertically = function () {
1489
+ _super.prototype.centerVerticallyInternal.call(this);
1490
+ };
1491
+ /**
1492
+ * @override
1493
+ * @inheritDoc
1494
+ */
1495
+ Rectangle.prototype.setHeight = function (height) {
1496
+ this.setPosition({ height: height });
1497
+ this.canvas.notifyObjectListChanged();
1498
+ };
1499
+ /**
1500
+ * @override
1501
+ * @inheritDoc
1502
+ */
1503
+ Rectangle.prototype.nudgeUp = function () {
1504
+ _super.prototype.nudgeInternal.call(this, 0, -1);
1505
+ };
1506
+ /**
1507
+ * @override
1508
+ * @inheritDoc
1509
+ */
1510
+ Rectangle.prototype.nudgeDown = function () {
1511
+ _super.prototype.nudgeInternal.call(this, 0, +1);
1512
+ };
1513
+ /**
1514
+ * @override
1515
+ * @inheritDoc
1516
+ */
1517
+ Rectangle.prototype.nudgeLeft = function () {
1518
+ _super.prototype.nudgeInternal.call(this, -1, 0);
1519
+ };
1520
+ /**
1521
+ * @override
1522
+ * @inheritDoc
1523
+ */
1524
+ Rectangle.prototype.nudgeRight = function () {
1525
+ _super.prototype.nudgeInternal.call(this, +1, 0);
1526
+ };
1527
+ return Rectangle;
1528
+ }(AbstractCanvasObject));
1529
+
1530
+ var Text = /** @class */ (function (_super) {
1531
+ __extends(Text, _super);
1532
+ function Text(canvas, fabricObject, persistedField) {
1533
+ var _this = _super.call(this, ObjectType.TEXT, canvas, fabricObject, persistedField) || this;
1534
+ _this.canvas = canvas;
1535
+ _this.fontLibrary = canvas.getFontLibrary();
1536
+ _this.baseComponentHeight = _this.fabricObject.height;
1537
+ _this.text$ = new BehaviorSubject(_this.fabricObject.text);
1538
+ _this.data.userText = _this.fabricObject.text;
1539
+ _this.textSelection$ = new BehaviorSubject(new TextSelection(_this.fabricObject, _this.fontLibrary));
1540
+ _this.fabricObject.set({ strokeWidth: 0 });
1541
+ _this.fabricObject.on("changed", function () { return _this.onTextChange(); });
1542
+ _this.fabricObject.on("selection:changed", function () { return _this.onSelectionChange(); });
1543
+ _this.applyBulletListMonkeyPatches();
1544
+ return _this;
1545
+ }
1546
+ /**
1547
+ * @override
1548
+ * @inheritDoc
1549
+ */
1550
+ Text.prototype.getText = function () {
1551
+ return this.text$.value;
1552
+ };
1553
+ /**
1554
+ * @override
1555
+ * @inheritDoc
1556
+ */
1557
+ Text.prototype.getText$ = function () {
1558
+ return this.text$;
1559
+ };
1560
+ /**
1561
+ * @override
1562
+ * @inheritDoc
1563
+ */
1564
+ Text.prototype.setText = function (text) {
1565
+ var style = this.fabricObject.getStyleAtPosition(0, true);
1566
+ this.fabricObject.set({ text: text });
1567
+ this.data.userText = this.fabricObject.text;
1568
+ this.text$.next(text);
1569
+ this.fabricObject.setSelectionStyles(style, 0, text.length);
1570
+ this.redraw();
1571
+ };
1572
+ /**
1573
+ * @override
1574
+ * @inheritDoc
1575
+ */
1576
+ Text.prototype.replaceText = function (target, replacement) {
1577
+ var start = this.getText().indexOf(target);
1578
+ this.fabricObject.insertChars(replacement, null, start, start + target.length);
1579
+ this.redraw();
1580
+ this.data.userText = this.fabricObject.text;
1581
+ this.text$.next(this.fabricObject.text);
1582
+ };
1583
+ /**
1584
+ * @override
1585
+ * @inheritDoc
1586
+ */
1587
+ Text.prototype.getColor = function () {
1588
+ return this.textSelection$.value.fill;
1589
+ };
1590
+ /**
1591
+ * @override
1592
+ * @inheritDoc
1593
+ */
1594
+ Text.prototype.setColor = function (color) {
1595
+ if (this.hasTextSelection()) {
1596
+ this.fabricObject.setSelectionStyles({ fill: color });
1597
+ }
1598
+ else {
1599
+ this.fabricObject.set({ fill: color });
1600
+ }
1601
+ this.data.color = color;
1602
+ this.updateTextSelection();
1603
+ this.redraw();
1604
+ };
1605
+ /**
1606
+ * @override
1607
+ * @inheritDoc
1608
+ */
1609
+ Text.prototype.getTextSelection = function () {
1610
+ return this.textSelection$.value;
1611
+ };
1612
+ /**
1613
+ * @override
1614
+ * @inheritDoc
1615
+ */
1616
+ Text.prototype.getTextSelection$ = function () {
1617
+ return this.textSelection$;
1618
+ };
1619
+ /**
1620
+ * @override
1621
+ * @inheritDoc
1622
+ */
1623
+ Text.prototype.setTextAlign = function (alignment) {
1624
+ this.fabricObject.set({ textAlign: alignment });
1625
+ this.redraw();
1626
+ this.updateTextSelection();
1627
+ };
1628
+ /**
1629
+ * @override
1630
+ * @inheritDoc
1631
+ */
1632
+ Text.prototype.setCharSpacing = function (spacing) {
1633
+ this.fabricObject.set({ charSpacing: spacing });
1634
+ this.redraw();
1635
+ this.updateTextSelection();
1636
+ };
1637
+ /**
1638
+ * @override
1639
+ * @inheritDoc
1640
+ */
1641
+ Text.prototype.setLineHeight = function (height) {
1642
+ this.fabricObject.set({ lineHeight: height });
1643
+ this.redraw();
1644
+ this.updateTextSelection();
1645
+ };
1646
+ /**
1647
+ * @override
1648
+ * @inheritDoc
1649
+ */
1650
+ Text.prototype.setBold = function (bold) {
1651
+ if (bold === void 0) { bold = true; }
1652
+ if (this.hasTextSelection()) {
1653
+ var start = this.textSelection$.value.start;
1654
+ for (var i = 0; i < this.textSelection$.value.length; i++) {
1655
+ var style = this.fabricObject.getStyleAtPosition(start + i, true);
1656
+ var font = this.fontLibrary.getFont(style.fontFamily);
1657
+ var fontWeight = bold ? font.boldWeight : font.normalWeight;
1658
+ this.fabricObject.setSelectionStyles({ fontWeight: fontWeight }, start + i, start + i + 1);
1659
+ }
1660
+ }
1661
+ else {
1662
+ var font = this.fontLibrary.getFont(this.fabricObject.fontFamily);
1663
+ var fontWeight = bold ? font.boldWeight : font.normalWeight;
1664
+ this.fabricObject.set({ fontWeight: fontWeight });
1665
+ }
1666
+ this.redraw();
1667
+ this.updateTextSelection();
1668
+ };
1669
+ /**
1670
+ * @override
1671
+ * @inheritDoc
1672
+ */
1673
+ Text.prototype.setBoldRegion = function (start, end, bold) {
1674
+ if (bold === void 0) { bold = true; }
1675
+ if (start > end)
1676
+ throw Error("Bad region specified: [" + start + " - " + end + "]");
1677
+ for (var i = start; i < end; i++) {
1678
+ var style = this.fabricObject.getStyleAtPosition(i, true);
1679
+ var font = this.fontLibrary.getFont(style.fontFamily);
1680
+ style.fontWeight = bold ? font.boldWeight : font.normalWeight;
1681
+ this.fabricObject.setSelectionStyles(style, i, i + 1);
1682
+ }
1683
+ this.redraw();
1684
+ };
1685
+ /**
1686
+ * @override
1687
+ * @inheritDoc
1688
+ */
1689
+ Text.prototype.setItalic = function (italic) {
1690
+ if (italic === void 0) { italic = true; }
1691
+ if (this.hasTextSelection()) {
1692
+ this.fabricObject.setSelectionStyles({ fontStyle: italic ? "italic" : "" });
1693
+ }
1694
+ else {
1695
+ this.fabricObject.set({ fontStyle: italic ? "italic" : "" });
1696
+ }
1697
+ this.redraw();
1698
+ this.updateTextSelection();
1699
+ };
1700
+ /**
1701
+ * @override
1702
+ * @inheritDoc
1703
+ */
1704
+ Text.prototype.setItalicRegion = function (start, end, italic) {
1705
+ if (italic === void 0) { italic = true; }
1706
+ if (start > end)
1707
+ throw Error("Bad region specified: [" + start + " - " + end + "]");
1708
+ this.fabricObject.setSelectionStyles({ fontStyle: italic ? "italic" : "" }, start, end);
1709
+ this.redraw();
1710
+ };
1711
+ /**
1712
+ * @override
1713
+ * @inheritDoc
1714
+ */
1715
+ Text.prototype.setStrikethrough = function (strikethrough) {
1716
+ if (strikethrough === void 0) { strikethrough = true; }
1717
+ if (this.hasTextSelection()) {
1718
+ this.fabricObject.setSelectionStyles({ linethrough: strikethrough });
1719
+ }
1720
+ else {
1721
+ this.fabricObject.set({ linethrough: strikethrough });
1722
+ }
1723
+ this.redraw();
1724
+ this.updateTextSelection();
1725
+ };
1726
+ /**
1727
+ * @override
1728
+ * @inheritDoc
1729
+ */
1730
+ Text.prototype.setUnderline = function (underline) {
1731
+ if (underline === void 0) { underline = true; }
1732
+ this.fabricObject.setSelectionStyles({ underline: underline });
1733
+ this.redraw();
1734
+ this.updateTextSelection();
1735
+ };
1736
+ /**
1737
+ * @override
1738
+ * @inheritDoc
1739
+ */
1740
+ Text.prototype.setFont = function (font) {
1741
+ if (this.hasTextSelection()) {
1742
+ this.fabricObject.setSelectionStyles({ fontFamily: font.family, fontWeight: font.normalWeight });
1743
+ }
1744
+ else {
1745
+ this.fabricObject.set({ fontFamily: font.family, fontWeight: font.normalWeight });
1746
+ }
1747
+ this.redraw();
1748
+ this.updateTextSelection();
1749
+ };
1750
+ /**
1751
+ * @override
1752
+ * @inheritDoc
1753
+ */
1754
+ Text.prototype.setFontSize = function (fontSize) {
1755
+ if (this.hasTextSelection()) {
1756
+ this.fabricObject.setSelectionStyles({ fontSize: fontSize });
1757
+ }
1758
+ else {
1759
+ this.fabricObject.set({ fontSize: fontSize });
1760
+ }
1761
+ this.redraw();
1762
+ this.updateTextSelection();
1763
+ };
1764
+ /**
1765
+ * @override
1766
+ * @inheritDoc
1767
+ */
1768
+ Text.prototype.setTextBackgroundColor = function (color) {
1769
+ if (this.hasTextSelection()) {
1770
+ this.fabricObject.setSelectionStyles({ textBackgroundColor: color });
1771
+ }
1772
+ else {
1773
+ this.fabricObject.set({ textBackgroundColor: color });
1774
+ }
1775
+ this.redraw();
1776
+ this.updateTextSelection();
1777
+ };
1778
+ /**
1779
+ * @override
1780
+ * @inheritDoc
1781
+ */
1782
+ Text.prototype.centerHorizontally = function () {
1783
+ _super.prototype.centerHorizontallyInternal.call(this);
1784
+ };
1785
+ /**
1786
+ * @override
1787
+ * @inheritDoc
1788
+ */
1789
+ Text.prototype.centerVertically = function () {
1790
+ _super.prototype.centerVerticallyInternal.call(this);
1791
+ };
1792
+ /**
1793
+ * @override
1794
+ * @inheritDoc
1795
+ */
1796
+ Text.prototype.setHeight = function (height) {
1797
+ //no-op
1798
+ };
1799
+ /**
1800
+ * @override
1801
+ * @inheritDoc
1802
+ */
1803
+ Text.prototype.nudgeUp = function () {
1804
+ _super.prototype.nudgeInternal.call(this, 0, -1);
1805
+ };
1806
+ /**
1807
+ * @override
1808
+ * @inheritDoc
1809
+ */
1810
+ Text.prototype.nudgeDown = function () {
1811
+ _super.prototype.nudgeInternal.call(this, 0, +1);
1812
+ };
1813
+ /**
1814
+ * @override
1815
+ * @inheritDoc
1816
+ */
1817
+ Text.prototype.nudgeLeft = function () {
1818
+ _super.prototype.nudgeInternal.call(this, -1, 0);
1819
+ };
1820
+ /**
1821
+ * @override
1822
+ * @inheritDoc
1823
+ */
1824
+ Text.prototype.nudgeRight = function () {
1825
+ _super.prototype.nudgeInternal.call(this, +1, 0);
1826
+ };
1827
+ /**
1828
+ * @override
1829
+ * @inheritDoc
1830
+ */
1831
+ Text.prototype.getBackgroundColor = function () {
1832
+ return this.fabricObject.backgroundColor;
1833
+ };
1834
+ /**
1835
+ * @override
1836
+ * @inheritDoc
1837
+ */
1838
+ Text.prototype.setBackgroundColor = function (color) {
1839
+ this.fabricObject.set({ backgroundColor: color });
1840
+ this.redraw();
1841
+ };
1842
+ /**
1843
+ * @override
1844
+ * @inheritDoc
1845
+ */
1846
+ Text.prototype.setPrefillType = function (prefillType) {
1847
+ this.data.prefillType = prefillType;
1848
+ };
1849
+ /**
1850
+ * @override
1851
+ * @inheritDoc
1852
+ */
1853
+ Text.prototype.getPrefillType = function () {
1854
+ return this.data.prefillType;
1855
+ };
1856
+ /**
1857
+ * @override
1858
+ * @inheritDoc
1859
+ */
1860
+ Text.prototype.isResizeCanvas = function () {
1861
+ return this.data.resizeCanvas;
1862
+ };
1863
+ /**
1864
+ * @override
1865
+ * @inheritDoc
1866
+ */
1867
+ Text.prototype.setResizeCanvas = function (value) {
1868
+ this.baseComponentHeight = this.fabricObject.height;
1869
+ this.data.resizeCanvas = value;
1870
+ };
1871
+ /**
1872
+ * @override
1873
+ * @inheritDoc
1874
+ */
1875
+ Text.prototype.focus = function () {
1876
+ this.fabricObject.hiddenTextarea.focus();
1877
+ };
1878
+ /**
1879
+ * @override
1880
+ * @inheritDoc
1881
+ */
1882
+ Text.prototype.isEditing = function () {
1883
+ return this.fabricObject.isEditing;
1884
+ };
1885
+ /**
1886
+ * @override
1887
+ * @inheritDoc
1888
+ */
1889
+ Text.prototype.enterEditing = function () {
1890
+ if (!this.isEditing()) {
1891
+ this.fabricObject.enterEditing();
1892
+ this.focus();
1893
+ this.selectAllText();
1894
+ this.redraw();
1895
+ }
1896
+ };
1897
+ /**
1898
+ * @override
1899
+ * @inheritDoc
1900
+ */
1901
+ Text.prototype.selectAllText = function () {
1902
+ this.fabricObject.selectAll();
1903
+ };
1904
+ /**
1905
+ * @override
1906
+ * @inheritDoc
1907
+ */
1908
+ Text.prototype.isBulleted = function () {
1909
+ return this.data.bulleted || false;
1910
+ };
1911
+ /**
1912
+ * @override
1913
+ * @inheritDoc
1914
+ */
1915
+ Text.prototype.enableBullets = function () {
1916
+ this.fabricObject["objectCaching"] = false;
1917
+ this.fabricObject["_bullets"] = this.fabricObject["_bullets"] || {};
1918
+ this.data.bulleted = true;
1919
+ this.notifyCanvasOfTextChanges();
1920
+ this.redraw();
1921
+ };
1922
+ /**
1923
+ * @override
1924
+ * @inheritDoc
1925
+ */
1926
+ Text.prototype.disableBullets = function () {
1927
+ this.removeBullets();
1928
+ this.data.bulleted = false;
1929
+ this.notifyCanvasOfTextChanges();
1930
+ this.redraw();
1931
+ };
1932
+ Text.prototype.getSizeIncreaseSinceCreation = function () {
1933
+ return Math.max(0, this.fabricObject.height - this.baseComponentHeight);
1934
+ };
1935
+ Text.prototype.removeBullets = function () {
1936
+ var _this = this;
1937
+ if (this.data.bulleted) {
1938
+ var bullets = this.fabricObject["_bullets"];
1939
+ delete this.fabricObject["_bullets"];
1940
+ each(bullets, function (bullet) { return _this.canvas.removeFabricObject(bullet); });
1941
+ this.fabricObject["objectCaching"] = true;
1942
+ }
1943
+ };
1944
+ Text.prototype.applyBulletListMonkeyPatches = function () {
1945
+ if (this.data.bulleted)
1946
+ this.enableBullets();
1947
+ var originalRender = this.fabricObject["_render"];
1948
+ this.fabricObject["_render"] = function (ctx) {
1949
+ var _this = this;
1950
+ var fabricText = this;
1951
+ if (fabricText["_bullets"]) {
1952
+ // Determine list of active bullets
1953
+ var currentBullets_1 = fabricText["_bullets"];
1954
+ var activeBullets_1 = {};
1955
+ each(fabricText._textLines, function (textLine, lineIndex) {
1956
+ if (textLine.length) {
1957
+ // Start of real lines will have offset 0 in styleMap
1958
+ var styleMap = fabricText._styleMap[lineIndex];
1959
+ if (styleMap.offset === 0) {
1960
+ if (currentBullets_1[lineIndex]) {
1961
+ // Reuse existing bullet for this line
1962
+ activeBullets_1[lineIndex] = currentBullets_1[lineIndex];
1963
+ }
1964
+ else {
1965
+ // Create new bullet for this line
1966
+ var options = {
1967
+ left: 0,
1968
+ top: 0,
1969
+ radius: 2,
1970
+ fill: fabricText.fill,
1971
+ scaleX: fabricText.scaleX,
1972
+ scaleY: fabricText.scaleY,
1973
+ strokeWidth: 0,
1974
+ lockMovementX: true,
1975
+ lockMovementY: true,
1976
+ hasControls: false,
1977
+ selectable: false,
1978
+ excludeFromJson: true
1979
+ };
1980
+ activeBullets_1[lineIndex] = new _this.canvas.fabric.Circle(options);
1981
+ }
1982
+ }
1983
+ }
1984
+ });
1985
+ // Add/Remove bullets from canvas
1986
+ var canvas_1 = fabricText.canvas;
1987
+ var canvasDirty_1 = false;
1988
+ canvas_1.renderOnAddRemove = false;
1989
+ each(activeBullets_1, function (bullet) {
1990
+ if (!canvas_1.contains(bullet)) {
1991
+ canvas_1.add(bullet);
1992
+ canvasDirty_1 = true;
1993
+ }
1994
+ });
1995
+ each(currentBullets_1, function (bullet, key) {
1996
+ if (!activeBullets_1[key]) {
1997
+ canvas_1.remove(bullet);
1998
+ canvasDirty_1 = true;
1999
+ }
2000
+ });
2001
+ canvas_1.renderOnAddRemove = true;
2002
+ fabricText["_bullets"] = activeBullets_1;
2003
+ if (canvasDirty_1) {
2004
+ // Fabric doesn't like it when you add new things within render so use a sneaky timeout to delay render
2005
+ setTimeout(canvas_1.renderAll.bind(canvas_1));
2006
+ }
2007
+ }
2008
+ originalRender.apply(fabricText, [ctx]);
2009
+ };
2010
+ var originalRenderTextLine = this.fabricObject["_renderTextLine"];
2011
+ this.fabricObject["_renderTextLine"] = function (method, ctx, line, left, top, lineIndex) {
2012
+ var fabricText = this;
2013
+ var bullets = fabricText._bullets;
2014
+ if (bullets && bullets[lineIndex]) {
2015
+ var bullet = bullets[lineIndex];
2016
+ var bulletIndent = 10;
2017
+ var bulletLeft = fabricText.left + (left - fabricText._getLeftOffset() - bulletIndent) * fabricText.scaleX;
2018
+ if (bullet.left !== bulletLeft) {
2019
+ bullet.set({ left: bulletLeft });
2020
+ }
2021
+ /**
2022
+ * What an out-and-out mess... this calculation is a joke, and the bulletOffset is just an approximation of values that look ok for
2023
+ * the font sizes we use. The 'top' value provided is actutally the... bottom? of the line... and not the line... the bottom of where
2024
+ * the font is rendered... subtracting the `fabricText.getHeightOfLine(lineIndex)` line hieght doesn't give you the top, because the
2025
+ * line height is actually the height of the font plus the excess line height added via the text line-height property.
2026
+ * I couldn't figure out how to get the actual top of the line in two days of trying to understand this ridiculous library.
2027
+ * So, here... bullets. That sort of work... sometimes.
2028
+ */
2029
+ var textOriginTop = fabricText.top + (fabricText.height / 2);
2030
+ var fontSize = this.getValueOfPropertyAt(lineIndex, 0, "fontSize");
2031
+ var bulletOffset = ((fontSize + 6) / 2);
2032
+ var bulletTop = textOriginTop + top - bulletOffset;
2033
+ if (bullet.top !== bulletTop) {
2034
+ bullet.set({ top: bulletTop });
2035
+ }
2036
+ if (bullet.fill !== fabricText.fill) {
2037
+ bullet.set({ fill: fabricText.fill });
2038
+ }
2039
+ }
2040
+ originalRenderTextLine.apply(fabricText, [method, ctx, line, left, top, lineIndex]);
2041
+ };
2042
+ };
2043
+ /**
2044
+ * @override
2045
+ * @inheritDoc
2046
+ */
2047
+ Text.prototype.updateFabricObjectBehavior = function () {
2048
+ _super.prototype.updateFabricObjectBehavior.call(this);
2049
+ var profile = this.canvas.getProfile();
2050
+ var allowManualPositioning = !this.canvas.getLayoutManager().isEnforcedPositioning;
2051
+ this.fabricObject.set({
2052
+ lockScalingY: true,
2053
+ lockScalingX: this.isPinToBottom(),
2054
+ lockScalingFlip: true
2055
+ });
2056
+ this.fabricObject.setControlsVisibility({
2057
+ tl: false,
2058
+ tr: false,
2059
+ br: false,
2060
+ bl: false,
2061
+ ml: allowManualPositioning && profile.allowScale && !this.isPinToBottom(),
2062
+ mt: false,
2063
+ mr: allowManualPositioning && profile.allowScale && !this.isPinToBottom(),
2064
+ mb: false
2065
+ });
2066
+ this.redraw();
2067
+ };
2068
+ Text.prototype.onTextChange = function () {
2069
+ this.data.userText = this.fabricObject.text;
2070
+ this.text$.next(this.fabricObject.text);
2071
+ if (this.getBoundingBox().width > this.canvas.getDimensions().widthPx) {
2072
+ this.fabricObject.set({ left: 0, width: this.canvas.getDimensions().widthPx });
2073
+ this.redraw();
2074
+ }
2075
+ };
2076
+ Text.prototype.onDelete = function () {
2077
+ this.removeBullets();
2078
+ };
2079
+ Text.prototype.onSelectionChange = function () {
2080
+ this.updateTextSelection();
2081
+ };
2082
+ Text.prototype.hasTextSelection = function () {
2083
+ return this.canvas.getSelection().isText && this.textSelection$.value.length > 0;
2084
+ };
2085
+ Text.prototype.updateTextSelection = function () {
2086
+ this.textSelection$.next(new TextSelection(this.fabricObject, this.fontLibrary));
2087
+ this.notifyCanvasOfTextChanges();
2088
+ };
2089
+ Text.prototype.notifyCanvasOfTextChanges = function () {
2090
+ this.canvas.notifyTextChanged();
2091
+ };
2092
+ return Text;
2093
+ }(AbstractCanvasObject));
2094
+ var TextSelection = /** @class */ (function () {
2095
+ function TextSelection(fabricObject, fontLibrary) {
2096
+ this.fabricObject = fabricObject;
2097
+ this.fontLibrary = fontLibrary;
2098
+ this.start = this.fabricObject.selectionStart;
2099
+ this.end = this.fabricObject.selectionEnd;
2100
+ this.length = this.end - this.start;
2101
+ var cursorLoc = this.fabricObject.get2DCursorLocation();
2102
+ this.lineIndex = cursorLoc.lineIndex;
2103
+ this.charIndex = cursorLoc.charIndex;
2104
+ var style = this.fabricObject.getCompleteStyleDeclaration(this.lineIndex, this.charIndex);
2105
+ var hasSelection = this.length > 0;
2106
+ this.font = hasSelection ? this.fontLibrary.getFont(style.fontFamily) : this.fontLibrary.getFont(this.fabricObject.fontFamily);
2107
+ this.fontSize = hasSelection ? style.fontSize : this.fabricObject.fontSize;
2108
+ this.fontWeight = hasSelection ? style.fontWeight : this.fabricObject.fontWeight;
2109
+ this.bold = this.font.isBold(this.fontWeight);
2110
+ this.italic = hasSelection ? style.fontStyle === "italic" : this.fabricObject.fontStyle === "italic";
2111
+ this.underline = hasSelection ? style.underline : this.fabricObject.underline;
2112
+ this.strikethrough = hasSelection ? style.linethrough : this.fabricObject.linethrough;
2113
+ this.overline = hasSelection ? style.overline : this.fabricObject.overline;
2114
+ this.textBackgroundColor = hasSelection ? style.textBackgroundColor : this.fabricObject.textBackgroundColor;
2115
+ this.textAlign = this.fabricObject.textAlign;
2116
+ this.charSpacing = this.fabricObject.charSpacing;
2117
+ this.lineHeight = this.fabricObject.lineHeight;
2118
+ this.fill = style.fill;
2119
+ this.stroke = style.stroke;
2120
+ this.strokeWidth = style.strokeWidth;
2121
+ }
2122
+ return TextSelection;
2123
+ }());
2124
+
2125
+ var VerticalRule = /** @class */ (function (_super) {
2126
+ __extends(VerticalRule, _super);
2127
+ function VerticalRule(canvas, fabricObject, persistedField) {
2128
+ var _this = _super.call(this, ObjectType.VERTICAL_RULE, canvas, fabricObject, persistedField) || this;
2129
+ _this.strokeWidth$ = new BehaviorSubject(2);
2130
+ _this.canvas = canvas;
2131
+ _this.strokeWidth$.next(_this.getDimensions().width.px);
2132
+ _this.fabricObject.set({ strokeWidth: 0 });
2133
+ return _this;
2134
+ }
2135
+ /**
2136
+ * @override
2137
+ * @inheritDoc
2138
+ */
2139
+ VerticalRule.prototype.getStrokeWidth = function () {
2140
+ return this.fabricObject.width;
2141
+ };
2142
+ /**
2143
+ * @override
2144
+ * @inheritDoc
2145
+ */
2146
+ VerticalRule.prototype.getStrokeWidth$ = function () {
2147
+ return this.strokeWidth$;
2148
+ };
2149
+ /**
2150
+ * @override
2151
+ * @inheritDoc
2152
+ */
2153
+ VerticalRule.prototype.setStrokeWidth = function (width) {
2154
+ this.strokeWidth$.next(width);
2155
+ this.data.strokeWidth = width;
2156
+ this.setPosition({ width: width });
2157
+ this.canvas.notifyObjectListChanged();
2158
+ this.redraw();
2159
+ };
2160
+ /**
2161
+ * @override
2162
+ * @inheritDoc
2163
+ */
2164
+ VerticalRule.prototype.getColor = function () {
2165
+ return this.fabricObject.fill;
2166
+ };
2167
+ /**
2168
+ * @override
2169
+ * @inheritDoc
2170
+ */
2171
+ VerticalRule.prototype.setColor = function (color) {
2172
+ this.fabricObject.set({ fill: color, stroke: color });
2173
+ this.data.color = color;
2174
+ this.redraw();
2175
+ };
2176
+ /**
2177
+ * @override
2178
+ * @inheritDoc
2179
+ */
2180
+ VerticalRule.prototype.centerHorizontally = function () {
2181
+ _super.prototype.centerHorizontallyInternal.call(this);
2182
+ };
2183
+ /**
2184
+ * @override
2185
+ * @inheritDoc
2186
+ */
2187
+ VerticalRule.prototype.centerVertically = function () {
2188
+ _super.prototype.centerVerticallyInternal.call(this);
2189
+ };
2190
+ /**
2191
+ * @override
2192
+ * @inheritDoc
2193
+ */
2194
+ VerticalRule.prototype.setHeight = function (height) {
2195
+ this.setPosition({ height: height });
2196
+ this.canvas.notifyObjectListChanged();
2197
+ };
2198
+ /**
2199
+ * @override
2200
+ * @inheritDoc
2201
+ */
2202
+ VerticalRule.prototype.nudgeUp = function () {
2203
+ _super.prototype.nudgeInternal.call(this, 0, -1);
2204
+ };
2205
+ /**
2206
+ * @override
2207
+ * @inheritDoc
2208
+ */
2209
+ VerticalRule.prototype.nudgeDown = function () {
2210
+ _super.prototype.nudgeInternal.call(this, 0, +1);
2211
+ };
2212
+ /**
2213
+ * @override
2214
+ * @inheritDoc
2215
+ */
2216
+ VerticalRule.prototype.nudgeLeft = function () {
2217
+ _super.prototype.nudgeInternal.call(this, -1, 0);
2218
+ };
2219
+ /**
2220
+ * @override
2221
+ * @inheritDoc
2222
+ */
2223
+ VerticalRule.prototype.nudgeRight = function () {
2224
+ _super.prototype.nudgeInternal.call(this, +1, 0);
2225
+ };
2226
+ return VerticalRule;
2227
+ }(AbstractCanvasObject));
2228
+
2229
+ /**
2230
+ * Profile with as many features disabled as possible.
2231
+ */
2232
+ var DefaultCanvasProfile = /** @class */ (function () {
2233
+ function DefaultCanvasProfile() {
2234
+ this.allowMultiSelect = false;
2235
+ this.enterTextEditImmediatelyOnSelect = true;
2236
+ this.allowObjectsToLeaveCanvas = false;
2237
+ this.allowMove = false;
2238
+ this.allowRotate = false;
2239
+ this.allowScale = false;
2240
+ this.defaultFont = "Lato";
2241
+ this.defaultFontSize = 8;
2242
+ this.defaultFontColor = "#000000";
2243
+ this.defaultLineHeight = 1.8;
2244
+ this.defaultCharacterSpacing = 0;
2245
+ this.defaultTextAlignment = "left";
2246
+ this.defaultBorderColor = "#000000";
2247
+ this.defaultBorderWidth = 1;
2248
+ this.defaultCutoffColor = "#000000";
2249
+ this.defaultCutoffWidth = 1;
2250
+ }
2251
+ return DefaultCanvasProfile;
2252
+ }());
2253
+
2254
+ var ImageType;
2255
+ (function (ImageType) {
2256
+ ImageType["DEFAULT"] = "DEFAULT";
2257
+ ImageType["BACKGROUND"] = "BACKGROUND";
2258
+ ImageType["LOGO"] = "LOGO";
2259
+ ImageType["PHOTO"] = "PHOTO";
2260
+ })(ImageType || (ImageType = {}));
2261
+
2262
+ var ObjectType;
2263
+ (function (ObjectType) {
2264
+ ObjectType["TEXT"] = "TEXT";
2265
+ ObjectType["BORDER"] = "BORDER";
2266
+ ObjectType["IMAGE"] = "IMAGE";
2267
+ ObjectType["RECTANGLE"] = "RECTANGLE";
2268
+ ObjectType["LIBRARY_IMAGE"] = "LIBRARY_IMAGE";
2269
+ ObjectType["CUTOFF"] = "CUTOFF";
2270
+ ObjectType["VERTICAL_RULE"] = "VERTICAL_RULE";
2271
+ ObjectType["HORIZONTAL_RULE"] = "HORIZONTAL_RULE";
2272
+ })(ObjectType || (ObjectType = {}));
2273
+
2274
+ var AbstractInteractiveCanvas = /** @class */ (function () {
2275
+ function AbstractInteractiveCanvas(fabric, fabricCanvas, devicePixelRatio) {
2276
+ this.fabric = fabric;
2277
+ this.fabricCanvas = fabricCanvas;
2278
+ this.devicePixelRatio = devicePixelRatio;
2279
+ this.fontLibrary = new FontLibrary();
2280
+ this.dimensionChangeRejections$ = new Subject();
2281
+ this.IMAGE_CAPTURE_COEFFICIENT = this.calculateImageCaptureCoefficient();
2282
+ this.baseDimensions = new PixelCanvasDimensions(this.fabricCanvas.width, this.fabricCanvas.height);
2283
+ var baseRestrictions = { width: { min: 1 }, height: { min: 1 } };
2284
+ //Initialize properties
2285
+ this.dimensions$ = new BehaviorSubject(this.baseDimensions);
2286
+ this.dimensionRestrictions$ = new BehaviorSubject(baseRestrictions);
2287
+ this.zoom$ = new BehaviorSubject(1);
2288
+ this.profile$ = new BehaviorSubject(new DefaultCanvasProfile());
2289
+ this.layoutManager$ = new BehaviorSubject(new AdvancedLayoutManager(this));
2290
+ this.backgroundColor$ = new BehaviorSubject(this.fabricCanvas.backgroundColor);
2291
+ this.objects$ = new BehaviorSubject([]);
2292
+ this.selection$ = new BehaviorSubject(new SelectionData([]));
2293
+ //Configure canvas behaviour
2294
+ this.fabricCanvas.stateful = true;
2295
+ this.fabricCanvas.preserveObjectStacking = true;
2296
+ this.fabricCanvas.perPixelTargetFind = true;
2297
+ this.fabricCanvas.targetFindTolerance = 10;
2298
+ this.setBackgroundColor("#FFFFFF");
2299
+ }
2300
+ /**
2301
+ * @override
2302
+ * @inheritDoc
2303
+ */
2304
+ AbstractInteractiveCanvas.prototype.getFontLibrary = function () {
2305
+ return this.fontLibrary;
2306
+ };
2307
+ /**
2308
+ * @override
2309
+ * @inheritDoc
2310
+ */
2311
+ AbstractInteractiveCanvas.prototype.getProfile = function () {
2312
+ return this.profile$.value;
2313
+ };
2314
+ /**
2315
+ * @override
2316
+ * @inheritDoc
2317
+ */
2318
+ AbstractInteractiveCanvas.prototype.getProfile$ = function () {
2319
+ return this.profile$;
2320
+ };
2321
+ /**
2322
+ * @override
2323
+ * @inheritDoc
2324
+ */
2325
+ AbstractInteractiveCanvas.prototype.setProfile = function (profile) {
2326
+ this.profile$.next(profile);
2327
+ this.onProfileChanged(profile);
2328
+ this.redraw();
2329
+ };
2330
+ /**
2331
+ * @override
2332
+ * @inheritDoc
2333
+ */
2334
+ AbstractInteractiveCanvas.prototype.getLayoutManager = function () {
2335
+ return this.layoutManager$.value;
2336
+ };
2337
+ /**
2338
+ * @override
2339
+ * @inheritDoc
2340
+ */
2341
+ AbstractInteractiveCanvas.prototype.getLayoutManager$ = function () {
2342
+ return this.layoutManager$;
2343
+ };
2344
+ /**
2345
+ * @override
2346
+ * @inheritDoc
2347
+ */
2348
+ AbstractInteractiveCanvas.prototype.setLayoutManager = function (layoutManager) {
2349
+ if (layoutManager === LayoutManager.STANDARD) {
2350
+ this.layoutManager$.next(new StandardLayoutManager(this));
2351
+ }
2352
+ else {
2353
+ this.layoutManager$.next(new AdvancedLayoutManager(this));
2354
+ }
2355
+ this.layoutManager$.value.onInit();
2356
+ };
2357
+ /**
2358
+ * @override
2359
+ * @inheritDoc
2360
+ */
2361
+ AbstractInteractiveCanvas.prototype.getBackgroundColor = function () {
2362
+ return this.backgroundColor$.value;
2363
+ };
2364
+ /**
2365
+ * @override
2366
+ * @inheritDoc
2367
+ */
2368
+ AbstractInteractiveCanvas.prototype.getBackgroundColor$ = function () {
2369
+ return this.backgroundColor$;
2370
+ };
2371
+ /**
2372
+ * @override
2373
+ * @inheritDoc
2374
+ */
2375
+ AbstractInteractiveCanvas.prototype.setBackgroundColor = function (color) {
2376
+ if (color === void 0) { color = "White"; }
2377
+ this.fabricCanvas.setBackgroundColor(color, function () {
2378
+ });
2379
+ this.fabricCanvas.renderAll();
2380
+ this.backgroundColor$.next(color);
2381
+ };
2382
+ /**
2383
+ * @override
2384
+ * @inheritDoc
2385
+ */
2386
+ AbstractInteractiveCanvas.prototype.getDimensions = function () {
2387
+ return this.dimensions$.value;
2388
+ };
2389
+ /**
2390
+ * @override
2391
+ * @inheritDoc
2392
+ */
2393
+ AbstractInteractiveCanvas.prototype.getBaseDimensions = function () {
2394
+ return this.baseDimensions;
2395
+ };
2396
+ /**
2397
+ * @override
2398
+ * @inheritDoc
2399
+ */
2400
+ AbstractInteractiveCanvas.prototype.getDimensions$ = function () {
2401
+ return this.dimensions$;
2402
+ };
2403
+ /**
2404
+ * @override
2405
+ * @inheritDoc
2406
+ */
2407
+ AbstractInteractiveCanvas.prototype.setDimensions = function (dimensions) {
2408
+ if (this.dimensionsExceedRestrictions(dimensions)) {
2409
+ console.debug("Requested canvas dimensions exceed current restrictions", dimensions, this.dimensionRestrictions$.value);
2410
+ this.dimensionChangeRejections$.next(dimensions);
2411
+ return;
2412
+ }
2413
+ this.updateCanvasDimensions(dimensions, this.zoom$.value);
2414
+ this.dimensions$.next(dimensions);
2415
+ this.onDimensionsChange();
2416
+ this.notifyObjectListChanged();
2417
+ };
2418
+ /**
2419
+ * @override
2420
+ * @inheritDoc
2421
+ */
2422
+ AbstractInteractiveCanvas.prototype.setBaseDimensions = function (dimensions) {
2423
+ this.baseDimensions = dimensions;
2424
+ this.setDimensions(dimensions);
2425
+ this.notifyObjectListChanged();
2426
+ };
2427
+ /**
2428
+ * @override
2429
+ * @inheritDoc
2430
+ */
2431
+ AbstractInteractiveCanvas.prototype.getDimensionChangeRejections$ = function () {
2432
+ return this.dimensionChangeRejections$;
2433
+ };
2434
+ /**
2435
+ * @override
2436
+ * @inheritDoc
2437
+ */
2438
+ AbstractInteractiveCanvas.prototype.getDimensionsRestrictions = function () {
2439
+ return this.dimensionRestrictions$.value;
2440
+ };
2441
+ /**
2442
+ * @override
2443
+ * @inheritDoc
2444
+ */
2445
+ AbstractInteractiveCanvas.prototype.getDimensionsRestrictions$ = function () {
2446
+ return this.dimensionRestrictions$;
2447
+ };
2448
+ /**
2449
+ * @override
2450
+ * @inheritDoc
2451
+ */
2452
+ AbstractInteractiveCanvas.prototype.setDimensionRestrictions = function (restrictions) {
2453
+ this.dimensionRestrictions$.next(restrictions);
2454
+ };
2455
+ AbstractInteractiveCanvas.prototype.getMargin = function () {
2456
+ var borderWidth = this.getObjectsByTypeInternal(ObjectType.BORDER)
2457
+ .map(function (border) { return border.getStrokeWidth(); })
2458
+ .reduce(function (current, next) { return Math.max(current, next); }, 0);
2459
+ var cutoffWidth = this.getObjectsByTypeInternal(ObjectType.CUTOFF)
2460
+ .map(function (border) { return border.getStrokeWidth(); })
2461
+ .reduce(function (current, next) { return Math.max(current, next); }, 0);
2462
+ // noinspection JSSuspiciousNameCombination
2463
+ return {
2464
+ top: borderWidth,
2465
+ right: borderWidth,
2466
+ bottom: Math.max(cutoffWidth, borderWidth),
2467
+ left: borderWidth
2468
+ };
2469
+ };
2470
+ /**
2471
+ * @override
2472
+ * @inheritDoc
2473
+ */
2474
+ AbstractInteractiveCanvas.prototype.getZoom = function () {
2475
+ return this.zoom$.value;
2476
+ };
2477
+ /**
2478
+ * @override
2479
+ * @inheritDoc
2480
+ */
2481
+ AbstractInteractiveCanvas.prototype.getZoom$ = function () {
2482
+ return this.zoom$;
2483
+ };
2484
+ /**
2485
+ * @override
2486
+ * @inheritDoc
2487
+ */
2488
+ AbstractInteractiveCanvas.prototype.setZoom = function (zoom) {
2489
+ this.updateCanvasDimensions(this.dimensions$.value, zoom);
2490
+ this.zoom$.next(zoom);
2491
+ };
2492
+ /**
2493
+ * @override
2494
+ * @inheritDoc
2495
+ */
2496
+ AbstractInteractiveCanvas.prototype.getObjects = function () {
2497
+ return this.objects$.value;
2498
+ };
2499
+ /**
2500
+ * Get the list of objects directly.
2501
+ */
2502
+ AbstractInteractiveCanvas.prototype.getObjectsInternal = function () {
2503
+ return this.objects$.value;
2504
+ };
2505
+ /**
2506
+ * @override
2507
+ * @inheritDoc
2508
+ */
2509
+ AbstractInteractiveCanvas.prototype.getObjectsByTypeInternal = function (type) {
2510
+ return _(this.getObjectsInternal())
2511
+ .filter(function (object) { return object.getType() === type; })
2512
+ .value();
2513
+ };
2514
+ /**
2515
+ * @override
2516
+ * @inheritDoc
2517
+ */
2518
+ AbstractInteractiveCanvas.prototype.getObjectsByTypesInternal = function () {
2519
+ var types = [];
2520
+ for (var _i = 0; _i < arguments.length; _i++) {
2521
+ types[_i] = arguments[_i];
2522
+ }
2523
+ return _(this.getObjectsInternal())
2524
+ .filter(function (object) { return _(types).includes(object.getType()); })
2525
+ .value();
2526
+ };
2527
+ /**
2528
+ * @override
2529
+ * @inheritDoc
2530
+ */
2531
+ AbstractInteractiveCanvas.prototype.getObjects$ = function () {
2532
+ return this.objects$;
2533
+ };
2534
+ /**
2535
+ * @override
2536
+ * @inheritDoc
2537
+ */
2538
+ AbstractInteractiveCanvas.prototype.removeObject = function (target) {
2539
+ var concreteObject = this.findFabricObject(target.getId());
2540
+ this.fabricCanvas.remove(concreteObject);
2541
+ this.objects$.next(_(this.objects$.value)
2542
+ .filter(function (object) { return object.getId() !== target.getId(); })
2543
+ .value());
2544
+ this.onObjectListChange();
2545
+ };
2546
+ /**
2547
+ * @override
2548
+ * @inheritDoc
2549
+ */
2550
+ AbstractInteractiveCanvas.prototype.addImageObject = function (imageUrl) {
2551
+ return __awaiter(this, void 0, void 0, function () {
2552
+ var maxSizeScale, maxWidth, options;
2553
+ var _this = this;
2554
+ return __generator(this, function (_a) {
2555
+ switch (_a.label) {
2556
+ case 0:
2557
+ maxSizeScale = 2;
2558
+ maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
2559
+ options = { crossOrigin: "anonymous" };
2560
+ return [4 /*yield*/, new Promise(function (resolve) {
2561
+ _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
2562
+ var id = _this.randomId();
2563
+ var newScale = 1;
2564
+ var curWidth = newScale * fabricImage.width;
2565
+ if (curWidth > maxWidth) {
2566
+ newScale = maxWidth / curWidth;
2567
+ }
2568
+ fabricImage.set({
2569
+ id: id,
2570
+ scaleX: newScale,
2571
+ scaleY: newScale
2572
+ });
2573
+ _this.fabricCanvas.add(fabricImage);
2574
+ var object = new Image(_this, fabricImage);
2575
+ _this.trackNewObject(object);
2576
+ _this.selectFabricObject(fabricImage);
2577
+ _this.syncObjectList();
2578
+ resolve(object);
2579
+ }, options);
2580
+ })];
2581
+ case 1: return [2 /*return*/, _a.sent()];
2582
+ }
2583
+ });
2584
+ });
2585
+ };
2586
+ /**
2587
+ * @override
2588
+ * @inheritDoc
2589
+ */
2590
+ AbstractInteractiveCanvas.prototype.addTextObject = function () {
2591
+ return __awaiter(this, void 0, void 0, function () {
2592
+ var profile, id, fontSizePx, canvasWidthPx, lowestTextPointPx, topPx, textOptions, fabricTextbox, object;
2593
+ return __generator(this, function (_a) {
2594
+ profile = this.profile$.value;
2595
+ id = this.randomId();
2596
+ fontSizePx = profile.defaultFontSize;
2597
+ canvasWidthPx = this.getDimensions().widthPx;
2598
+ lowestTextPointPx = this.getLowestTextPoint();
2599
+ topPx = Math.min(lowestTextPointPx, this.getDimensions().heightPx - fontSizePx);
2600
+ textOptions = {
2601
+ id: id,
2602
+ top: topPx,
2603
+ left: 5,
2604
+ width: canvasWidthPx - 10,
2605
+ fill: profile.defaultFontColor,
2606
+ fontSize: fontSizePx,
2607
+ fontFamily: profile.defaultFont,
2608
+ textAlign: profile.defaultTextAlignment,
2609
+ multiLine: true,
2610
+ lineHeight: profile.defaultLineHeight,
2611
+ charSpacing: profile.defaultCharacterSpacing
2612
+ };
2613
+ fabricTextbox = new this.fabric.Textbox("Text goes here", textOptions);
2614
+ this.fabricCanvas.add(fabricTextbox);
2615
+ object = new Text(this, fabricTextbox);
2616
+ this.trackNewObject(object);
2617
+ this.selectFabricObject(fabricTextbox);
2618
+ this.syncObjectList();
2619
+ return [2 /*return*/, object];
2620
+ });
2621
+ });
2622
+ };
2623
+ /**
2624
+ * @override
2625
+ * @inheritDoc
2626
+ */
2627
+ AbstractInteractiveCanvas.prototype.addRectangleObject = function () {
2628
+ return __awaiter(this, void 0, void 0, function () {
2629
+ var canvasWidth, canvasHeight, rectWidth, rectHeight, rectangleOptions, fabricRect, object;
2630
+ return __generator(this, function (_a) {
2631
+ canvasWidth = this.getDimensions().widthPx;
2632
+ canvasHeight = this.getDimensions().heightPx;
2633
+ rectWidth = canvasWidth / 2;
2634
+ rectHeight = canvasHeight / 2;
2635
+ rectangleOptions = {
2636
+ id: this.randomId(),
2637
+ width: canvasWidth / 2,
2638
+ height: 2,
2639
+ top: canvasHeight / 2 - rectHeight / 2,
2640
+ left: canvasWidth / 2 - rectWidth / 2,
2641
+ fill: "#000000"
2642
+ };
2643
+ fabricRect = new this.fabric.Rect(rectangleOptions);
2644
+ this.fabricCanvas.add(fabricRect);
2645
+ object = new Rectangle(this, fabricRect);
2646
+ this.trackNewObject(object);
2647
+ this.selectFabricObject(fabricRect);
2648
+ this.syncObjectList();
2649
+ return [2 /*return*/, object];
2650
+ });
2651
+ });
2652
+ };
2653
+ /**
2654
+ * @override
2655
+ * @inheritDoc
2656
+ */
2657
+ AbstractInteractiveCanvas.prototype.addCutoffObject = function () {
2658
+ return __awaiter(this, void 0, void 0, function () {
2659
+ var profile, initialThickness, canvasWidth, canvasHeight, rectangleOptions, fabricRect, object;
2660
+ return __generator(this, function (_a) {
2661
+ profile = this.profile$.value;
2662
+ initialThickness = profile.defaultCutoffWidth;
2663
+ canvasWidth = this.getDimensions().widthPx;
2664
+ canvasHeight = this.getDimensions().heightPx;
2665
+ rectangleOptions = {
2666
+ id: this.randomId(),
2667
+ width: canvasWidth,
2668
+ height: initialThickness,
2669
+ top: canvasHeight - initialThickness,
2670
+ left: 0,
2671
+ fill: profile.defaultCutoffColor
2672
+ };
2673
+ fabricRect = new this.fabric.Rect(rectangleOptions);
2674
+ this.fabricCanvas.add(fabricRect);
2675
+ object = new Cutoff(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.addBorderObject = function () {
2688
+ return __awaiter(this, void 0, void 0, function () {
2689
+ var profile, initialThickness, rectangleOptions, fabricRect, object;
2690
+ return __generator(this, function (_a) {
2691
+ profile = this.profile$.value;
2692
+ initialThickness = profile.defaultBorderWidth;
2693
+ rectangleOptions = {
2694
+ id: this.randomId(),
2695
+ fill: null,
2696
+ stroke: profile.defaultBorderColor,
2697
+ strokeWidth: initialThickness
2698
+ };
2699
+ fabricRect = new this.fabric.Rect(rectangleOptions);
2700
+ this.fabricCanvas.add(fabricRect);
2701
+ object = new Border(this, fabricRect);
2702
+ this.trackNewObject(object);
2703
+ this.selectFabricObject(fabricRect);
2704
+ this.syncObjectList();
2705
+ return [2 /*return*/, object];
2706
+ });
2707
+ });
2708
+ };
2709
+ /**
2710
+ * @override
2711
+ * @inheritDoc
2712
+ */
2713
+ AbstractInteractiveCanvas.prototype.addVerticalRuleObject = function () {
2714
+ return __awaiter(this, void 0, void 0, function () {
2715
+ var rectOptions, fabricRect, object;
2716
+ return __generator(this, function (_a) {
2717
+ rectOptions = {
2718
+ id: this.randomId(),
2719
+ width: 2,
2720
+ height: 10,
2721
+ top: 0,
2722
+ left: 0,
2723
+ fill: "#000000"
2724
+ };
2725
+ fabricRect = new this.fabric.Rect(rectOptions);
2726
+ this.fabricCanvas.add(fabricRect);
2727
+ object = new VerticalRule(this, fabricRect);
2728
+ this.trackNewObject(object);
2729
+ this.selectFabricObject(fabricRect);
2730
+ this.syncObjectList();
2731
+ return [2 /*return*/, object];
2732
+ });
2733
+ });
2734
+ };
2735
+ /**
2736
+ * @override
2737
+ * @inheritDoc
2738
+ */
2739
+ AbstractInteractiveCanvas.prototype.addHorizontalRuleObject = function () {
2740
+ return __awaiter(this, void 0, void 0, function () {
2741
+ var rectOptions, fabricRect, object;
2742
+ return __generator(this, function (_a) {
2743
+ rectOptions = {
2744
+ id: this.randomId(),
2745
+ width: 10,
2746
+ height: 2,
2747
+ top: 0,
2748
+ left: 0,
2749
+ fill: "#000000"
2750
+ };
2751
+ fabricRect = new this.fabric.Rect(rectOptions);
2752
+ this.fabricCanvas.add(fabricRect);
2753
+ object = new HorizontalRule(this, fabricRect);
2754
+ this.trackNewObject(object);
2755
+ this.selectFabricObject(fabricRect);
2756
+ this.syncObjectList();
2757
+ return [2 /*return*/, object];
2758
+ });
2759
+ });
2760
+ };
2761
+ /**
2762
+ * @override
2763
+ * @inheritDoc
2764
+ */
2765
+ AbstractInteractiveCanvas.prototype.setPrefillData = function (data) {
2766
+ _(this.getObjectsByTypeInternal(ObjectType.TEXT)).each(function (object) {
2767
+ var prefillType = object.getPrefillType();
2768
+ if (prefillType !== null) {
2769
+ object.setText(data[prefillType] || "");
2770
+ }
2771
+ });
2772
+ };
2773
+ /**
2774
+ * @override
2775
+ * @inheritDoc
2776
+ */
2777
+ AbstractInteractiveCanvas.prototype.distributeObjectsVertically = function () {
2778
+ var margin = this.getMargin();
2779
+ var canvasHeight = this.getDimensions().heightPx;
2780
+ var objectsToLayout = _(this.getObjectsByTypesInternal(ObjectType.TEXT, ObjectType.IMAGE))
2781
+ .sortBy(function (x) { return x.getBoundingBox().top; })
2782
+ .value();
2783
+ var objectsHeight = reduce(objectsToLayout, function (total, element) { return total + element.getBoundingBox().height; }, 0);
2784
+ var workableHeight = canvasHeight - margin.top - margin.bottom;
2785
+ var requiredPadding = workableHeight - objectsHeight;
2786
+ var spaceBetween = requiredPadding / (objectsToLayout.length - 1);
2787
+ var nextElementTop = margin.top;
2788
+ for (var _i = 0, objectsToLayout_1 = objectsToLayout; _i < objectsToLayout_1.length; _i++) {
2789
+ var element = objectsToLayout_1[_i];
2790
+ var elementHeight = element.getBoundingBox().height;
2791
+ element.setPosition({
2792
+ top: nextElementTop
2793
+ });
2794
+ element.setCoords();
2795
+ nextElementTop += elementHeight + spaceBetween;
2796
+ }
2797
+ this.redraw();
2798
+ };
2799
+ /**
2800
+ * @override
2801
+ * @inheritDoc
2802
+ */
2803
+ AbstractInteractiveCanvas.prototype.getSelection = function () {
2804
+ return this.selection$.value;
2805
+ };
2806
+ /**
2807
+ * @override
2808
+ * @inheritDoc
2809
+ */
2810
+ AbstractInteractiveCanvas.prototype.getSelection$ = function () {
2811
+ return this.selection$;
2812
+ };
2813
+ /**
2814
+ * @override
2815
+ * @inheritDoc
2816
+ */
2817
+ AbstractInteractiveCanvas.prototype.select = function (object) {
2818
+ var concreteField = this.findObject(object.getId());
2819
+ if (!concreteField) {
2820
+ console.error("Attempted to select unknown object", object);
2821
+ return;
2822
+ }
2823
+ this.selectFabricObject(concreteField.getFabricObjectInternal());
2824
+ this.fabricCanvas.renderAll();
2825
+ };
2826
+ /**
2827
+ * @override
2828
+ * @inheritDoc
2829
+ */
2830
+ AbstractInteractiveCanvas.prototype.clearSelection = function () {
2831
+ this.fabricCanvas.discardActiveObject();
2832
+ this.fabricCanvas.renderAll();
2833
+ };
2834
+ AbstractInteractiveCanvas.prototype.setMultiSelect = function (value) {
2835
+ this.fabricCanvas.selection = value;
2836
+ this.fabricCanvas.selectionKey = value ? "shiftKey" : null;
2837
+ };
2838
+ /**
2839
+ * @override
2840
+ * @inheritDoc
2841
+ */
2842
+ AbstractInteractiveCanvas.prototype.clear = function () {
2843
+ this.fabricCanvas.clear();
2844
+ this.setBackgroundColor("#FFFFFF");
2845
+ this.objects$.next([]);
2846
+ };
2847
+ /**
2848
+ * @override
2849
+ * @inheritDoc
2850
+ */
2851
+ AbstractInteractiveCanvas.prototype.save = function () {
2852
+ var objects = _(this.objects$.value)
2853
+ .map(function (x) { return x.persist(); })
2854
+ .value();
2855
+ var includedProperties = ["id"];
2856
+ var json = this.fabricCanvas.toJSON(includedProperties);
2857
+ var state = {
2858
+ dimensions: this.dimensions$.value,
2859
+ canvasJson: JSON.stringify(json),
2860
+ objects: objects,
2861
+ backgroundColor: this.backgroundColor$.value
2862
+ };
2863
+ console.debug("Saving canvas state", state);
2864
+ return state;
2865
+ };
2866
+ /**
2867
+ * @override
2868
+ * @inheritDoc
2869
+ */
2870
+ AbstractInteractiveCanvas.prototype.load = function (canvasState) {
2871
+ return __awaiter(this, void 0, void 0, function () {
2872
+ var json_1;
2873
+ var _this = this;
2874
+ return __generator(this, function (_a) {
2875
+ switch (_a.label) {
2876
+ case 0:
2877
+ console.debug("Loading canvas state", canvasState);
2878
+ this.setBaseDimensions(canvasState.dimensions);
2879
+ if (!(canvasState.canvasJson != undefined)) return [3 /*break*/, 2];
2880
+ json_1 = JSON.parse(canvasState.canvasJson);
2881
+ return [4 /*yield*/, new Promise(function (resolve) {
2882
+ _this.fabricCanvas.loadFromJSON(json_1, function () { return resolve(); });
2883
+ })];
2884
+ case 1:
2885
+ _a.sent();
2886
+ _a.label = 2;
2887
+ case 2:
2888
+ this.setBackgroundColor(canvasState.backgroundColor);
2889
+ this.bindFields(canvasState.objects);
2890
+ this.removePersistedBullets();
2891
+ this.layoutManager$.value.onInit();
2892
+ return [2 /*return*/];
2893
+ }
2894
+ });
2895
+ });
2896
+ };
2897
+ /**
2898
+ * 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
2899
+ * 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
2900
+ * deletes all circles on canvas load.
2901
+ */
2902
+ AbstractInteractiveCanvas.prototype.removePersistedBullets = function () {
2903
+ var _this = this;
2904
+ var objects = this.fabricCanvas.getObjects();
2905
+ each(objects, function (object) {
2906
+ if (object.type === "circle")
2907
+ _this.removeFabricObject(object);
2908
+ });
2909
+ };
2910
+ /**
2911
+ * @override
2912
+ * @inheritDoc
2913
+ */
2914
+ AbstractInteractiveCanvas.prototype.toSvg = function () {
2915
+ var _this = this;
2916
+ var widthInMm = round(this.dimensions$.value.widthMm, 1) + "mm";
2917
+ var heightInMm = round(this.dimensions$.value.heightMm, 1) + "mm";
2918
+ var options = {
2919
+ width: widthInMm,
2920
+ height: heightInMm,
2921
+ suppressPreamble: true,
2922
+ encoding: null
2923
+ };
2924
+ return this.executeWithNormalizedCanvas(function () { return _this.fabricCanvas.toSVG(options); });
2925
+ };
2926
+ /**
2927
+ * @override
2928
+ * @inheritDoc
2929
+ */
2930
+ AbstractInteractiveCanvas.prototype.toImage = function () {
2931
+ var _this = this;
2932
+ var options = {
2933
+ format: "png",
2934
+ multiplier: this.IMAGE_CAPTURE_COEFFICIENT,
2935
+ enableRetinaScaling: true
2936
+ };
2937
+ console.debug("Capturing canvas image", options);
2938
+ var base64 = this.executeWithNormalizedCanvas(function () { return _this.fabricCanvas.toDataURL(options); });
2939
+ return replace(base64, /^data:.*;base64,/, "");
2940
+ };
2941
+ /**
2942
+ * @override
2943
+ * @inheritDoc
2944
+ */
2945
+ AbstractInteractiveCanvas.prototype.createPNGStream = function () {
2946
+ return this.fabricCanvas.createPNGStream();
2947
+ };
2948
+ /**
2949
+ * Redraw the canvas.
2950
+ */
2951
+ AbstractInteractiveCanvas.prototype.redraw = function () {
2952
+ this.fabricCanvas.renderAll();
2953
+ };
2954
+ AbstractInteractiveCanvas.prototype.syncObjectList = function () {
2955
+ var _this = this;
2956
+ var newObjectList = _(this.fabricCanvas.getObjects())
2957
+ .map(function (obj) { return _this.findObject(obj.id); })
2958
+ .filter(function (x) { return x !== undefined; })
2959
+ .value();
2960
+ this.objects$.next(newObjectList);
2961
+ this.onObjectListChange();
2962
+ };
2963
+ /**
2964
+ * @override
2965
+ * @inheritDoc
2966
+ */
2967
+ AbstractInteractiveCanvas.prototype.notifyObjectListChanged = function () {
2968
+ this.onObjectListChange();
2969
+ };
2970
+ /**
2971
+ * @override
2972
+ * @inheritDoc
2973
+ */
2974
+ AbstractInteractiveCanvas.prototype.notifyTextChanged = function () {
2975
+ this.onTextChange();
2976
+ };
2977
+ /**
2978
+ * @override
2979
+ * @inheritDoc
2980
+ */
2981
+ AbstractInteractiveCanvas.prototype.notifyMarginChanged = function () {
2982
+ this.onMarginChange();
2983
+ };
2984
+ AbstractInteractiveCanvas.prototype.removeFabricObject = function (obj) {
2985
+ this.fabricCanvas.remove(obj);
2986
+ };
2987
+ AbstractInteractiveCanvas.prototype.selectFabricObject = function (object) {
2988
+ this.fabricCanvas.setActiveObject(object);
2989
+ };
2990
+ AbstractInteractiveCanvas.prototype.onTextChange = function () {
2991
+ this.layoutManager$.value.onTextChange();
2992
+ };
2993
+ AbstractInteractiveCanvas.prototype.onObjectListChange = function () {
2994
+ this.layoutManager$.value.onObjectListChange();
2995
+ };
2996
+ AbstractInteractiveCanvas.prototype.onDimensionsChange = function () {
2997
+ this.layoutManager$.value.onCanvasDimensionsChange();
2998
+ };
2999
+ AbstractInteractiveCanvas.prototype.onMarginChange = function () {
3000
+ this.layoutManager$.value.onMarginChange();
3001
+ };
3002
+ AbstractInteractiveCanvas.prototype.bindFields = function (persistedFields) {
3003
+ var _this = this;
3004
+ console.debug("Binding fields", persistedFields);
3005
+ var bound = _(persistedFields)
3006
+ .flatMap(function (field) {
3007
+ try {
3008
+ return [_this.bindField(field)];
3009
+ }
3010
+ catch (e) {
3011
+ console.error("Could not bind field", e, field);
3012
+ return []; //skip
3013
+ }
3014
+ })
3015
+ .value();
3016
+ console.debug("Bound fields", bound);
3017
+ this.objects$.next(bound);
3018
+ };
3019
+ AbstractInteractiveCanvas.prototype.bindField = function (persistedField) {
3020
+ console.debug("Binding field", persistedField);
3021
+ var fabricObject = this.findFabricObject(persistedField.id);
3022
+ switch (persistedField.type) {
3023
+ case ObjectType.TEXT:
3024
+ return new Text(this, fabricObject, persistedField);
3025
+ case ObjectType.RECTANGLE:
3026
+ return new Rectangle(this, fabricObject, persistedField);
3027
+ case ObjectType.BORDER:
3028
+ return new Border(this, fabricObject, persistedField);
3029
+ case ObjectType.CUTOFF:
3030
+ return new Cutoff(this, fabricObject, persistedField);
3031
+ case ObjectType.IMAGE:
3032
+ case ObjectType.LIBRARY_IMAGE:
3033
+ return new Image(this, fabricObject, persistedField);
3034
+ case ObjectType.HORIZONTAL_RULE:
3035
+ return new HorizontalRule(this, fabricObject, persistedField);
3036
+ case ObjectType.VERTICAL_RULE:
3037
+ return new VerticalRule(this, fabricObject, persistedField);
3038
+ default:
3039
+ throw new Error("Unknown field type: [" + persistedField.type + "]");
3040
+ }
3041
+ };
3042
+ AbstractInteractiveCanvas.prototype.findFabricObject = function (id) {
3043
+ var objects = this.fabricCanvas.getObjects();
3044
+ var fabricObject = find(objects, function (o) { return o.id == id; });
3045
+ if (!fabricObject) {
3046
+ throw new Error("Cannot find fabric object with id [" + id + "]");
3047
+ }
3048
+ return fabricObject;
3049
+ };
3050
+ AbstractInteractiveCanvas.prototype.findObject = function (id) {
3051
+ //Don't use strict compare here. fabric ids are numbers while ad builder ids are strings... mostly.
3052
+ return find(this.objects$.value, function (object) { return object.getId() == id; });
3053
+ };
3054
+ /**
3055
+ * Reset zoom and canvas dimensions to 1:1 scale, execute a function, then return the canvas to
3056
+ * the previous values.
3057
+ * @param func Function to execute.
3058
+ */
3059
+ AbstractInteractiveCanvas.prototype.executeWithNormalizedCanvas = function (func) {
3060
+ this.updateCanvasDimensions(this.dimensions$.value, 1);
3061
+ var response;
3062
+ try {
3063
+ response = func();
3064
+ }
3065
+ finally {
3066
+ this.updateCanvasDimensions(this.dimensions$.value, this.zoom$.value);
3067
+ }
3068
+ return response;
3069
+ };
3070
+ /**
3071
+ * Update the actual dimensions of the canvas by multiplying the requested dimensions by the current zoom factor.
3072
+ */
3073
+ AbstractInteractiveCanvas.prototype.updateCanvasDimensions = function (dims, zoom) {
3074
+ var width = dims.widthPx * zoom;
3075
+ var height = dims.heightPx * zoom;
3076
+ console.debug("Updating canvas dimensions: zoom: " + zoom + ", width: " + width + ", height: " + height);
3077
+ this.fabricCanvas.setZoom(zoom);
3078
+ this.fabricCanvas.setDimensions({
3079
+ width: width,
3080
+ height: height
3081
+ });
3082
+ };
3083
+ AbstractInteractiveCanvas.prototype.onProfileChanged = function (profile) {
3084
+ this.setMultiSelect(profile.allowMultiSelect);
3085
+ this.layoutManager$.value.onProfileChange(profile);
3086
+ console.debug("Canvas profile set", profile);
3087
+ };
3088
+ AbstractInteractiveCanvas.prototype.getFieldByCanvasObject = function (object) {
3089
+ if (!object) {
3090
+ return null;
3091
+ }
3092
+ return find(this.objects$.value, function (field) { return field.getId() == object.id; });
3093
+ };
3094
+ AbstractInteractiveCanvas.prototype.getFieldsByCanvasObject = function (objects) {
3095
+ var _this = this;
3096
+ return _(objects)
3097
+ .map(function (obj) { return _this.getFieldByCanvasObject(obj); })
3098
+ .reject(function (x) { return !x; })
3099
+ .value();
3100
+ };
3101
+ AbstractInteractiveCanvas.prototype.calculateImageCaptureCoefficient = function () {
3102
+ var precision = 3;
3103
+ var devicePixelRatio = this.devicePixelRatio || 1;
3104
+ return round(2 / devicePixelRatio, precision);
3105
+ };
3106
+ AbstractInteractiveCanvas.prototype.randomId = function () {
3107
+ var min = 0;
3108
+ var max = 999;
3109
+ var id = Math.floor(Math.random() * (max - min)) + min;
3110
+ return (new Date()).getTime() + "" + id;
3111
+ };
3112
+ AbstractInteractiveCanvas.prototype.getLowestTextPoint = function () {
3113
+ return _(this.objects$.value)
3114
+ .filter(function (object) { return object.getType() === ObjectType.TEXT; })
3115
+ .map(function (text) { return text.getDimensions().top.px + text.getDimensions().height.px; })
3116
+ .reduce(function (x, y) { return Math.max(x, y); }, 0);
3117
+ };
3118
+ AbstractInteractiveCanvas.prototype.trackNewObject = function (object) {
3119
+ this.objects$.next(__spreadArrays(this.objects$.value, [object]));
3120
+ };
3121
+ AbstractInteractiveCanvas.prototype.dimensionsExceedRestrictions = function (dimensions) {
3122
+ var restrictions = this.dimensionRestrictions$.value;
3123
+ return oc(restrictions).height.min() != undefined && dimensions.height < oc(restrictions).height.min()
3124
+ || oc(restrictions).height.max() != undefined && dimensions.height > oc(restrictions).height.max()
3125
+ || oc(restrictions).width.min() != undefined && dimensions.width < oc(restrictions).width.min()
3126
+ || oc(restrictions).width.max() != undefined && dimensions.width > oc(restrictions).width.max()
3127
+ || oc(restrictions).volume.min() != undefined && dimensions.height * dimensions.width < oc(restrictions).volume.min()
3128
+ || oc(restrictions).volume.max() != undefined && dimensions.height * dimensions.width > oc(restrictions).volume.max();
3129
+ };
3130
+ return AbstractInteractiveCanvas;
3131
+ }());
3132
+ var SelectionData = /** @class */ (function () {
3133
+ function SelectionData(objects) {
3134
+ this.objects = objects;
3135
+ this.empty = this.objects.length < 1;
3136
+ var selectedObject = this.objects[0];
3137
+ this.isText = selectedObject
3138
+ && selectedObject.getType() === ObjectType.TEXT
3139
+ && selectedObject.isEditing();
3140
+ }
3141
+ return SelectionData;
3142
+ }());
3143
+
3144
+ var HeadlessInteractiveCanvas = /** @class */ (function (_super) {
3145
+ __extends(HeadlessInteractiveCanvas, _super);
3146
+ function HeadlessInteractiveCanvas() {
3147
+ return _super.call(this, fabric, new fabric.StaticCanvas(null, { width: 640, height: 480 }), null) || this;
3148
+ }
3149
+ /**
3150
+ * @override
3151
+ */
3152
+ HeadlessInteractiveCanvas.prototype.selectFabricObject = function (object) {
3153
+ //no-op
3154
+ };
3155
+ return HeadlessInteractiveCanvas;
3156
+ }(AbstractInteractiveCanvas));
3157
+
3158
+ var InteractiveCanvas = /** @class */ (function (_super) {
3159
+ __extends(InteractiveCanvas, _super);
3160
+ function InteractiveCanvas(elementId, devicePixelRatio) {
3161
+ var _this = _super.call(this, window['fabric'], new window['fabric'].Canvas(elementId), devicePixelRatio) || this;
3162
+ _this.devicePixelRatio = devicePixelRatio;
3163
+ //Initialize watchers
3164
+ _this.fabricCanvas.on("selection:updated", function () { return _this.onFabricSelectionChanged(); });
3165
+ _this.fabricCanvas.on("selection:created", function () { return _this.onFabricSelectionChanged(); });
3166
+ _this.fabricCanvas.on("selection:cleared", function () { return _this.onFabricSelectionChanged(); });
3167
+ _this.fabricCanvas.on("text:editing:entered", function () { return _this.onTextEditing(); });
3168
+ _this.fabricCanvas.on("text:editing:exited", function () { return _this.onTextEditing(); });
3169
+ _this.fabricCanvas.on("text:changed", function () { return _this.onTextChange(); });
3170
+ return _this;
3171
+ }
3172
+ InteractiveCanvas.prototype.onFabricSelectionChanged = function () {
3173
+ var selected = this.getFieldsByCanvasObject(this.fabricCanvas.getActiveObjects());
3174
+ console.debug("Selection changed", selected);
3175
+ this.selection$.next(new SelectionData(selected));
3176
+ this.enterTextSelectionModeIfRequired();
3177
+ };
3178
+ InteractiveCanvas.prototype.enterTextSelectionModeIfRequired = function () {
3179
+ if (!this.profile$.value.enterTextEditImmediatelyOnSelect && !this.layoutManager$.value.enterTextEditImmediatelyOnSelect)
3180
+ return;
3181
+ if (!this.selection$.value || !this.selection$.value.objects[0] || this.selection$.value.objects[0].getType() !== ObjectType.TEXT)
3182
+ return;
3183
+ var text = this.selection$.value.objects[0];
3184
+ text.enterEditing();
3185
+ };
3186
+ InteractiveCanvas.prototype.onTextEditing = function () {
3187
+ var selected = this.getFieldsByCanvasObject(this.fabricCanvas.getActiveObjects());
3188
+ this.selection$.next(new SelectionData(selected));
3189
+ };
3190
+ return InteractiveCanvas;
3191
+ }(AbstractInteractiveCanvas));
3192
+
3193
+ 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 };