@leapfrog/interactive-canvas 2.0.7 → 2.0.8-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/package.json +1 -1
  2. package/dist/abstract-interactive-canvas.d.ts +0 -372
  3. package/dist/dimensions/millimeter-dimensions.d.ts +0 -15
  4. package/dist/dimensions/pixel-canvas-dimensions.d.ts +0 -13
  5. package/dist/dimensions/print-6-col-canvas-dimensions.d.ts +0 -18
  6. package/dist/dimensions/print-8-col-canvas-dimensions.d.ts +0 -18
  7. package/dist/font/font-library.d.ts +0 -27
  8. package/dist/font/font-library.interface.d.ts +0 -24
  9. package/dist/font/font.interface.d.ts +0 -20
  10. package/dist/font/unknown-font.d.ts +0 -6
  11. package/dist/headless-interactive-canvas.d.ts +0 -10
  12. package/dist/index.d.ts +0 -54
  13. package/dist/interactive-canvas.d.ts +0 -25
  14. package/dist/interactive-canvas.es.js +0 -3994
  15. package/dist/interactive-canvas.interface.d.ts +0 -297
  16. package/dist/interactive-canvas.js +0 -4027
  17. package/dist/layout/abstract-layout-manager.d.ts +0 -17
  18. package/dist/layout/advanced-layout-manager.d.ts +0 -16
  19. package/dist/layout/function/pin-to-bottom.layout-function.d.ts +0 -4
  20. package/dist/layout/function/redraw-all.layout-function.d.ts +0 -4
  21. package/dist/layout/function/reposition-backgrounds.layout-function.d.ts +0 -4
  22. package/dist/layout/function/reposition-borders.layout-function.d.ts +0 -4
  23. package/dist/layout/function/reposition-cutoffs.layout-function.d.ts +0 -4
  24. package/dist/layout/function/resize-canvas.layout-function.d.ts +0 -4
  25. package/dist/layout/function/stack-objects.layout-function.d.ts +0 -11
  26. package/dist/layout/function/update-object-dimensions.layout-function.d.ts +0 -4
  27. package/dist/layout/layout-manager.enum.d.ts +0 -5
  28. package/dist/layout/layout-manager.interface.d.ts +0 -13
  29. package/dist/layout/standard-layout-manager.d.ts +0 -40
  30. package/dist/mutator/mutable-background-color.interface.d.ts +0 -15
  31. package/dist/mutator/mutable-color.interface.d.ts +0 -6
  32. package/dist/mutator/mutable-image.interface.d.ts +0 -28
  33. package/dist/mutator/mutable-position.interface.d.ts +0 -63
  34. package/dist/mutator/mutable-stroke.interface.d.ts +0 -8
  35. package/dist/mutator/mutable-text-style.interface.d.ts +0 -163
  36. package/dist/mutator/mutable-text.interface.d.ts +0 -80
  37. package/dist/object/abstract-canvas-object.d.ts +0 -207
  38. package/dist/object/canvas-object-data.d.ts +0 -24
  39. package/dist/object/canvas-object.interface.d.ts +0 -109
  40. package/dist/object/impl/background-image.d.ts +0 -38
  41. package/dist/object/impl/border.d.ts +0 -70
  42. package/dist/object/impl/cutoff.d.ts +0 -59
  43. package/dist/object/impl/extended-fabric-object.interface.d.ts +0 -7
  44. package/dist/object/impl/extended-fabric-textbox.interface.d.ts +0 -7
  45. package/dist/object/impl/horizontal-rule.d.ts +0 -83
  46. package/dist/object/impl/image.d.ts +0 -76
  47. package/dist/object/impl/rectangle.d.ts +0 -78
  48. package/dist/object/impl/rich-text.d.ts +0 -39
  49. package/dist/object/impl/text.d.ts +0 -314
  50. package/dist/object/impl/vertical-rule.d.ts +0 -83
  51. package/dist/profile/canvas-profile.interface.d.ts +0 -22
  52. package/dist/profile/default-canvas-profile.d.ts +0 -23
  53. package/dist/types/canvas-dimension-restrictions.interface.d.ts +0 -15
  54. package/dist/types/canvas-dimensions.interface.d.ts +0 -21
  55. package/dist/types/canvas-state.interface.d.ts +0 -11
  56. package/dist/types/dimension-restrictions.interface.d.ts +0 -6
  57. package/dist/types/image-type.enum.d.ts +0 -6
  58. package/dist/types/margin.interface.d.ts +0 -6
  59. package/dist/types/object-dimensions.interface.d.ts +0 -23
  60. package/dist/types/object-type.enum.d.ts +0 -12
  61. package/dist/types/overflow.interface.d.ts +0 -4
  62. package/dist/types/position.interface.d.ts +0 -9
  63. package/dist/types/rect.interface.d.ts +0 -6
  64. package/dist/types/selection-data.interface.d.ts +0 -19
  65. package/dist/types/text-alignment.interface.d.ts +0 -1
  66. package/dist/undo-stack.d.ts +0 -27
  67. package/dist/undo-stack.interface.d.ts +0 -6
@@ -1,4027 +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 fontWeight = this.getValueOfPropertyAt(lineIndex, 0, "fontWeight");
2566
- var fontStyle = this.getValueOfPropertyAt(lineIndex, 0, "fontStyle");
2567
- var color = this.getValueOfPropertyAt(lineIndex, 0, "fill");
2568
- var angle = fabricText.angle;
2569
- var theta = angle * Math.PI / 180;
2570
- var yShift = (fabricText.height / 2) + top - fontSize - 1;
2571
- var xOffset = (xShift * Math.cos(theta)) - (yShift * Math.sin(theta));
2572
- var yOffset = (xShift * Math.sin(theta)) + (yShift * Math.cos(theta));
2573
- var lineNumber = _this.getOrderedListStart();
2574
- for (var i = 0; i < lineIndex; i++) {
2575
- if (orderedListMarkerEntities[i])
2576
- lineNumber++;
2577
- }
2578
- entry.set({
2579
- width: 1,
2580
- fontSize: fontSize,
2581
- fontFamily: fontFamily,
2582
- fontWeight: fontWeight,
2583
- fontStyle: fontStyle,
2584
- fill: color,
2585
- left: fabricText.left + xOffset,
2586
- top: fabricText.top + yOffset,
2587
- angle: fabricText.angle,
2588
- text: lineNumber + "."
2589
- });
2590
- }
2591
- originalRenderTextLine.apply(fabricText, [method, ctx, line, left, top, lineIndex]);
2592
- };
2593
- };
2594
- /**
2595
- * @override
2596
- * @inheritDoc
2597
- */
2598
- Text.prototype.updateFabricObjectBehavior = function () {
2599
- _super.prototype.updateFabricObjectBehavior.call(this);
2600
- var profile = this.canvas.getProfile();
2601
- var allowManualPositioning = !this.canvas.getLayoutManager().isEnforcedPositioning;
2602
- this.fabricObject.set({
2603
- lockScalingY: true,
2604
- lockScalingX: this.isPinToBottom(),
2605
- lockScalingFlip: true
2606
- });
2607
- this.fabricObject.setControlsVisibility({
2608
- tl: false,
2609
- tr: false,
2610
- br: false,
2611
- bl: false,
2612
- ml: allowManualPositioning && profile.allowScale && !this.isPinToBottom(),
2613
- mt: false,
2614
- mr: allowManualPositioning && profile.allowScale && !this.isPinToBottom(),
2615
- mb: false
2616
- });
2617
- this.redraw();
2618
- };
2619
- Text.prototype.onTextChange = function () {
2620
- this.data.userText = this.fabricObject.text;
2621
- this.text$.next(this.fabricObject.text);
2622
- if (this.getBoundingBox().width > this.canvas.getDimensions().widthPx) {
2623
- this.fabricObject.set({ left: 0, width: this.canvas.getDimensions().widthPx });
2624
- this.redraw();
2625
- }
2626
- };
2627
- Text.prototype.onDelete = function () {
2628
- this.removeBullets();
2629
- };
2630
- Text.prototype.onSelectionChange = function () {
2631
- this.updateTextSelection();
2632
- };
2633
- Text.prototype.hasTextSelection = function () {
2634
- return this.canvas.getSelection().isText && this.textSelection$.value.length > 0;
2635
- };
2636
- Text.prototype.updateTextSelection = function () {
2637
- if (this.canvas.headless)
2638
- return;
2639
- this.textSelection$.next(new TextSelection(this.fabricObject, this.fontLibrary));
2640
- this.notifyCanvasOfTextChanges();
2641
- };
2642
- Text.prototype.notifyCanvasOfTextChanges = function () {
2643
- this.canvas.notifyTextChanged();
2644
- };
2645
- return Text;
2646
- }(AbstractCanvasObject));
2647
- var TextSelection = /** @class */ (function () {
2648
- function TextSelection(fabricObject, fontLibrary) {
2649
- this.fabricObject = fabricObject;
2650
- this.fontLibrary = fontLibrary;
2651
- this.start = this.fabricObject.selectionStart;
2652
- this.end = this.fabricObject.selectionEnd;
2653
- this.length = this.end - this.start;
2654
- var cursorLoc = this.fabricObject.get2DCursorLocation();
2655
- this.lineIndex = cursorLoc.lineIndex;
2656
- this.charIndex = cursorLoc.charIndex;
2657
- var style = this.fabricObject.getCompleteStyleDeclaration(this.lineIndex, this.charIndex);
2658
- var hasSelection = this.length > 0;
2659
- this.font = hasSelection ? this.fontLibrary.getFont(style.fontFamily) : this.fontLibrary.getFont(this.fabricObject.fontFamily);
2660
- this.fontSize = hasSelection ? style.fontSize : this.fabricObject.fontSize;
2661
- this.fontWeight = hasSelection ? style.fontWeight : this.fabricObject.fontWeight;
2662
- this.bold = this.font.isBold(this.fontWeight);
2663
- this.italic = hasSelection ? style.fontStyle === "italic" : this.fabricObject.fontStyle === "italic";
2664
- this.underline = hasSelection ? style.underline : this.fabricObject.underline;
2665
- this.strikethrough = hasSelection ? style.linethrough : this.fabricObject.linethrough;
2666
- this.overline = hasSelection ? style.overline : this.fabricObject.overline;
2667
- this.textBackgroundColor = hasSelection ? style.textBackgroundColor : this.fabricObject.textBackgroundColor;
2668
- this.textAlign = this.fabricObject.textAlign;
2669
- this.charSpacing = this.fabricObject.charSpacing;
2670
- this.lineHeight = this.fabricObject.lineHeight;
2671
- this.fill = style.fill;
2672
- this.stroke = style.stroke;
2673
- this.strokeWidth = style.strokeWidth;
2674
- }
2675
- return TextSelection;
2676
- }());
2677
-
2678
- var VerticalRule = /** @class */ (function (_super) {
2679
- __extends(VerticalRule, _super);
2680
- function VerticalRule(canvas, fabricObject, persistedField) {
2681
- var _this = _super.call(this, exports.ObjectType.VERTICAL_RULE, canvas, fabricObject, persistedField) || this;
2682
- _this.strokeWidth$ = new rxjs.BehaviorSubject(2);
2683
- _this.canvas = canvas;
2684
- _this.strokeWidth$.next(_this.getDimensions().width.px);
2685
- _this.fabricObject.set({ strokeWidth: 0 });
2686
- return _this;
2687
- }
2688
- /**
2689
- * @override
2690
- * @inheritDoc
2691
- */
2692
- VerticalRule.prototype.getStrokeWidth = function () {
2693
- return this.fabricObject.width;
2694
- };
2695
- /**
2696
- * @override
2697
- * @inheritDoc
2698
- */
2699
- VerticalRule.prototype.getStrokeWidth$ = function () {
2700
- return this.strokeWidth$;
2701
- };
2702
- /**
2703
- * @override
2704
- * @inheritDoc
2705
- */
2706
- VerticalRule.prototype.setStrokeWidth = function (width) {
2707
- this.strokeWidth$.next(width);
2708
- this.data.strokeWidth = width;
2709
- this.setPosition({ width: width });
2710
- this.canvas.notifyObjectListChanged();
2711
- this.redraw();
2712
- };
2713
- /**
2714
- * @override
2715
- * @inheritDoc
2716
- */
2717
- VerticalRule.prototype.getColor = function () {
2718
- return this.fabricObject.fill;
2719
- };
2720
- /**
2721
- * @override
2722
- * @inheritDoc
2723
- */
2724
- VerticalRule.prototype.setColor = function (color) {
2725
- this.fabricObject.set({ fill: color, stroke: color });
2726
- this.data.color = color;
2727
- this.redraw();
2728
- };
2729
- /**
2730
- * @override
2731
- * @inheritDoc
2732
- */
2733
- VerticalRule.prototype.centerHorizontally = function () {
2734
- _super.prototype.centerHorizontallyInternal.call(this);
2735
- };
2736
- /**
2737
- * @override
2738
- * @inheritDoc
2739
- */
2740
- VerticalRule.prototype.centerVertically = function () {
2741
- _super.prototype.centerVerticallyInternal.call(this);
2742
- };
2743
- /**
2744
- * @override
2745
- * @inheritDoc
2746
- */
2747
- VerticalRule.prototype.nudgeUp = function () {
2748
- _super.prototype.nudgeInternal.call(this, 0, -1);
2749
- };
2750
- /**
2751
- * @override
2752
- * @inheritDoc
2753
- */
2754
- VerticalRule.prototype.nudgeDown = function () {
2755
- _super.prototype.nudgeInternal.call(this, 0, +1);
2756
- };
2757
- /**
2758
- * @override
2759
- * @inheritDoc
2760
- */
2761
- VerticalRule.prototype.nudgeLeft = function () {
2762
- _super.prototype.nudgeInternal.call(this, -1, 0);
2763
- };
2764
- /**
2765
- * @override
2766
- * @inheritDoc
2767
- */
2768
- VerticalRule.prototype.nudgeRight = function () {
2769
- _super.prototype.nudgeInternal.call(this, +1, 0);
2770
- };
2771
- return VerticalRule;
2772
- }(AbstractCanvasObject));
2773
-
2774
- /**
2775
- * Profile with as many features disabled as possible.
2776
- */
2777
- var DefaultCanvasProfile = /** @class */ (function () {
2778
- function DefaultCanvasProfile() {
2779
- this.allowMultiSelect = false;
2780
- this.enterTextEditImmediatelyOnSelect = true;
2781
- this.allowObjectsToLeaveCanvas = false;
2782
- this.allowMove = false;
2783
- this.allowRotate = false;
2784
- this.allowScale = false;
2785
- this.defaultFont = "Lato";
2786
- this.defaultFontSize = 9;
2787
- this.defaultFontColor = "#000000";
2788
- this.defaultLineHeight = 1.8;
2789
- this.defaultCharacterSpacing = 0;
2790
- this.defaultTextAlignment = "left";
2791
- this.defaultBorderColor = "#000000";
2792
- this.defaultBorderWidth = 1;
2793
- this.defaultCutoffColor = "#000000";
2794
- this.defaultCutoffWidth = 1;
2795
- }
2796
- return DefaultCanvasProfile;
2797
- }());
2798
-
2799
- (function (ImageType) {
2800
- ImageType["DEFAULT"] = "DEFAULT";
2801
- ImageType["BACKGROUND"] = "BACKGROUND";
2802
- ImageType["LOGO"] = "LOGO";
2803
- ImageType["PHOTO"] = "PHOTO";
2804
- })(exports.ImageType || (exports.ImageType = {}));
2805
-
2806
- (function (ObjectType) {
2807
- ObjectType["TEXT"] = "TEXT";
2808
- ObjectType["BORDER"] = "BORDER";
2809
- ObjectType["IMAGE"] = "IMAGE";
2810
- ObjectType["RECTANGLE"] = "RECTANGLE";
2811
- ObjectType["LIBRARY_IMAGE"] = "LIBRARY_IMAGE";
2812
- ObjectType["CUTOFF"] = "CUTOFF";
2813
- ObjectType["VERTICAL_RULE"] = "VERTICAL_RULE";
2814
- ObjectType["HORIZONTAL_RULE"] = "HORIZONTAL_RULE";
2815
- ObjectType["RICH_TEXT"] = "RICH_TEXT";
2816
- ObjectType["BACKGROUND_IMAGE"] = "BACKGROUND_IMAGE";
2817
- })(exports.ObjectType || (exports.ObjectType = {}));
2818
-
2819
- var AbstractInteractiveCanvas = /** @class */ (function () {
2820
- function AbstractInteractiveCanvas(fabric, fabricCanvas, devicePixelRatio) {
2821
- this.fabric = fabric;
2822
- this.fabricCanvas = fabricCanvas;
2823
- this.devicePixelRatio = devicePixelRatio;
2824
- this.fontLibrary = new FontLibrary();
2825
- this.dimensionChangeRejections$ = new rxjs.Subject();
2826
- this.IMAGE_CAPTURE_COEFFICIENT = this.calculateImageCaptureCoefficient();
2827
- /**
2828
- * Fabric defaults to only 2 decimal places when saving, which causes havoc on the scaling of large images.
2829
- */
2830
- fabric.Object.NUM_FRACTION_DIGITS = 10;
2831
- this.baseDimensions = new PixelCanvasDimensions(this.fabricCanvas.width, this.fabricCanvas.height);
2832
- var baseRestrictions = { width: { min: 1 }, height: { min: 1 } };
2833
- //Initialize properties
2834
- this.dimensions$ = new rxjs.BehaviorSubject(this.baseDimensions);
2835
- this.dimensionRestrictions$ = new rxjs.BehaviorSubject(baseRestrictions);
2836
- this.zoom$ = new rxjs.BehaviorSubject(1);
2837
- this.profile$ = new rxjs.BehaviorSubject(new DefaultCanvasProfile());
2838
- this.layoutManager$ = new rxjs.BehaviorSubject(new AdvancedLayoutManager(this));
2839
- this.backgroundColor$ = new rxjs.BehaviorSubject(this.fabricCanvas.backgroundColor);
2840
- this.objects$ = new rxjs.BehaviorSubject([]);
2841
- this.selection$ = new rxjs.BehaviorSubject(new SelectionData([]));
2842
- //Configure canvas behaviour
2843
- this.fabricCanvas.stateful = true;
2844
- this.fabricCanvas.preserveObjectStacking = true;
2845
- this.fabricCanvas.perPixelTargetFind = true;
2846
- this.fabricCanvas.targetFindTolerance = 10;
2847
- this.setBackgroundColor("#FFFFFF");
2848
- }
2849
- /**
2850
- * @override
2851
- * @inheritDoc
2852
- */
2853
- AbstractInteractiveCanvas.prototype.getFontLibrary = function () {
2854
- return this.fontLibrary;
2855
- };
2856
- /**
2857
- * @override
2858
- * @inheritDoc
2859
- */
2860
- AbstractInteractiveCanvas.prototype.getProfile = function () {
2861
- return this.profile$.value;
2862
- };
2863
- /**
2864
- * @override
2865
- * @inheritDoc
2866
- */
2867
- AbstractInteractiveCanvas.prototype.getProfile$ = function () {
2868
- return this.profile$;
2869
- };
2870
- /**
2871
- * @override
2872
- * @inheritDoc
2873
- */
2874
- AbstractInteractiveCanvas.prototype.setProfile = function (profile) {
2875
- this.profile$.next(profile);
2876
- this.onProfileChanged(profile);
2877
- this.redraw();
2878
- };
2879
- /**
2880
- * @override
2881
- * @inheritDoc
2882
- */
2883
- AbstractInteractiveCanvas.prototype.getLayoutManager = function () {
2884
- return this.layoutManager$.value;
2885
- };
2886
- /**
2887
- * @override
2888
- * @inheritDoc
2889
- */
2890
- AbstractInteractiveCanvas.prototype.getLayoutManager$ = function () {
2891
- return this.layoutManager$;
2892
- };
2893
- /**
2894
- * @override
2895
- * @inheritDoc
2896
- */
2897
- AbstractInteractiveCanvas.prototype.setLayoutManager = function (layoutManager) {
2898
- if (layoutManager === exports.LayoutManager.STANDARD) {
2899
- this.layoutManager$.next(new StandardLayoutManager(this));
2900
- }
2901
- else {
2902
- this.layoutManager$.next(new AdvancedLayoutManager(this));
2903
- }
2904
- this.layoutManager$.value.onInit();
2905
- };
2906
- /**
2907
- * @override
2908
- * @inheritDoc
2909
- */
2910
- AbstractInteractiveCanvas.prototype.getBackgroundColor = function () {
2911
- return this.backgroundColor$.value;
2912
- };
2913
- /**
2914
- * @override
2915
- * @inheritDoc
2916
- */
2917
- AbstractInteractiveCanvas.prototype.getBackgroundColor$ = function () {
2918
- return this.backgroundColor$;
2919
- };
2920
- /**
2921
- * @override
2922
- * @inheritDoc
2923
- */
2924
- AbstractInteractiveCanvas.prototype.setBackgroundColor = function (color) {
2925
- if (color === void 0) { color = "White"; }
2926
- this.fabricCanvas.setBackgroundColor(color, function () {
2927
- });
2928
- this.fabricCanvas.renderAll();
2929
- this.backgroundColor$.next(color);
2930
- };
2931
- /**
2932
- * @override
2933
- * @inheritDoc
2934
- */
2935
- AbstractInteractiveCanvas.prototype.getDimensions = function () {
2936
- return this.dimensions$.value;
2937
- };
2938
- /**
2939
- * @override
2940
- * @inheritDoc
2941
- */
2942
- AbstractInteractiveCanvas.prototype.getBaseDimensions = function () {
2943
- return this.baseDimensions;
2944
- };
2945
- /**
2946
- * @override
2947
- * @inheritDoc
2948
- */
2949
- AbstractInteractiveCanvas.prototype.getDimensions$ = function () {
2950
- return this.dimensions$;
2951
- };
2952
- /**
2953
- * @override
2954
- * @inheritDoc
2955
- */
2956
- AbstractInteractiveCanvas.prototype.setDimensions = function (dimensions) {
2957
- if (this.dimensionsExceedRestrictions(dimensions)) {
2958
- console.info("Requested canvas dimensions exceed current restrictions", dimensions, this.dimensionRestrictions$.value);
2959
- this.dimensionChangeRejections$.next(dimensions);
2960
- return;
2961
- }
2962
- this.updateCanvasDimensions(dimensions, this.zoom$.value);
2963
- this.dimensions$.next(dimensions);
2964
- this.onDimensionsChange();
2965
- };
2966
- /**
2967
- * @override
2968
- * @inheritDoc
2969
- */
2970
- AbstractInteractiveCanvas.prototype.setBaseDimensions = function (dimensions) {
2971
- this.baseDimensions = dimensions;
2972
- this.setDimensions(dimensions);
2973
- };
2974
- /**
2975
- * @override
2976
- * @inheritDoc
2977
- */
2978
- AbstractInteractiveCanvas.prototype.getDimensionChangeRejections$ = function () {
2979
- return this.dimensionChangeRejections$;
2980
- };
2981
- /**
2982
- * @override
2983
- * @inheritDoc
2984
- */
2985
- AbstractInteractiveCanvas.prototype.getDimensionsRestrictions = function () {
2986
- return this.dimensionRestrictions$.value;
2987
- };
2988
- /**
2989
- * @override
2990
- * @inheritDoc
2991
- */
2992
- AbstractInteractiveCanvas.prototype.getDimensionsRestrictions$ = function () {
2993
- return this.dimensionRestrictions$;
2994
- };
2995
- /**
2996
- * @override
2997
- * @inheritDoc
2998
- */
2999
- AbstractInteractiveCanvas.prototype.setDimensionRestrictions = function (restrictions) {
3000
- this.dimensionRestrictions$.next(restrictions);
3001
- var dimensions = this.getDimensions();
3002
- if (this.dimensionsExceedRestrictions(dimensions)) {
3003
- var newDimensions = dimensions
3004
- .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))));
3005
- this.setBaseDimensions(newDimensions);
3006
- }
3007
- };
3008
- AbstractInteractiveCanvas.prototype.getMargin = function () {
3009
- var borderWidth = this.getObjectsByTypeInternal(exports.ObjectType.BORDER)
3010
- .map(function (border) { return border.getStrokeWidth(); })
3011
- .reduce(function (current, next) { return Math.max(current, next); }, 0);
3012
- var borderPadding = this.getObjectsByTypeInternal(exports.ObjectType.BORDER)
3013
- .map(function (border) { return border.getPadding(); })
3014
- .reduce(function (current, next) {
3015
- return {
3016
- top: Math.max(current.top, next.top),
3017
- right: Math.max(current.right, next.right),
3018
- bottom: Math.max(current.bottom, next.bottom),
3019
- left: Math.max(current.left, next.left)
3020
- };
3021
- }, { top: 0, right: 0, bottom: 0, left: 0 });
3022
- var cutoffWidth = this.getObjectsByTypeInternal(exports.ObjectType.CUTOFF)
3023
- .map(function (border) { return border.getStrokeWidth(); })
3024
- .reduce(function (current, next) { return Math.max(current, next); }, 0);
3025
- // noinspection JSSuspiciousNameCombination
3026
- return {
3027
- top: borderWidth + borderPadding.top,
3028
- right: borderWidth + borderPadding.right,
3029
- bottom: Math.max(cutoffWidth, borderWidth) + borderPadding.bottom,
3030
- left: borderWidth + borderPadding.left
3031
- };
3032
- };
3033
- /**
3034
- * @override
3035
- * @inheritDoc
3036
- */
3037
- AbstractInteractiveCanvas.prototype.getZoom = function () {
3038
- return this.zoom$.value;
3039
- };
3040
- /**
3041
- * @override
3042
- * @inheritDoc
3043
- */
3044
- AbstractInteractiveCanvas.prototype.getZoom$ = function () {
3045
- return this.zoom$;
3046
- };
3047
- /**
3048
- * @override
3049
- * @inheritDoc
3050
- */
3051
- AbstractInteractiveCanvas.prototype.setZoom = function (zoom) {
3052
- this.updateCanvasDimensions(this.dimensions$.value, zoom);
3053
- this.zoom$.next(zoom);
3054
- };
3055
- /**
3056
- * @override
3057
- * @inheritDoc
3058
- */
3059
- AbstractInteractiveCanvas.prototype.getObjects = function () {
3060
- return this.objects$.value;
3061
- };
3062
- /**
3063
- * Get the list of objects directly.
3064
- */
3065
- AbstractInteractiveCanvas.prototype.getObjectsInternal = function () {
3066
- return this.objects$.value;
3067
- };
3068
- /**
3069
- * @override
3070
- * @inheritDoc
3071
- */
3072
- AbstractInteractiveCanvas.prototype.getObjectsByTypeInternal = function (type) {
3073
- return _(this.getObjectsInternal())
3074
- .filter(function (object) { return object.getType() === type; })
3075
- .value();
3076
- };
3077
- /**
3078
- * @override
3079
- * @inheritDoc
3080
- */
3081
- AbstractInteractiveCanvas.prototype.getObjectsByTypesInternal = function () {
3082
- var types = [];
3083
- for (var _i = 0; _i < arguments.length; _i++) {
3084
- types[_i] = arguments[_i];
3085
- }
3086
- return _(this.getObjectsInternal())
3087
- .filter(function (object) { return _(types).includes(object.getType()); })
3088
- .value();
3089
- };
3090
- /**
3091
- * @override
3092
- * @inheritDoc
3093
- */
3094
- AbstractInteractiveCanvas.prototype.getObjects$ = function () {
3095
- return this.objects$;
3096
- };
3097
- /**
3098
- * @override
3099
- * @inheritDoc
3100
- */
3101
- AbstractInteractiveCanvas.prototype.removeObject = function (target) {
3102
- var concreteObject = this.findFabricObject(target.getId());
3103
- this.fabricCanvas.remove(concreteObject);
3104
- this.objects$.next(_(this.objects$.value)
3105
- .filter(function (object) { return object.getId() !== target.getId(); })
3106
- .value());
3107
- this.onObjectListChange();
3108
- };
3109
- /**
3110
- * @override
3111
- * @inheritDoc
3112
- */
3113
- AbstractInteractiveCanvas.prototype.addImageObject = function (imageUrl) {
3114
- return __awaiter(this, void 0, void 0, function () {
3115
- var maxSizeScale, maxWidth, options;
3116
- var _this = this;
3117
- return __generator(this, function (_a) {
3118
- switch (_a.label) {
3119
- case 0:
3120
- maxSizeScale = 2;
3121
- maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3122
- options = { crossOrigin: "anonymous" };
3123
- return [4 /*yield*/, new Promise(function (resolve) {
3124
- _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
3125
- var id = _this.randomId();
3126
- var newScale = 1;
3127
- var curWidth = newScale * fabricImage.width;
3128
- if (curWidth > maxWidth) {
3129
- newScale = maxWidth / curWidth;
3130
- }
3131
- fabricImage.set({
3132
- id: id,
3133
- scaleX: newScale,
3134
- scaleY: newScale
3135
- });
3136
- _this.fabricCanvas.add(fabricImage);
3137
- var object = new Image(_this, fabricImage);
3138
- _this.trackNewObject(object);
3139
- _this.selectFabricObject(fabricImage);
3140
- _this.syncObjectList();
3141
- resolve(object);
3142
- }, options);
3143
- })];
3144
- case 1: return [2 /*return*/, _a.sent()];
3145
- }
3146
- });
3147
- });
3148
- };
3149
- /**
3150
- * @override
3151
- * @inheritDoc
3152
- */
3153
- AbstractInteractiveCanvas.prototype.addTextObject = function () {
3154
- return __awaiter(this, void 0, void 0, function () {
3155
- var profile, id, fontSizePx, canvasWidthPx, lowestTextPointPx, topPx, textOptions, fabricTextbox, object;
3156
- return __generator(this, function (_a) {
3157
- profile = this.profile$.value;
3158
- id = this.randomId();
3159
- fontSizePx = profile.defaultFontSize;
3160
- canvasWidthPx = this.getDimensions().widthPx;
3161
- lowestTextPointPx = this.getLowestTextPoint();
3162
- topPx = Math.min(lowestTextPointPx, this.getDimensions().heightPx - fontSizePx);
3163
- textOptions = {
3164
- id: id,
3165
- top: topPx,
3166
- left: 5,
3167
- width: canvasWidthPx - 10,
3168
- fill: profile.defaultFontColor,
3169
- fontSize: fontSizePx,
3170
- fontFamily: profile.defaultFont,
3171
- textAlign: profile.defaultTextAlignment,
3172
- multiLine: true,
3173
- lineHeight: profile.defaultLineHeight,
3174
- charSpacing: profile.defaultCharacterSpacing
3175
- };
3176
- fabricTextbox = new this.fabric.Textbox("Text goes here", textOptions);
3177
- this.fabricCanvas.add(fabricTextbox);
3178
- object = new Text(this, fabricTextbox);
3179
- this.trackNewObject(object);
3180
- this.selectFabricObject(fabricTextbox);
3181
- this.syncObjectList();
3182
- return [2 /*return*/, object];
3183
- });
3184
- });
3185
- };
3186
- /**
3187
- * @override
3188
- * @inheritDoc
3189
- */
3190
- AbstractInteractiveCanvas.prototype.addRichTextObject = function (imageUrl, htmlContent) {
3191
- return __awaiter(this, void 0, void 0, function () {
3192
- var maxSizeScale, maxWidth, options;
3193
- var _this = this;
3194
- return __generator(this, function (_a) {
3195
- switch (_a.label) {
3196
- case 0:
3197
- maxSizeScale = 2;
3198
- maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3199
- options = { crossOrigin: "anonymous" };
3200
- return [4 /*yield*/, new Promise(function (resolve) {
3201
- _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
3202
- var id = _this.randomId();
3203
- var newScale = 1;
3204
- var curWidth = newScale * fabricImage.width;
3205
- if (curWidth > maxWidth) {
3206
- newScale = maxWidth / curWidth;
3207
- }
3208
- fabricImage.set({
3209
- id: id,
3210
- scaleX: newScale,
3211
- scaleY: newScale
3212
- });
3213
- _this.fabricCanvas.add(fabricImage);
3214
- var object = new RichText(_this, fabricImage);
3215
- object.htmlContent = htmlContent;
3216
- _this.trackNewObject(object);
3217
- var margin = _this.getMargin();
3218
- object.setImageWidth(_this.dimensions$.value.widthPx - margin.left - margin.right);
3219
- _this.selectFabricObject(fabricImage);
3220
- _this.syncObjectList();
3221
- resolve(object);
3222
- }, options);
3223
- })];
3224
- case 1: return [2 /*return*/, _a.sent()];
3225
- }
3226
- });
3227
- });
3228
- };
3229
- /**
3230
- * @override
3231
- * @inheritDoc
3232
- */
3233
- AbstractInteractiveCanvas.prototype.addRectangleObject = function () {
3234
- return __awaiter(this, void 0, void 0, function () {
3235
- var canvasWidth, canvasHeight, rectWidth, rectHeight, rectangleOptions, fabricRect, object;
3236
- return __generator(this, function (_a) {
3237
- canvasWidth = this.getDimensions().widthPx;
3238
- canvasHeight = this.getDimensions().heightPx;
3239
- rectWidth = canvasWidth / 2;
3240
- rectHeight = canvasHeight / 2;
3241
- rectangleOptions = {
3242
- id: this.randomId(),
3243
- width: canvasWidth / 2,
3244
- height: 2,
3245
- top: canvasHeight / 2 - rectHeight / 2,
3246
- left: canvasWidth / 2 - rectWidth / 2,
3247
- fill: "#000000"
3248
- };
3249
- fabricRect = new this.fabric.Rect(rectangleOptions);
3250
- this.fabricCanvas.add(fabricRect);
3251
- object = new Rectangle(this, fabricRect);
3252
- this.trackNewObject(object);
3253
- this.selectFabricObject(fabricRect);
3254
- this.syncObjectList();
3255
- return [2 /*return*/, object];
3256
- });
3257
- });
3258
- };
3259
- /**
3260
- * @override
3261
- * @inheritDoc
3262
- */
3263
- AbstractInteractiveCanvas.prototype.addCutoffObject = function () {
3264
- return __awaiter(this, void 0, void 0, function () {
3265
- var profile, initialThickness, canvasWidth, canvasHeight, rectangleOptions, fabricRect, object;
3266
- return __generator(this, function (_a) {
3267
- profile = this.profile$.value;
3268
- initialThickness = profile.defaultCutoffWidth;
3269
- canvasWidth = this.getDimensions().widthPx;
3270
- canvasHeight = this.getDimensions().heightPx;
3271
- rectangleOptions = {
3272
- id: this.randomId(),
3273
- width: canvasWidth,
3274
- height: initialThickness,
3275
- top: canvasHeight - initialThickness,
3276
- left: 0,
3277
- fill: profile.defaultCutoffColor
3278
- };
3279
- fabricRect = new this.fabric.Rect(rectangleOptions);
3280
- this.fabricCanvas.add(fabricRect);
3281
- object = new Cutoff(this, fabricRect);
3282
- this.trackNewObject(object);
3283
- this.selectFabricObject(fabricRect);
3284
- this.syncObjectList();
3285
- return [2 /*return*/, object];
3286
- });
3287
- });
3288
- };
3289
- /**
3290
- * @override
3291
- * @inheritDoc
3292
- */
3293
- AbstractInteractiveCanvas.prototype.addBorderObject = function () {
3294
- return __awaiter(this, void 0, void 0, function () {
3295
- var profile, initialThickness, rectangleOptions, fabricRect, object;
3296
- return __generator(this, function (_a) {
3297
- profile = this.profile$.value;
3298
- initialThickness = profile.defaultBorderWidth;
3299
- rectangleOptions = {
3300
- id: this.randomId(),
3301
- fill: null,
3302
- stroke: profile.defaultBorderColor,
3303
- strokeWidth: initialThickness
3304
- };
3305
- fabricRect = new this.fabric.Rect(rectangleOptions);
3306
- this.fabricCanvas.add(fabricRect);
3307
- object = new Border(this, fabricRect);
3308
- this.trackNewObject(object);
3309
- this.selectFabricObject(fabricRect);
3310
- this.syncObjectList();
3311
- return [2 /*return*/, object];
3312
- });
3313
- });
3314
- };
3315
- /**
3316
- * @override
3317
- * @inheritDoc
3318
- */
3319
- AbstractInteractiveCanvas.prototype.addVerticalRuleObject = function () {
3320
- return __awaiter(this, void 0, void 0, function () {
3321
- var rectOptions, fabricRect, object;
3322
- return __generator(this, function (_a) {
3323
- rectOptions = {
3324
- id: this.randomId(),
3325
- width: 1,
3326
- height: 10,
3327
- top: 0,
3328
- left: 0,
3329
- fill: "#000000"
3330
- };
3331
- fabricRect = new this.fabric.Rect(rectOptions);
3332
- this.fabricCanvas.add(fabricRect);
3333
- object = new VerticalRule(this, fabricRect);
3334
- this.trackNewObject(object);
3335
- this.selectFabricObject(fabricRect);
3336
- this.syncObjectList();
3337
- return [2 /*return*/, object];
3338
- });
3339
- });
3340
- };
3341
- /**
3342
- * @override
3343
- * @inheritDoc
3344
- */
3345
- AbstractInteractiveCanvas.prototype.addHorizontalRuleObject = function () {
3346
- return __awaiter(this, void 0, void 0, function () {
3347
- var rectOptions, fabricRect, object;
3348
- return __generator(this, function (_a) {
3349
- rectOptions = {
3350
- id: this.randomId(),
3351
- width: 10,
3352
- height: 1,
3353
- top: 0,
3354
- left: 0,
3355
- fill: "#000000"
3356
- };
3357
- fabricRect = new this.fabric.Rect(rectOptions);
3358
- this.fabricCanvas.add(fabricRect);
3359
- object = new HorizontalRule(this, fabricRect);
3360
- this.trackNewObject(object);
3361
- this.selectFabricObject(fabricRect);
3362
- this.syncObjectList();
3363
- return [2 /*return*/, object];
3364
- });
3365
- });
3366
- };
3367
- /**
3368
- * @override
3369
- * @inheritDoc
3370
- */
3371
- AbstractInteractiveCanvas.prototype.addBackgroundImageObject = function (imageUrl) {
3372
- return __awaiter(this, void 0, void 0, function () {
3373
- var options;
3374
- var _this = this;
3375
- return __generator(this, function (_a) {
3376
- switch (_a.label) {
3377
- case 0:
3378
- options = { crossOrigin: "anonymous" };
3379
- return [4 /*yield*/, new Promise(function (resolve) {
3380
- _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
3381
- var id = _this.randomId();
3382
- fabricImage.set({
3383
- id: id,
3384
- });
3385
- _this.fabricCanvas.add(fabricImage);
3386
- var object = new BackgroundImage(_this, fabricImage);
3387
- _this.trackNewObject(object);
3388
- _this.selectFabricObject(fabricImage);
3389
- _this.syncObjectList();
3390
- resolve(object);
3391
- }, options);
3392
- })];
3393
- case 1: return [2 /*return*/, _a.sent()];
3394
- }
3395
- });
3396
- });
3397
- };
3398
- /**
3399
- * @override
3400
- * @inheritDoc
3401
- */
3402
- AbstractInteractiveCanvas.prototype.setPrefillData = function (data) {
3403
- _(this.getObjectsByTypeInternal(exports.ObjectType.TEXT)).each(function (object) {
3404
- var prefillType = object.getPrefillType();
3405
- if (prefillType !== undefined) {
3406
- object.setText(data[prefillType] || "");
3407
- }
3408
- });
3409
- };
3410
- /**
3411
- * @override
3412
- * @inheritDoc
3413
- */
3414
- AbstractInteractiveCanvas.prototype.distributeObjectsVertically = function () {
3415
- var margin = this.getMargin();
3416
- var canvasHeight = this.getDimensions().heightPx;
3417
- var objectsToLayout = _(this.getObjectsByTypesInternal(exports.ObjectType.TEXT, exports.ObjectType.IMAGE))
3418
- .sortBy(function (x) { return x.getBoundingBox().top; })
3419
- .value();
3420
- var objectsHeight = _.reduce(objectsToLayout, function (total, element) { return total + element.getBoundingBox().height; }, 0);
3421
- var workableHeight = canvasHeight - margin.top - margin.bottom;
3422
- var requiredPadding = workableHeight - objectsHeight;
3423
- var spaceBetween = requiredPadding / (objectsToLayout.length - 1);
3424
- var nextElementTop = margin.top;
3425
- for (var _i = 0, objectsToLayout_1 = objectsToLayout; _i < objectsToLayout_1.length; _i++) {
3426
- var element = objectsToLayout_1[_i];
3427
- var elementHeight = element.getBoundingBox().height;
3428
- element.setPosition({
3429
- top: nextElementTop
3430
- });
3431
- element.setCoords();
3432
- nextElementTop += elementHeight + spaceBetween;
3433
- }
3434
- this.redraw();
3435
- };
3436
- /**
3437
- * @override
3438
- * @inheritDoc
3439
- */
3440
- AbstractInteractiveCanvas.prototype.getSelection = function () {
3441
- return this.selection$.value;
3442
- };
3443
- /**
3444
- * @override
3445
- * @inheritDoc
3446
- */
3447
- AbstractInteractiveCanvas.prototype.getSelection$ = function () {
3448
- return this.selection$;
3449
- };
3450
- /**
3451
- * @override
3452
- * @inheritDoc
3453
- */
3454
- AbstractInteractiveCanvas.prototype.select = function (object) {
3455
- var concreteField = this.findObject(object.getId());
3456
- if (!concreteField) {
3457
- console.error("Attempted to select unknown object", object);
3458
- return;
3459
- }
3460
- this.selectFabricObject(concreteField.getFabricObjectInternal());
3461
- this.fabricCanvas.renderAll();
3462
- };
3463
- /**
3464
- * @override
3465
- * @inheritDoc
3466
- */
3467
- AbstractInteractiveCanvas.prototype.clearSelection = function () {
3468
- this.fabricCanvas.discardActiveObject();
3469
- this.fabricCanvas.renderAll();
3470
- };
3471
- AbstractInteractiveCanvas.prototype.setMultiSelect = function (value) {
3472
- this.fabricCanvas.selection = value;
3473
- this.fabricCanvas.selectionKey = value ? "shiftKey" : null;
3474
- };
3475
- /**
3476
- * @override
3477
- * @inheritDoc
3478
- */
3479
- AbstractInteractiveCanvas.prototype.clear = function () {
3480
- this.fabricCanvas.clear();
3481
- this.setBackgroundColor("#FFFFFF");
3482
- this.objects$.next([]);
3483
- };
3484
- /**
3485
- * @override
3486
- * @inheritDoc
3487
- */
3488
- AbstractInteractiveCanvas.prototype.save = function () {
3489
- var objects = _(this.objects$.value)
3490
- .map(function (x) { return x.persist(); })
3491
- .value();
3492
- var includedProperties = ["id", "_isSlaveObject", "_isOrderedList", "_orderedListStart"];
3493
- var json = this.fabricCanvas.toJSON(includedProperties);
3494
- return {
3495
- dimensions: this.dimensions$.value,
3496
- canvasJson: JSON.stringify(json),
3497
- objects: objects,
3498
- backgroundColor: this.backgroundColor$.value
3499
- };
3500
- };
3501
- /**
3502
- * @override
3503
- * @inheritDoc
3504
- */
3505
- AbstractInteractiveCanvas.prototype.load = function (canvasState) {
3506
- return __awaiter(this, void 0, void 0, function () {
3507
- var json_1;
3508
- var _this = this;
3509
- return __generator(this, function (_a) {
3510
- switch (_a.label) {
3511
- case 0:
3512
- this.setBaseDimensions(canvasState.dimensions);
3513
- if (!(canvasState.canvasJson != undefined)) return [3 /*break*/, 2];
3514
- json_1 = JSON.parse(canvasState.canvasJson);
3515
- return [4 /*yield*/, new Promise(function (resolve) {
3516
- _this.fabricCanvas.loadFromJSON(json_1, function () { return resolve(); });
3517
- })];
3518
- case 1:
3519
- _a.sent();
3520
- _a.label = 2;
3521
- case 2:
3522
- if (canvasState.backgroundColor)
3523
- this.setBackgroundColor(canvasState.backgroundColor);
3524
- this.backgroundColor$.next(this.fabricCanvas.backgroundColor);
3525
- this.bindFields(canvasState.objects);
3526
- this.removePersistedBullets();
3527
- this.removeSlaveObjects();
3528
- this.layoutManager$.value.onInit();
3529
- return [2 /*return*/];
3530
- }
3531
- });
3532
- });
3533
- };
3534
- /**
3535
- * 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
3536
- * 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
3537
- * deletes all circles on canvas load.
3538
- */
3539
- AbstractInteractiveCanvas.prototype.removePersistedBullets = function () {
3540
- var _this = this;
3541
- var objects = this.fabricCanvas.getObjects();
3542
- _.each(objects, function (object) {
3543
- if (object.type === "circle")
3544
- _this.removeFabricObject(object);
3545
- });
3546
- };
3547
- /**
3548
- * Remove any slave objects persisted to the canvas; their master objects will recreate them.
3549
- */
3550
- AbstractInteractiveCanvas.prototype.removeSlaveObjects = function () {
3551
- var _this = this;
3552
- var objects = this.fabricCanvas.getObjects();
3553
- _.each(objects, function (object) {
3554
- if (object._isSlaveObject)
3555
- _this.removeFabricObject(object);
3556
- });
3557
- };
3558
- /**
3559
- * @override
3560
- * @inheritDoc
3561
- */
3562
- AbstractInteractiveCanvas.prototype.toSvg = function () {
3563
- var _this = this;
3564
- var widthInMm = _.round(this.dimensions$.value.widthMm, 1) + "mm";
3565
- var heightInMm = _.round(this.dimensions$.value.heightMm, 1) + "mm";
3566
- var options = {
3567
- width: widthInMm,
3568
- height: heightInMm,
3569
- suppressPreamble: true,
3570
- encoding: null
3571
- };
3572
- var svg = this.executeWithNormalizedCanvas(function () { return _this.fabricCanvas.toSVG(options); });
3573
- this.redraw();
3574
- return svg;
3575
- };
3576
- /**
3577
- * @override
3578
- * @inheritDoc
3579
- */
3580
- AbstractInteractiveCanvas.prototype.toImage = function (multiplier) {
3581
- var _this = this;
3582
- console.log("Multiplier in interactive canvas : %s", multiplier);
3583
- var activeObject = this.fabricCanvas.getActiveObject();
3584
- this.fabricCanvas.discardActiveObject().renderAll();
3585
- var options = {
3586
- format: "png",
3587
- multiplier: multiplier || this.IMAGE_CAPTURE_COEFFICIENT,
3588
- enableRetinaScaling: true
3589
- };
3590
- var base64 = this.executeWithNormalizedCanvas(function () { return _this.fabricCanvas.toDataURL(options); });
3591
- if (activeObject) {
3592
- this.fabricCanvas.setActiveObject(activeObject);
3593
- }
3594
- return _.replace(base64, /^data:.*;base64,/, "");
3595
- };
3596
- /**
3597
- * @override
3598
- * @inheritDoc
3599
- */
3600
- AbstractInteractiveCanvas.prototype.createPNGStream = function () {
3601
- return this.fabricCanvas.createPNGStream();
3602
- };
3603
- /**
3604
- * @override
3605
- * @inheritDoc
3606
- */
3607
- AbstractInteractiveCanvas.prototype.registerUndoState = function () {
3608
- throw Error("Undo not supported on this canvas type");
3609
- };
3610
- /**
3611
- * @override
3612
- * @inheritDoc
3613
- */
3614
- AbstractInteractiveCanvas.prototype.undo = function () {
3615
- throw Error("Undo not supported on this canvas type");
3616
- };
3617
- /**
3618
- * @override
3619
- * @inheritDoc
3620
- */
3621
- AbstractInteractiveCanvas.prototype.redo = function () {
3622
- throw Error("Redo not supported on this canvas type");
3623
- };
3624
- /**
3625
- * Redraw the canvas.
3626
- */
3627
- AbstractInteractiveCanvas.prototype.redraw = function () {
3628
- this.fabricCanvas.renderAll();
3629
- };
3630
- AbstractInteractiveCanvas.prototype.syncObjectList = function () {
3631
- var _this = this;
3632
- var newObjectList = _(this.fabricCanvas.getObjects())
3633
- .map(function (obj) { return _this.findObject(obj.id); })
3634
- .filter(function (x) { return x !== undefined; })
3635
- .value();
3636
- this.objects$.next(newObjectList);
3637
- this.onObjectListChange();
3638
- };
3639
- /**
3640
- * @override
3641
- * @inheritDoc
3642
- */
3643
- AbstractInteractiveCanvas.prototype.notifyObjectListChanged = function () {
3644
- this.onObjectListChange();
3645
- };
3646
- /**
3647
- * @override
3648
- * @inheritDoc
3649
- */
3650
- AbstractInteractiveCanvas.prototype.notifyTextChanged = function () {
3651
- this.onTextChange();
3652
- };
3653
- /**
3654
- * @override
3655
- * @inheritDoc
3656
- */
3657
- AbstractInteractiveCanvas.prototype.notifyMarginChanged = function () {
3658
- this.onMarginChange();
3659
- };
3660
- AbstractInteractiveCanvas.prototype.removeFabricObject = function (obj) {
3661
- this.fabricCanvas.remove(obj);
3662
- };
3663
- AbstractInteractiveCanvas.prototype.selectFabricObject = function (object) {
3664
- this.fabricCanvas.setActiveObject(object);
3665
- };
3666
- AbstractInteractiveCanvas.prototype.onTextChange = function () {
3667
- this.layoutManager$.value.onTextChange();
3668
- };
3669
- AbstractInteractiveCanvas.prototype.onObjectListChange = function () {
3670
- this.layoutManager$.value.onObjectListChange();
3671
- };
3672
- AbstractInteractiveCanvas.prototype.onDimensionsChange = function () {
3673
- this.layoutManager$.value.onCanvasDimensionsChange();
3674
- };
3675
- AbstractInteractiveCanvas.prototype.onMarginChange = function () {
3676
- this.layoutManager$.value.onMarginChange();
3677
- };
3678
- AbstractInteractiveCanvas.prototype.bindFields = function (persistedFields) {
3679
- var _this = this;
3680
- var bound = _(persistedFields)
3681
- .flatMap(function (field) {
3682
- try {
3683
- return [_this.bindField(field)];
3684
- }
3685
- catch (e) {
3686
- console.error("Could not bind field", e, field);
3687
- return []; //skip
3688
- }
3689
- })
3690
- .value();
3691
- this.objects$.next(bound);
3692
- };
3693
- AbstractInteractiveCanvas.prototype.bindField = function (persistedField) {
3694
- var fabricObject = this.findFabricObject(persistedField.id);
3695
- switch (persistedField.type) {
3696
- case exports.ObjectType.TEXT:
3697
- return new Text(this, fabricObject, persistedField);
3698
- case exports.ObjectType.RECTANGLE:
3699
- return new Rectangle(this, fabricObject, persistedField);
3700
- case exports.ObjectType.BORDER:
3701
- return new Border(this, fabricObject, persistedField);
3702
- case exports.ObjectType.CUTOFF:
3703
- return new Cutoff(this, fabricObject, persistedField);
3704
- case exports.ObjectType.IMAGE:
3705
- case exports.ObjectType.LIBRARY_IMAGE:
3706
- return new Image(this, fabricObject, persistedField);
3707
- case exports.ObjectType.HORIZONTAL_RULE:
3708
- return new HorizontalRule(this, fabricObject, persistedField);
3709
- case exports.ObjectType.VERTICAL_RULE:
3710
- return new VerticalRule(this, fabricObject, persistedField);
3711
- case exports.ObjectType.RICH_TEXT:
3712
- return new RichText(this, fabricObject, persistedField);
3713
- case exports.ObjectType.BACKGROUND_IMAGE:
3714
- return new BackgroundImage(this, fabricObject, persistedField);
3715
- default:
3716
- throw new Error("Unknown field type: [" + persistedField.type + "]");
3717
- }
3718
- };
3719
- AbstractInteractiveCanvas.prototype.findFabricObject = function (id) {
3720
- var objects = this.fabricCanvas.getObjects();
3721
- var fabricObject = _.find(objects, function (o) { return o.id == id; });
3722
- if (!fabricObject) {
3723
- throw new Error("Cannot find fabric object with id [" + id + "]");
3724
- }
3725
- return fabricObject;
3726
- };
3727
- AbstractInteractiveCanvas.prototype.findObject = function (id) {
3728
- //Don't use strict compare here. fabric ids are numbers while ad builder ids are strings... mostly.
3729
- return _.find(this.objects$.value, function (object) { return object.getId() == id; });
3730
- };
3731
- /**
3732
- * Reset zoom and canvas dimensions to 1:1 scale, execute a function, then return the canvas to
3733
- * the previous values.
3734
- * @param func Function to execute.
3735
- */
3736
- AbstractInteractiveCanvas.prototype.executeWithNormalizedCanvas = function (func) {
3737
- this.updateCanvasDimensions(this.dimensions$.value, 1);
3738
- var response;
3739
- try {
3740
- response = func();
3741
- }
3742
- finally {
3743
- this.updateCanvasDimensions(this.dimensions$.value, this.zoom$.value);
3744
- }
3745
- return response;
3746
- };
3747
- /**
3748
- * Update the actual dimensions of the canvas by multiplying the requested dimensions by the current zoom factor.
3749
- */
3750
- AbstractInteractiveCanvas.prototype.updateCanvasDimensions = function (dims, zoom) {
3751
- var width = dims.widthPx * zoom;
3752
- var height = dims.heightPx * zoom;
3753
- this.fabricCanvas.setZoom(zoom);
3754
- this.fabricCanvas.setDimensions({
3755
- width: width,
3756
- height: height
3757
- });
3758
- };
3759
- AbstractInteractiveCanvas.prototype.onProfileChanged = function (profile) {
3760
- this.setMultiSelect(profile.allowMultiSelect);
3761
- this.layoutManager$.value.onProfileChange(profile);
3762
- };
3763
- AbstractInteractiveCanvas.prototype.getFieldByCanvasObject = function (object) {
3764
- if (!object) {
3765
- return null;
3766
- }
3767
- return _.find(this.objects$.value, function (field) { return field.getId() == object.id; });
3768
- };
3769
- AbstractInteractiveCanvas.prototype.getFieldsByCanvasObject = function (objects) {
3770
- var _this = this;
3771
- return _(objects)
3772
- .map(function (obj) { return _this.getFieldByCanvasObject(obj); })
3773
- .reject(function (x) { return !x; })
3774
- .value();
3775
- };
3776
- AbstractInteractiveCanvas.prototype.calculateImageCaptureCoefficient = function () {
3777
- var precision = 3;
3778
- var devicePixelRatio = this.devicePixelRatio || 1;
3779
- return _.round(2 / devicePixelRatio, precision);
3780
- };
3781
- AbstractInteractiveCanvas.prototype.randomId = function () {
3782
- var min = 0;
3783
- var max = 999;
3784
- var id = Math.floor(Math.random() * (max - min)) + min;
3785
- return (new Date()).getTime() + "" + id;
3786
- };
3787
- AbstractInteractiveCanvas.prototype.getLowestTextPoint = function () {
3788
- return _(this.objects$.value)
3789
- .filter(function (object) { return object.getType() === exports.ObjectType.TEXT; })
3790
- .map(function (text) { return text.getDimensions().top.px + text.getDimensions().height.px; })
3791
- .reduce(function (x, y) { return Math.max(x, y); }, 0);
3792
- };
3793
- AbstractInteractiveCanvas.prototype.trackNewObject = function (object) {
3794
- this.objects$.next(__spreadArrays(this.objects$.value, [object]));
3795
- };
3796
- AbstractInteractiveCanvas.prototype.dimensionsExceedRestrictions = function (dimensions) {
3797
- var restrictions = this.dimensionRestrictions$.value;
3798
- return tsOptchain.oc(restrictions).height.min() != undefined && dimensions.height < tsOptchain.oc(restrictions).height.min()
3799
- || tsOptchain.oc(restrictions).height.max() != undefined && dimensions.height > tsOptchain.oc(restrictions).height.max()
3800
- || tsOptchain.oc(restrictions).width.min() != undefined && dimensions.width < tsOptchain.oc(restrictions).width.min()
3801
- || tsOptchain.oc(restrictions).width.max() != undefined && dimensions.width > tsOptchain.oc(restrictions).width.max()
3802
- || tsOptchain.oc(restrictions).volume.min() != undefined && dimensions.height * dimensions.width < tsOptchain.oc(restrictions).volume.min()
3803
- || tsOptchain.oc(restrictions).volume.max() != undefined && dimensions.height * dimensions.width > tsOptchain.oc(restrictions).volume.max();
3804
- };
3805
- return AbstractInteractiveCanvas;
3806
- }());
3807
- var SelectionData = /** @class */ (function () {
3808
- function SelectionData(objects) {
3809
- this.objects = objects;
3810
- this.empty = this.objects.length < 1;
3811
- var selectedObject = this.objects[0];
3812
- this.isText = selectedObject
3813
- && selectedObject.getType() === exports.ObjectType.TEXT
3814
- && selectedObject.isEditing();
3815
- }
3816
- return SelectionData;
3817
- }());
3818
-
3819
- var HeadlessInteractiveCanvas = /** @class */ (function (_super) {
3820
- __extends(HeadlessInteractiveCanvas, _super);
3821
- function HeadlessInteractiveCanvas() {
3822
- var _this = _super.call(this, fabric.fabric, new fabric.fabric.StaticCanvas(null, { width: 640, height: 480 }), null) || this;
3823
- _this.headless = true;
3824
- return _this;
3825
- }
3826
- /**
3827
- * @override
3828
- */
3829
- HeadlessInteractiveCanvas.prototype.selectFabricObject = function (object) {
3830
- //no-op
3831
- };
3832
- return HeadlessInteractiveCanvas;
3833
- }(AbstractInteractiveCanvas));
3834
-
3835
- var UndoStack = /** @class */ (function () {
3836
- function UndoStack(canvas, size) {
3837
- if (size === void 0) { size = 100; }
3838
- this.canvas = canvas;
3839
- this.undoStates = [];
3840
- this.redoStates = [];
3841
- this.lastState = null;
3842
- this.size = size;
3843
- }
3844
- /**
3845
- * @override
3846
- * @inheritDoc
3847
- */
3848
- UndoStack.prototype.save = function (state) {
3849
- var currentState = this.canvas.save();
3850
- var undoState = this.undoStates[this.undoStates.length - 1];
3851
- if (_.isEqual(undoState, state))
3852
- return;
3853
- this.undoStates.push(state);
3854
- if (this.undoStates.length > this.size)
3855
- this.undoStates.splice(0, 1);
3856
- this.clearRedoStackIfCanvasChanged(currentState);
3857
- };
3858
- /**
3859
- * @override
3860
- * @inheritDoc
3861
- */
3862
- UndoStack.prototype.undo = function () {
3863
- return __awaiter(this, void 0, void 0, function () {
3864
- var undoState, currentState;
3865
- return __generator(this, function (_a) {
3866
- switch (_a.label) {
3867
- case 0:
3868
- undoState = this.undoStates.pop();
3869
- if (!undoState)
3870
- return [2 /*return*/];
3871
- currentState = this.canvas.save();
3872
- if (_.isEqual(undoState, currentState)) {
3873
- undoState = this.undoStates.pop();
3874
- if (!undoState)
3875
- return [2 /*return*/];
3876
- }
3877
- this.lastState = undoState;
3878
- this.redoStates.push(currentState);
3879
- return [4 /*yield*/, this.canvas.load(undoState)];
3880
- case 1:
3881
- _a.sent();
3882
- return [2 /*return*/];
3883
- }
3884
- });
3885
- });
3886
- };
3887
- /**
3888
- * @override
3889
- * @inheritDoc
3890
- */
3891
- UndoStack.prototype.redo = function () {
3892
- return __awaiter(this, void 0, void 0, function () {
3893
- var redoState, currentState;
3894
- return __generator(this, function (_a) {
3895
- switch (_a.label) {
3896
- case 0:
3897
- redoState = this.redoStates.pop();
3898
- if (!redoState)
3899
- return [2 /*return*/];
3900
- currentState = this.canvas.save();
3901
- this.lastState = redoState;
3902
- this.undoStates.push(currentState);
3903
- return [4 /*yield*/, this.canvas.load(redoState)];
3904
- case 1:
3905
- _a.sent();
3906
- return [2 /*return*/];
3907
- }
3908
- });
3909
- });
3910
- };
3911
- UndoStack.prototype.clearRedoStackIfCanvasChanged = function (currentState) {
3912
- if (_.isEqual(this.lastState, currentState))
3913
- return;
3914
- this.redoStates = [];
3915
- };
3916
- return UndoStack;
3917
- }());
3918
-
3919
- var InteractiveCanvas = /** @class */ (function (_super) {
3920
- __extends(InteractiveCanvas, _super);
3921
- function InteractiveCanvas(canvas, devicePixelRatio) {
3922
- var _this = _super.call(this, window['fabric'], new window['fabric'].Canvas(canvas), devicePixelRatio) || this;
3923
- _this.devicePixelRatio = devicePixelRatio;
3924
- _this.headless = false;
3925
- _this.undoStack = new UndoStack(_this);
3926
- //Initialize watchers
3927
- _this.fabricCanvas.on("selection:updated", function () { return _this.onFabricSelectionChanged(); });
3928
- _this.fabricCanvas.on("selection:created", function () { return _this.onFabricSelectionChanged(); });
3929
- _this.fabricCanvas.on("selection:cleared", function () { return _this.onFabricSelectionChanged(); });
3930
- _this.fabricCanvas.on("text:editing:entered", function () { return _this.onTextEditing(); });
3931
- _this.fabricCanvas.on("text:editing:exited", function () { return _this.onTextEditing(); });
3932
- _this.fabricCanvas.on("text:changed", function () { return _this.onTextChange(); });
3933
- return _this;
3934
- }
3935
- /**
3936
- * @override
3937
- * @inheritDoc
3938
- */
3939
- InteractiveCanvas.prototype.registerUndoState = function () {
3940
- this.undoStack.save(this.save());
3941
- };
3942
- /**
3943
- * @override
3944
- * @inheritDoc
3945
- */
3946
- InteractiveCanvas.prototype.undo = function () {
3947
- return __awaiter(this, void 0, void 0, function () {
3948
- return __generator(this, function (_a) {
3949
- switch (_a.label) {
3950
- case 0: return [4 /*yield*/, this.undoStack.undo()];
3951
- case 1:
3952
- _a.sent();
3953
- return [2 /*return*/];
3954
- }
3955
- });
3956
- });
3957
- };
3958
- /**
3959
- * @override
3960
- * @inheritDoc
3961
- */
3962
- InteractiveCanvas.prototype.redo = function () {
3963
- return __awaiter(this, void 0, void 0, function () {
3964
- return __generator(this, function (_a) {
3965
- switch (_a.label) {
3966
- case 0: return [4 /*yield*/, this.undoStack.redo()];
3967
- case 1:
3968
- _a.sent();
3969
- return [2 /*return*/];
3970
- }
3971
- });
3972
- });
3973
- };
3974
- InteractiveCanvas.prototype.onFabricSelectionChanged = function () {
3975
- var selected = this.getFieldsByCanvasObject(this.fabricCanvas.getActiveObjects());
3976
- this.selection$.next(new SelectionData(selected));
3977
- this.enterTextSelectionModeIfRequired();
3978
- };
3979
- InteractiveCanvas.prototype.enterTextSelectionModeIfRequired = function () {
3980
- if (!this.profile$.value.enterTextEditImmediatelyOnSelect && !this.layoutManager$.value.enterTextEditImmediatelyOnSelect)
3981
- return;
3982
- if (!this.selection$.value || !this.selection$.value.objects[0] || this.selection$.value.objects[0].getType() !== exports.ObjectType.TEXT)
3983
- return;
3984
- var text = this.selection$.value.objects[0];
3985
- text.enterEditing();
3986
- };
3987
- InteractiveCanvas.prototype.onTextEditing = function () {
3988
- var selected = this.getFieldsByCanvasObject(this.fabricCanvas.getActiveObjects());
3989
- this.selection$.next(new SelectionData(selected));
3990
- };
3991
- return InteractiveCanvas;
3992
- }(AbstractInteractiveCanvas));
3993
-
3994
- exports.AbstractCanvasObject = AbstractCanvasObject;
3995
- exports.AbstractInteractiveCanvas = AbstractInteractiveCanvas;
3996
- exports.AbstractLayoutManager = AbstractLayoutManager;
3997
- exports.AdvancedLayoutManager = AdvancedLayoutManager;
3998
- exports.BackgroundImage = BackgroundImage;
3999
- exports.Border = Border;
4000
- exports.CanvasObjectData = CanvasObjectData;
4001
- exports.Cutoff = Cutoff;
4002
- exports.DefaultCanvasProfile = DefaultCanvasProfile;
4003
- exports.Font = Font;
4004
- exports.FontLibrary = FontLibrary;
4005
- exports.HeadlessInteractiveCanvas = HeadlessInteractiveCanvas;
4006
- exports.HorizontalRule = HorizontalRule;
4007
- exports.Image = Image;
4008
- exports.InteractiveCanvas = InteractiveCanvas;
4009
- exports.MillimeterDimensions = MillimeterDimensions;
4010
- exports.PixelCanvasDimensions = PixelCanvasDimensions;
4011
- exports.Print6ColCanvasDimensions = Print6ColCanvasDimensions;
4012
- exports.Print8ColCanvasDimensions = Print8ColCanvasDimensions;
4013
- exports.Rectangle = Rectangle;
4014
- exports.RichText = RichText;
4015
- exports.SelectionData = SelectionData;
4016
- exports.StandardLayoutManager = StandardLayoutManager;
4017
- exports.Text = Text;
4018
- exports.UNKNOWN_FONT = UNKNOWN_FONT;
4019
- exports.UnknownFont = UnknownFont;
4020
- exports.VerticalRule = VerticalRule;
4021
- exports.isMutableBackgroundColor = isMutableBackgroundColor;
4022
- exports.isMutableColor = isMutableColor;
4023
- exports.isMutableImage = isMutableImage;
4024
- exports.isMutablePosition = isMutablePosition;
4025
- exports.isMutableStroke = isMutableStroke;
4026
- exports.isMutableText = isMutableText;
4027
- exports.isMutableTextStyle = isMutableTextStyle;