redhotmagma-graphics-editor 1.44.10 → 1.45.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.
@@ -56,6 +56,9 @@ var Canvas = exports.Canvas = /*#__PURE__*/function (_AbstractCanvas) {
56
56
  args[_key] = arguments[_key];
57
57
  }
58
58
  _this = _super.call.apply(_super, [this].concat(args));
59
+ _defineProperty(_assertThisInitialized(_this), "Image", Canvas.Image);
60
+ _defineProperty(_assertThisInitialized(_this), "Text", Canvas.Text);
61
+ _defineProperty(_assertThisInitialized(_this), "Group", Canvas.Group);
59
62
  _defineProperty(_assertThisInitialized(_this), "_project", void 0);
60
63
  _defineProperty(_assertThisInitialized(_this), "_view", void 0);
61
64
  // prevents dragging out the object (by default lets dragging only up to the 2/3 of the object width)
@@ -527,7 +530,7 @@ var Canvas = exports.Canvas = /*#__PURE__*/function (_AbstractCanvas) {
527
530
  }, {
528
531
  key: "addText",
529
532
  value: function addText(options) {
530
- var text = new _Text.Text(options);
533
+ var text = new this.Text(options);
531
534
  this.add(text);
532
535
  return text;
533
536
  }
@@ -540,7 +543,7 @@ var Canvas = exports.Canvas = /*#__PURE__*/function (_AbstractCanvas) {
540
543
  while (1) switch (_context6.prev = _context6.next) {
541
544
  case 0:
542
545
  src = options.src;
543
- image = new _Image.Image(options);
546
+ image = new this.Image(options);
544
547
  if (!src) {
545
548
  _context6.next = 5;
546
549
  break;
@@ -571,7 +574,7 @@ var Canvas = exports.Canvas = /*#__PURE__*/function (_AbstractCanvas) {
571
574
  while (1) switch (_context7.prev = _context7.next) {
572
575
  case 0:
573
576
  src = options.src;
574
- group = new _Group.Group(options);
577
+ group = new this.Group(options);
575
578
  if (!src) {
576
579
  _context7.next = 5;
577
580
  break;
@@ -702,4 +705,7 @@ var Canvas = exports.Canvas = /*#__PURE__*/function (_AbstractCanvas) {
702
705
  }()
703
706
  }]);
704
707
  return Canvas;
705
- }(_Canvas.AbstractCanvas);
708
+ }(_Canvas.AbstractCanvas);
709
+ _defineProperty(Canvas, "Image", _Image.Image);
710
+ _defineProperty(Canvas, "Text", _Text.Text);
711
+ _defineProperty(Canvas, "Group", _Group.Group);
@@ -3,8 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.createCanvasRepository = void 0;
6
+ exports.createCanvasRepository = exports.AdapterClasses = void 0;
7
7
  var _Canvas = require("./Canvas");
8
+ var AdapterClasses = exports.AdapterClasses = {
9
+ Canvas: _Canvas.Canvas
10
+ };
8
11
  var createCanvasRepository = exports.createCanvasRepository = function createCanvasRepository() {
9
12
  var repositoryMap = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Map();
10
13
  var activeCanvas;
@@ -14,7 +17,7 @@ var createCanvasRepository = exports.createCanvasRepository = function createCan
14
17
  var _options$updateCanvas = options.updateCanvas,
15
18
  updateCanvas = _options$updateCanvas === void 0 ? false : _options$updateCanvas;
16
19
  if (!repositoryMap.has(key) || updateCanvas) {
17
- var _canvas = new _Canvas.Canvas();
20
+ var _canvas = new AdapterClasses.Canvas();
18
21
  _canvas.init(); // create canvas element
19
22
  if (canvasSize) {
20
23
  _canvas.setSize(canvasSize);
@@ -30,7 +30,8 @@ function getFontsFromSvg(node, fontToFileMap) {
30
30
  if (node.nodeName === 'text') {
31
31
  var fontFamily = (0, _String.trimQuotes)((0, _DOM.getClosestAttribute)(node, 'font-family'));
32
32
  var fontWeight = (0, _DOM.getClosestAttribute)(node, 'font-weight');
33
- var type = (0, _TextToSVGUtils.convertFontWeightToType)(fontWeight);
33
+ var fontStyle = (0, _DOM.getClosestAttribute)(node, 'font-style');
34
+ var type = (0, _TextToSVGUtils.convertFontWeightToType)(fontWeight, fontStyle);
34
35
  if (fontFamily) {
35
36
  var url = fontToFileMap[fontFamily] && fontToFileMap[fontFamily][type];
36
37
  if (url) {
@@ -50,8 +51,9 @@ function replaceTextNodesWithPaths(node, textToSVGInstances) {
50
51
  if (node.nodeName === 'text') {
51
52
  var fontFamily = (0, _String.trimQuotes)((0, _DOM.getClosestAttribute)(node, 'font-family'));
52
53
  var fontWeight = (0, _DOM.getClosestAttribute)(node, 'font-weight');
54
+ var fontStyle = (0, _DOM.getClosestAttribute)(node, 'font-style');
53
55
  var fontSize = (0, _DOM.getClosestAttribute)(node, 'font-size').replace('px', '');
54
- var type = (0, _TextToSVGUtils.convertFontWeightToType)(fontWeight);
56
+ var type = (0, _TextToSVGUtils.convertFontWeightToType)(fontWeight, fontStyle);
55
57
  var key = "".concat(fontFamily, ".").concat(type);
56
58
  var textToSVG = textToSVGInstances[key];
57
59
  if (!textToSVG) {
@@ -4,16 +4,18 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.convertFontWeightToType = exports.cleanText = void 0;
7
- var convertFontWeightToType = exports.convertFontWeightToType = function convertFontWeightToType(fontWeight) {
7
+ var convertFontWeightToType = exports.convertFontWeightToType = function convertFontWeightToType(fontWeight, fontStyle) {
8
8
  var isBold = false;
9
9
  var isItalic = false;
10
- if (fontWeight !== undefined && fontWeight !== null) {
11
- if (fontWeight.indexOf('bold') !== -1) {
12
- isBold = true;
13
- }
14
- if (fontWeight.indexOf('italic') !== -1) {
15
- isItalic = true;
16
- }
10
+ if (fontWeight && /bold|700/i.test(fontWeight)) {
11
+ isBold = true;
12
+ }
13
+ // paper.js exports the italic value as part of the fontWeight attribute
14
+ if (fontWeight && /italic/i.test(fontWeight)) {
15
+ isItalic = true;
16
+ }
17
+ if (fontStyle && /italic/i.test(fontStyle)) {
18
+ isItalic = true;
17
19
  }
18
20
  return isBold && isItalic ? 'boldItalic' : isBold ? 'bold' : isItalic ? 'italic' : 'regular';
19
21
  };
@@ -6,7 +6,10 @@ describe('CanvasAdapters/PaperJs/Utils/TextToSVGUtils', function () {
6
6
  expect((0, _TextToSVGUtils.convertFontWeightToType)('bold')).toEqual('bold');
7
7
  expect((0, _TextToSVGUtils.convertFontWeightToType)('italic')).toEqual('italic');
8
8
  expect((0, _TextToSVGUtils.convertFontWeightToType)('bold italic')).toEqual('boldItalic');
9
+ expect((0, _TextToSVGUtils.convertFontWeightToType)('700 italic')).toEqual('boldItalic');
9
10
  expect((0, _TextToSVGUtils.convertFontWeightToType)('')).toEqual('regular');
11
+ expect((0, _TextToSVGUtils.convertFontWeightToType)(false, 'italic')).toEqual('italic');
12
+ expect((0, _TextToSVGUtils.convertFontWeightToType)('700', 'italic')).toEqual('boldItalic');
10
13
  });
11
14
  test('cleanText replaces nbsps with normal spaces', function () {
12
15
  var textWithUnicodeNbsp = "hello".concat(String.fromCharCode(160), "world");
package/Utils/DOM.js CHANGED
@@ -76,7 +76,7 @@ function getNextNode(node, rootNode) {
76
76
  }
77
77
  var getClosestAttribute = exports.getClosestAttribute = function getClosestAttribute(node, attributeName) {
78
78
  var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
79
- return node.hasAttribute(attributeName) ? node.getAttribute(attributeName) : node.parentNode ? getClosestAttribute(node.parentNode, attributeName) : defaultValue;
79
+ return node.hasAttribute && node.hasAttribute(attributeName) ? node.getAttribute(attributeName) : node.parentNode ? getClosestAttribute(node.parentNode, attributeName) : defaultValue;
80
80
  };
81
81
  var placeCaretAtEnd = exports.placeCaretAtEnd = function placeCaretAtEnd(el) {
82
82
  el.focus();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redhotmagma-graphics-editor",
3
- "version": "1.44.10",
3
+ "version": "1.45.1",
4
4
  "license": "UNLICENSED",
5
5
  "private": false,
6
6
  "main": "./index.js",
@@ -15,6 +15,13 @@ import { useColorLayer } from './Utils/UseColorLayer';
15
15
  const logger = new Logger('Canvas');
16
16
 
17
17
  export class Canvas extends AbstractCanvas {
18
+ public static Image = Image;
19
+ public static Text = Text;
20
+ public static Group = Group;
21
+ Image = Canvas.Image;
22
+ Text = Canvas.Text;
23
+ Group = Canvas.Group;
24
+
18
25
  protected _project: paper.Project;
19
26
  protected _view: paper.View;
20
27
  // prevents dragging out the object (by default lets dragging only up to the 2/3 of the object width)
@@ -300,14 +307,14 @@ export class Canvas extends AbstractCanvas {
300
307
  }
301
308
 
302
309
  addText(options: any): Text {
303
- const text = new Text(options);
310
+ const text = new this.Text(options);
304
311
  this.add(text);
305
312
  return text;
306
313
  }
307
314
 
308
- async addImage(options: any): Image {
315
+ async addImage(options: any): Promise<Image> {
309
316
  const { src } = options;
310
- const image = new Image(options);
317
+ const image = new this.Image(options);
311
318
  if (src) {
312
319
  await image.loadFromURL(src);
313
320
  }
@@ -316,9 +323,9 @@ export class Canvas extends AbstractCanvas {
316
323
  return image;
317
324
  }
318
325
 
319
- async addGroup(options: any): Group {
326
+ async addGroup(options: any): Promise<Group> {
320
327
  const { src } = options;
321
- const group = new Group(options);
328
+ const group = new this.Group(options);
322
329
  if (src) {
323
330
  await group.loadFromURL(src);
324
331
  }
@@ -327,7 +334,7 @@ export class Canvas extends AbstractCanvas {
327
334
  return group;
328
335
  }
329
336
 
330
- async addItemFromObject(options: any, index?: number, overwrite?: boolean): Promise<CanvasObject> {
337
+ async addItemFromObject(options: any, index?: number, overwrite?: boolean): Promise<typeof CanvasObject> {
331
338
  const { type, ...props } = options;
332
339
  let object;
333
340
  switch (type) {
@@ -1,12 +1,16 @@
1
1
  import { Canvas } from './Canvas';
2
2
 
3
+ export const AdapterClasses = {
4
+ Canvas,
5
+ };
6
+
3
7
  export const createCanvasRepository = (repositoryMap = new Map()) => {
4
8
  let activeCanvas;
5
9
 
6
10
  const requestCanvas = (key, canvasSize = null, options = {}) => {
7
11
  const { updateCanvas = false } = options;
8
12
  if (!repositoryMap.has(key) || updateCanvas) {
9
- const canvas = new Canvas();
13
+ const canvas = new AdapterClasses.Canvas();
10
14
  canvas.init(); // create canvas element
11
15
  if (canvasSize) {
12
16
  canvas.setSize(canvasSize);
@@ -9,7 +9,8 @@ function getFontsFromSvg(node, fontToFileMap) {
9
9
  if (node.nodeName === 'text') {
10
10
  const fontFamily = trimQuotes(getClosestAttribute(node, 'font-family'));
11
11
  const fontWeight = getClosestAttribute(node, 'font-weight');
12
- const type = convertFontWeightToType(fontWeight);
12
+ const fontStyle = getClosestAttribute(node, 'font-style');
13
+ const type = convertFontWeightToType(fontWeight, fontStyle);
13
14
 
14
15
  if (fontFamily) {
15
16
  const url = fontToFileMap[fontFamily] && fontToFileMap[fontFamily][type];
@@ -33,8 +34,9 @@ function replaceTextNodesWithPaths(node, textToSVGInstances) {
33
34
  if (node.nodeName === 'text') {
34
35
  const fontFamily = trimQuotes(getClosestAttribute(node, 'font-family'));
35
36
  const fontWeight = getClosestAttribute(node, 'font-weight');
37
+ const fontStyle = getClosestAttribute(node, 'font-style');
36
38
  const fontSize = getClosestAttribute(node, 'font-size').replace('px', '');
37
- const type = convertFontWeightToType(fontWeight);
39
+ const type = convertFontWeightToType(fontWeight, fontStyle);
38
40
  const key = `${fontFamily}.${type}`;
39
41
 
40
42
  let textToSVG = textToSVGInstances[key];
@@ -1,13 +1,15 @@
1
- export const convertFontWeightToType = fontWeight => {
1
+ export const convertFontWeightToType = (fontWeight, fontStyle) => {
2
2
  let isBold = false;
3
3
  let isItalic = false;
4
- if (fontWeight !== undefined && fontWeight !== null) {
5
- if (fontWeight.indexOf('bold') !== -1) {
6
- isBold = true;
7
- }
8
- if (fontWeight.indexOf('italic') !== -1) {
9
- isItalic = true;
10
- }
4
+ if (fontWeight && /bold|700/i.test(fontWeight)) {
5
+ isBold = true;
6
+ }
7
+ // paper.js exports the italic value as part of the fontWeight attribute
8
+ if (fontWeight && /italic/i.test(fontWeight)) {
9
+ isItalic = true;
10
+ }
11
+ if (fontStyle && /italic/i.test(fontStyle)) {
12
+ isItalic = true;
11
13
  }
12
14
  return isBold && isItalic ? 'boldItalic' : isBold ? 'bold' : isItalic ? 'italic' : 'regular';
13
15
  };
@@ -5,7 +5,10 @@ describe('CanvasAdapters/PaperJs/Utils/TextToSVGUtils', () => {
5
5
  expect(convertFontWeightToType('bold')).toEqual('bold');
6
6
  expect(convertFontWeightToType('italic')).toEqual('italic');
7
7
  expect(convertFontWeightToType('bold italic')).toEqual('boldItalic');
8
+ expect(convertFontWeightToType('700 italic')).toEqual('boldItalic');
8
9
  expect(convertFontWeightToType('')).toEqual('regular');
10
+ expect(convertFontWeightToType(false, 'italic')).toEqual('italic');
11
+ expect(convertFontWeightToType('700', 'italic')).toEqual('boldItalic');
9
12
  });
10
13
  test('cleanText replaces nbsps with normal spaces', () => {
11
14
  const textWithUnicodeNbsp = `hello${String.fromCharCode(160)}world`;
package/src/Utils/DOM.ts CHANGED
@@ -68,7 +68,7 @@ export const getClosestAttribute = (
68
68
  attributeName: string,
69
69
  defaultValue: any = ''
70
70
  ): any =>
71
- (<Element>node).hasAttribute(attributeName)
71
+ (<Element>node).hasAttribute && (<Element>node).hasAttribute(attributeName)
72
72
  ? (<Element>node).getAttribute(attributeName)
73
73
  : node.parentNode
74
74
  ? getClosestAttribute(node.parentNode, attributeName)