@leapfrog/interactive-canvas 1.0.2 → 1.0.3

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