@leapfrog/interactive-canvas 1.14.4 → 1.14.6

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