@leapfrog/interactive-canvas 1.0.0 → 1.0.2

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