@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,3254 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _ = require('lodash');
6
+ var tsOptchain = require('ts-optchain');
7
+ var rxjs = require('rxjs');
8
+ var fabric = require('fabric');
9
+
10
+ var PixelCanvasDimensions = /** @class */ (function () {
11
+ function PixelCanvasDimensions(width, height) {
12
+ this.widthUnit = "px";
13
+ this.heightUnit = "px";
14
+ this.width = width;
15
+ this.widthPx = width;
16
+ this.height = height;
17
+ this.heightPx = height;
18
+ }
19
+ PixelCanvasDimensions.prototype.withSize = function (width, height) {
20
+ return new PixelCanvasDimensions(width, height);
21
+ };
22
+ PixelCanvasDimensions.prototype.widthPxToMm = function (px) {
23
+ return void 0;
24
+ };
25
+ PixelCanvasDimensions.prototype.heightPxToMm = function (px) {
26
+ return void 0;
27
+ };
28
+ return PixelCanvasDimensions;
29
+ }());
30
+
31
+ var COLUMN_WIDTH_MM = 41;
32
+ var GUTTER_WIDTH_MM = 3;
33
+ var PIXELS_PER_MM = 3.937;
34
+ var Print6ColCanvasDimensions = /** @class */ (function () {
35
+ function Print6ColCanvasDimensions(width, height) {
36
+ this.widthUnit = "column(s)";
37
+ this.heightUnit = "cm";
38
+ this.width = width;
39
+ this.gutterCount = Math.max(0, width - 1);
40
+ this.gutterTotalMm = this.gutterCount * GUTTER_WIDTH_MM;
41
+ this.columnsTotalMm = width * COLUMN_WIDTH_MM;
42
+ this.widthMm = Math.max(0, this.columnsTotalMm + this.gutterTotalMm);
43
+ this.widthPx = _.round(this.widthMm * PIXELS_PER_MM);
44
+ this.height = height;
45
+ this.heightMm = Math.max(0, height * 10);
46
+ this.heightPx = _.round(this.heightMm * PIXELS_PER_MM);
47
+ }
48
+ Print6ColCanvasDimensions.prototype.withSize = function (width, height) {
49
+ return new Print6ColCanvasDimensions(width, height);
50
+ };
51
+ Print6ColCanvasDimensions.prototype.widthPxToMm = function (px) {
52
+ return px / PIXELS_PER_MM;
53
+ };
54
+ Print6ColCanvasDimensions.prototype.heightPxToMm = function (px) {
55
+ return px / PIXELS_PER_MM;
56
+ };
57
+ return Print6ColCanvasDimensions;
58
+ }());
59
+
60
+ var COLUMN_WIDTH_MM$1 = 30.8772;
61
+ var GUTTER_WIDTH_MM$1 = 2.1053;
62
+ var PIXELS_PER_MM$1 = 3.937;
63
+ var Print8ColCanvasDimensions = /** @class */ (function () {
64
+ function Print8ColCanvasDimensions(width, height) {
65
+ this.widthUnit = "column(s)";
66
+ this.heightUnit = "cm";
67
+ this.width = width;
68
+ this.gutterCount = Math.max(0, width - 1);
69
+ this.gutterTotalMm = this.gutterCount * GUTTER_WIDTH_MM$1;
70
+ this.columnsTotalMm = width * COLUMN_WIDTH_MM$1;
71
+ this.widthMm = Math.max(0, this.columnsTotalMm + this.gutterTotalMm);
72
+ this.widthPx = _.round(this.widthMm * PIXELS_PER_MM$1);
73
+ this.height = height;
74
+ this.heightMm = Math.max(0, height * 10);
75
+ this.heightPx = _.round(this.heightMm * PIXELS_PER_MM$1);
76
+ }
77
+ Print8ColCanvasDimensions.prototype.withSize = function (width, height) {
78
+ return new Print8ColCanvasDimensions(width, height);
79
+ };
80
+ Print8ColCanvasDimensions.prototype.widthPxToMm = function (px) {
81
+ return px / PIXELS_PER_MM$1;
82
+ };
83
+ Print8ColCanvasDimensions.prototype.heightPxToMm = function (px) {
84
+ return px / PIXELS_PER_MM$1;
85
+ };
86
+ return Print8ColCanvasDimensions;
87
+ }());
88
+
89
+ /*! *****************************************************************************
90
+ Copyright (c) Microsoft Corporation.
91
+
92
+ Permission to use, copy, modify, and/or distribute this software for any
93
+ purpose with or without fee is hereby granted.
94
+
95
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
96
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
97
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
98
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
99
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
100
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
101
+ PERFORMANCE OF THIS SOFTWARE.
102
+ ***************************************************************************** */
103
+ /* global Reflect, Promise */
104
+
105
+ var extendStatics = function(d, b) {
106
+ extendStatics = Object.setPrototypeOf ||
107
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
108
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
109
+ return extendStatics(d, b);
110
+ };
111
+
112
+ function __extends(d, b) {
113
+ extendStatics(d, b);
114
+ function __() { this.constructor = d; }
115
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
116
+ }
117
+
118
+ function __awaiter(thisArg, _arguments, P, generator) {
119
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
120
+ return new (P || (P = Promise))(function (resolve, reject) {
121
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
122
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
123
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
124
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
125
+ });
126
+ }
127
+
128
+ function __generator(thisArg, body) {
129
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
130
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
131
+ function verb(n) { return function (v) { return step([n, v]); }; }
132
+ function step(op) {
133
+ if (f) throw new TypeError("Generator is already executing.");
134
+ while (_) try {
135
+ 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;
136
+ if (y = 0, t) op = [op[0] & 2, t.value];
137
+ switch (op[0]) {
138
+ case 0: case 1: t = op; break;
139
+ case 4: _.label++; return { value: op[1], done: false };
140
+ case 5: _.label++; y = op[1]; op = [0]; continue;
141
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
142
+ default:
143
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
144
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
145
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
146
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
147
+ if (t[2]) _.ops.pop();
148
+ _.trys.pop(); continue;
149
+ }
150
+ op = body.call(thisArg, _);
151
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
152
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
153
+ }
154
+ }
155
+
156
+ function __spreadArrays() {
157
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
158
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
159
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
160
+ r[k] = a[j];
161
+ return r;
162
+ }
163
+
164
+ var Font = /** @class */ (function () {
165
+ function Font() {
166
+ }
167
+ /**
168
+ * @override
169
+ * @inheritDoc
170
+ */
171
+ Font.prototype.isBold = function (fontWeight) {
172
+ return fontWeight === this.boldWeight;
173
+ };
174
+ return Font;
175
+ }());
176
+
177
+ var UnknownFont = /** @class */ (function (_super) {
178
+ __extends(UnknownFont, _super);
179
+ function UnknownFont() {
180
+ var _this = _super !== null && _super.apply(this, arguments) || this;
181
+ _this.family = "_Unknown_";
182
+ _this.normalWeight = 400;
183
+ _this.boldWeight = 700;
184
+ return _this;
185
+ }
186
+ return UnknownFont;
187
+ }(Font));
188
+
189
+ var UNKNOWN_FONT = new UnknownFont();
190
+ var FontLibrary = /** @class */ (function () {
191
+ function FontLibrary() {
192
+ this._fonts = {};
193
+ }
194
+ /**
195
+ * @override
196
+ * @inheritDoc
197
+ */
198
+ FontLibrary.prototype.registerFont = function (font) {
199
+ this._fonts[font.family] = font;
200
+ };
201
+ /**
202
+ * @override
203
+ * @inheritDoc
204
+ */
205
+ FontLibrary.prototype.registerFonts = function (fonts) {
206
+ var _this = this;
207
+ _.each(fonts, function (font) { return _this._fonts[font.family] = font; });
208
+ };
209
+ /**
210
+ * @override
211
+ * @inheritDoc
212
+ */
213
+ FontLibrary.prototype.getFonts = function () {
214
+ return _.values(this._fonts);
215
+ };
216
+ /**
217
+ * @override
218
+ * @inheritDoc
219
+ */
220
+ FontLibrary.prototype.getFont = function (family) {
221
+ return this._fonts[family] || UNKNOWN_FONT;
222
+ };
223
+ return FontLibrary;
224
+ }());
225
+
226
+ var AbstractLayoutManager = /** @class */ (function () {
227
+ function AbstractLayoutManager(canvas) {
228
+ this.isEnforcedPositioning = false;
229
+ this.enterTextEditImmediatelyOnSelect = false;
230
+ this.canvas = canvas;
231
+ }
232
+ AbstractLayoutManager.prototype.getCanvas = function () {
233
+ return this.canvas;
234
+ };
235
+ AbstractLayoutManager.prototype.onInit = function () {
236
+ //noop
237
+ };
238
+ AbstractLayoutManager.prototype.onProfileChange = function (profile) {
239
+ //noop
240
+ };
241
+ AbstractLayoutManager.prototype.onCanvasDimensionsChange = function () {
242
+ //noop
243
+ };
244
+ AbstractLayoutManager.prototype.onTextChange = function () {
245
+ //noop
246
+ };
247
+ AbstractLayoutManager.prototype.onObjectListChange = function () {
248
+ //noop
249
+ };
250
+ AbstractLayoutManager.prototype.onMarginChange = function () {
251
+ //noop
252
+ };
253
+ return AbstractLayoutManager;
254
+ }());
255
+
256
+ var RedrawAllLayoutFunction = /** @class */ (function () {
257
+ function RedrawAllLayoutFunction() {
258
+ }
259
+ RedrawAllLayoutFunction.prototype.execute = function (canvas) {
260
+ canvas.getObjectsInternal().forEach(function (object) { return object.setCoords(); });
261
+ canvas.redraw();
262
+ };
263
+ return RedrawAllLayoutFunction;
264
+ }());
265
+
266
+ (function (LayoutManager) {
267
+ LayoutManager["LEGACY"] = "LEGACY";
268
+ LayoutManager["STANDARD"] = "STANDARD";
269
+ LayoutManager["ADVANCED"] = "ADVANCED";
270
+ })(exports.LayoutManager || (exports.LayoutManager = {}));
271
+
272
+ var UpdateObjectDimensionsLayoutFunction = /** @class */ (function () {
273
+ function UpdateObjectDimensionsLayoutFunction() {
274
+ }
275
+ UpdateObjectDimensionsLayoutFunction.prototype.execute = function (canvas) {
276
+ canvas.getObjectsInternal().forEach(function (object) { return object.updateDimensions(); });
277
+ };
278
+ return UpdateObjectDimensionsLayoutFunction;
279
+ }());
280
+
281
+ var MIN_MAGNITUDE = 1;
282
+ var BULLET_MARGIN = 15;
283
+ var StackObjectsLayoutFunction = /** @class */ (function () {
284
+ function StackObjectsLayoutFunction() {
285
+ }
286
+ StackObjectsLayoutFunction.prototype.execute = function (canvas) {
287
+ var canvasMargin = canvas.getMargin();
288
+ var canvasDimensions = canvas.getDimensions();
289
+ var allObjects = canvas.getObjectsByTypesInternal(exports.ObjectType.TEXT, exports.ObjectType.IMAGE, exports.ObjectType.RECTANGLE, exports.ObjectType.VERTICAL_RULE, exports.ObjectType.HORIZONTAL_RULE);
290
+ var objects = _(allObjects)
291
+ .partition(function (object) { return object.isPinToBottom(); })
292
+ .value();
293
+ var footerObjects = objects[0];
294
+ var bodyObjects = objects[1];
295
+ var bodyWidth = canvasDimensions.widthPx - canvasMargin.right - canvasMargin.left;
296
+ //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.
297
+ this.applyWidths(allObjects, bodyWidth);
298
+ var bodyRows = this.getRows(bodyObjects);
299
+ var footerRows = this.getRows(footerObjects);
300
+ var minimumHeight = canvasMargin.top + bodyRows.totalHeight + footerRows.totalHeight + canvasMargin.bottom;
301
+ this.adjustCanvasSize(canvas, minimumHeight);
302
+ this.layoutFooter(footerRows, canvas);
303
+ this.layoutBody(bodyRows, canvas, footerRows.totalHeight);
304
+ };
305
+ StackObjectsLayoutFunction.prototype.layoutFooter = function (footerRows, canvas) {
306
+ var canvasDimensions = canvas.getDimensions();
307
+ var canvasMargin = canvas.getMargin();
308
+ var layoutAreaWidth = canvasDimensions.widthPx - canvasMargin.left - canvasMargin.right;
309
+ var nextObjectTop = canvasDimensions.heightPx - canvasMargin.bottom - footerRows.totalHeight;
310
+ var currentColumnWidthPercent = 0;
311
+ _.each(footerRows.rows, function (row) {
312
+ _.each(row.objects, function (object) {
313
+ var columnOffset = currentColumnWidthPercent / 100 * layoutAreaWidth;
314
+ var objectMargin = tsOptchain.oc(object.getMargin()).left(0);
315
+ var left = canvasMargin.left + columnOffset + objectMargin;
316
+ if (object.getType() === exports.ObjectType.VERTICAL_RULE)
317
+ object.setPosition({ height: row.height });
318
+ object.setPosition({
319
+ top: nextObjectTop + tsOptchain.oc(object.getMargin()).top(0),
320
+ left: left
321
+ });
322
+ currentColumnWidthPercent += object.getColumnWidthPercent();
323
+ });
324
+ currentColumnWidthPercent = 0;
325
+ nextObjectTop += row.height;
326
+ });
327
+ };
328
+ StackObjectsLayoutFunction.prototype.layoutBody = function (bodyRows, canvas, footerHeight) {
329
+ var canvasDimensions = canvas.getDimensions();
330
+ var canvasMargin = canvas.getMargin();
331
+ var workableWidth = canvasDimensions.widthPx - canvasMargin.left - canvasMargin.right;
332
+ var workableHeight = canvasDimensions.heightPx - canvasMargin.top - canvasMargin.bottom - footerHeight;
333
+ var leftoverSpace = workableHeight - bodyRows.totalHeight;
334
+ var spacesRequired = this.countSpaces(bodyRows);
335
+ var spaceSize = leftoverSpace / spacesRequired;
336
+ var nextObjectTop = canvasMargin.top;
337
+ var currentColumnWidthPercent = 0;
338
+ for (var i = 0; i < bodyRows.rows.length; i++) {
339
+ var row = bodyRows.rows[i];
340
+ var nextRow = bodyRows.rows[i + 1];
341
+ for (var _i = 0, _a = row.objects; _i < _a.length; _i++) {
342
+ var object = _a[_i];
343
+ var columnOffset = currentColumnWidthPercent / 100 * workableWidth;
344
+ var objectMargin = tsOptchain.oc(object.getMargin()).left(0);
345
+ var objectLeft = canvasMargin.left + columnOffset + objectMargin;
346
+ if (object.getType() === exports.ObjectType.TEXT && object.isBulleted())
347
+ objectLeft += BULLET_MARGIN;
348
+ if (object.getType() === exports.ObjectType.VERTICAL_RULE)
349
+ object.setPosition({ height: row.height });
350
+ object.setPosition({
351
+ top: nextObjectTop + tsOptchain.oc(object.getMargin()).top(0),
352
+ left: objectLeft
353
+ });
354
+ currentColumnWidthPercent += object.getColumnWidthPercent();
355
+ }
356
+ currentColumnWidthPercent = 0;
357
+ nextObjectTop += row.height;
358
+ var thisRowIsHR = row.objects.length === 1 && row.objects[0].getType() === exports.ObjectType.HORIZONTAL_RULE;
359
+ var nextRowIsHR = tsOptchain.oc(nextRow).objects.length() === 1 && tsOptchain.oc(nextRow).objects()[0].getType() === exports.ObjectType.HORIZONTAL_RULE;
360
+ if (!thisRowIsHR && !nextRowIsHR) {
361
+ nextObjectTop += spaceSize;
362
+ }
363
+ }
364
+ };
365
+ StackObjectsLayoutFunction.prototype.applyWidths = function (objects, layoutWidth) {
366
+ _(objects).forEach(function (object) {
367
+ var targetWidth = object.getColumnWidthPercent() / 100 * layoutWidth;
368
+ var actualWidth = targetWidth - tsOptchain.oc(object.getMargin()).left(0) - tsOptchain.oc(object.getMargin()).right(0);
369
+ if (object.getType() === exports.ObjectType.VERTICAL_RULE)
370
+ return;
371
+ if (object.getType() === exports.ObjectType.TEXT && object.isBulleted())
372
+ actualWidth -= BULLET_MARGIN;
373
+ object.setPosition({
374
+ width: actualWidth
375
+ });
376
+ object.updateDimensions();
377
+ });
378
+ };
379
+ StackObjectsLayoutFunction.prototype.adjustCanvasSize = function (canvas, requiredHeight) {
380
+ var dimensions = canvas.getDimensions();
381
+ var targetDimensions = canvas.getDimensions().withSize(dimensions.width, MIN_MAGNITUDE);
382
+ while (targetDimensions.heightPx < requiredHeight) {
383
+ targetDimensions = targetDimensions.withSize(dimensions.width, targetDimensions.height + 1);
384
+ }
385
+ if (dimensions.height !== targetDimensions.height) {
386
+ canvas.setDimensions(targetDimensions);
387
+ }
388
+ };
389
+ StackObjectsLayoutFunction.prototype.getRows = function (objectsToLayout) {
390
+ var _this = this;
391
+ var result = { totalHeight: 0, rows: [] };
392
+ var currentWidth = 0;
393
+ var row = { objects: [], height: 1 };
394
+ _.each(objectsToLayout, function (object) {
395
+ var nextWidth = object.getColumnWidthPercent();
396
+ var objectHeight = _this.getObjectHeight(object);
397
+ if (currentWidth + nextWidth > 100) {
398
+ result.rows.push(row);
399
+ row = { objects: [], height: 1 };
400
+ currentWidth = 0;
401
+ }
402
+ row.objects.push(object);
403
+ if (object.getType() !== exports.ObjectType.VERTICAL_RULE)
404
+ row.height = Math.max(row.height, objectHeight);
405
+ currentWidth += nextWidth;
406
+ });
407
+ if (row.objects.length)
408
+ result.rows.push(row);
409
+ result.totalHeight = _.reduce(result.rows, function (x, y) { return x + y.height; }, 0);
410
+ return result;
411
+ };
412
+ StackObjectsLayoutFunction.prototype.getObjectHeight = function (element) {
413
+ return element.getBoundingBox().height
414
+ + tsOptchain.oc(element.getMargin()).top(0)
415
+ + tsOptchain.oc(element.getMargin()).bottom(0);
416
+ };
417
+ StackObjectsLayoutFunction.prototype.countSpaces = function (rows) {
418
+ var count = 0;
419
+ for (var i = 0; i < rows.rows.length - 1; i++) {
420
+ var row = rows.rows[i];
421
+ var nextRow = rows.rows[i + 1];
422
+ var thisRowIsHR = row.objects.length === 1 && row.objects[0].getType() === exports.ObjectType.HORIZONTAL_RULE;
423
+ var nextRowIsHR = tsOptchain.oc(nextRow).objects.length() === 1 && tsOptchain.oc(nextRow).objects()[0].getType() === exports.ObjectType.HORIZONTAL_RULE;
424
+ if (!thisRowIsHR && !nextRowIsHR) {
425
+ count++;
426
+ }
427
+ }
428
+ return count;
429
+ };
430
+ return StackObjectsLayoutFunction;
431
+ }());
432
+
433
+ var RepositionBordersLayoutFunction = /** @class */ (function () {
434
+ function RepositionBordersLayoutFunction() {
435
+ }
436
+ RepositionBordersLayoutFunction.prototype.execute = function (canvas) {
437
+ canvas.getObjectsByTypeInternal(exports.ObjectType.BORDER)
438
+ .forEach(function (border) { return border.updatePosition(); });
439
+ };
440
+ return RepositionBordersLayoutFunction;
441
+ }());
442
+
443
+ var StandardLayoutManager = /** @class */ (function (_super) {
444
+ __extends(StandardLayoutManager, _super);
445
+ function StandardLayoutManager(canvas) {
446
+ var _this = _super.call(this, canvas) || this;
447
+ _this.key = exports.LayoutManager.STANDARD;
448
+ _this.isEnforcedPositioning = true;
449
+ _this.enterTextEditImmediatelyOnSelect = true;
450
+ _this.redrawAll = new RedrawAllLayoutFunction();
451
+ _this.repositionCutoffs = new RepositionCutoffsLayoutFunction();
452
+ _this.repositionBorders = new RepositionBordersLayoutFunction();
453
+ _this.stackObjects = new StackObjectsLayoutFunction();
454
+ _this.updateObjectDimensions = new UpdateObjectDimensionsLayoutFunction();
455
+ return _this;
456
+ }
457
+ /**
458
+ * @override
459
+ */
460
+ StandardLayoutManager.prototype.onInit = function () {
461
+ this.stackAndRedraw();
462
+ };
463
+ /**
464
+ * @override
465
+ */
466
+ StandardLayoutManager.prototype.onProfileChange = function (profile) {
467
+ this.canvas.setMultiSelect(false);
468
+ this.redrawAll.execute(this.canvas);
469
+ };
470
+ /**
471
+ * @override
472
+ */
473
+ StandardLayoutManager.prototype.onTextChange = function () {
474
+ this.stackAndRedraw();
475
+ };
476
+ /**
477
+ * @override
478
+ */
479
+ StandardLayoutManager.prototype.onObjectListChange = function () {
480
+ this.stackAndRedraw();
481
+ };
482
+ /**
483
+ * @override
484
+ */
485
+ StandardLayoutManager.prototype.onMarginChange = function () {
486
+ this.stackAndRedraw();
487
+ };
488
+ StandardLayoutManager.prototype.stackAndRedraw = function () {
489
+ this.stackObjects.execute(this.canvas);
490
+ this.repositionCutoffs.execute(this.canvas);
491
+ this.repositionBorders.execute(this.canvas);
492
+ this.updateObjectDimensions.execute(this.canvas);
493
+ this.redrawAll.execute(this.canvas);
494
+ };
495
+ return StandardLayoutManager;
496
+ }(AbstractLayoutManager));
497
+
498
+ function isMutableBackgroundColor(object) {
499
+ return "setBackgroundColor" in object;
500
+ }
501
+
502
+ function isMutableColor(object) {
503
+ return "setColor" in object;
504
+ }
505
+
506
+ function isMutableImage(object) {
507
+ return "setImageUrl" in object;
508
+ }
509
+
510
+ function isMutablePosition(object) {
511
+ return "centerHorizontally" in object;
512
+ }
513
+
514
+ function isMutableText(object) {
515
+ return "setText" in object;
516
+ }
517
+
518
+ function isMutableTextStyle(object) {
519
+ return "getTextSelection" in object;
520
+ }
521
+
522
+ /**
523
+ * Data for a persisted canvas object.
524
+ */
525
+ var CanvasObjectData = /** @class */ (function () {
526
+ function CanvasObjectData(type, id) {
527
+ this.id = id;
528
+ this.type = type;
529
+ }
530
+ return CanvasObjectData;
531
+ }());
532
+
533
+ var ROTATION_SNAP_ANGLE = 45;
534
+ var AbstractCanvasObject = /** @class */ (function () {
535
+ /**
536
+ * Create a new instance.
537
+ * @param type Object type.
538
+ * @param canvas Interactive canvas that the field will be rendered on.
539
+ * @param fabricObject The fabric object to bind this field to.
540
+ * @param data Existing persisted field data to load from, if applicable.
541
+ */
542
+ function AbstractCanvasObject(type, canvas, fabricObject, data) {
543
+ var _this = this;
544
+ this.canvas = canvas;
545
+ this.fabricObject = fabricObject;
546
+ this.data = data ? data : new CanvasObjectData(type, this.fabricObject.id);
547
+ this.dimensions$ = new rxjs.BehaviorSubject(new FieldDimensions(this.fabricObject, this.canvas));
548
+ this.updatePinToBottomDistance();
549
+ this.canvas.getProfile$().subscribe(function (profile) { return _this.onProfileChange(profile); });
550
+ this.canvas.getLayoutManager$().subscribe(function (layoutManager) { return _this.onLayoutManagerChange(layoutManager); });
551
+ this.fabricObject.on("moving", function (e) { return _this.onMove(e); });
552
+ this.fabricObject.on("rotating", function (e) { return _this.onRotate(e); });
553
+ this.fabricObject.on("scaling", function (e) { return _this.onScale(e); });
554
+ }
555
+ /**
556
+ * @override
557
+ * @inheritDoc
558
+ */
559
+ AbstractCanvasObject.prototype.getId = function () {
560
+ return this.data.id;
561
+ };
562
+ /**
563
+ * @override
564
+ * @inheritDoc
565
+ */
566
+ AbstractCanvasObject.prototype.getType = function () {
567
+ return this.data.type;
568
+ };
569
+ /**
570
+ * @override
571
+ * @inheritDoc
572
+ */
573
+ AbstractCanvasObject.prototype.getDescription = function () {
574
+ switch (this.data.type) {
575
+ case exports.ObjectType.BORDER:
576
+ return "Border";
577
+ case exports.ObjectType.CUTOFF:
578
+ return "Bottom Border";
579
+ case exports.ObjectType.IMAGE:
580
+ return "Image";
581
+ case exports.ObjectType.LIBRARY_IMAGE:
582
+ return "Library Image";
583
+ case exports.ObjectType.RECTANGLE:
584
+ return "Rectangle";
585
+ case exports.ObjectType.TEXT:
586
+ return "Text";
587
+ default:
588
+ return "Field";
589
+ }
590
+ };
591
+ /**
592
+ * @override
593
+ * @inheritDoc
594
+ */
595
+ AbstractCanvasObject.prototype.setSortCaption = function (value) {
596
+ this.data.sortCaption = value;
597
+ };
598
+ /**
599
+ * @override
600
+ * @inheritDoc
601
+ */
602
+ AbstractCanvasObject.prototype.isSortCaption = function () {
603
+ return this.data.sortCaption;
604
+ };
605
+ /**
606
+ * @override
607
+ * @inheritDoc
608
+ */
609
+ AbstractCanvasObject.prototype.getDimensions = function () {
610
+ return this.dimensions$.value;
611
+ };
612
+ /**
613
+ * @override
614
+ * @inheritDoc
615
+ */
616
+ AbstractCanvasObject.prototype.getDimensions$ = function () {
617
+ return this.dimensions$;
618
+ };
619
+ /**
620
+ * @override
621
+ * @inheritDoc
622
+ */
623
+ AbstractCanvasObject.prototype.getMargin = function () {
624
+ return this.data.margin || {
625
+ top: 0,
626
+ right: 0,
627
+ bottom: 0,
628
+ left: 0
629
+ };
630
+ };
631
+ /**
632
+ * @override
633
+ * @inheritDoc
634
+ */
635
+ AbstractCanvasObject.prototype.setMargin = function (margin) {
636
+ if (!margin)
637
+ return;
638
+ var newMargin = tsOptchain.oc(this.data).margin({
639
+ top: 0,
640
+ right: 0,
641
+ bottom: 0,
642
+ left: 0
643
+ });
644
+ if (margin.top != undefined)
645
+ newMargin.top = margin.top || 0;
646
+ if (margin.right != undefined)
647
+ newMargin.right = margin.right || 0;
648
+ if (margin.bottom != undefined)
649
+ newMargin.bottom = margin.bottom || 0;
650
+ if (margin.left != undefined)
651
+ newMargin.left = margin.left || 0;
652
+ this.data.margin = newMargin;
653
+ this.canvas.notifyObjectListChanged();
654
+ };
655
+ /**
656
+ * @override
657
+ * @inheritDoc
658
+ */
659
+ AbstractCanvasObject.prototype.getColumnWidthPercent = function () {
660
+ return Math.max(Math.min(tsOptchain.oc(this.data).columnWidthPercent(100), 100), 0);
661
+ };
662
+ /**
663
+ * @override
664
+ * @inheritDoc
665
+ */
666
+ AbstractCanvasObject.prototype.setColumnWidthPercent = function (percent) {
667
+ this.data.columnWidthPercent = percent;
668
+ this.canvas.notifyObjectListChanged();
669
+ };
670
+ /**
671
+ * @override
672
+ * @inheritDoc
673
+ */
674
+ AbstractCanvasObject.prototype.bringForward = function () {
675
+ this.fabricObject.bringForward();
676
+ this.canvas.syncObjectList();
677
+ };
678
+ /**
679
+ * @override
680
+ * @inheritDoc
681
+ */
682
+ AbstractCanvasObject.prototype.bringToFront = function () {
683
+ this.fabricObject.bringToFront();
684
+ this.canvas.syncObjectList();
685
+ };
686
+ /**
687
+ * @override
688
+ * @inheritDoc
689
+ */
690
+ AbstractCanvasObject.prototype.sendBackward = function () {
691
+ this.fabricObject.sendBackwards();
692
+ this.canvas.syncObjectList();
693
+ };
694
+ /**
695
+ * @override
696
+ * @inheritDoc
697
+ */
698
+ AbstractCanvasObject.prototype.sendToBack = function () {
699
+ this.fabricObject.sendToBack();
700
+ this.canvas.syncObjectList();
701
+ };
702
+ /**
703
+ * @override
704
+ * @inheritDoc
705
+ */
706
+ AbstractCanvasObject.prototype.moveUp = function () {
707
+ this.sendBackward();
708
+ };
709
+ /**
710
+ * @override
711
+ * @inheritDoc
712
+ */
713
+ AbstractCanvasObject.prototype.moveDown = function () {
714
+ this.bringForward();
715
+ };
716
+ /**
717
+ * @override
718
+ * @inheritDoc
719
+ */
720
+ AbstractCanvasObject.prototype.delete = function () {
721
+ this.canvas.removeObject(this);
722
+ this.onDelete();
723
+ };
724
+ /**
725
+ * @override
726
+ * @inheritDoc
727
+ */
728
+ AbstractCanvasObject.prototype.persist = function () {
729
+ return _.clone(this.data);
730
+ };
731
+ /**
732
+ * @override
733
+ * @inheritDoc
734
+ */
735
+ AbstractCanvasObject.prototype.isPinToBottom = function () {
736
+ return this.data.pinToBottom;
737
+ };
738
+ /**
739
+ * @override
740
+ * @inheritDoc
741
+ */
742
+ AbstractCanvasObject.prototype.setPinToBottom = function (value) {
743
+ if (value === void 0) { value = true; }
744
+ this.updatePinToBottomDistance();
745
+ this.data.pinToBottom = value;
746
+ this.canvas.notifyObjectListChanged();
747
+ this.updateFabricObjectBehavior();
748
+ };
749
+ /**
750
+ * Get the underlying fabric object.
751
+ * (Internal use only, do not expose this outside the canvas module).
752
+ */
753
+ AbstractCanvasObject.prototype.getFabricObjectInternal = function () {
754
+ return this.fabricObject;
755
+ };
756
+ AbstractCanvasObject.prototype.setPosition = function (position) {
757
+ var scale = this.getDimensions().scale;
758
+ if (position.top != undefined)
759
+ this.fabricObject.set({ top: position.top });
760
+ if (position.left != undefined)
761
+ this.fabricObject.set({ left: position.left });
762
+ if (position.width != undefined)
763
+ this.fabricObject.set({ width: position.width / scale.x });
764
+ if (position.height != undefined)
765
+ this.fabricObject.set({ height: position.height / scale.y });
766
+ };
767
+ AbstractCanvasObject.prototype.setCoords = function () {
768
+ this.fabricObject.setCoords();
769
+ };
770
+ AbstractCanvasObject.prototype.updateDimensions = function () {
771
+ this.setCoords();
772
+ var dimensions = new FieldDimensions(this.fabricObject, this.canvas);
773
+ this.dimensions$.next(dimensions);
774
+ };
775
+ AbstractCanvasObject.prototype.getBoundingBox = function () {
776
+ var absolute = true;
777
+ var calculate = true;
778
+ return this.fabricObject.getBoundingRect(absolute, calculate);
779
+ };
780
+ AbstractCanvasObject.prototype.moveToPinnedPosition = function () {
781
+ if (this.data.pinToBottom) {
782
+ this.setPosition({
783
+ top: this.canvas.getDimensions().heightPx - this.pinToBottomDistance
784
+ });
785
+ }
786
+ };
787
+ AbstractCanvasObject.prototype.updatePinToBottomDistance = function () {
788
+ this.pinToBottomDistance = this.canvas.getDimensions().heightPx - this.getDimensions().top.px;
789
+ };
790
+ AbstractCanvasObject.prototype.redraw = function () {
791
+ this.canvas.redraw();
792
+ };
793
+ // noinspection JSUnusedLocalSymbols
794
+ AbstractCanvasObject.prototype.onProfileChange = function (profile) {
795
+ this.updateFabricObjectBehavior();
796
+ this.redraw();
797
+ };
798
+ // noinspection JSUnusedLocalSymbols
799
+ AbstractCanvasObject.prototype.onLayoutManagerChange = function (layoutManager) {
800
+ this.updateFabricObjectBehavior();
801
+ this.redraw();
802
+ };
803
+ // noinspection JSUnusedLocalSymbols
804
+ AbstractCanvasObject.prototype.onMove = function (event) {
805
+ if (!this.canvas.getProfile().allowObjectsToLeaveCanvas) {
806
+ this.preventLeavingCanvas();
807
+ }
808
+ this.updateDimensions();
809
+ };
810
+ AbstractCanvasObject.prototype.onRotate = function (event) {
811
+ if (event.e.shiftKey) {
812
+ this.fabricObject.set({ snapAngle: 0 });
813
+ }
814
+ else {
815
+ this.fabricObject.set({ snapAngle: ROTATION_SNAP_ANGLE });
816
+ }
817
+ this.updateDimensions();
818
+ };
819
+ // noinspection JSUnusedLocalSymbols
820
+ AbstractCanvasObject.prototype.onScale = function (event) {
821
+ this.updateDimensions();
822
+ };
823
+ AbstractCanvasObject.prototype.onDelete = function () {
824
+ //no-op
825
+ };
826
+ AbstractCanvasObject.prototype.preventLeavingCanvas = function () {
827
+ var overflow = this.getCanvasOverflow();
828
+ if (overflow.hasOverflow) {
829
+ var adjustX = overflow.left || -overflow.right;
830
+ var adjustY = overflow.top || -overflow.bottom;
831
+ this.fabricObject.set({
832
+ top: this.fabricObject.top + adjustY,
833
+ left: this.fabricObject.left + adjustX
834
+ });
835
+ this.redraw();
836
+ }
837
+ };
838
+ AbstractCanvasObject.prototype.getCanvasOverflow = function () {
839
+ var bound = this.getBoundingBox();
840
+ var canvasDimensions = this.canvas.getDimensions();
841
+ var top = Math.max(0, 0 - bound.top);
842
+ var right = Math.max(0, bound.left + bound.width - canvasDimensions.widthPx);
843
+ var bottom = Math.max(0, bound.top + bound.height - canvasDimensions.heightPx);
844
+ var left = Math.max(0, 0 - bound.left);
845
+ return {
846
+ top: top,
847
+ right: right,
848
+ bottom: bottom,
849
+ left: left,
850
+ hasOverflow: top > 0 || right > 0 || bottom > 0 || left > 0
851
+ };
852
+ };
853
+ AbstractCanvasObject.prototype.centerHorizontallyInternal = function () {
854
+ var canvasWidth = this.canvas.getDimensions().widthPx;
855
+ var objectWidth = this.getDimensions().width.px;
856
+ this.fabricObject.set({ left: ((canvasWidth - objectWidth) / 2) - 0.5 });
857
+ this.updateDimensions();
858
+ this.redraw();
859
+ };
860
+ AbstractCanvasObject.prototype.centerVerticallyInternal = function () {
861
+ var canvasHeight = this.canvas.getDimensions().heightPx;
862
+ var objectHeight = this.getDimensions().height.px;
863
+ this.fabricObject.set({ top: ((canvasHeight - objectHeight) / 2) - 0.5 });
864
+ this.redraw();
865
+ this.updateDimensions();
866
+ };
867
+ AbstractCanvasObject.prototype.nudgeInternal = function (x, y) {
868
+ if (x === void 0) { x = 0; }
869
+ if (y === void 0) { y = 0; }
870
+ this.fabricObject.set({
871
+ top: this.getDimensions().top.px + y,
872
+ left: this.getDimensions().left.px + x
873
+ });
874
+ this.redraw();
875
+ this.updateDimensions();
876
+ };
877
+ AbstractCanvasObject.prototype.updateFabricObjectBehavior = function () {
878
+ var allowManualPositioning = !this.canvas.getLayoutManager().isEnforcedPositioning;
879
+ var profile = this.canvas.getProfile();
880
+ var options = {
881
+ allowMove: allowManualPositioning && profile.allowMove && !this.isPinToBottom(),
882
+ allowRotate: allowManualPositioning && profile.allowRotate && !this.isPinToBottom(),
883
+ allowScale: allowManualPositioning && profile.allowScale && !this.isPinToBottom()
884
+ };
885
+ this.fabricObject.set({
886
+ lockMovementX: !options.allowMove,
887
+ lockMovementY: !options.allowMove,
888
+ lockScalingX: !options.allowScale,
889
+ lockScalingY: !options.allowScale,
890
+ lockScalingFlip: true,
891
+ lockRotation: !options.allowRotate
892
+ });
893
+ this.fabricObject.setControlsVisibility({
894
+ tl: options.allowScale,
895
+ tr: options.allowScale,
896
+ br: options.allowScale,
897
+ bl: options.allowScale,
898
+ ml: options.allowScale,
899
+ mt: options.allowScale,
900
+ mr: options.allowScale,
901
+ mb: options.allowScale,
902
+ mtr: options.allowRotate
903
+ });
904
+ };
905
+ return AbstractCanvasObject;
906
+ }());
907
+ var FieldDimensions = /** @class */ (function () {
908
+ function FieldDimensions(fabricObject, canvas) {
909
+ var widthPxToMmFunction = canvas.getDimensions().widthPxToMm;
910
+ var heightPxToMmFunction = canvas.getDimensions().heightPxToMm;
911
+ this.top = { px: fabricObject.top, mm: heightPxToMmFunction(fabricObject.top) };
912
+ this.left = { px: fabricObject.left, mm: widthPxToMmFunction(fabricObject.left) };
913
+ var widthPx = fabricObject.width * fabricObject.scaleX;
914
+ var heightPx = fabricObject.height * fabricObject.scaleY;
915
+ this.width = { px: widthPx, mm: widthPxToMmFunction(widthPx) };
916
+ this.height = { px: heightPx, mm: heightPxToMmFunction(heightPx) };
917
+ this.scale = { x: fabricObject.scaleX, y: fabricObject.scaleY };
918
+ this.angle = fabricObject.angle;
919
+ }
920
+ return FieldDimensions;
921
+ }());
922
+
923
+ var Border = /** @class */ (function (_super) {
924
+ __extends(Border, _super);
925
+ function Border(canvas, fabricObject, persistedField) {
926
+ var _this = _super.call(this, exports.ObjectType.BORDER, canvas, fabricObject, persistedField) || this;
927
+ _this.strokeWidth$ = new rxjs.BehaviorSubject(0);
928
+ _this.canvas = canvas;
929
+ _this.strokeWidth$.next(_this.fabricObject.strokeWidth);
930
+ _this.updatePosition();
931
+ return _this;
932
+ }
933
+ /**
934
+ * @override
935
+ * @inheritDoc
936
+ */
937
+ Border.prototype.getColor = function () {
938
+ return this.fabricObject.stroke;
939
+ };
940
+ /**
941
+ * @override
942
+ * @inheritDoc
943
+ */
944
+ Border.prototype.setColor = function (color) {
945
+ this.fabricObject.set({ stroke: color });
946
+ this.data.color = color;
947
+ this.redraw();
948
+ };
949
+ /**
950
+ * @override
951
+ * @inheritDoc
952
+ */
953
+ Border.prototype.getStrokeWidth = function () {
954
+ return this.fabricObject.strokeWidth;
955
+ };
956
+ /**
957
+ * @override
958
+ * @inheritDoc
959
+ */
960
+ Border.prototype.getStrokeWidth$ = function () {
961
+ return this.strokeWidth$;
962
+ };
963
+ /**
964
+ * @override
965
+ * @inheritDoc
966
+ */
967
+ Border.prototype.setStrokeWidth = function (width) {
968
+ this.strokeWidth$.next(width);
969
+ this.data.strokeWidth = width;
970
+ this.updatePosition();
971
+ this.canvas.notifyMarginChanged();
972
+ this.redraw();
973
+ };
974
+ Border.prototype.getPadding = function () {
975
+ return this.data.padding || {
976
+ top: 2,
977
+ right: 2,
978
+ bottom: 2,
979
+ left: 2
980
+ };
981
+ };
982
+ Border.prototype.setPadding = function (padding) {
983
+ if (!padding)
984
+ return;
985
+ this.data.padding = {
986
+ top: padding.top || 2,
987
+ right: padding.right || 2,
988
+ bottom: padding.bottom || 2,
989
+ left: padding.left || 2
990
+ };
991
+ this.canvas.notifyMarginChanged();
992
+ this.redraw();
993
+ };
994
+ Border.prototype.updatePosition = function () {
995
+ this.fabricObject.set({
996
+ strokeWidth: this.strokeWidth$.value,
997
+ top: 0,
998
+ left: 0,
999
+ width: this.canvas.getDimensions().widthPx - this.strokeWidth$.value,
1000
+ height: this.canvas.getDimensions().heightPx - this.strokeWidth$.value
1001
+ });
1002
+ };
1003
+ /**
1004
+ * @override
1005
+ * @inheritDoc
1006
+ */
1007
+ Border.prototype.updateFabricObjectBehavior = function () {
1008
+ _super.prototype.updateFabricObjectBehavior.call(this);
1009
+ this.fabricObject.set({
1010
+ lockMovementX: true,
1011
+ lockMovementY: true,
1012
+ lockScalingX: true,
1013
+ lockScalingY: true,
1014
+ lockScalingFlip: true
1015
+ });
1016
+ this.fabricObject.setControlsVisibility({
1017
+ tl: false,
1018
+ tr: false,
1019
+ br: false,
1020
+ bl: false,
1021
+ ml: false,
1022
+ mt: false,
1023
+ mr: false,
1024
+ mb: false,
1025
+ mtr: false
1026
+ });
1027
+ this.redraw();
1028
+ };
1029
+ return Border;
1030
+ }(AbstractCanvasObject));
1031
+
1032
+ var Cutoff = /** @class */ (function (_super) {
1033
+ __extends(Cutoff, _super);
1034
+ function Cutoff(canvas, fabricObject, persistedField) {
1035
+ var _this = _super.call(this, exports.ObjectType.CUTOFF, canvas, fabricObject, persistedField) || this;
1036
+ _this.strokeWidth$ = new rxjs.BehaviorSubject(0);
1037
+ _this.canvas = canvas;
1038
+ _this.strokeWidth$.next(_this.fabricObject.height);
1039
+ _this.updatePosition();
1040
+ _this.fabricObject.set({ strokeWidth: 0 });
1041
+ return _this;
1042
+ }
1043
+ /**
1044
+ * @override
1045
+ * @inheritDoc
1046
+ */
1047
+ Cutoff.prototype.getColor = function () {
1048
+ return this.fabricObject.fill;
1049
+ };
1050
+ /**
1051
+ * @override
1052
+ * @inheritDoc
1053
+ */
1054
+ Cutoff.prototype.setColor = function (color) {
1055
+ this.fabricObject.set({ fill: color, stroke: color });
1056
+ this.data.color = color;
1057
+ this.redraw();
1058
+ };
1059
+ /**
1060
+ * @override
1061
+ * @inheritDoc
1062
+ */
1063
+ Cutoff.prototype.getStrokeWidth = function () {
1064
+ // noinspection JSSuspiciousNameCombination
1065
+ return this.fabricObject.height;
1066
+ };
1067
+ /**
1068
+ * @override
1069
+ * @inheritDoc
1070
+ */
1071
+ Cutoff.prototype.getStrokeWidth$ = function () {
1072
+ return this.strokeWidth$;
1073
+ };
1074
+ /**
1075
+ * @override
1076
+ * @inheritDoc
1077
+ */
1078
+ Cutoff.prototype.setStrokeWidth = function (width) {
1079
+ this.strokeWidth$.next(width);
1080
+ this.data.strokeWidth = width;
1081
+ this.updatePosition();
1082
+ this.canvas.notifyMarginChanged();
1083
+ this.redraw();
1084
+ };
1085
+ Cutoff.prototype.updatePosition = function () {
1086
+ this.fabricObject.set({
1087
+ top: this.canvas.getDimensions().heightPx - this.strokeWidth$.value,
1088
+ left: 0,
1089
+ width: this.canvas.getDimensions().widthPx,
1090
+ height: this.strokeWidth$.value
1091
+ });
1092
+ };
1093
+ /**
1094
+ * @override
1095
+ * @inheritDoc
1096
+ */
1097
+ Cutoff.prototype.updateFabricObjectBehavior = function () {
1098
+ this.fabricObject.set({
1099
+ lockMovementX: true,
1100
+ lockMovementY: true,
1101
+ lockScalingX: true,
1102
+ lockScalingY: true,
1103
+ lockScalingFlip: true
1104
+ });
1105
+ this.fabricObject.setControlsVisibility({
1106
+ tl: false,
1107
+ tr: false,
1108
+ br: false,
1109
+ bl: false,
1110
+ ml: false,
1111
+ mt: false,
1112
+ mr: false,
1113
+ mb: false,
1114
+ mtr: false
1115
+ });
1116
+ this.redraw();
1117
+ };
1118
+ return Cutoff;
1119
+ }(AbstractCanvasObject));
1120
+
1121
+ var HorizontalRule = /** @class */ (function (_super) {
1122
+ __extends(HorizontalRule, _super);
1123
+ function HorizontalRule(canvas, fabricObject, persistedField) {
1124
+ var _this = _super.call(this, exports.ObjectType.HORIZONTAL_RULE, canvas, fabricObject, persistedField) || this;
1125
+ _this.strokeWidth$ = new rxjs.BehaviorSubject(2);
1126
+ _this.canvas = canvas;
1127
+ _this.strokeWidth$.next(_this.getDimensions().height.px);
1128
+ _this.fabricObject.set({ strokeWidth: 0 });
1129
+ return _this;
1130
+ }
1131
+ /**
1132
+ * @override
1133
+ * @inheritDoc
1134
+ */
1135
+ HorizontalRule.prototype.getStrokeWidth = function () {
1136
+ // noinspection JSSuspiciousNameCombination
1137
+ return this.fabricObject.height;
1138
+ };
1139
+ /**
1140
+ * @override
1141
+ * @inheritDoc
1142
+ */
1143
+ HorizontalRule.prototype.getStrokeWidth$ = function () {
1144
+ return this.strokeWidth$;
1145
+ };
1146
+ /**
1147
+ * @override
1148
+ * @inheritDoc
1149
+ */
1150
+ HorizontalRule.prototype.setStrokeWidth = function (value) {
1151
+ this.strokeWidth$.next(value);
1152
+ this.data.strokeWidth = value;
1153
+ this.setPosition({ height: value });
1154
+ this.canvas.notifyObjectListChanged();
1155
+ this.redraw();
1156
+ };
1157
+ /**
1158
+ * @override
1159
+ * @inheritDoc
1160
+ */
1161
+ HorizontalRule.prototype.getColor = function () {
1162
+ return this.fabricObject.fill;
1163
+ };
1164
+ /**
1165
+ * @override
1166
+ * @inheritDoc
1167
+ */
1168
+ HorizontalRule.prototype.setColor = function (color) {
1169
+ this.fabricObject.set({ fill: color, stroke: color });
1170
+ this.data.color = color;
1171
+ this.redraw();
1172
+ };
1173
+ /**
1174
+ * @override
1175
+ * @inheritDoc
1176
+ */
1177
+ HorizontalRule.prototype.centerHorizontally = function () {
1178
+ _super.prototype.centerHorizontallyInternal.call(this);
1179
+ };
1180
+ /**
1181
+ * @override
1182
+ * @inheritDoc
1183
+ */
1184
+ HorizontalRule.prototype.centerVertically = function () {
1185
+ _super.prototype.centerVerticallyInternal.call(this);
1186
+ };
1187
+ /**
1188
+ * @override
1189
+ * @inheritDoc
1190
+ */
1191
+ HorizontalRule.prototype.setHeight = function (height) {
1192
+ this.setPosition({ height: height });
1193
+ this.canvas.notifyObjectListChanged();
1194
+ };
1195
+ /**
1196
+ * @override
1197
+ * @inheritDoc
1198
+ */
1199
+ HorizontalRule.prototype.nudgeUp = function () {
1200
+ _super.prototype.nudgeInternal.call(this, 0, -1);
1201
+ };
1202
+ /**
1203
+ * @override
1204
+ * @inheritDoc
1205
+ */
1206
+ HorizontalRule.prototype.nudgeDown = function () {
1207
+ _super.prototype.nudgeInternal.call(this, 0, +1);
1208
+ };
1209
+ /**
1210
+ * @override
1211
+ * @inheritDoc
1212
+ */
1213
+ HorizontalRule.prototype.nudgeLeft = function () {
1214
+ _super.prototype.nudgeInternal.call(this, -1, 0);
1215
+ };
1216
+ /**
1217
+ * @override
1218
+ * @inheritDoc
1219
+ */
1220
+ HorizontalRule.prototype.nudgeRight = function () {
1221
+ _super.prototype.nudgeInternal.call(this, +1, 0);
1222
+ };
1223
+ return HorizontalRule;
1224
+ }(AbstractCanvasObject));
1225
+
1226
+ var Image = /** @class */ (function (_super) {
1227
+ __extends(Image, _super);
1228
+ function Image(canvas, fabricObject, persistedField) {
1229
+ var _this = _super.call(this, exports.ObjectType.IMAGE, canvas, fabricObject, persistedField) || this;
1230
+ _this.DEFAULT_RESTRICTIONS = {
1231
+ minWidth: 1,
1232
+ minHeight: 1
1233
+ };
1234
+ _this.canvas = canvas;
1235
+ _this.imageUrl$ = new rxjs.BehaviorSubject(_this.fabricObject.getSrc());
1236
+ _this.data.imagePath = _this.imageUrl$.value;
1237
+ _this.layoutWidth = _this.getBoundingBox().width;
1238
+ return _this;
1239
+ }
1240
+ /**
1241
+ * @override
1242
+ * @inheritDoc
1243
+ */
1244
+ Image.prototype.getImageUrl = function () {
1245
+ return this.imageUrl$.value;
1246
+ };
1247
+ /**
1248
+ * @override
1249
+ * @inheritDoc
1250
+ */
1251
+ Image.prototype.getImageUrl$ = function () {
1252
+ return this.imageUrl$;
1253
+ };
1254
+ /**
1255
+ * @override
1256
+ * @inheritDoc
1257
+ */
1258
+ Image.prototype.setImageUrl = function (url) {
1259
+ this.setCanvasImage(url);
1260
+ this.data.imagePath = url;
1261
+ this.imageUrl$.next(url);
1262
+ this.canvas.notifyObjectListChanged();
1263
+ };
1264
+ /**
1265
+ * @override
1266
+ * @inheritDoc
1267
+ */
1268
+ Image.prototype.setImageWidth = function (px, sizeRestrictions) {
1269
+ if (sizeRestrictions === void 0) { sizeRestrictions = this.DEFAULT_RESTRICTIONS; }
1270
+ var minWidth = Math.max(sizeRestrictions.minWidth, this.getMinWidthBasedOnMinHeight(sizeRestrictions.minHeight));
1271
+ var maxWidth = Math.min(sizeRestrictions.maxWidth, this.getMaxWidthBasedOnMaxHeight(sizeRestrictions.maxHeight));
1272
+ if (px < minWidth) {
1273
+ px = minWidth;
1274
+ }
1275
+ else if (px > maxWidth) {
1276
+ px = maxWidth;
1277
+ }
1278
+ var premultipliedWidth = this.fabricObject.width;
1279
+ var newScale = px / premultipliedWidth;
1280
+ this.fabricObject.set({ scaleX: newScale, scaleY: newScale });
1281
+ this.setCoords();
1282
+ this.canvas.notifyObjectListChanged();
1283
+ return px;
1284
+ };
1285
+ /**
1286
+ * @override
1287
+ * @inheritDoc
1288
+ */
1289
+ Image.prototype.centerHorizontally = function () {
1290
+ _super.prototype.centerHorizontallyInternal.call(this);
1291
+ };
1292
+ /**
1293
+ * @override
1294
+ * @inheritDoc
1295
+ */
1296
+ Image.prototype.centerVertically = function () {
1297
+ _super.prototype.centerVerticallyInternal.call(this);
1298
+ };
1299
+ /**
1300
+ * @override
1301
+ * @inheritDoc
1302
+ */
1303
+ Image.prototype.setHeight = function (height) {
1304
+ this.setPosition({ height: height });
1305
+ this.canvas.notifyObjectListChanged();
1306
+ };
1307
+ /**
1308
+ * @override
1309
+ * @inheritDoc
1310
+ */
1311
+ Image.prototype.nudgeUp = function () {
1312
+ _super.prototype.nudgeInternal.call(this, 0, -1);
1313
+ };
1314
+ /**
1315
+ * @override
1316
+ * @inheritDoc
1317
+ */
1318
+ Image.prototype.nudgeDown = function () {
1319
+ _super.prototype.nudgeInternal.call(this, 0, +1);
1320
+ };
1321
+ /**
1322
+ * @override
1323
+ * @inheritDoc
1324
+ */
1325
+ Image.prototype.nudgeLeft = function () {
1326
+ _super.prototype.nudgeInternal.call(this, -1, 0);
1327
+ };
1328
+ /**
1329
+ * @override
1330
+ * @inheritDoc
1331
+ */
1332
+ Image.prototype.nudgeRight = function () {
1333
+ _super.prototype.nudgeInternal.call(this, +1, 0);
1334
+ };
1335
+ /**
1336
+ * @override
1337
+ * @inheritDoc
1338
+ */
1339
+ Image.prototype.setPosition = function (position) {
1340
+ if (this.canvas.getLayoutManager().isEnforcedPositioning) {
1341
+ if (position.top != undefined)
1342
+ this.fabricObject.set({ top: position.top });
1343
+ if (position.width != undefined) {
1344
+ this.layoutWidth = position.width;
1345
+ }
1346
+ if (position.height != undefined) {
1347
+ var premultipliedHeight = this.fabricObject.height;
1348
+ var newScale = position.height / premultipliedHeight;
1349
+ this.fabricObject.set({ scaleX: newScale, scaleY: newScale });
1350
+ this.setCoords();
1351
+ }
1352
+ if (position.left != undefined || position.width != undefined || position.height != undefined) {
1353
+ this.centerHorizontallyInLayoutWidth(position.left || 0);
1354
+ }
1355
+ }
1356
+ else {
1357
+ _super.prototype.setPosition.call(this, position);
1358
+ }
1359
+ };
1360
+ Image.prototype.centerHorizontallyInLayoutWidth = function (left) {
1361
+ var imageWidth = this.getBoundingBox().width;
1362
+ this.fabricObject.set({ left: left + (this.layoutWidth / 2) - (imageWidth / 2) });
1363
+ };
1364
+ Image.prototype.setCanvasImage = function (url) {
1365
+ var _this = this;
1366
+ var imageOptions = { crossOrigin: "anonymous" };
1367
+ //Don't let the image be more than half the canvas in size.
1368
+ var maxSizeScale = 2;
1369
+ var maxWidth = this.canvas.getDimensions().widthPx / maxSizeScale;
1370
+ this.fabricObject.setSrc(url, function (img) {
1371
+ var newScale = 1;
1372
+ var curWidth = newScale * img.width;
1373
+ if (curWidth > maxWidth) {
1374
+ newScale = maxWidth / curWidth;
1375
+ }
1376
+ img.set({
1377
+ scaleX: newScale,
1378
+ scaleY: newScale
1379
+ });
1380
+ img.setCoords();
1381
+ _this.redraw();
1382
+ }, imageOptions);
1383
+ };
1384
+ Image.prototype.getMinWidthBasedOnMinHeight = function (minHeight) {
1385
+ if (!minHeight) {
1386
+ return 0;
1387
+ }
1388
+ var premultipliedHeight = this.fabricObject.height;
1389
+ var newScale = minHeight / premultipliedHeight;
1390
+ return this.getBoundingBox().width * newScale;
1391
+ };
1392
+ Image.prototype.getMaxWidthBasedOnMaxHeight = function (maxHeight) {
1393
+ if (!maxHeight) {
1394
+ return Infinity;
1395
+ }
1396
+ var premultipliedHeight = this.fabricObject.height;
1397
+ var newScale = maxHeight / premultipliedHeight;
1398
+ return this.getBoundingBox().width * newScale;
1399
+ };
1400
+ return Image;
1401
+ }(AbstractCanvasObject));
1402
+
1403
+ var Rectangle = /** @class */ (function (_super) {
1404
+ __extends(Rectangle, _super);
1405
+ function Rectangle(canvas, fabricObject, persistedField) {
1406
+ var _this = _super.call(this, exports.ObjectType.RECTANGLE, canvas, fabricObject, persistedField) || this;
1407
+ _this.canvas = canvas;
1408
+ _this.fabricObject.set({ strokeWidth: 0 });
1409
+ return _this;
1410
+ }
1411
+ /**
1412
+ * @override
1413
+ * @inheritDoc
1414
+ */
1415
+ Rectangle.prototype.getColor = function () {
1416
+ return this.fabricObject.fill;
1417
+ };
1418
+ /**
1419
+ * @override
1420
+ * @inheritDoc
1421
+ */
1422
+ Rectangle.prototype.setColor = function (color) {
1423
+ this.fabricObject.set({ fill: color, stroke: color });
1424
+ this.data.color = color;
1425
+ this.redraw();
1426
+ };
1427
+ /**
1428
+ * @override
1429
+ * @inheritDoc
1430
+ */
1431
+ Rectangle.prototype.centerHorizontally = function () {
1432
+ _super.prototype.centerHorizontallyInternal.call(this);
1433
+ };
1434
+ /**
1435
+ * @override
1436
+ * @inheritDoc
1437
+ */
1438
+ Rectangle.prototype.centerVertically = function () {
1439
+ _super.prototype.centerVerticallyInternal.call(this);
1440
+ };
1441
+ /**
1442
+ * @override
1443
+ * @inheritDoc
1444
+ */
1445
+ Rectangle.prototype.setHeight = function (height) {
1446
+ this.setPosition({ height: height });
1447
+ this.canvas.notifyObjectListChanged();
1448
+ };
1449
+ /**
1450
+ * @override
1451
+ * @inheritDoc
1452
+ */
1453
+ Rectangle.prototype.nudgeUp = function () {
1454
+ _super.prototype.nudgeInternal.call(this, 0, -1);
1455
+ };
1456
+ /**
1457
+ * @override
1458
+ * @inheritDoc
1459
+ */
1460
+ Rectangle.prototype.nudgeDown = function () {
1461
+ _super.prototype.nudgeInternal.call(this, 0, +1);
1462
+ };
1463
+ /**
1464
+ * @override
1465
+ * @inheritDoc
1466
+ */
1467
+ Rectangle.prototype.nudgeLeft = function () {
1468
+ _super.prototype.nudgeInternal.call(this, -1, 0);
1469
+ };
1470
+ /**
1471
+ * @override
1472
+ * @inheritDoc
1473
+ */
1474
+ Rectangle.prototype.nudgeRight = function () {
1475
+ _super.prototype.nudgeInternal.call(this, +1, 0);
1476
+ };
1477
+ return Rectangle;
1478
+ }(AbstractCanvasObject));
1479
+
1480
+ var Text = /** @class */ (function (_super) {
1481
+ __extends(Text, _super);
1482
+ function Text(canvas, fabricObject, persistedField) {
1483
+ var _this = _super.call(this, exports.ObjectType.TEXT, canvas, fabricObject, persistedField) || this;
1484
+ _this.canvas = canvas;
1485
+ _this.fontLibrary = canvas.getFontLibrary();
1486
+ _this.baseComponentHeight = _this.fabricObject.height;
1487
+ _this.text$ = new rxjs.BehaviorSubject(_this.fabricObject.text);
1488
+ _this.data.userText = _this.fabricObject.text;
1489
+ _this.textSelection$ = new rxjs.BehaviorSubject(new TextSelection(_this.fabricObject, _this.fontLibrary));
1490
+ _this.fabricObject.set({ strokeWidth: 0 });
1491
+ _this.fabricObject.on("changed", function () { return _this.onTextChange(); });
1492
+ _this.fabricObject.on("selection:changed", function () { return _this.onSelectionChange(); });
1493
+ _this.applyBulletListMonkeyPatches();
1494
+ return _this;
1495
+ }
1496
+ /**
1497
+ * @override
1498
+ * @inheritDoc
1499
+ */
1500
+ Text.prototype.getText = function () {
1501
+ return this.text$.value;
1502
+ };
1503
+ /**
1504
+ * @override
1505
+ * @inheritDoc
1506
+ */
1507
+ Text.prototype.getText$ = function () {
1508
+ return this.text$;
1509
+ };
1510
+ /**
1511
+ * @override
1512
+ * @inheritDoc
1513
+ */
1514
+ Text.prototype.setText = function (text) {
1515
+ var style = this.fabricObject.getStyleAtPosition(0, true);
1516
+ this.fabricObject.set({ text: text });
1517
+ this.data.userText = this.fabricObject.text;
1518
+ this.text$.next(text);
1519
+ this.fabricObject.setSelectionStyles(style, 0, text.length);
1520
+ this.redraw();
1521
+ };
1522
+ /**
1523
+ * @override
1524
+ * @inheritDoc
1525
+ */
1526
+ Text.prototype.replaceText = function (target, replacement) {
1527
+ var start = this.getText().indexOf(target);
1528
+ this.fabricObject.insertChars(replacement, null, start, start + target.length);
1529
+ this.redraw();
1530
+ this.data.userText = this.fabricObject.text;
1531
+ this.text$.next(this.fabricObject.text);
1532
+ };
1533
+ /**
1534
+ * @override
1535
+ * @inheritDoc
1536
+ */
1537
+ Text.prototype.getColor = function () {
1538
+ return this.textSelection$.value.fill;
1539
+ };
1540
+ /**
1541
+ * @override
1542
+ * @inheritDoc
1543
+ */
1544
+ Text.prototype.setColor = function (color) {
1545
+ if (this.hasTextSelection()) {
1546
+ this.fabricObject.setSelectionStyles({ fill: color });
1547
+ }
1548
+ else {
1549
+ this.fabricObject.set({ fill: color });
1550
+ }
1551
+ this.data.color = color;
1552
+ this.updateTextSelection();
1553
+ this.redraw();
1554
+ };
1555
+ /**
1556
+ * @override
1557
+ * @inheritDoc
1558
+ */
1559
+ Text.prototype.getTextSelection = function () {
1560
+ return this.textSelection$.value;
1561
+ };
1562
+ /**
1563
+ * @override
1564
+ * @inheritDoc
1565
+ */
1566
+ Text.prototype.getTextSelection$ = function () {
1567
+ return this.textSelection$;
1568
+ };
1569
+ /**
1570
+ * @override
1571
+ * @inheritDoc
1572
+ */
1573
+ Text.prototype.setTextAlign = function (alignment) {
1574
+ this.fabricObject.set({ textAlign: alignment });
1575
+ this.redraw();
1576
+ this.updateTextSelection();
1577
+ };
1578
+ /**
1579
+ * @override
1580
+ * @inheritDoc
1581
+ */
1582
+ Text.prototype.setCharSpacing = function (spacing) {
1583
+ this.fabricObject.set({ charSpacing: spacing });
1584
+ this.redraw();
1585
+ this.updateTextSelection();
1586
+ };
1587
+ /**
1588
+ * @override
1589
+ * @inheritDoc
1590
+ */
1591
+ Text.prototype.setLineHeight = function (height) {
1592
+ this.fabricObject.set({ lineHeight: height });
1593
+ this.redraw();
1594
+ this.updateTextSelection();
1595
+ };
1596
+ /**
1597
+ * @override
1598
+ * @inheritDoc
1599
+ */
1600
+ Text.prototype.setBold = function (bold) {
1601
+ if (bold === void 0) { bold = true; }
1602
+ if (this.hasTextSelection()) {
1603
+ var start = this.textSelection$.value.start;
1604
+ for (var i = 0; i < this.textSelection$.value.length; i++) {
1605
+ var style = this.fabricObject.getStyleAtPosition(start + i, true);
1606
+ var font = this.fontLibrary.getFont(style.fontFamily);
1607
+ var fontWeight = bold ? font.boldWeight : font.normalWeight;
1608
+ this.fabricObject.setSelectionStyles({ fontWeight: fontWeight }, start + i, start + i + 1);
1609
+ }
1610
+ }
1611
+ else {
1612
+ var font = this.fontLibrary.getFont(this.fabricObject.fontFamily);
1613
+ var fontWeight = bold ? font.boldWeight : font.normalWeight;
1614
+ this.fabricObject.set({ fontWeight: fontWeight });
1615
+ }
1616
+ this.redraw();
1617
+ this.updateTextSelection();
1618
+ };
1619
+ /**
1620
+ * @override
1621
+ * @inheritDoc
1622
+ */
1623
+ Text.prototype.setBoldRegion = function (start, end, bold) {
1624
+ if (bold === void 0) { bold = true; }
1625
+ if (start > end)
1626
+ throw Error("Bad region specified: [" + start + " - " + end + "]");
1627
+ for (var i = start; i < end; i++) {
1628
+ var style = this.fabricObject.getStyleAtPosition(i, true);
1629
+ var font = this.fontLibrary.getFont(style.fontFamily);
1630
+ style.fontWeight = bold ? font.boldWeight : font.normalWeight;
1631
+ this.fabricObject.setSelectionStyles(style, i, i + 1);
1632
+ }
1633
+ this.redraw();
1634
+ };
1635
+ /**
1636
+ * @override
1637
+ * @inheritDoc
1638
+ */
1639
+ Text.prototype.setItalic = function (italic) {
1640
+ if (italic === void 0) { italic = true; }
1641
+ if (this.hasTextSelection()) {
1642
+ this.fabricObject.setSelectionStyles({ fontStyle: italic ? "italic" : "" });
1643
+ }
1644
+ else {
1645
+ this.fabricObject.set({ fontStyle: italic ? "italic" : "" });
1646
+ }
1647
+ this.redraw();
1648
+ this.updateTextSelection();
1649
+ };
1650
+ /**
1651
+ * @override
1652
+ * @inheritDoc
1653
+ */
1654
+ Text.prototype.setItalicRegion = function (start, end, italic) {
1655
+ if (italic === void 0) { italic = true; }
1656
+ if (start > end)
1657
+ throw Error("Bad region specified: [" + start + " - " + end + "]");
1658
+ this.fabricObject.setSelectionStyles({ fontStyle: italic ? "italic" : "" }, start, end);
1659
+ this.redraw();
1660
+ };
1661
+ /**
1662
+ * @override
1663
+ * @inheritDoc
1664
+ */
1665
+ Text.prototype.setStrikethrough = function (strikethrough) {
1666
+ if (strikethrough === void 0) { strikethrough = true; }
1667
+ if (this.hasTextSelection()) {
1668
+ this.fabricObject.setSelectionStyles({ linethrough: strikethrough });
1669
+ }
1670
+ else {
1671
+ this.fabricObject.set({ linethrough: strikethrough });
1672
+ }
1673
+ this.redraw();
1674
+ this.updateTextSelection();
1675
+ };
1676
+ /**
1677
+ * @override
1678
+ * @inheritDoc
1679
+ */
1680
+ Text.prototype.setUnderline = function (underline) {
1681
+ if (underline === void 0) { underline = true; }
1682
+ this.fabricObject.setSelectionStyles({ underline: underline });
1683
+ this.redraw();
1684
+ this.updateTextSelection();
1685
+ };
1686
+ /**
1687
+ * @override
1688
+ * @inheritDoc
1689
+ */
1690
+ Text.prototype.setFont = function (font) {
1691
+ if (this.hasTextSelection()) {
1692
+ this.fabricObject.setSelectionStyles({ fontFamily: font.family, fontWeight: font.normalWeight });
1693
+ }
1694
+ else {
1695
+ this.fabricObject.set({ fontFamily: font.family, fontWeight: font.normalWeight });
1696
+ }
1697
+ this.redraw();
1698
+ this.updateTextSelection();
1699
+ };
1700
+ /**
1701
+ * @override
1702
+ * @inheritDoc
1703
+ */
1704
+ Text.prototype.setFontSize = function (fontSize) {
1705
+ if (this.hasTextSelection()) {
1706
+ this.fabricObject.setSelectionStyles({ fontSize: fontSize });
1707
+ }
1708
+ else {
1709
+ this.fabricObject.set({ fontSize: fontSize });
1710
+ }
1711
+ this.redraw();
1712
+ this.updateTextSelection();
1713
+ };
1714
+ /**
1715
+ * @override
1716
+ * @inheritDoc
1717
+ */
1718
+ Text.prototype.setTextBackgroundColor = function (color) {
1719
+ if (this.hasTextSelection()) {
1720
+ this.fabricObject.setSelectionStyles({ textBackgroundColor: color });
1721
+ }
1722
+ else {
1723
+ this.fabricObject.set({ textBackgroundColor: color });
1724
+ }
1725
+ this.redraw();
1726
+ this.updateTextSelection();
1727
+ };
1728
+ /**
1729
+ * @override
1730
+ * @inheritDoc
1731
+ */
1732
+ Text.prototype.centerHorizontally = function () {
1733
+ _super.prototype.centerHorizontallyInternal.call(this);
1734
+ };
1735
+ /**
1736
+ * @override
1737
+ * @inheritDoc
1738
+ */
1739
+ Text.prototype.centerVertically = function () {
1740
+ _super.prototype.centerVerticallyInternal.call(this);
1741
+ };
1742
+ /**
1743
+ * @override
1744
+ * @inheritDoc
1745
+ */
1746
+ Text.prototype.setHeight = function (height) {
1747
+ //no-op
1748
+ };
1749
+ /**
1750
+ * @override
1751
+ * @inheritDoc
1752
+ */
1753
+ Text.prototype.nudgeUp = function () {
1754
+ _super.prototype.nudgeInternal.call(this, 0, -1);
1755
+ };
1756
+ /**
1757
+ * @override
1758
+ * @inheritDoc
1759
+ */
1760
+ Text.prototype.nudgeDown = function () {
1761
+ _super.prototype.nudgeInternal.call(this, 0, +1);
1762
+ };
1763
+ /**
1764
+ * @override
1765
+ * @inheritDoc
1766
+ */
1767
+ Text.prototype.nudgeLeft = function () {
1768
+ _super.prototype.nudgeInternal.call(this, -1, 0);
1769
+ };
1770
+ /**
1771
+ * @override
1772
+ * @inheritDoc
1773
+ */
1774
+ Text.prototype.nudgeRight = function () {
1775
+ _super.prototype.nudgeInternal.call(this, +1, 0);
1776
+ };
1777
+ /**
1778
+ * @override
1779
+ * @inheritDoc
1780
+ */
1781
+ Text.prototype.getBackgroundColor = function () {
1782
+ return this.fabricObject.backgroundColor;
1783
+ };
1784
+ /**
1785
+ * @override
1786
+ * @inheritDoc
1787
+ */
1788
+ Text.prototype.setBackgroundColor = function (color) {
1789
+ this.fabricObject.set({ backgroundColor: color });
1790
+ this.redraw();
1791
+ };
1792
+ /**
1793
+ * @override
1794
+ * @inheritDoc
1795
+ */
1796
+ Text.prototype.setPrefillType = function (prefillType) {
1797
+ this.data.prefillType = prefillType;
1798
+ };
1799
+ /**
1800
+ * @override
1801
+ * @inheritDoc
1802
+ */
1803
+ Text.prototype.getPrefillType = function () {
1804
+ return this.data.prefillType;
1805
+ };
1806
+ /**
1807
+ * @override
1808
+ * @inheritDoc
1809
+ */
1810
+ Text.prototype.isResizeCanvas = function () {
1811
+ return this.data.resizeCanvas;
1812
+ };
1813
+ /**
1814
+ * @override
1815
+ * @inheritDoc
1816
+ */
1817
+ Text.prototype.setResizeCanvas = function (value) {
1818
+ this.baseComponentHeight = this.fabricObject.height;
1819
+ this.data.resizeCanvas = value;
1820
+ };
1821
+ /**
1822
+ * @override
1823
+ * @inheritDoc
1824
+ */
1825
+ Text.prototype.focus = function () {
1826
+ this.fabricObject.hiddenTextarea.focus();
1827
+ };
1828
+ /**
1829
+ * @override
1830
+ * @inheritDoc
1831
+ */
1832
+ Text.prototype.isEditing = function () {
1833
+ return this.fabricObject.isEditing;
1834
+ };
1835
+ /**
1836
+ * @override
1837
+ * @inheritDoc
1838
+ */
1839
+ Text.prototype.enterEditing = function () {
1840
+ if (!this.isEditing()) {
1841
+ this.fabricObject.enterEditing();
1842
+ this.focus();
1843
+ this.selectAllText();
1844
+ this.redraw();
1845
+ }
1846
+ };
1847
+ /**
1848
+ * @override
1849
+ * @inheritDoc
1850
+ */
1851
+ Text.prototype.selectAllText = function () {
1852
+ this.fabricObject.selectAll();
1853
+ };
1854
+ /**
1855
+ * @override
1856
+ * @inheritDoc
1857
+ */
1858
+ Text.prototype.isBulleted = function () {
1859
+ return this.data.bulleted || false;
1860
+ };
1861
+ /**
1862
+ * @override
1863
+ * @inheritDoc
1864
+ */
1865
+ Text.prototype.enableBullets = function () {
1866
+ this.fabricObject["objectCaching"] = false;
1867
+ this.fabricObject["_bullets"] = this.fabricObject["_bullets"] || {};
1868
+ this.data.bulleted = true;
1869
+ this.notifyCanvasOfTextChanges();
1870
+ this.redraw();
1871
+ };
1872
+ /**
1873
+ * @override
1874
+ * @inheritDoc
1875
+ */
1876
+ Text.prototype.disableBullets = function () {
1877
+ this.removeBullets();
1878
+ this.data.bulleted = false;
1879
+ this.notifyCanvasOfTextChanges();
1880
+ this.redraw();
1881
+ };
1882
+ Text.prototype.getSizeIncreaseSinceCreation = function () {
1883
+ return Math.max(0, this.fabricObject.height - this.baseComponentHeight);
1884
+ };
1885
+ Text.prototype.removeBullets = function () {
1886
+ var _this = this;
1887
+ if (this.data.bulleted) {
1888
+ var bullets = this.fabricObject["_bullets"];
1889
+ delete this.fabricObject["_bullets"];
1890
+ _.each(bullets, function (bullet) { return _this.canvas.removeFabricObject(bullet); });
1891
+ this.fabricObject["objectCaching"] = true;
1892
+ }
1893
+ };
1894
+ Text.prototype.applyBulletListMonkeyPatches = function () {
1895
+ if (this.data.bulleted)
1896
+ this.enableBullets();
1897
+ var originalRender = this.fabricObject["_render"];
1898
+ this.fabricObject["_render"] = function (ctx) {
1899
+ var _this = this;
1900
+ var fabricText = this;
1901
+ if (fabricText["_bullets"]) {
1902
+ // Determine list of active bullets
1903
+ var currentBullets_1 = fabricText["_bullets"];
1904
+ var activeBullets_1 = {};
1905
+ _.each(fabricText._textLines, function (textLine, lineIndex) {
1906
+ if (textLine.length) {
1907
+ // Start of real lines will have offset 0 in styleMap
1908
+ var styleMap = fabricText._styleMap[lineIndex];
1909
+ if (styleMap.offset === 0) {
1910
+ if (currentBullets_1[lineIndex]) {
1911
+ // Reuse existing bullet for this line
1912
+ activeBullets_1[lineIndex] = currentBullets_1[lineIndex];
1913
+ }
1914
+ else {
1915
+ // Create new bullet for this line
1916
+ var options = {
1917
+ left: 0,
1918
+ top: 0,
1919
+ radius: 2,
1920
+ fill: fabricText.fill,
1921
+ scaleX: fabricText.scaleX,
1922
+ scaleY: fabricText.scaleY,
1923
+ strokeWidth: 0,
1924
+ lockMovementX: true,
1925
+ lockMovementY: true,
1926
+ hasControls: false,
1927
+ selectable: false,
1928
+ excludeFromJson: true
1929
+ };
1930
+ activeBullets_1[lineIndex] = new _this.canvas.fabric.Circle(options);
1931
+ }
1932
+ }
1933
+ }
1934
+ });
1935
+ // Add/Remove bullets from canvas
1936
+ var canvas_1 = fabricText.canvas;
1937
+ var canvasDirty_1 = false;
1938
+ canvas_1.renderOnAddRemove = false;
1939
+ _.each(activeBullets_1, function (bullet) {
1940
+ if (!canvas_1.contains(bullet)) {
1941
+ canvas_1.add(bullet);
1942
+ canvasDirty_1 = true;
1943
+ }
1944
+ });
1945
+ _.each(currentBullets_1, function (bullet, key) {
1946
+ if (!activeBullets_1[key]) {
1947
+ canvas_1.remove(bullet);
1948
+ canvasDirty_1 = true;
1949
+ }
1950
+ });
1951
+ canvas_1.renderOnAddRemove = true;
1952
+ fabricText["_bullets"] = activeBullets_1;
1953
+ if (canvasDirty_1) {
1954
+ // Fabric doesn't like it when you add new things within render so use a sneaky timeout to delay render
1955
+ setTimeout(canvas_1.renderAll.bind(canvas_1));
1956
+ }
1957
+ }
1958
+ originalRender.apply(fabricText, [ctx]);
1959
+ };
1960
+ var originalRenderTextLine = this.fabricObject["_renderTextLine"];
1961
+ this.fabricObject["_renderTextLine"] = function (method, ctx, line, left, top, lineIndex) {
1962
+ var fabricText = this;
1963
+ var bullets = fabricText._bullets;
1964
+ if (bullets && bullets[lineIndex]) {
1965
+ var bullet = bullets[lineIndex];
1966
+ var bulletIndent = 10;
1967
+ var bulletLeft = fabricText.left + (left - fabricText._getLeftOffset() - bulletIndent) * fabricText.scaleX;
1968
+ if (bullet.left !== bulletLeft) {
1969
+ bullet.set({ left: bulletLeft });
1970
+ }
1971
+ /**
1972
+ * What an out-and-out mess... this calculation is a joke, and the bulletOffset is just an approximation of values that look ok for
1973
+ * the font sizes we use. The 'top' value provided is actutally the... bottom? of the line... and not the line... the bottom of where
1974
+ * the font is rendered... subtracting the `fabricText.getHeightOfLine(lineIndex)` line hieght doesn't give you the top, because the
1975
+ * line height is actually the height of the font plus the excess line height added via the text line-height property.
1976
+ * I couldn't figure out how to get the actual top of the line in two days of trying to understand this ridiculous library.
1977
+ * So, here... bullets. That sort of work... sometimes.
1978
+ */
1979
+ var textOriginTop = fabricText.top + (fabricText.height / 2);
1980
+ var fontSize = this.getValueOfPropertyAt(lineIndex, 0, "fontSize");
1981
+ var bulletOffset = ((fontSize + 6) / 2);
1982
+ var bulletTop = textOriginTop + top - bulletOffset;
1983
+ if (bullet.top !== bulletTop) {
1984
+ bullet.set({ top: bulletTop });
1985
+ }
1986
+ if (bullet.fill !== fabricText.fill) {
1987
+ bullet.set({ fill: fabricText.fill });
1988
+ }
1989
+ }
1990
+ originalRenderTextLine.apply(fabricText, [method, ctx, line, left, top, lineIndex]);
1991
+ };
1992
+ };
1993
+ /**
1994
+ * @override
1995
+ * @inheritDoc
1996
+ */
1997
+ Text.prototype.updateFabricObjectBehavior = function () {
1998
+ _super.prototype.updateFabricObjectBehavior.call(this);
1999
+ var profile = this.canvas.getProfile();
2000
+ var allowManualPositioning = !this.canvas.getLayoutManager().isEnforcedPositioning;
2001
+ this.fabricObject.set({
2002
+ lockScalingY: true,
2003
+ lockScalingX: this.isPinToBottom(),
2004
+ lockScalingFlip: true
2005
+ });
2006
+ this.fabricObject.setControlsVisibility({
2007
+ tl: false,
2008
+ tr: false,
2009
+ br: false,
2010
+ bl: false,
2011
+ ml: allowManualPositioning && profile.allowScale && !this.isPinToBottom(),
2012
+ mt: false,
2013
+ mr: allowManualPositioning && profile.allowScale && !this.isPinToBottom(),
2014
+ mb: false
2015
+ });
2016
+ this.redraw();
2017
+ };
2018
+ Text.prototype.onTextChange = function () {
2019
+ this.data.userText = this.fabricObject.text;
2020
+ this.text$.next(this.fabricObject.text);
2021
+ if (this.getBoundingBox().width > this.canvas.getDimensions().widthPx) {
2022
+ this.fabricObject.set({ left: 0, width: this.canvas.getDimensions().widthPx });
2023
+ this.redraw();
2024
+ }
2025
+ };
2026
+ Text.prototype.onDelete = function () {
2027
+ this.removeBullets();
2028
+ };
2029
+ Text.prototype.onSelectionChange = function () {
2030
+ this.updateTextSelection();
2031
+ };
2032
+ Text.prototype.hasTextSelection = function () {
2033
+ return this.canvas.getSelection().isText && this.textSelection$.value.length > 0;
2034
+ };
2035
+ Text.prototype.updateTextSelection = function () {
2036
+ this.textSelection$.next(new TextSelection(this.fabricObject, this.fontLibrary));
2037
+ this.notifyCanvasOfTextChanges();
2038
+ };
2039
+ Text.prototype.notifyCanvasOfTextChanges = function () {
2040
+ this.canvas.notifyTextChanged();
2041
+ };
2042
+ return Text;
2043
+ }(AbstractCanvasObject));
2044
+ var TextSelection = /** @class */ (function () {
2045
+ function TextSelection(fabricObject, fontLibrary) {
2046
+ this.fabricObject = fabricObject;
2047
+ this.fontLibrary = fontLibrary;
2048
+ this.start = this.fabricObject.selectionStart;
2049
+ this.end = this.fabricObject.selectionEnd;
2050
+ this.length = this.end - this.start;
2051
+ var cursorLoc = this.fabricObject.get2DCursorLocation();
2052
+ this.lineIndex = cursorLoc.lineIndex;
2053
+ this.charIndex = cursorLoc.charIndex;
2054
+ var style = this.fabricObject.getCompleteStyleDeclaration(this.lineIndex, this.charIndex);
2055
+ var hasSelection = this.length > 0;
2056
+ this.font = hasSelection ? this.fontLibrary.getFont(style.fontFamily) : this.fontLibrary.getFont(this.fabricObject.fontFamily);
2057
+ this.fontSize = hasSelection ? style.fontSize : this.fabricObject.fontSize;
2058
+ this.fontWeight = hasSelection ? style.fontWeight : this.fabricObject.fontWeight;
2059
+ this.bold = this.font.isBold(this.fontWeight);
2060
+ this.italic = hasSelection ? style.fontStyle === "italic" : this.fabricObject.fontStyle === "italic";
2061
+ this.underline = hasSelection ? style.underline : this.fabricObject.underline;
2062
+ this.strikethrough = hasSelection ? style.linethrough : this.fabricObject.linethrough;
2063
+ this.overline = hasSelection ? style.overline : this.fabricObject.overline;
2064
+ this.textBackgroundColor = hasSelection ? style.textBackgroundColor : this.fabricObject.textBackgroundColor;
2065
+ this.textAlign = this.fabricObject.textAlign;
2066
+ this.charSpacing = this.fabricObject.charSpacing;
2067
+ this.lineHeight = this.fabricObject.lineHeight;
2068
+ this.fill = style.fill;
2069
+ this.stroke = style.stroke;
2070
+ this.strokeWidth = style.strokeWidth;
2071
+ }
2072
+ return TextSelection;
2073
+ }());
2074
+
2075
+ var VerticalRule = /** @class */ (function (_super) {
2076
+ __extends(VerticalRule, _super);
2077
+ function VerticalRule(canvas, fabricObject, persistedField) {
2078
+ var _this = _super.call(this, exports.ObjectType.VERTICAL_RULE, canvas, fabricObject, persistedField) || this;
2079
+ _this.strokeWidth$ = new rxjs.BehaviorSubject(2);
2080
+ _this.canvas = canvas;
2081
+ _this.strokeWidth$.next(_this.getDimensions().width.px);
2082
+ _this.fabricObject.set({ strokeWidth: 0 });
2083
+ return _this;
2084
+ }
2085
+ /**
2086
+ * @override
2087
+ * @inheritDoc
2088
+ */
2089
+ VerticalRule.prototype.getStrokeWidth = function () {
2090
+ return this.fabricObject.width;
2091
+ };
2092
+ /**
2093
+ * @override
2094
+ * @inheritDoc
2095
+ */
2096
+ VerticalRule.prototype.getStrokeWidth$ = function () {
2097
+ return this.strokeWidth$;
2098
+ };
2099
+ /**
2100
+ * @override
2101
+ * @inheritDoc
2102
+ */
2103
+ VerticalRule.prototype.setStrokeWidth = function (width) {
2104
+ this.strokeWidth$.next(width);
2105
+ this.data.strokeWidth = width;
2106
+ this.setPosition({ width: width });
2107
+ this.canvas.notifyObjectListChanged();
2108
+ this.redraw();
2109
+ };
2110
+ /**
2111
+ * @override
2112
+ * @inheritDoc
2113
+ */
2114
+ VerticalRule.prototype.getColor = function () {
2115
+ return this.fabricObject.fill;
2116
+ };
2117
+ /**
2118
+ * @override
2119
+ * @inheritDoc
2120
+ */
2121
+ VerticalRule.prototype.setColor = function (color) {
2122
+ this.fabricObject.set({ fill: color, stroke: color });
2123
+ this.data.color = color;
2124
+ this.redraw();
2125
+ };
2126
+ /**
2127
+ * @override
2128
+ * @inheritDoc
2129
+ */
2130
+ VerticalRule.prototype.centerHorizontally = function () {
2131
+ _super.prototype.centerHorizontallyInternal.call(this);
2132
+ };
2133
+ /**
2134
+ * @override
2135
+ * @inheritDoc
2136
+ */
2137
+ VerticalRule.prototype.centerVertically = function () {
2138
+ _super.prototype.centerVerticallyInternal.call(this);
2139
+ };
2140
+ /**
2141
+ * @override
2142
+ * @inheritDoc
2143
+ */
2144
+ VerticalRule.prototype.setHeight = function (height) {
2145
+ this.setPosition({ height: height });
2146
+ this.canvas.notifyObjectListChanged();
2147
+ };
2148
+ /**
2149
+ * @override
2150
+ * @inheritDoc
2151
+ */
2152
+ VerticalRule.prototype.nudgeUp = function () {
2153
+ _super.prototype.nudgeInternal.call(this, 0, -1);
2154
+ };
2155
+ /**
2156
+ * @override
2157
+ * @inheritDoc
2158
+ */
2159
+ VerticalRule.prototype.nudgeDown = function () {
2160
+ _super.prototype.nudgeInternal.call(this, 0, +1);
2161
+ };
2162
+ /**
2163
+ * @override
2164
+ * @inheritDoc
2165
+ */
2166
+ VerticalRule.prototype.nudgeLeft = function () {
2167
+ _super.prototype.nudgeInternal.call(this, -1, 0);
2168
+ };
2169
+ /**
2170
+ * @override
2171
+ * @inheritDoc
2172
+ */
2173
+ VerticalRule.prototype.nudgeRight = function () {
2174
+ _super.prototype.nudgeInternal.call(this, +1, 0);
2175
+ };
2176
+ return VerticalRule;
2177
+ }(AbstractCanvasObject));
2178
+
2179
+ /**
2180
+ * Profile with as many features disabled as possible.
2181
+ */
2182
+ var DefaultCanvasProfile = /** @class */ (function () {
2183
+ function DefaultCanvasProfile() {
2184
+ this.allowMultiSelect = false;
2185
+ this.enterTextEditImmediatelyOnSelect = true;
2186
+ this.allowObjectsToLeaveCanvas = false;
2187
+ this.allowMove = false;
2188
+ this.allowRotate = false;
2189
+ this.allowScale = false;
2190
+ this.defaultFont = "Lato";
2191
+ this.defaultFontSize = 8;
2192
+ this.defaultFontColor = "#000000";
2193
+ this.defaultLineHeight = 1.8;
2194
+ this.defaultCharacterSpacing = 0;
2195
+ this.defaultTextAlignment = "left";
2196
+ this.defaultBorderColor = "#000000";
2197
+ this.defaultBorderWidth = 1;
2198
+ this.defaultCutoffColor = "#000000";
2199
+ this.defaultCutoffWidth = 1;
2200
+ }
2201
+ return DefaultCanvasProfile;
2202
+ }());
2203
+
2204
+ (function (ImageType) {
2205
+ ImageType["DEFAULT"] = "DEFAULT";
2206
+ ImageType["BACKGROUND"] = "BACKGROUND";
2207
+ ImageType["LOGO"] = "LOGO";
2208
+ ImageType["PHOTO"] = "PHOTO";
2209
+ })(exports.ImageType || (exports.ImageType = {}));
2210
+
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
+ })(exports.ObjectType || (exports.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 rxjs.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 rxjs.BehaviorSubject(this.baseDimensions);
2234
+ this.dimensionRestrictions$ = new rxjs.BehaviorSubject(baseRestrictions);
2235
+ this.zoom$ = new rxjs.BehaviorSubject(1);
2236
+ this.profile$ = new rxjs.BehaviorSubject(new DefaultCanvasProfile());
2237
+ this.layoutManager$ = new rxjs.BehaviorSubject(new AdvancedLayoutManager(this));
2238
+ this.backgroundColor$ = new rxjs.BehaviorSubject(this.fabricCanvas.backgroundColor);
2239
+ this.objects$ = new rxjs.BehaviorSubject([]);
2240
+ this.selection$ = new rxjs.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 === exports.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(exports.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(exports.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(exports.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(exports.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(exports.ObjectType.TEXT, exports.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 exports.ObjectType.TEXT:
2984
+ return new Text(this, fabricObject, persistedField);
2985
+ case exports.ObjectType.RECTANGLE:
2986
+ return new Rectangle(this, fabricObject, persistedField);
2987
+ case exports.ObjectType.BORDER:
2988
+ return new Border(this, fabricObject, persistedField);
2989
+ case exports.ObjectType.CUTOFF:
2990
+ return new Cutoff(this, fabricObject, persistedField);
2991
+ case exports.ObjectType.IMAGE:
2992
+ case exports.ObjectType.LIBRARY_IMAGE:
2993
+ return new Image(this, fabricObject, persistedField);
2994
+ case exports.ObjectType.HORIZONTAL_RULE:
2995
+ return new HorizontalRule(this, fabricObject, persistedField);
2996
+ case exports.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() === exports.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 tsOptchain.oc(restrictions).height.min() != undefined && dimensions.height < tsOptchain.oc(restrictions).height.min()
3084
+ || tsOptchain.oc(restrictions).height.max() != undefined && dimensions.height > tsOptchain.oc(restrictions).height.max()
3085
+ || tsOptchain.oc(restrictions).width.min() != undefined && dimensions.width < tsOptchain.oc(restrictions).width.min()
3086
+ || tsOptchain.oc(restrictions).width.max() != undefined && dimensions.width > tsOptchain.oc(restrictions).width.max()
3087
+ || tsOptchain.oc(restrictions).volume.min() != undefined && dimensions.height * dimensions.width < tsOptchain.oc(restrictions).volume.min()
3088
+ || tsOptchain.oc(restrictions).volume.max() != undefined && dimensions.height * dimensions.width > tsOptchain.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() === exports.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.fabric, new fabric.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() !== exports.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(exports.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(exports.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 = exports.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
+ exports.AbstractCanvasObject = AbstractCanvasObject;
3226
+ exports.AbstractInteractiveCanvas = AbstractInteractiveCanvas;
3227
+ exports.AbstractLayoutManager = AbstractLayoutManager;
3228
+ exports.AdvancedLayoutManager = AdvancedLayoutManager;
3229
+ exports.Border = Border;
3230
+ exports.CanvasObjectData = CanvasObjectData;
3231
+ exports.Cutoff = Cutoff;
3232
+ exports.DefaultCanvasProfile = DefaultCanvasProfile;
3233
+ exports.Font = Font;
3234
+ exports.FontLibrary = FontLibrary;
3235
+ exports.HeadlessInteractiveCanvas = HeadlessInteractiveCanvas;
3236
+ exports.HorizontalRule = HorizontalRule;
3237
+ exports.Image = Image;
3238
+ exports.InteractiveCanvas = InteractiveCanvas;
3239
+ exports.PixelCanvasDimensions = PixelCanvasDimensions;
3240
+ exports.Print6ColCanvasDimensions = Print6ColCanvasDimensions;
3241
+ exports.Print8ColCanvasDimensions = Print8ColCanvasDimensions;
3242
+ exports.Rectangle = Rectangle;
3243
+ exports.SelectionData = SelectionData;
3244
+ exports.StandardLayoutManager = StandardLayoutManager;
3245
+ exports.Text = Text;
3246
+ exports.UNKNOWN_FONT = UNKNOWN_FONT;
3247
+ exports.UnknownFont = UnknownFont;
3248
+ exports.VerticalRule = VerticalRule;
3249
+ exports.isMutableBackgroundColor = isMutableBackgroundColor;
3250
+ exports.isMutableColor = isMutableColor;
3251
+ exports.isMutableImage = isMutableImage;
3252
+ exports.isMutablePosition = isMutablePosition;
3253
+ exports.isMutableText = isMutableText;
3254
+ exports.isMutableTextStyle = isMutableTextStyle;