@idraw/renderer 0.4.2 → 1.0.0-alpha.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.
Files changed (68) hide show
  1. package/dist/esm/calculator.d.ts +17 -12
  2. package/dist/esm/calculator.js +103 -88
  3. package/dist/esm/draw/base.d.ts +19 -0
  4. package/dist/esm/draw/base.js +141 -0
  5. package/dist/esm/draw/box.d.ts +2 -28
  6. package/dist/esm/draw/box.js +3 -335
  7. package/dist/esm/draw/circle.d.ts +2 -2
  8. package/dist/esm/draw/circle.js +4 -72
  9. package/dist/esm/draw/color.d.ts +3 -3
  10. package/dist/esm/draw/color.js +10 -8
  11. package/dist/esm/draw/ellipse.d.ts +2 -0
  12. package/dist/esm/draw/ellipse.js +6 -0
  13. package/dist/esm/draw/foreign-object.d.ts +2 -0
  14. package/dist/esm/draw/foreign-object.js +15 -0
  15. package/dist/esm/draw/global.d.ts +2 -2
  16. package/dist/esm/draw/global.js +2 -2
  17. package/dist/esm/draw/group.d.ts +3 -3
  18. package/dist/esm/draw/group.js +60 -94
  19. package/dist/esm/draw/image.d.ts +2 -2
  20. package/dist/esm/draw/image.js +70 -86
  21. package/dist/esm/draw/index.d.ts +6 -5
  22. package/dist/esm/draw/index.js +6 -5
  23. package/dist/esm/draw/layout.d.ts +2 -2
  24. package/dist/esm/draw/layout.js +21 -35
  25. package/dist/esm/draw/materials.d.ts +2 -0
  26. package/dist/esm/draw/materials.js +27 -0
  27. package/dist/esm/draw/path.d.ts +2 -2
  28. package/dist/esm/draw/path.js +4 -89
  29. package/dist/esm/draw/rect.d.ts +2 -2
  30. package/dist/esm/draw/rect.js +4 -22
  31. package/dist/esm/draw/svg-code.d.ts +2 -0
  32. package/dist/esm/draw/svg-code.js +15 -0
  33. package/dist/esm/draw/text.d.ts +2 -2
  34. package/dist/esm/draw/text.js +60 -50
  35. package/dist/esm/index.d.ts +1 -1
  36. package/dist/esm/index.js +15 -15
  37. package/dist/esm/loader.d.ts +4 -4
  38. package/dist/esm/loader.js +68 -70
  39. package/dist/esm/virtual/base.d.ts +2 -0
  40. package/dist/esm/virtual/base.js +52 -0
  41. package/dist/esm/virtual/circle.d.ts +2 -0
  42. package/dist/esm/virtual/circle.js +38 -0
  43. package/dist/esm/virtual/ellipse.d.ts +2 -0
  44. package/dist/esm/virtual/ellipse.js +40 -0
  45. package/dist/esm/virtual/index.d.ts +6 -0
  46. package/dist/esm/virtual/index.js +77 -0
  47. package/dist/esm/virtual/path.d.ts +2 -0
  48. package/dist/esm/virtual/path.js +10 -0
  49. package/dist/esm/virtual/rect.d.ts +2 -0
  50. package/dist/esm/virtual/rect.js +146 -0
  51. package/dist/esm/virtual/text.d.ts +2 -0
  52. package/dist/esm/virtual/text.js +165 -0
  53. package/dist/esm/visible/index.d.ts +22 -0
  54. package/dist/esm/{view-visible → visible}/index.js +29 -23
  55. package/dist/index.global.js +1794 -1578
  56. package/dist/index.global.min.js +1 -1
  57. package/package.json +3 -3
  58. package/dist/esm/draw/elements.d.ts +0 -2
  59. package/dist/esm/draw/elements.js +0 -27
  60. package/dist/esm/draw/html.d.ts +0 -2
  61. package/dist/esm/draw/html.js +0 -17
  62. package/dist/esm/draw/svg.d.ts +0 -2
  63. package/dist/esm/draw/svg.js +0 -17
  64. package/dist/esm/view-visible/index.d.ts +0 -22
  65. package/dist/esm/virtual-flat/index.d.ts +0 -7
  66. package/dist/esm/virtual-flat/index.js +0 -45
  67. package/dist/esm/virtual-flat/text.d.ts +0 -2
  68. package/dist/esm/virtual-flat/text.js +0 -151
@@ -1,24 +1,6 @@
1
- import { rotateElement, calcViewElementSize } from '@idraw/util';
2
- import { drawBox, drawBoxShadow } from './box';
3
- export function drawRect(ctx, elem, opts) {
4
- const { viewScaleInfo, viewSizeInfo, parentOpacity } = opts;
5
- const { x, y, w, h, angle } = calcViewElementSize(elem, { viewScaleInfo }) || elem;
6
- const viewElem = Object.assign(Object.assign({}, elem), { x, y, w, h, angle });
7
- rotateElement(ctx, { x, y, w, h, angle }, () => {
8
- drawBoxShadow(ctx, viewElem, {
9
- viewScaleInfo,
10
- viewSizeInfo,
11
- renderContent: () => {
12
- drawBox(ctx, viewElem, {
13
- originElem: elem,
14
- calcElemSize: { x, y, w, h, angle },
15
- viewScaleInfo,
16
- viewSizeInfo,
17
- parentOpacity,
18
- renderContent: () => {
19
- }
20
- });
21
- }
22
- });
1
+ import { drawBase, rotateViewMaterial } from './base';
2
+ export function drawRect(ctx, mtrl, opts) {
3
+ rotateViewMaterial(ctx, mtrl, opts, () => {
4
+ drawBase(ctx, mtrl, opts);
23
5
  });
24
6
  }
@@ -0,0 +1,2 @@
1
+ import type { StrictMaterial, RendererDrawMaterialOptions, ViewContext2D } from '@idraw/types';
2
+ export declare function drawSVGCode(ctx: ViewContext2D, mtrl: StrictMaterial<'svgCode'>, opts: RendererDrawMaterialOptions): void;
@@ -0,0 +1,15 @@
1
+ import { calcViewMaterialSize } from '@idraw/util';
2
+ import { rotateViewMaterial } from './base';
3
+ export function drawSVGCode(ctx, mtrl, opts) {
4
+ const content = opts.loader.getContent(mtrl);
5
+ const { viewScaleInfo } = opts;
6
+ const { x, y, width, height } = calcViewMaterialSize(mtrl, { viewScaleInfo }) || mtrl;
7
+ rotateViewMaterial(ctx, mtrl, opts, () => {
8
+ if (!content && !opts.loader.isDestroyed()) {
9
+ opts.loader.load(mtrl, opts.materialAssets || {});
10
+ }
11
+ if (mtrl.type === 'svgCode' && content) {
12
+ ctx.drawImage(content, x, y, width, height);
13
+ }
14
+ });
15
+ }
@@ -1,2 +1,2 @@
1
- import type { Element, RendererDrawElementOptions, ViewContext2D } from '@idraw/types';
2
- export declare function drawText(ctx: ViewContext2D, elem: Element<'text'>, opts: RendererDrawElementOptions): void;
1
+ import type { StrictMaterial, RendererDrawMaterialOptions, ViewContext2D } from '@idraw/types';
2
+ export declare function drawText(ctx: ViewContext2D, mtrl: StrictMaterial<'text'>, opts: RendererDrawMaterialOptions): void;
@@ -1,64 +1,74 @@
1
- import { rotateElement, calcViewElementSize, enhanceFontFamliy } from '@idraw/util';
2
- import { is, isColorStr, getDefaultElementDetailConfig } from '@idraw/util';
3
- import { drawBox, drawBoxShadow, getOpacity } from './box';
4
- const detailConfig = getDefaultElementDetailConfig();
5
- export function drawText(ctx, elem, opts) {
6
- const { viewScaleInfo, viewSizeInfo, parentOpacity, calculator } = opts;
7
- const { x, y, w, h, angle } = calcViewElementSize(elem, { viewScaleInfo }) || elem;
8
- const viewElem = Object.assign(Object.assign({}, elem), { x, y, w, h, angle });
9
- rotateElement(ctx, { x, y, w, h, angle }, () => {
10
- var _a, _b;
11
- drawBoxShadow(ctx, viewElem, {
12
- viewScaleInfo,
13
- viewSizeInfo,
14
- renderContent: () => {
15
- drawBox(ctx, viewElem, {
16
- originElem: elem,
17
- calcElemSize: { x, y, w, h, angle },
18
- viewScaleInfo,
19
- viewSizeInfo,
20
- parentOpacity
21
- });
22
- }
23
- });
1
+ import { enhanceFontFamliy, calcViewMaterialSize } from '@idraw/util';
2
+ import { getDefaultMaterialAttributes } from '@idraw/util';
3
+ import { rotateViewMaterial, drawClipPath } from './base';
4
+ import { createColor } from './color';
5
+ const defaultAttrs = getDefaultMaterialAttributes();
6
+ export function drawText(ctx, mtrl, opts) {
7
+ const { viewScaleInfo, viewSizeInfo, calculator } = opts;
8
+ const viewMaterialSize = calcViewMaterialSize(mtrl, { viewScaleInfo }) || mtrl;
9
+ const { scale } = viewScaleInfo;
10
+ rotateViewMaterial(ctx, mtrl, opts, ({ viewWorldSize }) => {
24
11
  {
25
- const detail = Object.assign(Object.assign({}, detailConfig), elem.detail);
26
- const originFontSize = detail.fontSize || detailConfig.fontSize;
12
+ const attributes = Object.assign(Object.assign({}, defaultAttrs), mtrl);
13
+ const { x, y } = viewWorldSize;
14
+ const originFontSize = attributes.fontSize || defaultAttrs.fontSize;
27
15
  const fontSize = originFontSize * viewScaleInfo.scale;
16
+ const { opacity, fill, fillOpacity, stroke, strokeWidth, strokeOpacity, strokeLinecap, strokeLinejoin, strokeDasharray, strokeDashoffset, strokeMiterlimit, } = attributes;
28
17
  if (fontSize < 2) {
29
18
  return;
30
19
  }
31
- const { parentOpacity } = opts;
32
- const opacity = getOpacity(elem) * parentOpacity;
33
- ctx.globalAlpha = opacity;
34
- ctx.fillStyle = elem.detail.color || detailConfig.color;
20
+ const originGlobalAlpha = ctx.globalAlpha;
21
+ const virtualTextAttributes = calculator.getVirtualItem(mtrl.id);
35
22
  ctx.textBaseline = 'top';
36
23
  ctx.$setFont({
37
- fontWeight: detail.fontWeight,
24
+ fontWeight: attributes.fontWeight,
38
25
  fontSize: fontSize,
39
- fontFamily: enhanceFontFamliy(detail.fontFamily)
26
+ fontFamily: enhanceFontFamliy(attributes.fontFamily),
40
27
  });
41
- {
42
- const virtualTextDetail = calculator.getVirtualFlatItem(elem.uuid);
43
- if (Array.isArray(virtualTextDetail === null || virtualTextDetail === void 0 ? void 0 : virtualTextDetail.textLines) && ((_a = virtualTextDetail === null || virtualTextDetail === void 0 ? void 0 : virtualTextDetail.textLines) === null || _a === void 0 ? void 0 : _a.length) > 0) {
44
- if (detail.textShadowColor !== undefined && isColorStr(detail.textShadowColor)) {
45
- ctx.shadowColor = detail.textShadowColor;
46
- }
47
- if (detail.textShadowOffsetX !== undefined && is.number(detail.textShadowOffsetX)) {
48
- ctx.shadowOffsetX = detail.textShadowOffsetX;
49
- }
50
- if (detail.textShadowOffsetY !== undefined && is.number(detail.textShadowOffsetY)) {
51
- ctx.shadowOffsetY = detail.textShadowOffsetY;
28
+ drawClipPath(ctx, mtrl, {
29
+ viewScaleInfo,
30
+ viewSizeInfo,
31
+ calculator,
32
+ renderContent: () => {
33
+ var _a, _b, _c;
34
+ if (fill) {
35
+ if (typeof fillOpacity === 'number' && fillOpacity > 0) {
36
+ ctx.globalAlpha = originGlobalAlpha * fillOpacity * opacity;
37
+ }
38
+ ctx.fillStyle = createColor(ctx, fill, { viewMaterialSize, viewScaleInfo, opacity: mtrl.opacity || 1 });
39
+ if (Array.isArray(virtualTextAttributes === null || virtualTextAttributes === void 0 ? void 0 : virtualTextAttributes.textLines) && ((_a = virtualTextAttributes === null || virtualTextAttributes === void 0 ? void 0 : virtualTextAttributes.textLines) === null || _a === void 0 ? void 0 : _a.length) > 0) {
40
+ (_b = virtualTextAttributes === null || virtualTextAttributes === void 0 ? void 0 : virtualTextAttributes.textLines) === null || _b === void 0 ? void 0 : _b.forEach((line) => {
41
+ ctx.fillText(line.text, x + line.x * viewScaleInfo.scale, y + line.y * viewScaleInfo.scale);
42
+ });
43
+ }
44
+ ctx.globalAlpha = originGlobalAlpha;
52
45
  }
53
- if (detail.textShadowBlur !== undefined && is.number(detail.textShadowBlur)) {
54
- ctx.shadowBlur = detail.textShadowBlur;
46
+ if (typeof strokeWidth === 'number' && strokeWidth > 0) {
47
+ if (typeof strokeOpacity === 'number' && strokeOpacity > 0) {
48
+ ctx.globalAlpha = originGlobalAlpha * strokeOpacity * opacity;
49
+ }
50
+ ctx.lineCap = strokeLinecap;
51
+ ctx.lineJoin = strokeLinejoin;
52
+ ctx.lineDashOffset = strokeDashoffset;
53
+ ctx.miterLimit = strokeMiterlimit;
54
+ if (Array.isArray(strokeDasharray)) {
55
+ const lineDash = strokeDasharray.map((dash) => scale * dash);
56
+ ctx.setLineDash(lineDash);
57
+ }
58
+ ctx.lineWidth = strokeWidth * scale;
59
+ ctx.strokeStyle = stroke;
60
+ (_c = virtualTextAttributes === null || virtualTextAttributes === void 0 ? void 0 : virtualTextAttributes.textLines) === null || _c === void 0 ? void 0 : _c.forEach((line) => {
61
+ ctx.strokeText(line.text, x + line.x * viewScaleInfo.scale, y + line.y * viewScaleInfo.scale);
62
+ });
63
+ ctx.setLineDash([]);
64
+ ctx.lineCap = defaultAttrs.strokeLinecap;
65
+ ctx.lineJoin = defaultAttrs.strokeLinejoin;
66
+ ctx.lineDashOffset = defaultAttrs.strokeDashoffset;
67
+ ctx.miterLimit = defaultAttrs.strokeMiterlimit;
68
+ ctx.globalAlpha = originGlobalAlpha;
55
69
  }
56
- (_b = virtualTextDetail === null || virtualTextDetail === void 0 ? void 0 : virtualTextDetail.textLines) === null || _b === void 0 ? void 0 : _b.forEach((line) => {
57
- ctx.fillText(line.text, x + line.x * viewScaleInfo.scale, y + line.y * viewScaleInfo.scale);
58
- });
59
- }
60
- }
61
- ctx.globalAlpha = parentOpacity;
70
+ },
71
+ });
62
72
  }
63
73
  });
64
74
  }
@@ -17,4 +17,4 @@ export declare class Renderer extends EventEmitter<RendererEventMap> implements
17
17
  getLoader(): Loader;
18
18
  getCalculator(): Calculator;
19
19
  }
20
- export { drawCircle, drawRect, drawImage, drawSVG, drawHTML, drawText, drawGroup, drawElement, drawElementList, drawLayout, drawGlobalBackground } from './draw';
20
+ export { drawCircle, drawRect, drawImage, drawSVGCode, drawForeignObject, drawText, drawGroup, drawMaterial, drawMaterialList, drawLayout, drawGlobalBackground, } from './draw';
package/dist/esm/index.js CHANGED
@@ -11,7 +11,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
11
11
  };
12
12
  var _Renderer_instances, _Renderer_opts, _Renderer_loader, _Renderer_calculator, _Renderer_hasDestroyed, _Renderer_init;
13
13
  import { EventEmitter } from '@idraw/util';
14
- import { drawElementList, drawLayout, drawGlobalBackground } from './draw/index';
14
+ import { drawMaterialList, drawLayout, drawGlobalBackground } from './draw/index';
15
15
  import { Loader } from './loader';
16
16
  import { Calculator } from './calculator';
17
17
  export { Calculator };
@@ -25,7 +25,7 @@ export class Renderer extends EventEmitter {
25
25
  _Renderer_hasDestroyed.set(this, false);
26
26
  __classPrivateFieldSet(this, _Renderer_opts, opts, "f");
27
27
  __classPrivateFieldSet(this, _Renderer_calculator, new Calculator({
28
- tempContext: opts.tempContext
28
+ tempContext: opts.tempContext,
29
29
  }), "f");
30
30
  __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_init).call(this);
31
31
  }
@@ -48,29 +48,29 @@ export class Renderer extends EventEmitter {
48
48
  const { sharer } = __classPrivateFieldGet(this, _Renderer_opts, "f");
49
49
  const viewContext = __classPrivateFieldGet(this, _Renderer_opts, "f").viewContext;
50
50
  viewContext.clearRect(0, 0, viewContext.canvas.width, viewContext.canvas.height);
51
- const parentElementSize = {
51
+ const parentMaterialSize = {
52
52
  x: 0,
53
53
  y: 0,
54
- w: opts.viewSizeInfo.width,
55
- h: opts.viewSizeInfo.height
54
+ width: opts.viewSizeInfo.width,
55
+ height: opts.viewSizeInfo.height,
56
56
  };
57
57
  if (opts.forceDrawAll === true) {
58
- __classPrivateFieldGet(this, _Renderer_calculator, "f").resetVirtualFlatItemMap(data, {
58
+ __classPrivateFieldGet(this, _Renderer_calculator, "f").resetVirtualItemMap(data, {
59
59
  viewScaleInfo: opts.viewScaleInfo,
60
- viewSizeInfo: opts.viewSizeInfo
60
+ viewSizeInfo: opts.viewSizeInfo,
61
61
  });
62
62
  }
63
63
  const drawOpts = Object.assign({ loader,
64
64
  calculator,
65
- parentElementSize, elementAssets: data.assets, parentOpacity: 1, overrideElementMap: sharer === null || sharer === void 0 ? void 0 : sharer.getActiveOverrideElemenentMap() }, opts);
65
+ parentMaterialSize, materialAssets: data.assets, parentOpacity: 1, overrideMaterialMap: sharer === null || sharer === void 0 ? void 0 : sharer.getActiveOverrideMaterialMap(), tempContext: __classPrivateFieldGet(this, _Renderer_opts, "f").tempContext }, opts);
66
66
  drawGlobalBackground(viewContext, data.global, drawOpts);
67
67
  if (data.layout) {
68
68
  drawLayout(viewContext, data.layout, drawOpts, () => {
69
- drawElementList(viewContext, data, drawOpts);
69
+ drawMaterialList(viewContext, data, drawOpts);
70
70
  });
71
71
  }
72
72
  else {
73
- drawElementList(viewContext, data, drawOpts);
73
+ drawMaterialList(viewContext, data, drawOpts);
74
74
  }
75
75
  }
76
76
  scale(num) {
@@ -78,7 +78,7 @@ export class Renderer extends EventEmitter {
78
78
  if (!sharer) {
79
79
  return;
80
80
  }
81
- const { data, offsetTop, offsetBottom, offsetLeft, offsetRight, width, height, contextHeight, contextWidth, devicePixelRatio } = sharer.getActiveStoreSnapshot();
81
+ const { data, offsetTop, offsetBottom, offsetLeft, offsetRight, width, height, contextHeight, contextWidth, devicePixelRatio, } = sharer.getActiveStoreSnapshot();
82
82
  if (data) {
83
83
  this.drawData(data, {
84
84
  viewScaleInfo: {
@@ -86,15 +86,15 @@ export class Renderer extends EventEmitter {
86
86
  offsetTop,
87
87
  offsetBottom,
88
88
  offsetLeft,
89
- offsetRight
89
+ offsetRight,
90
90
  },
91
91
  viewSizeInfo: {
92
92
  width,
93
93
  height,
94
94
  contextHeight,
95
95
  contextWidth,
96
- devicePixelRatio
97
- }
96
+ devicePixelRatio,
97
+ },
98
98
  });
99
99
  }
100
100
  }
@@ -120,4 +120,4 @@ _Renderer_opts = new WeakMap(), _Renderer_loader = new WeakMap(), _Renderer_calc
120
120
  console.error(e);
121
121
  });
122
122
  };
123
- export { drawCircle, drawRect, drawImage, drawSVG, drawHTML, drawText, drawGroup, drawElement, drawElementList, drawLayout, drawGlobalBackground } from './draw';
123
+ export { drawCircle, drawRect, drawImage, drawSVGCode, drawForeignObject, drawText, drawGroup, drawMaterial, drawMaterialList, drawLayout, drawGlobalBackground, } from './draw';
@@ -1,14 +1,14 @@
1
- import type { RendererLoader, LoaderEventMap, LoadContent, LoadItemMap, LoadElementType, Element, ElementAssets, RecursivePartial } from '@idraw/types';
1
+ import type { RendererLoader, LoaderEventMap, LoadContent, LoadItemMap, LoadMaterialType, StrictMaterial, MaterialAssets, RecursivePartial } from '@idraw/types';
2
2
  import { EventEmitter } from '@idraw/util';
3
3
  export declare class Loader extends EventEmitter<LoaderEventMap> implements RendererLoader {
4
4
  #private;
5
5
  constructor();
6
6
  isDestroyed(): boolean;
7
7
  reset(): void;
8
- resetElementAsset(element: Element<LoadElementType> | RecursivePartial<Element>): void;
8
+ resetMaterialAsset(material: StrictMaterial<LoadMaterialType> | RecursivePartial<StrictMaterial>): void;
9
9
  destroy(): void;
10
- load(element: Element<LoadElementType>, assets: ElementAssets): void;
11
- getContent(element: Element<LoadElementType>): LoadContent | null;
10
+ load(material: StrictMaterial<LoadMaterialType>, assets: MaterialAssets): void;
11
+ getContent(material: StrictMaterial<LoadMaterialType>): LoadContent | null;
12
12
  getLoadItemMap(): LoadItemMap;
13
13
  setLoadItemMap(itemMap: LoadItemMap): void;
14
14
  }
@@ -18,28 +18,27 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
18
18
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
19
19
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
20
20
  };
21
- var _Loader_instances, _Loader_loadFuncMap, _Loader_currentLoadItemMap, _Loader_storageLoadItemMap, _Loader_hasDestroyed, _Loader_registerLoadFunc, _Loader_getLoadElementSource, _Loader_createLoadItem, _Loader_emitLoad, _Loader_emitError, _Loader_loadResource, _Loader_isExistingErrorStorage;
22
- import { loadImage, loadHTML, loadSVG, EventEmitter, createAssetId, isAssetId, createUUID } from '@idraw/util';
23
- const supportElementTypes = ['image', 'svg', 'html'];
24
- const getAssetIdFromElement = (element) => {
25
- var _a, _b, _c;
21
+ var _Loader_instances, _Loader_loadFuncMap, _Loader_currentLoadItemMap, _Loader_storageLoadItemMap, _Loader_hasDestroyed, _Loader_registerLoadFunc, _Loader_getLoadMaterialSource, _Loader_createLoadItem, _Loader_emitLoad, _Loader_emitError, _Loader_loadResource, _Loader_isExistingErrorStorage;
22
+ import { loadImage, loadForeignObject, loadSVGCode, EventEmitter, createAssetId, isAssetId, createUUID, } from '@idraw/util';
23
+ const supportMaterialTypes = ['image', 'svgCode', 'foreignObject'];
24
+ const getAssetIdFromMaterial = (material) => {
26
25
  let source = null;
27
- if (element.type === 'image') {
28
- source = ((_a = element === null || element === void 0 ? void 0 : element.detail) === null || _a === void 0 ? void 0 : _a.src) || null;
26
+ if (material.type === 'image') {
27
+ source = (material === null || material === void 0 ? void 0 : material.href) || null;
29
28
  }
30
- else if (element.type === 'svg') {
31
- source = ((_b = element === null || element === void 0 ? void 0 : element.detail) === null || _b === void 0 ? void 0 : _b.svg) || null;
29
+ else if (material.type === 'svgCode') {
30
+ source = (material === null || material === void 0 ? void 0 : material.code) || null;
32
31
  }
33
- else if (element.type === 'html') {
34
- source = ((_c = element === null || element === void 0 ? void 0 : element.detail) === null || _c === void 0 ? void 0 : _c.html) || null;
32
+ else if (material.type === 'foreignObject') {
33
+ source = (material === null || material === void 0 ? void 0 : material.content) || null;
35
34
  }
36
35
  if (typeof source === 'string' && source) {
37
36
  if (isAssetId(source)) {
38
37
  return source;
39
38
  }
40
- return createAssetId(source, element.uuid);
39
+ return createAssetId(source, material.id);
41
40
  }
42
- return createAssetId(`${createUUID()}-${element.uuid}-${createUUID()}-${createUUID()}`, element.uuid);
41
+ return createAssetId(`${createUUID()}-${material.id}-${createUUID()}-${createUUID()}`, material.id);
43
42
  };
44
43
  export class Loader extends EventEmitter {
45
44
  constructor() {
@@ -49,37 +48,37 @@ export class Loader extends EventEmitter {
49
48
  _Loader_currentLoadItemMap.set(this, {});
50
49
  _Loader_storageLoadItemMap.set(this, {});
51
50
  _Loader_hasDestroyed.set(this, false);
52
- __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_registerLoadFunc).call(this, 'image', (elem, assets) => __awaiter(this, void 0, void 0, function* () {
51
+ __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_registerLoadFunc).call(this, 'image', (mtrl, assets) => __awaiter(this, void 0, void 0, function* () {
53
52
  var _a;
54
- const src = ((_a = assets[elem.detail.src]) === null || _a === void 0 ? void 0 : _a.value) || elem.detail.src;
55
- const content = yield loadImage(src);
53
+ const href = ((_a = assets[mtrl.href]) === null || _a === void 0 ? void 0 : _a.value) || mtrl.href;
54
+ const content = yield loadImage(href);
56
55
  return {
57
- uuid: elem.uuid,
56
+ id: mtrl.id,
58
57
  lastModified: Date.now(),
59
- content
58
+ content,
60
59
  };
61
60
  }));
62
- __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_registerLoadFunc).call(this, 'html', (elem, assets) => __awaiter(this, void 0, void 0, function* () {
61
+ __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_registerLoadFunc).call(this, 'foreignObject', (mtrl, assets) => __awaiter(this, void 0, void 0, function* () {
63
62
  var _a;
64
- const html = ((_a = assets[elem.detail.html]) === null || _a === void 0 ? void 0 : _a.value) || elem.detail.html;
65
- const content = yield loadHTML(html, {
66
- width: elem.detail.originW || elem.w,
67
- height: elem.detail.originH || elem.h
63
+ const html = ((_a = assets[mtrl.content]) === null || _a === void 0 ? void 0 : _a.value) || mtrl.content;
64
+ const content = yield loadForeignObject(html, {
65
+ width: mtrl.originW || mtrl.width,
66
+ height: mtrl.originH || mtrl.height,
68
67
  });
69
68
  return {
70
- uuid: elem.uuid,
69
+ id: mtrl.id,
71
70
  lastModified: Date.now(),
72
- content
71
+ content,
73
72
  };
74
73
  }));
75
- __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_registerLoadFunc).call(this, 'svg', (elem, assets) => __awaiter(this, void 0, void 0, function* () {
74
+ __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_registerLoadFunc).call(this, 'svgCode', (mtrl, assets) => __awaiter(this, void 0, void 0, function* () {
76
75
  var _a;
77
- const svg = ((_a = assets[elem.detail.svg]) === null || _a === void 0 ? void 0 : _a.value) || elem.detail.svg;
78
- const content = yield loadSVG(svg);
76
+ const svg = ((_a = assets[mtrl.code]) === null || _a === void 0 ? void 0 : _a.value) || mtrl.code;
77
+ const content = yield loadSVGCode(svg);
79
78
  return {
80
- uuid: elem.uuid,
79
+ id: mtrl.id,
81
80
  lastModified: Date.now(),
82
- content
81
+ content,
83
82
  };
84
83
  }));
85
84
  }
@@ -93,27 +92,27 @@ export class Loader extends EventEmitter {
93
92
  __classPrivateFieldSet(this, _Loader_currentLoadItemMap, {}, "f");
94
93
  __classPrivateFieldSet(this, _Loader_storageLoadItemMap, {}, "f");
95
94
  }
96
- resetElementAsset(element) {
97
- var _a, _b, _c;
98
- if (supportElementTypes.includes(element.type)) {
95
+ resetMaterialAsset(material) {
96
+ if (supportMaterialTypes.includes(material.type)) {
99
97
  let assetId = null;
100
98
  let resource = null;
101
- if (element.type === 'image' && typeof ((_a = element === null || element === void 0 ? void 0 : element.detail) === null || _a === void 0 ? void 0 : _a.src) === 'string') {
102
- resource = element.detail.src;
99
+ if (material.type === 'image' && typeof (material === null || material === void 0 ? void 0 : material.href) === 'string') {
100
+ resource = material.href;
103
101
  }
104
- else if (element.type === 'svg' && typeof ((_b = element === null || element === void 0 ? void 0 : element.detail) === null || _b === void 0 ? void 0 : _b.svg) === 'string') {
105
- resource = element.detail.svg;
102
+ else if (material.type === 'svgCode' && typeof (material === null || material === void 0 ? void 0 : material.code) === 'string') {
103
+ resource = material.code;
106
104
  }
107
- else if (element.type === 'html' && typeof ((_c = element === null || element === void 0 ? void 0 : element.detail) === null || _c === void 0 ? void 0 : _c.html) === 'string') {
108
- resource = element.detail.html;
105
+ else if (material.type === 'foreignObject' &&
106
+ typeof (material === null || material === void 0 ? void 0 : material.content) === 'string') {
107
+ resource = material.content;
109
108
  }
110
109
  if (typeof resource === 'string') {
111
- this.load(element, {});
110
+ this.load(material, {});
112
111
  if (isAssetId(resource)) {
113
112
  assetId = resource;
114
113
  }
115
- else if (element.uuid) {
116
- assetId = createAssetId(resource, element.uuid);
114
+ else if (material.id) {
115
+ assetId = createAssetId(resource, material.id);
117
116
  }
118
117
  }
119
118
  if (assetId && isAssetId(assetId)) {
@@ -129,20 +128,20 @@ export class Loader extends EventEmitter {
129
128
  __classPrivateFieldSet(this, _Loader_currentLoadItemMap, null, "f");
130
129
  __classPrivateFieldSet(this, _Loader_storageLoadItemMap, null, "f");
131
130
  }
132
- load(element, assets) {
131
+ load(material, assets) {
133
132
  if (__classPrivateFieldGet(this, _Loader_hasDestroyed, "f") === true) {
134
133
  return;
135
134
  }
136
- if (__classPrivateFieldGet(this, _Loader_instances, "m", _Loader_isExistingErrorStorage).call(this, element)) {
135
+ if (__classPrivateFieldGet(this, _Loader_instances, "m", _Loader_isExistingErrorStorage).call(this, material)) {
137
136
  return;
138
137
  }
139
- if (supportElementTypes.includes(element.type)) {
140
- __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_loadResource).call(this, element, assets);
138
+ if (supportMaterialTypes.includes(material.type)) {
139
+ __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_loadResource).call(this, material, assets);
141
140
  }
142
141
  }
143
- getContent(element) {
142
+ getContent(material) {
144
143
  var _a, _b;
145
- const assetId = getAssetIdFromElement(element);
144
+ const assetId = getAssetIdFromMaterial(material);
146
145
  return ((_b = (_a = __classPrivateFieldGet(this, _Loader_storageLoadItemMap, "f")) === null || _a === void 0 ? void 0 : _a[assetId]) === null || _b === void 0 ? void 0 : _b.content) || null;
147
146
  }
148
147
  getLoadItemMap() {
@@ -154,31 +153,30 @@ export class Loader extends EventEmitter {
154
153
  }
155
154
  _Loader_loadFuncMap = new WeakMap(), _Loader_currentLoadItemMap = new WeakMap(), _Loader_storageLoadItemMap = new WeakMap(), _Loader_hasDestroyed = new WeakMap(), _Loader_instances = new WeakSet(), _Loader_registerLoadFunc = function _Loader_registerLoadFunc(type, func) {
156
155
  __classPrivateFieldGet(this, _Loader_loadFuncMap, "f")[type] = func;
157
- }, _Loader_getLoadElementSource = function _Loader_getLoadElementSource(element) {
158
- var _a, _b, _c;
156
+ }, _Loader_getLoadMaterialSource = function _Loader_getLoadMaterialSource(material) {
159
157
  let source = null;
160
- if (element.type === 'image') {
161
- source = ((_a = element === null || element === void 0 ? void 0 : element.detail) === null || _a === void 0 ? void 0 : _a.src) || null;
158
+ if (material.type === 'image') {
159
+ source = (material === null || material === void 0 ? void 0 : material.href) || null;
162
160
  }
163
- else if (element.type === 'svg') {
164
- source = ((_b = element === null || element === void 0 ? void 0 : element.detail) === null || _b === void 0 ? void 0 : _b.svg) || null;
161
+ else if (material.type === 'svgCode') {
162
+ source = (material === null || material === void 0 ? void 0 : material.code) || null;
165
163
  }
166
- else if (element.type === 'html') {
167
- source = ((_c = element === null || element === void 0 ? void 0 : element.detail) === null || _c === void 0 ? void 0 : _c.html) || null;
164
+ else if (material.type === 'foreignObject') {
165
+ source = (material === null || material === void 0 ? void 0 : material.content) || null;
168
166
  }
169
167
  return source;
170
- }, _Loader_createLoadItem = function _Loader_createLoadItem(element) {
168
+ }, _Loader_createLoadItem = function _Loader_createLoadItem(material) {
171
169
  return {
172
- element,
170
+ material,
173
171
  status: 'null',
174
172
  content: null,
175
173
  error: null,
176
174
  startTime: -1,
177
175
  endTime: -1,
178
- source: __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_getLoadElementSource).call(this, element)
176
+ source: __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_getLoadMaterialSource).call(this, material),
179
177
  };
180
178
  }, _Loader_emitLoad = function _Loader_emitLoad(item) {
181
- const assetId = getAssetIdFromElement(item.element);
179
+ const assetId = getAssetIdFromMaterial(item.material);
182
180
  const storageItem = __classPrivateFieldGet(this, _Loader_storageLoadItemMap, "f")[assetId];
183
181
  if (!__classPrivateFieldGet(this, _Loader_hasDestroyed, "f")) {
184
182
  if (storageItem) {
@@ -194,7 +192,7 @@ _Loader_loadFuncMap = new WeakMap(), _Loader_currentLoadItemMap = new WeakMap(),
194
192
  }
195
193
  }, _Loader_emitError = function _Loader_emitError(item) {
196
194
  var _a;
197
- const assetId = getAssetIdFromElement(item.element);
195
+ const assetId = getAssetIdFromMaterial(item.material);
198
196
  const storageItem = (_a = __classPrivateFieldGet(this, _Loader_storageLoadItemMap, "f")) === null || _a === void 0 ? void 0 : _a[assetId];
199
197
  if (!__classPrivateFieldGet(this, _Loader_hasDestroyed, "f")) {
200
198
  if (storageItem) {
@@ -208,17 +206,17 @@ _Loader_loadFuncMap = new WeakMap(), _Loader_currentLoadItemMap = new WeakMap(),
208
206
  this.trigger('error', Object.assign(Object.assign({}, item), { countTime: item.endTime - item.startTime }));
209
207
  }
210
208
  }
211
- }, _Loader_loadResource = function _Loader_loadResource(element, assets) {
212
- const item = __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_createLoadItem).call(this, element);
213
- const assetId = getAssetIdFromElement(element);
209
+ }, _Loader_loadResource = function _Loader_loadResource(material, assets) {
210
+ const item = __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_createLoadItem).call(this, material);
211
+ const assetId = getAssetIdFromMaterial(material);
214
212
  if (__classPrivateFieldGet(this, _Loader_currentLoadItemMap, "f")[assetId]) {
215
213
  return;
216
214
  }
217
215
  __classPrivateFieldGet(this, _Loader_currentLoadItemMap, "f")[assetId] = item;
218
- const loadFunc = __classPrivateFieldGet(this, _Loader_loadFuncMap, "f")[element.type];
216
+ const loadFunc = __classPrivateFieldGet(this, _Loader_loadFuncMap, "f")[material.type];
219
217
  if (typeof loadFunc === 'function' && !__classPrivateFieldGet(this, _Loader_hasDestroyed, "f")) {
220
218
  item.startTime = Date.now();
221
- loadFunc(element, assets)
219
+ loadFunc(material, assets)
222
220
  .then((result) => {
223
221
  if (!__classPrivateFieldGet(this, _Loader_hasDestroyed, "f")) {
224
222
  item.content = result.content;
@@ -228,21 +226,21 @@ _Loader_loadFuncMap = new WeakMap(), _Loader_currentLoadItemMap = new WeakMap(),
228
226
  }
229
227
  })
230
228
  .catch((err) => {
231
- console.warn(`Load element source "${item.source}" fail`, err, element);
229
+ console.warn(`Load material source "${item.source}" fail`, err, material);
232
230
  item.endTime = Date.now();
233
231
  item.status = 'error';
234
232
  item.error = err;
235
233
  __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_emitError).call(this, item);
236
234
  });
237
235
  }
238
- }, _Loader_isExistingErrorStorage = function _Loader_isExistingErrorStorage(element) {
236
+ }, _Loader_isExistingErrorStorage = function _Loader_isExistingErrorStorage(material) {
239
237
  var _a;
240
- const assetId = getAssetIdFromElement(element);
238
+ const assetId = getAssetIdFromMaterial(material);
241
239
  const existItem = (_a = __classPrivateFieldGet(this, _Loader_currentLoadItemMap, "f")) === null || _a === void 0 ? void 0 : _a[assetId];
242
240
  if (existItem &&
243
241
  existItem.status === 'error' &&
244
242
  existItem.source &&
245
- existItem.source === __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_getLoadElementSource).call(this, element)) {
243
+ existItem.source === __classPrivateFieldGet(this, _Loader_instances, "m", _Loader_getLoadMaterialSource).call(this, material)) {
246
244
  return true;
247
245
  }
248
246
  return false;
@@ -0,0 +1,2 @@
1
+ import type { Material, VirtualBaseAttributes, CalcVirtualAttributesOptions } from '@idraw/types';
2
+ export declare function calcVirtualBaseAttributes(mtrl: Material, opts: CalcVirtualAttributesOptions): VirtualBaseAttributes;
@@ -0,0 +1,52 @@
1
+ import { limitAngle } from '@idraw/util';
2
+ function calculateWorldPosition(targetRect, parents) {
3
+ parents = [...parents].reverse();
4
+ if (!parents || parents.length === 0) {
5
+ return {
6
+ x: targetRect.x + targetRect.width / 2,
7
+ y: targetRect.y + targetRect.height / 2,
8
+ };
9
+ }
10
+ let centerX = targetRect.x + targetRect.width / 2;
11
+ let centerY = targetRect.y + targetRect.height / 2;
12
+ for (let i = 0; i < parents.length; i++) {
13
+ const parent = parents[i];
14
+ const transformed = applyRectTransformation({ x: centerX, y: centerY }, parent);
15
+ centerX = transformed.x;
16
+ centerY = transformed.y;
17
+ }
18
+ return { x: centerX, y: centerY };
19
+ }
20
+ function applyRectTransformation(point, rect) {
21
+ const rectCenterX = rect.width / 2;
22
+ const rectCenterY = rect.height / 2;
23
+ const centeredX = point.x - rectCenterX;
24
+ const centeredY = point.y - rectCenterY;
25
+ let rotatedX = centeredX;
26
+ let rotatedY = centeredY;
27
+ if (typeof rect.angle === 'number' && rect.angle !== 0) {
28
+ const angleRad = (rect.angle * Math.PI) / 180;
29
+ const cos = Math.cos(angleRad);
30
+ const sin = Math.sin(angleRad);
31
+ rotatedX = centeredX * cos - centeredY * sin;
32
+ rotatedY = centeredX * sin + centeredY * cos;
33
+ }
34
+ const localX = rotatedX + rectCenterX;
35
+ const localY = rotatedY + rectCenterY;
36
+ const worldX = localX + rect.x;
37
+ const worldY = localY + rect.y;
38
+ return { x: worldX, y: worldY };
39
+ }
40
+ export function calcVirtualBaseAttributes(mtrl, opts) {
41
+ const { groupQueue = [] } = opts;
42
+ let worldAngle = mtrl.angle || 0;
43
+ groupQueue.forEach((group) => {
44
+ worldAngle += group.angle || 0;
45
+ });
46
+ const worldCenter = calculateWorldPosition(mtrl, groupQueue);
47
+ const attributes = {
48
+ worldCenter,
49
+ worldAngle: limitAngle(worldAngle),
50
+ };
51
+ return attributes;
52
+ }
@@ -0,0 +1,2 @@
1
+ import type { StrictMaterial, VirtualRectAttributes, CalcVirtualAttributesOptions } from '@idraw/types';
2
+ export declare function calcVirtualCircleAttributes(mtrl: StrictMaterial<'circle'>, opts: CalcVirtualAttributesOptions): VirtualRectAttributes;