@idraw/util 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 (96) hide show
  1. package/dist/esm/converter/data-svg.d.ts +2 -0
  2. package/dist/esm/converter/data-svg.js +19 -0
  3. package/dist/esm/converter/material-svg.d.ts +2 -0
  4. package/dist/esm/converter/material-svg.js +63 -0
  5. package/dist/esm/converter/svg-material.d.ts +2 -0
  6. package/dist/esm/converter/svg-material.js +188 -0
  7. package/dist/esm/index.d.ts +32 -21
  8. package/dist/esm/index.js +32 -21
  9. package/dist/esm/static.d.ts +1 -0
  10. package/dist/esm/static.js +1 -0
  11. package/dist/esm/tool/file.js +2 -2
  12. package/dist/esm/tool/html.js +27 -27
  13. package/dist/esm/tool/id.d.ts +1 -0
  14. package/dist/esm/tool/id.js +4 -0
  15. package/dist/esm/tool/image.js +6 -6
  16. package/dist/esm/tool/path-to-box.d.ts +3 -0
  17. package/dist/esm/tool/path-to-box.js +353 -0
  18. package/dist/esm/tool/path-to-command.d.ts +4 -0
  19. package/dist/esm/tool/path-to-command.js +392 -0
  20. package/dist/esm/tool/path.d.ts +18 -0
  21. package/dist/esm/tool/path.js +421 -0
  22. package/dist/esm/tool/svg-path.d.ts +3 -0
  23. package/dist/esm/{view → tool}/svg-path.js +4 -16
  24. package/dist/esm/tool/time.d.ts +2 -2
  25. package/dist/esm/tool/time.js +4 -4
  26. package/dist/esm/tool/uuid.d.ts +1 -1
  27. package/dist/esm/tool/uuid.js +6 -6
  28. package/dist/esm/view/box.d.ts +0 -2
  29. package/dist/esm/view/box.js +1 -173
  30. package/dist/esm/view/canvas.js +3 -3
  31. package/dist/esm/view/check.d.ts +8 -7
  32. package/dist/esm/view/check.js +34 -34
  33. package/dist/esm/view/config.d.ts +0 -9
  34. package/dist/esm/view/config.js +1 -71
  35. package/dist/esm/view/context2d.d.ts +10 -3
  36. package/dist/esm/view/context2d.js +39 -18
  37. package/dist/esm/view/controller.d.ts +5 -5
  38. package/dist/esm/view/controller.js +105 -102
  39. package/dist/esm/view/data.d.ts +2 -2
  40. package/dist/esm/view/data.js +65 -65
  41. package/dist/esm/view/dom.d.ts +23 -0
  42. package/dist/esm/view/dom.js +190 -0
  43. package/dist/esm/view/flat.d.ts +2 -2
  44. package/dist/esm/view/flat.js +53 -53
  45. package/dist/esm/view/group.d.ts +3 -3
  46. package/dist/esm/view/group.js +25 -28
  47. package/dist/esm/view/handle-material.d.ts +24 -0
  48. package/dist/esm/view/{handle-element.js → handle-material.js} +98 -75
  49. package/dist/esm/view/is.d.ts +10 -12
  50. package/dist/esm/view/is.js +15 -19
  51. package/dist/esm/view/load.d.ts +2 -2
  52. package/dist/esm/view/load.js +7 -7
  53. package/dist/esm/view/material.d.ts +40 -0
  54. package/dist/esm/view/material.js +476 -0
  55. package/dist/esm/view/middleware.d.ts +1 -0
  56. package/dist/esm/view/middleware.js +14 -2
  57. package/dist/esm/view/modify-record.d.ts +4 -4
  58. package/dist/esm/view/modify-record.js +2 -2
  59. package/dist/esm/view/parser.d.ts +2 -2
  60. package/dist/esm/view/parser.js +3 -3
  61. package/dist/esm/view/path.d.ts +2 -0
  62. package/dist/esm/view/path.js +30 -0
  63. package/dist/esm/view/point-move-material.d.ts +5 -0
  64. package/dist/esm/view/{point-move-element.js → point-move-material.js} +6 -6
  65. package/dist/esm/view/point.d.ts +3 -8
  66. package/dist/esm/view/point.js +1 -21
  67. package/dist/esm/view/position.d.ts +9 -9
  68. package/dist/esm/view/position.js +1 -1
  69. package/dist/esm/view/rect.d.ts +2 -2
  70. package/dist/esm/view/rect.js +4 -4
  71. package/dist/esm/view/resize-material.d.ts +4 -0
  72. package/dist/esm/view/resize-material.js +266 -0
  73. package/dist/esm/view/rotate.d.ts +11 -11
  74. package/dist/esm/view/rotate.js +24 -24
  75. package/dist/esm/view/static.d.ts +13 -0
  76. package/dist/esm/view/static.js +89 -0
  77. package/dist/esm/view/styles.d.ts +11 -0
  78. package/dist/esm/view/styles.js +78 -0
  79. package/dist/esm/view/vertex.d.ts +8 -8
  80. package/dist/esm/view/vertex.js +28 -28
  81. package/dist/esm/view/view-box.d.ts +2 -6
  82. package/dist/esm/view/view-box.js +8 -64
  83. package/dist/esm/view/view-calc.d.ts +23 -20
  84. package/dist/esm/view/view-calc.js +104 -93
  85. package/dist/esm/view/view-content.d.ts +2 -2
  86. package/dist/esm/view/view-content.js +34 -34
  87. package/dist/index.global.js +3144 -1548
  88. package/dist/index.global.min.js +1 -1
  89. package/package.json +1 -1
  90. package/dist/esm/view/element.d.ts +0 -35
  91. package/dist/esm/view/element.js +0 -471
  92. package/dist/esm/view/handle-element.d.ts +0 -24
  93. package/dist/esm/view/point-move-element.d.ts +0 -5
  94. package/dist/esm/view/resize-element.d.ts +0 -4
  95. package/dist/esm/view/resize-element.js +0 -255
  96. package/dist/esm/view/svg-path.d.ts +0 -10
@@ -1,15 +1,15 @@
1
- import type { ElementPosition } from '@idraw/types';
1
+ import type { MaterialPosition } from '@idraw/types';
2
2
  export declare function calcResultMovePosition(opts: {
3
- from: ElementPosition;
4
- to: ElementPosition;
3
+ from: MaterialPosition;
4
+ to: MaterialPosition;
5
5
  }): {
6
- from: ElementPosition;
7
- to: ElementPosition;
6
+ from: MaterialPosition;
7
+ to: MaterialPosition;
8
8
  } | null;
9
9
  export declare function calcRevertMovePosition(opts: {
10
- from: ElementPosition;
11
- to: ElementPosition;
10
+ from: MaterialPosition;
11
+ to: MaterialPosition;
12
12
  }): {
13
- from: ElementPosition;
14
- to: ElementPosition;
13
+ from: MaterialPosition;
14
+ to: MaterialPosition;
15
15
  } | null;
@@ -74,6 +74,6 @@ export function calcRevertMovePosition(opts) {
74
74
  }
75
75
  return {
76
76
  from: [...result.to],
77
- to: [...result.from]
77
+ to: [...result.from],
78
78
  };
79
79
  }
@@ -1,2 +1,2 @@
1
- import type { ElementSize } from '@idraw/types';
2
- export declare function checkRectIntersect(rect1: ElementSize, rect2: ElementSize): boolean;
1
+ import type { MaterialSize } from '@idraw/types';
2
+ export declare function checkRectIntersect(rect1: MaterialSize, rect2: MaterialSize): boolean;
@@ -1,11 +1,11 @@
1
1
  export function checkRectIntersect(rect1, rect2) {
2
2
  const rect1MinX = rect1.x;
3
3
  const rect1MinY = rect1.y;
4
- const rect1MaxX = rect1.x + rect1.w;
5
- const rect1MaxY = rect1.y + rect1.h;
4
+ const rect1MaxX = rect1.x + rect1.width;
5
+ const rect1MaxY = rect1.y + rect1.height;
6
6
  const rect2MinX = rect2.x;
7
7
  const rect2MinY = rect2.y;
8
- const rect2MaxX = rect2.x + rect2.w;
9
- const rect2MaxY = rect2.y + rect2.h;
8
+ const rect2MaxX = rect2.x + rect2.width;
9
+ const rect2MaxY = rect2.y + rect2.height;
10
10
  return rect1MinX <= rect2MaxX && rect1MaxX >= rect2MinX && rect1MinY <= rect2MaxY && rect1MaxY >= rect2MinY;
11
11
  }
@@ -0,0 +1,4 @@
1
+ import type { StrictMaterial, MaterialSize, MaterialOperations, ModifyRecord } from '@idraw/types';
2
+ export declare function resizeEffectGroupMaterial(mtrl: StrictMaterial<'group'>, size: Partial<MaterialSize>, opts?: {
3
+ resizeEffect?: MaterialOperations['resizeEffect'];
4
+ }): ModifyRecord<'resizeMaterials'> | null;
@@ -0,0 +1,266 @@
1
+ import { istype } from '../tool/istype';
2
+ import { formatNumber } from '../tool/number';
3
+ import { toFlattenMaterial } from './modify-record';
4
+ const doNum = (n) => {
5
+ return formatNumber(n, { decimalPlaces: 4 });
6
+ };
7
+ function resizeMaterialBaseAttributesByRatio(mtrl, opts) {
8
+ const beforeMtrl = {};
9
+ const afterMtrl = {};
10
+ const record = {
11
+ type: 'modifyMaterial',
12
+ time: Date.now(),
13
+ content: {
14
+ method: 'modifyMaterial',
15
+ id: mtrl.id,
16
+ before: null,
17
+ after: null,
18
+ },
19
+ };
20
+ const { xRatio, yRatio, maxRatio } = opts;
21
+ const middleRatio = (xRatio + yRatio) / 2;
22
+ const { strokeWidth, cornerRadius, strokeDasharray, shadowOffsetX, shadowOffsetY, shadowBlur } = mtrl;
23
+ if (typeof strokeWidth === 'number') {
24
+ mtrl.strokeWidth = doNum(strokeWidth * middleRatio);
25
+ beforeMtrl.strokeWidth = strokeWidth;
26
+ afterMtrl.strokeWidth = mtrl.strokeWidth;
27
+ }
28
+ else if (Array.isArray(mtrl.strokeWidth)) {
29
+ const bw = strokeWidth;
30
+ mtrl.strokeWidth = [doNum(bw[0] * yRatio), doNum(bw[1] * xRatio), doNum(bw[2] * yRatio), doNum(bw[3] * xRatio)];
31
+ beforeMtrl.strokeWidth = [...bw];
32
+ afterMtrl.strokeWidth = [...strokeWidth];
33
+ }
34
+ if (typeof cornerRadius === 'number') {
35
+ mtrl.cornerRadius = doNum(cornerRadius * middleRatio);
36
+ beforeMtrl.cornerRadius = cornerRadius;
37
+ afterMtrl.cornerRadius = mtrl.cornerRadius;
38
+ }
39
+ else if (Array.isArray(mtrl.cornerRadius)) {
40
+ const br = cornerRadius;
41
+ mtrl.cornerRadius = [br[0] * xRatio, br[1] * xRatio, br[2] * yRatio, br[3] * yRatio];
42
+ beforeMtrl.cornerRadius = [...br];
43
+ afterMtrl.cornerRadius = [...cornerRadius];
44
+ }
45
+ if (Array.isArray(strokeDasharray)) {
46
+ strokeDasharray.forEach((dash, i) => {
47
+ mtrl.strokeDasharray[i] = doNum(dash * maxRatio);
48
+ });
49
+ beforeMtrl.strokeDasharray = [...strokeDasharray];
50
+ afterMtrl.strokeDasharray = [...mtrl.strokeDasharray];
51
+ }
52
+ if (typeof shadowOffsetX === 'number') {
53
+ mtrl.shadowOffsetX = doNum(shadowOffsetX * maxRatio);
54
+ beforeMtrl.shadowOffsetX = shadowOffsetX;
55
+ afterMtrl.shadowOffsetX = mtrl.shadowOffsetX;
56
+ }
57
+ if (typeof shadowOffsetY === 'number') {
58
+ mtrl.shadowOffsetY = doNum(shadowOffsetY * maxRatio);
59
+ beforeMtrl.shadowOffsetY = shadowOffsetY;
60
+ afterMtrl.shadowOffsetY = mtrl.shadowOffsetY;
61
+ }
62
+ if (typeof shadowBlur === 'number') {
63
+ mtrl.shadowBlur = doNum(shadowBlur * maxRatio);
64
+ beforeMtrl.shadowBlur = shadowBlur;
65
+ afterMtrl.shadowBlur = mtrl.shadowBlur;
66
+ }
67
+ record.content.before = toFlattenMaterial(beforeMtrl);
68
+ record.content.after = toFlattenMaterial(afterMtrl);
69
+ return record;
70
+ }
71
+ function resizeMaterialBaseByRatio(mtrl, opts) {
72
+ const { xRatio, yRatio } = opts;
73
+ const { id, x, y, width, height } = mtrl;
74
+ mtrl.x = doNum(x * xRatio);
75
+ mtrl.y = doNum(y * yRatio);
76
+ mtrl.width = doNum(width * xRatio);
77
+ mtrl.height = doNum(height * yRatio);
78
+ const record = {
79
+ type: 'modifyMaterial',
80
+ time: Date.now(),
81
+ content: {
82
+ method: 'modifyMaterial',
83
+ id: id,
84
+ before: { x, y, width, height },
85
+ after: { x: mtrl.x, y: mtrl.y, width: mtrl.width, height: mtrl.height },
86
+ },
87
+ };
88
+ const attributesRecord = resizeMaterialBaseAttributesByRatio(mtrl, opts);
89
+ record.content.before = Object.assign(Object.assign({}, record.content.before), attributesRecord.content.before);
90
+ record.content.after = Object.assign(Object.assign({}, record.content.after), attributesRecord.content.after);
91
+ return record;
92
+ }
93
+ function resizeTextMaterialAttributesByRatio(mtrl, opts) {
94
+ const { minRatio, maxRatio } = opts;
95
+ const { fontSize, lineHeight } = mtrl;
96
+ const ratio = (minRatio + maxRatio) / 2;
97
+ const beforeFlattenMtrl = {};
98
+ const afterFlattenMtrl = {};
99
+ if (fontSize && fontSize > 0) {
100
+ mtrl.fontSize = doNum(fontSize * ratio);
101
+ beforeFlattenMtrl['fontSize'] = fontSize;
102
+ afterFlattenMtrl['fontSize'] = mtrl.fontSize;
103
+ }
104
+ if (lineHeight && lineHeight > 0) {
105
+ mtrl.lineHeight = doNum(lineHeight * ratio);
106
+ beforeFlattenMtrl['lineHeight'] = lineHeight;
107
+ afterFlattenMtrl['lineHeight'] = mtrl.lineHeight;
108
+ }
109
+ const record = {
110
+ type: 'modifyMaterial',
111
+ time: Date.now(),
112
+ content: {
113
+ method: 'modifyMaterial',
114
+ id: mtrl.id,
115
+ before: beforeFlattenMtrl,
116
+ after: afterFlattenMtrl,
117
+ },
118
+ };
119
+ return record;
120
+ }
121
+ function deepResizeMaterialByRatio(mtrl, opts, record) {
122
+ const { type, id } = mtrl;
123
+ const rootRecord = resizeMaterialBaseByRatio(mtrl, opts);
124
+ const rootRecordBefore = Object.assign(Object.assign({}, rootRecord.content.before), { id });
125
+ const rootRecordAfter = Object.assign(Object.assign({}, rootRecord.content.after), { id });
126
+ record === null || record === void 0 ? void 0 : record.content.before.push(rootRecordBefore);
127
+ record === null || record === void 0 ? void 0 : record.content.after.push(rootRecordAfter);
128
+ if (type === 'circle') {
129
+ }
130
+ else if (type === 'text') {
131
+ const textRecord = resizeTextMaterialAttributesByRatio(mtrl, opts);
132
+ Object.keys(textRecord.content.before || {}).forEach((key) => {
133
+ var _a;
134
+ rootRecordBefore[key] = (_a = textRecord.content.before) === null || _a === void 0 ? void 0 : _a[key];
135
+ });
136
+ Object.keys(textRecord.content.after || {}).forEach((key) => {
137
+ var _a;
138
+ rootRecordAfter[key] = (_a = textRecord.content.after) === null || _a === void 0 ? void 0 : _a[key];
139
+ });
140
+ }
141
+ else if (type === 'image') {
142
+ }
143
+ else if (type === 'svgCode') {
144
+ }
145
+ else if (type === 'foreignObject') {
146
+ }
147
+ else if (type === 'path') {
148
+ }
149
+ else if (type === 'group' && Array.isArray(mtrl.children)) {
150
+ mtrl.children.forEach((child) => {
151
+ deepResizeMaterialByRatio(child, opts, record);
152
+ });
153
+ }
154
+ }
155
+ function fixedResizeGroupMaterialChildren(mtrl, opts, record) {
156
+ if (!(mtrl.type === 'group' && Array.isArray(mtrl.children))) {
157
+ return;
158
+ }
159
+ const { moveX, moveY, moveH, moveW } = opts;
160
+ let childChangedX = 0;
161
+ let childChangedY = 0;
162
+ let needReszieChildren = false;
163
+ if ((moveX !== 0 || moveY !== 0) && (moveH !== 0 || moveW !== 0)) {
164
+ needReszieChildren = true;
165
+ childChangedX = -moveX;
166
+ childChangedY = -moveY;
167
+ }
168
+ if (needReszieChildren !== true) {
169
+ return;
170
+ }
171
+ mtrl.children.forEach((child) => {
172
+ const { id, x, y } = child;
173
+ const afterX = x + childChangedX;
174
+ const afterY = y + childChangedY;
175
+ const before = { id, x, y };
176
+ const after = { id, x: afterX, y: afterY };
177
+ child.x = afterX;
178
+ child.y = afterY;
179
+ record === null || record === void 0 ? void 0 : record.content.before.push(before);
180
+ record === null || record === void 0 ? void 0 : record.content.after.push(after);
181
+ });
182
+ }
183
+ export function resizeEffectGroupMaterial(mtrl, size, opts) {
184
+ const record = {
185
+ type: 'resizeMaterials',
186
+ time: Date.now(),
187
+ content: {
188
+ method: 'modifyMaterials',
189
+ before: [],
190
+ after: [],
191
+ },
192
+ };
193
+ const id = mtrl.id;
194
+ const originalX = mtrl.x;
195
+ const originalY = mtrl.y;
196
+ const originalW = mtrl.width;
197
+ const originalH = mtrl.height;
198
+ const resizeX = (istype.number(size.x) ? size.x : mtrl.x);
199
+ const resizeY = (istype.number(size.y) ? size.y : mtrl.y);
200
+ const resizeW = (size.width && size.width > 0 ? size.width : mtrl.width) || 0;
201
+ const resizeH = (size.height && size.height > 0 ? size.height : mtrl.height) || 0;
202
+ const beforeGroupMtrl = {
203
+ id,
204
+ x: originalX,
205
+ y: originalY,
206
+ width: originalW,
207
+ height: originalH,
208
+ };
209
+ const afterGroupMtrl = {
210
+ id,
211
+ x: resizeX,
212
+ y: resizeY,
213
+ width: resizeW,
214
+ height: resizeH,
215
+ };
216
+ if ((opts === null || opts === void 0 ? void 0 : opts.resizeEffect) === 'deepResize') {
217
+ record.content.before.push(beforeGroupMtrl);
218
+ record.content.after.push(afterGroupMtrl);
219
+ const xRatio = resizeW / mtrl.width;
220
+ const yRatio = resizeH / mtrl.height;
221
+ if (xRatio === yRatio && xRatio === 1) {
222
+ return record;
223
+ }
224
+ const minRatio = Math.min(xRatio, yRatio);
225
+ const maxRatio = Math.max(xRatio, yRatio);
226
+ mtrl.width = resizeW;
227
+ mtrl.height = resizeH;
228
+ const resizeRadioOpts = { xRatio, yRatio, minRatio, maxRatio };
229
+ if (mtrl.type === 'group' && Array.isArray(mtrl.children)) {
230
+ mtrl.children.forEach((child) => {
231
+ deepResizeMaterialByRatio(child, resizeRadioOpts, record);
232
+ });
233
+ }
234
+ const groupAttributesRecord = resizeMaterialBaseAttributesByRatio(mtrl, resizeRadioOpts);
235
+ Object.keys(groupAttributesRecord.content.before || {}).forEach((key) => {
236
+ var _a;
237
+ beforeGroupMtrl[key] = (_a = groupAttributesRecord.content.before) === null || _a === void 0 ? void 0 : _a[key];
238
+ });
239
+ Object.keys(groupAttributesRecord.content.after || {}).forEach((key) => {
240
+ var _a;
241
+ afterGroupMtrl[key] = (_a = groupAttributesRecord.content.after) === null || _a === void 0 ? void 0 : _a[key];
242
+ });
243
+ return record;
244
+ }
245
+ if ((opts === null || opts === void 0 ? void 0 : opts.resizeEffect) === 'fixed') {
246
+ record.content.before.push(beforeGroupMtrl);
247
+ record.content.after.push(afterGroupMtrl);
248
+ const moveX = resizeX - mtrl.x;
249
+ const moveY = resizeY - mtrl.y;
250
+ const moveW = resizeW - mtrl.width;
251
+ const moveH = resizeH - mtrl.height;
252
+ fixedResizeGroupMaterialChildren(mtrl, { moveX, moveY, moveH, moveW }, record);
253
+ mtrl.width = resizeW;
254
+ mtrl.height = resizeH;
255
+ mtrl.x = resizeX;
256
+ mtrl.y = resizeY;
257
+ return record;
258
+ }
259
+ mtrl.width = resizeW;
260
+ mtrl.height = resizeH;
261
+ mtrl.x = resizeX;
262
+ mtrl.y = resizeY;
263
+ record.content.before.push(beforeGroupMtrl);
264
+ record.content.after.push(afterGroupMtrl);
265
+ return record;
266
+ }
@@ -1,14 +1,14 @@
1
- import type { ViewContext2D, PointSize, ElementSize, ViewRectVertexes, Element } from '@idraw/types';
1
+ import type { ViewContext2D, Point, MaterialSize, ViewRectVertexes, StrictMaterial } from '@idraw/types';
2
2
  export declare function parseRadianToAngle(radian: number): number;
3
3
  export declare function parseAngleToRadian(angle: number): number;
4
- export declare function rotateByCenter(ctx: ViewContext2D | CanvasRenderingContext2D | ViewContext2D, angle: number, center: PointSize, callback: (ctx: ViewContext2D | CanvasRenderingContext2D) => void): void;
5
- export declare function rotateElement(ctx: ViewContext2D | CanvasRenderingContext2D | ViewContext2D, elemSize: ElementSize, callback: (ctx: ViewContext2D | CanvasRenderingContext2D) => void): void;
6
- export declare function calcElementCenter(elem: ElementSize): PointSize;
7
- export declare function calcElementCenterFromVertexes(ves: ViewRectVertexes): PointSize;
8
- export declare function calcRadian(center: PointSize, start: PointSize, end: PointSize): number;
9
- export declare function rotatePoint(center: PointSize, start: PointSize, radian: number): PointSize;
10
- export declare function rotatePointInGroup(point: PointSize, groupQueue: Element<'group'>[]): PointSize;
11
- export declare function getElementRotateVertexes(elemSize: ElementSize, center: PointSize, angle: number): ViewRectVertexes;
12
- export declare function rotateElementVertexes(elemSize: ElementSize): ViewRectVertexes;
13
- export declare function rotateVertexes(center: PointSize, ves: ViewRectVertexes, radian: number): ViewRectVertexes;
4
+ export declare function rotateByCenter(ctx: ViewContext2D | CanvasRenderingContext2D | ViewContext2D, angle: number, center: Point, callback: (ctx: ViewContext2D | CanvasRenderingContext2D) => void): void;
5
+ export declare function rotateMaterial(ctx: ViewContext2D | CanvasRenderingContext2D | ViewContext2D, mtrlSize: MaterialSize, callback: (ctx: ViewContext2D | CanvasRenderingContext2D) => void): void;
6
+ export declare function calcMaterialCenter(mtrl: MaterialSize): Point;
7
+ export declare function calcMaterialCenterFromVertexes(ves: ViewRectVertexes): Point;
8
+ export declare function calcRadian(center: Point, start: Point, end: Point): number;
9
+ export declare function rotatePoint(center: Point, start: Point, radian: number): Point;
10
+ export declare function rotatePointInGroup(point: Point, groupQueue: StrictMaterial<'group'>[]): Point;
11
+ export declare function getMaterialRotateVertexes(mtrlSize: MaterialSize, center: Point, angle: number): ViewRectVertexes;
12
+ export declare function rotateMaterialVertexes(mtrlSize: MaterialSize): ViewRectVertexes;
13
+ export declare function rotateVertexes(center: Point, ves: ViewRectVertexes, radian: number): ViewRectVertexes;
14
14
  export declare function limitAngle(angle: number): number;
@@ -19,31 +19,31 @@ export function rotateByCenter(ctx, angle, center, callback) {
19
19
  ctx.translate(-center.x, -center.y);
20
20
  }
21
21
  }
22
- export function rotateElement(ctx, elemSize, callback) {
23
- const center = calcElementCenter(elemSize);
24
- rotateByCenter(ctx, elemSize.angle || 0, center, () => {
22
+ export function rotateMaterial(ctx, mtrlSize, callback) {
23
+ const center = calcMaterialCenter(mtrlSize);
24
+ rotateByCenter(ctx, mtrlSize.angle || 0, center, () => {
25
25
  callback(ctx);
26
26
  });
27
27
  }
28
- export function calcElementCenter(elem) {
28
+ export function calcMaterialCenter(mtrl) {
29
29
  const p = {
30
- x: elem.x + elem.w / 2,
31
- y: elem.y + elem.h / 2
30
+ x: mtrl.x + mtrl.width / 2,
31
+ y: mtrl.y + mtrl.height / 2,
32
32
  };
33
33
  return p;
34
34
  }
35
- export function calcElementCenterFromVertexes(ves) {
35
+ export function calcMaterialCenterFromVertexes(ves) {
36
36
  const startX = Math.min(ves[0].x, ves[1].x, ves[2].x, ves[3].x);
37
37
  const startY = Math.min(ves[0].y, ves[1].y, ves[2].y, ves[3].y);
38
38
  const endX = Math.max(ves[0].x, ves[1].x, ves[2].x, ves[3].x);
39
39
  const endY = Math.max(ves[0].y, ves[1].y, ves[2].y, ves[3].y);
40
- const elemSize = {
40
+ const mtrlSize = {
41
41
  x: startX,
42
42
  y: startY,
43
- w: endX - startX,
44
- h: endY - startY
43
+ width: endX - startX,
44
+ height: endY - startY,
45
45
  };
46
- return calcElementCenter(elemSize);
46
+ return calcMaterialCenter(mtrlSize);
47
47
  }
48
48
  export function calcRadian(center, start, end) {
49
49
  const startRadian = calcLineRadian(center, start);
@@ -149,25 +149,25 @@ export function rotatePointInGroup(point, groupQueue) {
149
149
  let resultX = point.x;
150
150
  let resultY = point.y;
151
151
  groupQueue.forEach((group) => {
152
- const { x, y, w, h, angle = 0 } = group;
153
- const center = calcElementCenter({ x, y, w, h, angle });
152
+ const { x, y, width, height, angle = 0 } = group;
153
+ const center = calcMaterialCenter({ x, y, width, height, angle });
154
154
  const temp = rotatePoint(center, { x: resultX, y: resultY }, parseAngleToRadian(angle));
155
155
  resultX = temp.x;
156
156
  resultY = temp.y;
157
157
  });
158
158
  return {
159
159
  x: resultX,
160
- y: resultY
160
+ y: resultY,
161
161
  };
162
162
  }
163
163
  return point;
164
164
  }
165
- export function getElementRotateVertexes(elemSize, center, angle) {
166
- const { x, y, w, h } = elemSize;
165
+ export function getMaterialRotateVertexes(mtrlSize, center, angle) {
166
+ const { x, y, width, height } = mtrlSize;
167
167
  let p1 = { x, y };
168
- let p2 = { x: x + w, y };
169
- let p3 = { x: x + w, y: y + h };
170
- let p4 = { x, y: y + h };
168
+ let p2 = { x: x + width, y };
169
+ let p3 = { x: x + width, y: y + height };
170
+ let p4 = { x, y: y + height };
171
171
  if (angle && (angle > 0 || angle < 0)) {
172
172
  const radian = parseAngleToRadian(limitAngle(angle));
173
173
  p1 = rotatePoint(center, p1, radian);
@@ -177,17 +177,17 @@ export function getElementRotateVertexes(elemSize, center, angle) {
177
177
  }
178
178
  return [p1, p2, p3, p4];
179
179
  }
180
- export function rotateElementVertexes(elemSize) {
181
- const { angle = 0 } = elemSize;
182
- const center = calcElementCenter(elemSize);
183
- return getElementRotateVertexes(elemSize, center, angle);
180
+ export function rotateMaterialVertexes(mtrlSize) {
181
+ const { angle = 0 } = mtrlSize;
182
+ const center = calcMaterialCenter(mtrlSize);
183
+ return getMaterialRotateVertexes(mtrlSize, center, angle);
184
184
  }
185
185
  export function rotateVertexes(center, ves, radian) {
186
186
  return [
187
187
  rotatePoint(center, { x: ves[0].x, y: ves[0].y }, radian),
188
188
  rotatePoint(center, { x: ves[1].x, y: ves[1].y }, radian),
189
189
  rotatePoint(center, { x: ves[2].x, y: ves[2].y }, radian),
190
- rotatePoint(center, { x: ves[3].x, y: ves[3].y }, radian)
190
+ rotatePoint(center, { x: ves[3].x, y: ves[3].y }, radian),
191
191
  ];
192
192
  }
193
193
  export function limitAngle(angle) {
@@ -0,0 +1,13 @@
1
+ import type { DefaultMaterialAttributes, MaterialSize, MaterialRectAttributes, MaterialCircleAttributes, MaterialTextAttributes, MaterialSVGCodeAttributes, MaterialImageAttributes, MaterialGroupAttributes } from '@idraw/types';
2
+ export declare const defaultText = "Text";
3
+ export declare const defaultFill = "#787878";
4
+ export declare const defaultStroke = "#525252";
5
+ export declare const defaultImageSlotSVG = "<svg viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" width=\"200\" height=\"200\" fill=\"#787878\"><path d=\"M928 160H96c-17.7 0-32 14.3-32 32v640c0 17.7 14.3 32 32 32h832c17.7 0 32-14.3 32-32V192c0-17.7-14.3-32-32-32zM338 304c35.3 0 64 28.7 64 64s-28.7 64-64 64-64-28.7-64-64 28.7-64 64-64z m513.9 437.1c-1.4 1.2-3.3 1.9-5.2 1.9H177.2c-4.4 0-8-3.6-8-8 0-1.9 0.7-3.7 1.9-5.2l170.3-202c2.8-3.4 7.9-3.8 11.3-1 0.3 0.3 0.7 0.6 1 1l99.4 118 158.1-187.5c2.8-3.4 7.9-3.8 11.3-1 0.3 0.3 0.7 0.6 1 1l229.6 271.6c2.6 3.3 2.2 8.4-1.2 11.2z\" ></path></svg>";
6
+ export declare const defaultImageHref: string;
7
+ export declare function getDefaultMaterialAttributes(): DefaultMaterialAttributes;
8
+ export declare function getDefaultMaterialRectAttributes(): MaterialRectAttributes;
9
+ export declare function getDefaultMaterialCircleAttributes(): MaterialCircleAttributes;
10
+ export declare function getDefaultMaterialTextAttributes(materialSize: MaterialSize): MaterialTextAttributes;
11
+ export declare function getDefaultMaterialSVGAttributes(): MaterialSVGCodeAttributes;
12
+ export declare function getDefaultMaterialImageAttributes(): MaterialImageAttributes;
13
+ export declare function getDefaultMaterialGroupAttributes(): MaterialGroupAttributes;
@@ -0,0 +1,89 @@
1
+ export const defaultText = 'Text';
2
+ export const defaultFill = '#787878';
3
+ export const defaultStroke = '#525252';
4
+ export const defaultImageSlotSVG = `<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" width="200" height="200" fill="${defaultFill}"><path d="M928 160H96c-17.7 0-32 14.3-32 32v640c0 17.7 14.3 32 32 32h832c17.7 0 32-14.3 32-32V192c0-17.7-14.3-32-32-32zM338 304c35.3 0 64 28.7 64 64s-28.7 64-64 64-64-28.7-64-64 28.7-64 64-64z m513.9 437.1c-1.4 1.2-3.3 1.9-5.2 1.9H177.2c-4.4 0-8-3.6-8-8 0-1.9 0.7-3.7 1.9-5.2l170.3-202c2.8-3.4 7.9-3.8 11.3-1 0.3 0.3 0.7 0.6 1 1l99.4 118 158.1-187.5c2.8-3.4 7.9-3.8 11.3-1 0.3 0.3 0.7 0.6 1 1l229.6 271.6c2.6 3.3 2.2 8.4-1.2 11.2z" ></path></svg>`;
5
+ export const defaultImageHref = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(defaultImageSlotSVG)}`;
6
+ export function getDefaultMaterialAttributes() {
7
+ const config = {
8
+ opacity: 1,
9
+ display: 'inline',
10
+ visibility: 'visible',
11
+ overflow: 'visible',
12
+ fill: defaultFill,
13
+ fillOpacity: 1,
14
+ fillRule: 'nonzero',
15
+ stroke: defaultStroke,
16
+ strokeWidth: 0,
17
+ strokeOpacity: 1,
18
+ strokeLinecap: 'butt',
19
+ strokeLinejoin: 'miter',
20
+ strokeDasharray: [],
21
+ strokeDashoffset: 0,
22
+ strokeMiterlimit: 0,
23
+ text: defaultText,
24
+ fontSize: 12,
25
+ fontFamily: 'system-ui',
26
+ fontWeight: 'normal',
27
+ fontStyle: 'normal',
28
+ textAnchor: 'start',
29
+ textDecoration: 'none',
30
+ letterSpacing: 'normal',
31
+ wordSpacing: 'normal',
32
+ writingMode: 'horizontal-tb',
33
+ textAlign: 'left',
34
+ verticalAlign: 'top',
35
+ href: defaultImageHref,
36
+ };
37
+ return config;
38
+ }
39
+ export function getDefaultMaterialRectAttributes() {
40
+ const attributesConfig = getDefaultMaterialAttributes();
41
+ const attributes = {
42
+ fill: attributesConfig.fill,
43
+ };
44
+ return attributes;
45
+ }
46
+ export function getDefaultMaterialCircleAttributes() {
47
+ const attributesConfig = getDefaultMaterialAttributes();
48
+ const attributes = {
49
+ fill: attributesConfig.fill,
50
+ cx: 200,
51
+ cy: 200,
52
+ r: 100,
53
+ };
54
+ return attributes;
55
+ }
56
+ export function getDefaultMaterialTextAttributes(materialSize) {
57
+ const attributesConfig = getDefaultMaterialAttributes();
58
+ const attributes = {
59
+ text: defaultText,
60
+ fill: attributesConfig.fill,
61
+ fontFamily: attributesConfig.fontFamily,
62
+ fontWeight: attributesConfig.fontWeight,
63
+ fontSize: materialSize.width / defaultText.length,
64
+ textAlign: 'center',
65
+ verticalAlign: 'middle',
66
+ };
67
+ return attributes;
68
+ }
69
+ export function getDefaultMaterialSVGAttributes() {
70
+ const attributes = {
71
+ code: `<svg t="1701004189871" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" width="200" height="200"><path d="M908.1 353.1l-253.9-36.9L540.7 86.1c-3.1-6.3-8.2-11.4-14.5-14.5-15.8-7.8-35-1.3-42.9 14.5L369.8 316.2l-253.9 36.9c-7 1-13.4 4.3-18.3 9.3-12.3 12.7-12.1 32.9 0.6 45.3l183.7 179.1-43.4 252.9c-1.2 6.9-0.1 14.1 3.2 20.3 8.2 15.6 27.6 21.7 43.2 13.4L512 754l227.1 119.4c6.2 3.3 13.4 4.4 20.3 3.2 17.4-3 29.1-19.5 26.1-36.9l-43.4-252.9 183.7-179.1c5-4.9 8.3-11.3 9.3-18.3 2.7-17.5-9.5-33.7-27-36.3zM664.8 561.6l36.1 210.3L512 672.7 323.1 772l36.1-210.3-152.8-149L417.6 382 512 190.7 606.4 382l211.2 30.7-152.8 148.9z" fill="${defaultFill}"></path></svg>`,
72
+ };
73
+ return attributes;
74
+ }
75
+ export function getDefaultMaterialImageAttributes() {
76
+ const attributes = {
77
+ href: defaultImageHref,
78
+ };
79
+ return attributes;
80
+ }
81
+ export function getDefaultMaterialGroupAttributes() {
82
+ const attributesConfig = getDefaultMaterialAttributes();
83
+ const attributes = {
84
+ children: [],
85
+ fill: attributesConfig.fill,
86
+ overflow: 'hidden',
87
+ };
88
+ return attributes;
89
+ }
@@ -0,0 +1,11 @@
1
+ import type { StylesProps } from '@idraw/types';
2
+ export declare function parseStyles(styles: StylesProps, selector: string): string;
3
+ export declare function injectStyles(opts: {
4
+ styles: StylesProps;
5
+ rootClassName: string;
6
+ type?: 'default' | 'element';
7
+ }): void;
8
+ export declare function removeStyles(opts: {
9
+ rootClassName: string;
10
+ type: 'default' | 'element';
11
+ }): void;
@@ -0,0 +1,78 @@
1
+ const defaultPixelKeys = [
2
+ 'width',
3
+ 'height',
4
+ 'left',
5
+ 'right',
6
+ 'top',
7
+ 'bottom',
8
+ 'margin',
9
+ 'marginLeft',
10
+ 'marginRight',
11
+ 'marginTop',
12
+ 'marginBottom',
13
+ 'padding',
14
+ 'paddingLeft',
15
+ 'paddingRight',
16
+ 'paddingTop',
17
+ 'paddingBottom',
18
+ ];
19
+ const parseCSSNumValue = (key, value) => {
20
+ const str = defaultPixelKeys.includes(key) ? `${value}px` : value;
21
+ return str;
22
+ };
23
+ function toKebabCase(str) {
24
+ return str.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase());
25
+ }
26
+ export function parseStyles(styles, selector) {
27
+ const baseStyles = [];
28
+ const nestedStyles = [];
29
+ for (const key in styles) {
30
+ let value = styles[key];
31
+ if (typeof value === 'number') {
32
+ value = parseCSSNumValue(key, value);
33
+ }
34
+ if (key.startsWith('@')) {
35
+ nestedStyles.push(`${key} { ${parseStyles(value, selector)} }`);
36
+ }
37
+ else if (key.startsWith('&') || key.startsWith('.') || key.startsWith('#')) {
38
+ const nestedSelector = key.startsWith('&') ? key.replace('&', selector) : `${selector} ${key}`;
39
+ nestedStyles.push(parseStyles(value, nestedSelector));
40
+ }
41
+ else {
42
+ baseStyles.push(`${toKebabCase(key)}: ${value};`);
43
+ }
44
+ }
45
+ const cssString = baseStyles.length ? `${selector} { ${baseStyles.join(' ')} }` : '';
46
+ return [cssString, ...nestedStyles].filter(Boolean).join('\n');
47
+ }
48
+ const ATTR_STYLE_KEY = 'data-idraw-style-id';
49
+ const styleSheetMap = {};
50
+ export function injectStyles(opts) {
51
+ const { styles, rootClassName, type } = opts;
52
+ const cssString = parseStyles(styles, `.${rootClassName}`);
53
+ if (type === 'element') {
54
+ const style = document.createElement('style');
55
+ style.setAttribute(ATTR_STYLE_KEY, rootClassName);
56
+ style.textContent = cssString;
57
+ const container = document.head || document.body;
58
+ container.appendChild(style);
59
+ }
60
+ else {
61
+ const sheet = new CSSStyleSheet();
62
+ sheet.replaceSync(cssString);
63
+ styleSheetMap[rootClassName] = sheet;
64
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
65
+ }
66
+ }
67
+ export function removeStyles(opts) {
68
+ const { rootClassName, type } = opts;
69
+ if (type === 'element') {
70
+ const style = document.querySelector(`[${ATTR_STYLE_KEY}="${rootClassName}"]`);
71
+ style === null || style === void 0 ? void 0 : style.remove();
72
+ }
73
+ else if (styleSheetMap[rootClassName]) {
74
+ const sheet = styleSheetMap[rootClassName];
75
+ const newSheets = document.adoptedStyleSheets.filter((s) => s !== sheet);
76
+ document.adoptedStyleSheets = newSheets;
77
+ }
78
+ }