@leapfrog/interactive-canvas 1.7.0 → 1.8.1

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.
@@ -21,6 +21,7 @@ import { ISelectionData } from "./types/selection-data.interface";
21
21
  import { ICanvasState } from "./types/canvas-state.interface";
22
22
  import { ITextSelection } from "./mutator/mutable-text-style.interface";
23
23
  import { fabric } from "fabric";
24
+ import { IRichText } from "./object/impl/rich-text";
24
25
  export declare abstract class AbstractInteractiveCanvas implements IInteractiveCanvas {
25
26
  fabric: any;
26
27
  protected readonly fabricCanvas: fabric.Canvas;
@@ -188,6 +189,11 @@ export declare abstract class AbstractInteractiveCanvas implements IInteractiveC
188
189
  * @inheritDoc
189
190
  */
190
191
  addTextObject(): Promise<IText>;
192
+ /**
193
+ * @override
194
+ * @inheritDoc
195
+ */
196
+ addRichTextObject(imageUrl: string, htmlContent: string): Promise<IRichText>;
191
197
  /**
192
198
  * @override
193
199
  * @inheritDoc
package/dist/index.d.ts CHANGED
@@ -18,13 +18,14 @@ export * from "./mutator/mutable-position.interface";
18
18
  export * from "./mutator/mutable-text.interface";
19
19
  export * from "./mutator/mutable-text-style.interface";
20
20
  export * from "./object/abstract-canvas-object";
21
- export * from "./object/canvas-object.interface";
22
21
  export * from "./object/canvas-object-data";
22
+ export * from "./object/canvas-object.interface";
23
23
  export * from "./object/impl/border";
24
24
  export * from "./object/impl/cutoff";
25
25
  export * from "./object/impl/horizontal-rule";
26
26
  export * from "./object/impl/image";
27
27
  export * from "./object/impl/rectangle";
28
+ export * from "./object/impl/rich-text";
28
29
  export * from "./object/impl/text";
29
30
  export * from "./object/impl/vertical-rule";
30
31
  export * from "./profile/canvas-profile.interface";
@@ -391,7 +391,7 @@ var StackObjectsLayoutFunction = /** @class */ (function () {
391
391
  StackObjectsLayoutFunction.prototype.execute = function (canvas) {
392
392
  var canvasMargin = canvas.getMargin();
393
393
  var canvasDimensions = canvas.getDimensions();
394
- var allObjects = canvas.getObjectsByTypesInternal(ObjectType.TEXT, ObjectType.IMAGE, ObjectType.RECTANGLE, ObjectType.VERTICAL_RULE, ObjectType.HORIZONTAL_RULE);
394
+ var allObjects = canvas.getObjectsByTypesInternal(ObjectType.TEXT, ObjectType.RICH_TEXT, ObjectType.IMAGE, ObjectType.RECTANGLE, ObjectType.VERTICAL_RULE, ObjectType.HORIZONTAL_RULE);
395
395
  var objects = _(allObjects)
396
396
  .partition(function (object) { return object.isPinToBottom(); })
397
397
  .value();
@@ -1660,6 +1660,57 @@ var Rectangle = /** @class */ (function (_super) {
1660
1660
  return Rectangle;
1661
1661
  }(AbstractCanvasObject));
1662
1662
 
1663
+ var RichText = /** @class */ (function (_super) {
1664
+ __extends(RichText, _super);
1665
+ function RichText(canvas, fabricObject, persistedField) {
1666
+ var _this = _super.call(this, canvas, fabricObject, persistedField) || this;
1667
+ _this.data.type = ObjectType.RICH_TEXT;
1668
+ return _this;
1669
+ }
1670
+ Object.defineProperty(RichText.prototype, "htmlContent", {
1671
+ /**
1672
+ * @override
1673
+ * @inheritDoc
1674
+ */
1675
+ get: function () {
1676
+ return this.data.userText;
1677
+ },
1678
+ /**
1679
+ * @override
1680
+ * @inheritDoc
1681
+ */
1682
+ set: function (text) {
1683
+ this.data.userText = text;
1684
+ },
1685
+ enumerable: false,
1686
+ configurable: true
1687
+ });
1688
+ /**
1689
+ * @override
1690
+ * @inheritDoc
1691
+ */
1692
+ RichText.prototype.setRichTextContent = function (renderedContentUrl, htmlContent) {
1693
+ return __awaiter(this, void 0, void 0, function () {
1694
+ var canvasWidthPx;
1695
+ return __generator(this, function (_a) {
1696
+ switch (_a.label) {
1697
+ case 0:
1698
+ canvasWidthPx = this.canvas.getDimensions().widthPx;
1699
+ if (!renderedContentUrl)
1700
+ return [2 /*return*/];
1701
+ this.htmlContent = htmlContent;
1702
+ return [4 /*yield*/, this.setImageUrl(renderedContentUrl)];
1703
+ case 1:
1704
+ _a.sent();
1705
+ this.setImageWidth(canvasWidthPx);
1706
+ return [2 /*return*/];
1707
+ }
1708
+ });
1709
+ });
1710
+ };
1711
+ return RichText;
1712
+ }(Image));
1713
+
1663
1714
  var Text = /** @class */ (function (_super) {
1664
1715
  __extends(Text, _super);
1665
1716
  function Text(canvas, fabricObject, persistedField) {
@@ -2415,6 +2466,7 @@ var ObjectType;
2415
2466
  ObjectType["CUTOFF"] = "CUTOFF";
2416
2467
  ObjectType["VERTICAL_RULE"] = "VERTICAL_RULE";
2417
2468
  ObjectType["HORIZONTAL_RULE"] = "HORIZONTAL_RULE";
2469
+ ObjectType["RICH_TEXT"] = "RICH_TEXT";
2418
2470
  })(ObjectType || (ObjectType = {}));
2419
2471
 
2420
2472
  var AbstractInteractiveCanvas = /** @class */ (function () {
@@ -2784,6 +2836,48 @@ var AbstractInteractiveCanvas = /** @class */ (function () {
2784
2836
  });
2785
2837
  });
2786
2838
  };
2839
+ /**
2840
+ * @override
2841
+ * @inheritDoc
2842
+ */
2843
+ AbstractInteractiveCanvas.prototype.addRichTextObject = function (imageUrl, htmlContent) {
2844
+ return __awaiter(this, void 0, void 0, function () {
2845
+ var maxSizeScale, maxWidth, options;
2846
+ var _this = this;
2847
+ return __generator(this, function (_a) {
2848
+ switch (_a.label) {
2849
+ case 0:
2850
+ maxSizeScale = 2;
2851
+ maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
2852
+ options = { crossOrigin: "anonymous" };
2853
+ return [4 /*yield*/, new Promise(function (resolve) {
2854
+ _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
2855
+ var id = _this.randomId();
2856
+ var newScale = 1;
2857
+ var curWidth = newScale * fabricImage.width;
2858
+ if (curWidth > maxWidth) {
2859
+ newScale = maxWidth / curWidth;
2860
+ }
2861
+ fabricImage.set({
2862
+ id: id,
2863
+ scaleX: newScale,
2864
+ scaleY: newScale
2865
+ });
2866
+ _this.fabricCanvas.add(fabricImage);
2867
+ var object = new RichText(_this, fabricImage);
2868
+ object.htmlContent = htmlContent;
2869
+ _this.trackNewObject(object);
2870
+ object.setImageWidth(_this.dimensions$.value.widthPx);
2871
+ _this.selectFabricObject(fabricImage);
2872
+ _this.syncObjectList();
2873
+ resolve(object);
2874
+ }, options);
2875
+ })];
2876
+ case 1: return [2 /*return*/, _a.sent()];
2877
+ }
2878
+ });
2879
+ });
2880
+ };
2787
2881
  /**
2788
2882
  * @override
2789
2883
  * @inheritDoc
@@ -3201,6 +3295,8 @@ var AbstractInteractiveCanvas = /** @class */ (function () {
3201
3295
  return new HorizontalRule(this, fabricObject, persistedField);
3202
3296
  case ObjectType.VERTICAL_RULE:
3203
3297
  return new VerticalRule(this, fabricObject, persistedField);
3298
+ case ObjectType.RICH_TEXT:
3299
+ return new RichText(this, fabricObject, persistedField);
3204
3300
  default:
3205
3301
  throw new Error("Unknown field type: [" + persistedField.type + "]");
3206
3302
  }
@@ -3356,4 +3452,4 @@ var InteractiveCanvas = /** @class */ (function (_super) {
3356
3452
  return InteractiveCanvas;
3357
3453
  }(AbstractInteractiveCanvas));
3358
3454
 
3359
- export { AbstractCanvasObject, AbstractInteractiveCanvas, AbstractLayoutManager, AdvancedLayoutManager, Border, CanvasObjectData, Cutoff, DefaultCanvasProfile, Font, FontLibrary, HeadlessInteractiveCanvas, HorizontalRule, Image, ImageType, InteractiveCanvas, LayoutManager, MillimeterDimensions, ObjectType, PixelCanvasDimensions, Print6ColCanvasDimensions, Print8ColCanvasDimensions, Rectangle, SelectionData, StandardLayoutManager, Text, UNKNOWN_FONT, UnknownFont, VerticalRule, isMutableBackgroundColor, isMutableColor, isMutableImage, isMutablePosition, isMutableText, isMutableTextStyle };
3455
+ export { AbstractCanvasObject, AbstractInteractiveCanvas, AbstractLayoutManager, AdvancedLayoutManager, Border, CanvasObjectData, Cutoff, DefaultCanvasProfile, Font, FontLibrary, HeadlessInteractiveCanvas, HorizontalRule, Image, ImageType, InteractiveCanvas, LayoutManager, MillimeterDimensions, ObjectType, PixelCanvasDimensions, Print6ColCanvasDimensions, Print8ColCanvasDimensions, Rectangle, RichText, SelectionData, StandardLayoutManager, Text, UNKNOWN_FONT, UnknownFont, VerticalRule, isMutableBackgroundColor, isMutableColor, isMutableImage, isMutablePosition, isMutableText, isMutableTextStyle };
@@ -19,6 +19,7 @@ import { ICanvasState } from "./types/canvas-state.interface";
19
19
  import { AbstractCanvasObject } from "./object/abstract-canvas-object";
20
20
  import { ObjectType } from "./types/object-type.enum";
21
21
  import { fabric } from "fabric";
22
+ import { IRichText } from "./object/impl/rich-text";
22
23
  /**
23
24
  * New leapfrog canvas implementation c. 2019.
24
25
  */
@@ -146,6 +147,12 @@ export interface IInteractiveCanvas {
146
147
  * Add a text object to the canvas.
147
148
  */
148
149
  addTextObject(): Promise<IText>;
150
+ /**
151
+ * Add a rich text element to the canvas.
152
+ * @param imageUrl Url for text-rendered-as-image content.
153
+ * @param htmlContent Rich-text content as HTML.
154
+ */
155
+ addRichTextObject(imageUrl: string, htmlContent: string): Promise<IRichText>;
149
156
  /**
150
157
  * Add a rectangle object to the canvas.
151
158
  */
@@ -393,7 +393,7 @@ var StackObjectsLayoutFunction = /** @class */ (function () {
393
393
  StackObjectsLayoutFunction.prototype.execute = function (canvas) {
394
394
  var canvasMargin = canvas.getMargin();
395
395
  var canvasDimensions = canvas.getDimensions();
396
- var allObjects = canvas.getObjectsByTypesInternal(exports.ObjectType.TEXT, exports.ObjectType.IMAGE, exports.ObjectType.RECTANGLE, exports.ObjectType.VERTICAL_RULE, exports.ObjectType.HORIZONTAL_RULE);
396
+ var allObjects = canvas.getObjectsByTypesInternal(exports.ObjectType.TEXT, exports.ObjectType.RICH_TEXT, exports.ObjectType.IMAGE, exports.ObjectType.RECTANGLE, exports.ObjectType.VERTICAL_RULE, exports.ObjectType.HORIZONTAL_RULE);
397
397
  var objects = _(allObjects)
398
398
  .partition(function (object) { return object.isPinToBottom(); })
399
399
  .value();
@@ -1662,6 +1662,57 @@ var Rectangle = /** @class */ (function (_super) {
1662
1662
  return Rectangle;
1663
1663
  }(AbstractCanvasObject));
1664
1664
 
1665
+ var RichText = /** @class */ (function (_super) {
1666
+ __extends(RichText, _super);
1667
+ function RichText(canvas, fabricObject, persistedField) {
1668
+ var _this = _super.call(this, canvas, fabricObject, persistedField) || this;
1669
+ _this.data.type = exports.ObjectType.RICH_TEXT;
1670
+ return _this;
1671
+ }
1672
+ Object.defineProperty(RichText.prototype, "htmlContent", {
1673
+ /**
1674
+ * @override
1675
+ * @inheritDoc
1676
+ */
1677
+ get: function () {
1678
+ return this.data.userText;
1679
+ },
1680
+ /**
1681
+ * @override
1682
+ * @inheritDoc
1683
+ */
1684
+ set: function (text) {
1685
+ this.data.userText = text;
1686
+ },
1687
+ enumerable: false,
1688
+ configurable: true
1689
+ });
1690
+ /**
1691
+ * @override
1692
+ * @inheritDoc
1693
+ */
1694
+ RichText.prototype.setRichTextContent = function (renderedContentUrl, htmlContent) {
1695
+ return __awaiter(this, void 0, void 0, function () {
1696
+ var canvasWidthPx;
1697
+ return __generator(this, function (_a) {
1698
+ switch (_a.label) {
1699
+ case 0:
1700
+ canvasWidthPx = this.canvas.getDimensions().widthPx;
1701
+ if (!renderedContentUrl)
1702
+ return [2 /*return*/];
1703
+ this.htmlContent = htmlContent;
1704
+ return [4 /*yield*/, this.setImageUrl(renderedContentUrl)];
1705
+ case 1:
1706
+ _a.sent();
1707
+ this.setImageWidth(canvasWidthPx);
1708
+ return [2 /*return*/];
1709
+ }
1710
+ });
1711
+ });
1712
+ };
1713
+ return RichText;
1714
+ }(Image));
1715
+
1665
1716
  var Text = /** @class */ (function (_super) {
1666
1717
  __extends(Text, _super);
1667
1718
  function Text(canvas, fabricObject, persistedField) {
@@ -2415,6 +2466,7 @@ var DefaultCanvasProfile = /** @class */ (function () {
2415
2466
  ObjectType["CUTOFF"] = "CUTOFF";
2416
2467
  ObjectType["VERTICAL_RULE"] = "VERTICAL_RULE";
2417
2468
  ObjectType["HORIZONTAL_RULE"] = "HORIZONTAL_RULE";
2469
+ ObjectType["RICH_TEXT"] = "RICH_TEXT";
2418
2470
  })(exports.ObjectType || (exports.ObjectType = {}));
2419
2471
 
2420
2472
  var AbstractInteractiveCanvas = /** @class */ (function () {
@@ -2784,6 +2836,48 @@ var AbstractInteractiveCanvas = /** @class */ (function () {
2784
2836
  });
2785
2837
  });
2786
2838
  };
2839
+ /**
2840
+ * @override
2841
+ * @inheritDoc
2842
+ */
2843
+ AbstractInteractiveCanvas.prototype.addRichTextObject = function (imageUrl, htmlContent) {
2844
+ return __awaiter(this, void 0, void 0, function () {
2845
+ var maxSizeScale, maxWidth, options;
2846
+ var _this = this;
2847
+ return __generator(this, function (_a) {
2848
+ switch (_a.label) {
2849
+ case 0:
2850
+ maxSizeScale = 2;
2851
+ maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
2852
+ options = { crossOrigin: "anonymous" };
2853
+ return [4 /*yield*/, new Promise(function (resolve) {
2854
+ _this.fabric.Image.fromURL(imageUrl, function (fabricImage) {
2855
+ var id = _this.randomId();
2856
+ var newScale = 1;
2857
+ var curWidth = newScale * fabricImage.width;
2858
+ if (curWidth > maxWidth) {
2859
+ newScale = maxWidth / curWidth;
2860
+ }
2861
+ fabricImage.set({
2862
+ id: id,
2863
+ scaleX: newScale,
2864
+ scaleY: newScale
2865
+ });
2866
+ _this.fabricCanvas.add(fabricImage);
2867
+ var object = new RichText(_this, fabricImage);
2868
+ object.htmlContent = htmlContent;
2869
+ _this.trackNewObject(object);
2870
+ object.setImageWidth(_this.dimensions$.value.widthPx);
2871
+ _this.selectFabricObject(fabricImage);
2872
+ _this.syncObjectList();
2873
+ resolve(object);
2874
+ }, options);
2875
+ })];
2876
+ case 1: return [2 /*return*/, _a.sent()];
2877
+ }
2878
+ });
2879
+ });
2880
+ };
2787
2881
  /**
2788
2882
  * @override
2789
2883
  * @inheritDoc
@@ -3201,6 +3295,8 @@ var AbstractInteractiveCanvas = /** @class */ (function () {
3201
3295
  return new HorizontalRule(this, fabricObject, persistedField);
3202
3296
  case exports.ObjectType.VERTICAL_RULE:
3203
3297
  return new VerticalRule(this, fabricObject, persistedField);
3298
+ case exports.ObjectType.RICH_TEXT:
3299
+ return new RichText(this, fabricObject, persistedField);
3204
3300
  default:
3205
3301
  throw new Error("Unknown field type: [" + persistedField.type + "]");
3206
3302
  }
@@ -3375,6 +3471,7 @@ exports.PixelCanvasDimensions = PixelCanvasDimensions;
3375
3471
  exports.Print6ColCanvasDimensions = Print6ColCanvasDimensions;
3376
3472
  exports.Print8ColCanvasDimensions = Print8ColCanvasDimensions;
3377
3473
  exports.Rectangle = Rectangle;
3474
+ exports.RichText = RichText;
3378
3475
  exports.SelectionData = SelectionData;
3379
3476
  exports.StandardLayoutManager = StandardLayoutManager;
3380
3477
  exports.Text = Text;
@@ -0,0 +1,39 @@
1
+ import { CanvasObjectData, IInteractiveCanvas, Image, IMutableImage, IMutablePosition } from "../..";
2
+ import { ICanvasObject } from "../canvas-object.interface";
3
+ import { fabric } from "fabric";
4
+ /**
5
+ * Rich text field.
6
+ * Displays complex text by taking a rendered image of a html block of rich text and displays it on the canvas.
7
+ * The rendering of the text content to an image must be handled by the caller.
8
+ * This is an attempt to work around the poor rich-text support of fabric, which supports virtually no rich-text features on the canvas itself.
9
+ */
10
+ export interface IRichText extends ICanvasObject, IMutableImage, IMutablePosition {
11
+ /**
12
+ * HTML rich text content.
13
+ */
14
+ htmlContent: string;
15
+ /**
16
+ * Set the rich text content by providing the rendered image version of the content and the html content.
17
+ * @param renderedContentUrl Rendered version of the rich-text content.
18
+ * @param htmlContent Html version of the rich text content.
19
+ */
20
+ setRichTextContent(renderedContentUrl: string, htmlContent: string): Promise<void>;
21
+ }
22
+ export declare class RichText extends Image implements IRichText {
23
+ constructor(canvas: IInteractiveCanvas, fabricObject: fabric.Image, persistedField?: CanvasObjectData);
24
+ /**
25
+ * @override
26
+ * @inheritDoc
27
+ */
28
+ get htmlContent(): string;
29
+ /**
30
+ * @override
31
+ * @inheritDoc
32
+ */
33
+ set htmlContent(text: string);
34
+ /**
35
+ * @override
36
+ * @inheritDoc
37
+ */
38
+ setRichTextContent(renderedContentUrl: string, htmlContent: string): Promise<void>;
39
+ }
@@ -6,5 +6,6 @@ export declare enum ObjectType {
6
6
  LIBRARY_IMAGE = "LIBRARY_IMAGE",
7
7
  CUTOFF = "CUTOFF",
8
8
  VERTICAL_RULE = "VERTICAL_RULE",
9
- HORIZONTAL_RULE = "HORIZONTAL_RULE"
9
+ HORIZONTAL_RULE = "HORIZONTAL_RULE",
10
+ RICH_TEXT = "RICH_TEXT"
10
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leapfrog/interactive-canvas",
3
- "version": "1.7.0",
3
+ "version": "1.8.1",
4
4
  "description": "Leapfrog interactive canvas",
5
5
  "files": [
6
6
  "dist"