@idraw/util 0.4.0-beta.4 → 0.4.0-beta.41

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 (111) hide show
  1. package/dist/esm/index.d.ts +46 -30
  2. package/dist/esm/index.js +46 -30
  3. package/dist/esm/{lib → tool}/color.js +9 -6
  4. package/dist/esm/{lib → tool}/event.d.ts +4 -2
  5. package/dist/esm/tool/event.js +70 -0
  6. package/dist/esm/{lib → tool}/file.d.ts +2 -1
  7. package/dist/esm/{lib → tool}/file.js +4 -1
  8. package/dist/esm/tool/flat-object.d.ts +6 -0
  9. package/dist/esm/tool/flat-object.js +28 -0
  10. package/dist/esm/tool/get-set-del.d.ts +4 -0
  11. package/dist/esm/tool/get-set-del.js +65 -0
  12. package/dist/esm/tool/hash.d.ts +1 -0
  13. package/dist/esm/tool/hash.js +33 -0
  14. package/dist/esm/{lib → tool}/html.d.ts +1 -1
  15. package/dist/esm/{lib → tool}/html.js +6 -2
  16. package/dist/esm/{lib → tool}/istype.d.ts +1 -0
  17. package/dist/esm/{lib → tool}/istype.js +3 -0
  18. package/dist/esm/tool/merge.d.ts +1 -0
  19. package/dist/esm/tool/merge.js +17 -0
  20. package/dist/esm/tool/omit.d.ts +1 -0
  21. package/dist/esm/tool/omit.js +7 -0
  22. package/dist/esm/tool/store.d.ts +16 -0
  23. package/dist/esm/tool/store.js +52 -0
  24. package/dist/esm/{lib → tool}/time.d.ts +1 -0
  25. package/dist/esm/{lib → tool}/time.js +13 -1
  26. package/dist/esm/tool/unflat-object.d.ts +3 -0
  27. package/dist/esm/tool/unflat-object.js +91 -0
  28. package/dist/esm/tool/uuid.js +13 -0
  29. package/dist/esm/view/box.d.ts +2 -0
  30. package/dist/esm/view/box.js +173 -0
  31. package/dist/esm/{lib → view}/canvas.d.ts +0 -1
  32. package/dist/esm/view/canvas.js +57 -0
  33. package/dist/esm/{lib → view}/check.js +14 -14
  34. package/dist/esm/view/config.d.ts +9 -0
  35. package/dist/esm/{lib → view}/config.js +9 -9
  36. package/dist/esm/{lib → view}/context2d.d.ts +4 -0
  37. package/dist/esm/{lib → view}/context2d.js +20 -0
  38. package/dist/esm/view/controller.d.ts +12 -0
  39. package/dist/esm/view/controller.js +321 -0
  40. package/dist/esm/view/data.d.ts +10 -0
  41. package/dist/esm/view/data.js +202 -0
  42. package/dist/esm/{lib → view}/element.d.ts +5 -0
  43. package/dist/esm/{lib → view}/element.js +65 -4
  44. package/dist/esm/view/flat.d.ts +2 -0
  45. package/dist/esm/view/flat.js +133 -0
  46. package/dist/esm/view/group.d.ts +3 -0
  47. package/dist/esm/view/group.js +81 -0
  48. package/dist/esm/{lib → view}/handle-element.d.ts +11 -1
  49. package/dist/esm/view/handle-element.js +286 -0
  50. package/dist/esm/view/handle-global.d.ts +4 -0
  51. package/dist/esm/view/handle-global.js +28 -0
  52. package/dist/esm/view/handle-layout.d.ts +4 -0
  53. package/dist/esm/view/handle-layout.js +28 -0
  54. package/dist/esm/{lib → view}/is.d.ts +3 -1
  55. package/dist/esm/{lib → view}/is.js +22 -6
  56. package/dist/esm/view/modify-record.d.ts +4 -0
  57. package/dist/esm/view/modify-record.js +10 -0
  58. package/dist/esm/view/point-move-element.d.ts +5 -0
  59. package/dist/esm/view/point-move-element.js +26 -0
  60. package/dist/esm/view/position.d.ts +15 -0
  61. package/dist/esm/view/position.js +79 -0
  62. package/dist/esm/view/rect.js +11 -0
  63. package/dist/esm/view/resize-element.d.ts +2 -0
  64. package/dist/esm/view/resize-element.js +101 -0
  65. package/dist/esm/{lib → view}/rotate.js +8 -13
  66. package/dist/esm/view/text.d.ts +1 -0
  67. package/dist/esm/view/text.js +4 -0
  68. package/dist/esm/{lib → view}/view-box.js +4 -2
  69. package/dist/esm/{lib → view}/view-calc.d.ts +16 -3
  70. package/dist/esm/{lib → view}/view-calc.js +127 -3
  71. package/dist/esm/view/view-content.d.ts +14 -0
  72. package/dist/esm/view/view-content.js +88 -0
  73. package/dist/index.global.js +1977 -326
  74. package/dist/index.global.min.js +1 -1
  75. package/package.json +2 -2
  76. package/dist/esm/lib/canvas.js +0 -81
  77. package/dist/esm/lib/config.d.ts +0 -14
  78. package/dist/esm/lib/controller.d.ts +0 -6
  79. package/dist/esm/lib/controller.js +0 -103
  80. package/dist/esm/lib/data.d.ts +0 -5
  81. package/dist/esm/lib/data.js +0 -92
  82. package/dist/esm/lib/event.js +0 -50
  83. package/dist/esm/lib/handle-element.js +0 -226
  84. package/dist/esm/lib/rect.js +0 -11
  85. package/dist/esm/lib/store.d.ts +0 -12
  86. package/dist/esm/lib/store.js +0 -22
  87. package/dist/esm/lib/uuid.js +0 -31
  88. /package/dist/esm/{lib → tool}/color.d.ts +0 -0
  89. /package/dist/esm/{lib → tool}/image.d.ts +0 -0
  90. /package/dist/esm/{lib → tool}/image.js +0 -0
  91. /package/dist/esm/{lib → tool}/number.d.ts +0 -0
  92. /package/dist/esm/{lib → tool}/number.js +0 -0
  93. /package/dist/esm/{lib → tool}/uuid.d.ts +0 -0
  94. /package/dist/esm/{lib → view}/check.d.ts +0 -0
  95. /package/dist/esm/{lib → view}/load.d.ts +0 -0
  96. /package/dist/esm/{lib → view}/load.js +0 -0
  97. /package/dist/esm/{lib → view}/matrix.d.ts +0 -0
  98. /package/dist/esm/{lib → view}/matrix.js +0 -0
  99. /package/dist/esm/{lib → view}/middleware.d.ts +0 -0
  100. /package/dist/esm/{lib → view}/middleware.js +0 -0
  101. /package/dist/esm/{lib → view}/parser.d.ts +0 -0
  102. /package/dist/esm/{lib → view}/parser.js +0 -0
  103. /package/dist/esm/{lib → view}/point.d.ts +0 -0
  104. /package/dist/esm/{lib → view}/point.js +0 -0
  105. /package/dist/esm/{lib → view}/rect.d.ts +0 -0
  106. /package/dist/esm/{lib → view}/rotate.d.ts +0 -0
  107. /package/dist/esm/{lib → view}/svg-path.d.ts +0 -0
  108. /package/dist/esm/{lib → view}/svg-path.js +0 -0
  109. /package/dist/esm/{lib → view}/vertex.d.ts +0 -0
  110. /package/dist/esm/{lib → view}/vertex.js +0 -0
  111. /package/dist/esm/{lib → view}/view-box.d.ts +0 -0
@@ -0,0 +1,10 @@
1
+ import { flatObject } from '../tool/flat-object';
2
+ export function toFlattenElement(elem) {
3
+ return flatObject(elem, { ignorePaths: ['detail.children'] });
4
+ }
5
+ export function toFlattenLayout(layout) {
6
+ return flatObject(layout);
7
+ }
8
+ export function toFlattenGlobal(global) {
9
+ return flatObject(global);
10
+ }
@@ -0,0 +1,5 @@
1
+ import type { PointSize, Element } from '@idraw/types';
2
+ export declare function calcPointMoveElementInGroup(start: PointSize, end: PointSize, groupQueue: Element<'group'>[]): {
3
+ moveX: number;
4
+ moveY: number;
5
+ };
@@ -0,0 +1,26 @@
1
+ import { rotatePointInGroup } from './rotate';
2
+ export function calcPointMoveElementInGroup(start, end, groupQueue) {
3
+ let moveX = end.x - start.x;
4
+ let moveY = end.y - start.y;
5
+ const pointGroupQueue = [];
6
+ groupQueue.forEach((group) => {
7
+ const { x, y, w, h, angle = 0 } = group;
8
+ pointGroupQueue.push({
9
+ x,
10
+ y,
11
+ w,
12
+ h,
13
+ angle: 0 - angle
14
+ });
15
+ });
16
+ if ((groupQueue === null || groupQueue === void 0 ? void 0 : groupQueue.length) > 0) {
17
+ const startInGroup = rotatePointInGroup(start, pointGroupQueue);
18
+ const endInGroup = rotatePointInGroup(end, pointGroupQueue);
19
+ moveX = endInGroup.x - startInGroup.x;
20
+ moveY = endInGroup.y - startInGroup.y;
21
+ }
22
+ return {
23
+ moveX,
24
+ moveY
25
+ };
26
+ }
@@ -0,0 +1,15 @@
1
+ import type { ElementPosition } from '@idraw/types';
2
+ export declare function calcResultMovePosition(opts: {
3
+ from: ElementPosition;
4
+ to: ElementPosition;
5
+ }): {
6
+ from: ElementPosition;
7
+ to: ElementPosition;
8
+ } | null;
9
+ export declare function calcRevertMovePosition(opts: {
10
+ from: ElementPosition;
11
+ to: ElementPosition;
12
+ }): {
13
+ from: ElementPosition;
14
+ to: ElementPosition;
15
+ } | null;
@@ -0,0 +1,79 @@
1
+ export function calcResultMovePosition(opts) {
2
+ const from = [...opts.from];
3
+ const to = [...opts.to];
4
+ if (from.length === 0 || to.length === 0) {
5
+ return null;
6
+ }
7
+ if (from.length <= to.length) {
8
+ for (let i = 0; i < from.length; i++) {
9
+ if (to[i] === from[i]) {
10
+ if (i === from.length - 1) {
11
+ return null;
12
+ }
13
+ continue;
14
+ }
15
+ }
16
+ }
17
+ let moveDirection = null;
18
+ if (from.length >= 1 && to.length >= 1) {
19
+ if (from.length <= to.length) {
20
+ if (from.length === 1) {
21
+ if (from[0] < to[0]) {
22
+ moveDirection = 'up-down';
23
+ }
24
+ }
25
+ else {
26
+ for (let i = 0; i < from.length; i++) {
27
+ if (from[i] === to[i]) {
28
+ if (from.length === from.length - 1) {
29
+ moveDirection = 'up-down';
30
+ break;
31
+ }
32
+ }
33
+ else {
34
+ break;
35
+ }
36
+ }
37
+ }
38
+ }
39
+ if (from.length >= to.length) {
40
+ if (to.length === 1) {
41
+ if (to[0] < from[0]) {
42
+ moveDirection = 'down-up';
43
+ }
44
+ }
45
+ else {
46
+ for (let i = 0; i < to.length; i++) {
47
+ if (i === to.length - 1 && to[i] < from[i]) {
48
+ moveDirection = 'down-up';
49
+ }
50
+ if (from[i] === to[i]) {
51
+ continue;
52
+ }
53
+ else {
54
+ break;
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+ const startEffectIndex = from.length - 1;
61
+ const endEffectIndex = to.length - 1;
62
+ if (moveDirection === 'up-down' && startEffectIndex >= 0) {
63
+ to[startEffectIndex] -= 1;
64
+ }
65
+ else if (moveDirection === 'down-up' && endEffectIndex >= 0) {
66
+ from[endEffectIndex] += 1;
67
+ }
68
+ return { from, to };
69
+ }
70
+ export function calcRevertMovePosition(opts) {
71
+ const result = calcResultMovePosition(opts);
72
+ if (!result) {
73
+ return result;
74
+ }
75
+ return {
76
+ from: [...result.to],
77
+ to: [...result.from]
78
+ };
79
+ }
@@ -0,0 +1,11 @@
1
+ export function checkRectIntersect(rect1, rect2) {
2
+ const rect1MinX = rect1.x;
3
+ const rect1MinY = rect1.y;
4
+ const rect1MaxX = rect1.x + rect1.w;
5
+ const rect1MaxY = rect1.y + rect1.h;
6
+ const rect2MinX = rect2.x;
7
+ const rect2MinY = rect2.y;
8
+ const rect2MaxX = rect2.x + rect2.w;
9
+ const rect2MaxY = rect2.y + rect2.h;
10
+ return rect1MinX <= rect2MaxX && rect1MaxX >= rect2MinX && rect1MinY <= rect2MaxY && rect1MaxY >= rect2MinY;
11
+ }
@@ -0,0 +1,2 @@
1
+ import type { Element, ElementSize } from '@idraw/types';
2
+ export declare function deepResizeGroupElement(elem: Element<'group'>, size: Pick<Partial<ElementSize>, 'w' | 'h'>): Element<'group'>;
@@ -0,0 +1,101 @@
1
+ import { formatNumber } from '../tool/number';
2
+ const doNum = (n) => {
3
+ return formatNumber(n, { decimalPlaces: 4 });
4
+ };
5
+ function resizeElementBaseDetail(elem, opts) {
6
+ const { detail } = elem;
7
+ const { xRatio, yRatio, maxRatio } = opts;
8
+ const middleRatio = (xRatio + yRatio) / 2;
9
+ const { borderWidth, borderRadius, borderDash, shadowOffsetX, shadowOffsetY, shadowBlur } = detail;
10
+ if (typeof borderWidth === 'number') {
11
+ detail.borderWidth = doNum(borderWidth * middleRatio);
12
+ }
13
+ else if (Array.isArray(detail.borderWidth)) {
14
+ const bw = borderWidth;
15
+ detail.borderWidth = [doNum(bw[0] * yRatio), doNum(bw[1] * xRatio), doNum(bw[2] * yRatio), doNum(bw[3] * xRatio)];
16
+ }
17
+ if (typeof borderRadius === 'number') {
18
+ detail.borderRadius = doNum(borderRadius * middleRatio);
19
+ }
20
+ else if (Array.isArray(detail.borderRadius)) {
21
+ const br = borderRadius;
22
+ detail.borderRadius = [br[0] * xRatio, br[1] * xRatio, br[2] * yRatio, br[3] * yRatio];
23
+ }
24
+ if (Array.isArray(borderDash)) {
25
+ borderDash.forEach((dash, i) => {
26
+ detail.borderDash[i] = doNum(dash * maxRatio);
27
+ });
28
+ }
29
+ if (typeof shadowOffsetX === 'number') {
30
+ detail.shadowOffsetX = doNum(shadowOffsetX * maxRatio);
31
+ }
32
+ if (typeof shadowOffsetY === 'number') {
33
+ detail.shadowOffsetX = doNum(shadowOffsetY * maxRatio);
34
+ }
35
+ if (typeof shadowBlur === 'number') {
36
+ detail.shadowOffsetX = doNum(shadowBlur * maxRatio);
37
+ }
38
+ }
39
+ function resizeElementBase(elem, opts) {
40
+ const { xRatio, yRatio } = opts;
41
+ const { x, y, w, h } = elem;
42
+ elem.x = doNum(x * xRatio);
43
+ elem.y = doNum(y * yRatio);
44
+ elem.w = doNum(w * xRatio);
45
+ elem.h = doNum(h * yRatio);
46
+ resizeElementBaseDetail(elem, opts);
47
+ }
48
+ function resizeTextElementDetail(elem, opts) {
49
+ const { minRatio, maxRatio } = opts;
50
+ const { fontSize, lineHeight } = elem.detail;
51
+ const ratio = (minRatio + maxRatio) / 2;
52
+ if (fontSize && fontSize > 0) {
53
+ elem.detail.fontSize = doNum(fontSize * ratio);
54
+ }
55
+ if (lineHeight && lineHeight > 0) {
56
+ elem.detail.lineHeight = doNum(lineHeight * ratio);
57
+ }
58
+ }
59
+ function resizeElement(elem, opts) {
60
+ const { type } = elem;
61
+ resizeElementBase(elem, opts);
62
+ if (type === 'circle') {
63
+ }
64
+ else if (type === 'text') {
65
+ resizeTextElementDetail(elem, opts);
66
+ }
67
+ else if (type === 'image') {
68
+ }
69
+ else if (type === 'svg') {
70
+ }
71
+ else if (type === 'html') {
72
+ }
73
+ else if (type === 'path') {
74
+ }
75
+ else if (type === 'group' && Array.isArray(elem.detail.children)) {
76
+ elem.detail.children.forEach((child) => {
77
+ resizeElement(child, opts);
78
+ });
79
+ }
80
+ }
81
+ export function deepResizeGroupElement(elem, size) {
82
+ const resizeW = size.w && size.w > 0 ? size.w : elem.w;
83
+ const resizeH = size.h && size.h > 0 ? size.h : elem.h;
84
+ const xRatio = resizeW / elem.w;
85
+ const yRatio = resizeH / elem.h;
86
+ if (xRatio === yRatio && xRatio === 1) {
87
+ return elem;
88
+ }
89
+ const minRatio = Math.min(xRatio, yRatio);
90
+ const maxRatio = Math.max(xRatio, yRatio);
91
+ elem.w = resizeW;
92
+ elem.h = resizeH;
93
+ const opts = { xRatio, yRatio, minRatio, maxRatio };
94
+ if (elem.type === 'group' && Array.isArray(elem.detail.children)) {
95
+ elem.detail.children.forEach((child) => {
96
+ resizeElement(child, opts);
97
+ });
98
+ }
99
+ resizeElementBaseDetail(elem, opts);
100
+ return elem;
101
+ }
@@ -46,18 +46,10 @@ export function calcElementCenterFromVertexes(ves) {
46
46
  return calcElementCenter(elemSize);
47
47
  }
48
48
  export function calcRadian(center, start, end) {
49
- const startAngle = calcLineRadian(center, start);
50
- const endAngle = calcLineRadian(center, end);
51
- if (endAngle !== null && startAngle !== null) {
52
- if (startAngle > (Math.PI * 3) / 2 && endAngle < Math.PI / 2) {
53
- return endAngle + (Math.PI * 2 - startAngle);
54
- }
55
- else if (endAngle > (Math.PI * 3) / 2 && startAngle < Math.PI / 2) {
56
- return startAngle + (Math.PI * 2 - endAngle);
57
- }
58
- else {
59
- return endAngle - startAngle;
60
- }
49
+ const startRadian = calcLineRadian(center, start);
50
+ const endRadian = calcLineRadian(center, end);
51
+ if (endRadian !== null && startRadian !== null) {
52
+ return endRadian - startRadian;
61
53
  }
62
54
  else {
63
55
  return 0;
@@ -199,12 +191,15 @@ export function rotateVertexes(center, ves, radian) {
199
191
  ];
200
192
  }
201
193
  export function limitAngle(angle) {
202
- if (!(angle > 0 || angle < 0) || angle === 0) {
194
+ if (!(angle > 0 || angle < 0) || angle === 0 || angle === 360) {
203
195
  return 0;
204
196
  }
205
197
  let num = angle % 360;
206
198
  if (num < 0) {
207
199
  num += 360;
208
200
  }
201
+ else if (angle === 360) {
202
+ num = 0;
203
+ }
209
204
  return num;
210
205
  }
@@ -0,0 +1 @@
1
+ export declare function enhanceFontFamliy(fontFamily?: string): string;
@@ -0,0 +1,4 @@
1
+ const baseFontFamilyList = ['-apple-system', '"system-ui"', ' "Segoe UI"', ' Roboto', '"Helvetica Neue"', 'Arial', '"Noto Sans"', ' sans-serif'];
2
+ export function enhanceFontFamliy(fontFamily) {
3
+ return [fontFamily, ...baseFontFamilyList].join(', ');
4
+ }
@@ -4,8 +4,10 @@ export function calcViewBoxSize(viewElem, opts) {
4
4
  const { viewScaleInfo } = opts;
5
5
  const { scale } = viewScaleInfo;
6
6
  let { borderRadius } = viewElem.detail;
7
+ const { borderDash } = viewElem.detail;
8
+ const hasBorderDash = Array.isArray(borderDash) && borderDash.length > 0;
7
9
  const { boxSizing = defaultElemConfig.boxSizing, borderWidth } = viewElem.detail;
8
- if (typeof borderWidth !== 'number') {
10
+ if (Array.isArray(borderWidth)) {
9
11
  borderRadius = 0;
10
12
  }
11
13
  let { x, y, w, h } = viewElem;
@@ -21,7 +23,7 @@ export function calcViewBoxSize(viewElem, opts) {
21
23
  if (typeof borderWidth === 'number') {
22
24
  bw = (borderWidth || 0) * scale;
23
25
  }
24
- if (boxSizing === 'border-box') {
26
+ if (boxSizing === 'border-box' && !hasBorderDash) {
25
27
  x = viewElem.x + bw / 2;
26
28
  y = viewElem.y + bw / 2;
27
29
  w = viewElem.w - bw;
@@ -1,4 +1,4 @@
1
- import { Point, PointSize, Data, ViewScaleInfo, ViewSizeInfo, Element, ElementType, ElementSize, ViewContext2D, ViewRectVertexes } from '@idraw/types';
1
+ import { Point, PointSize, Data, ViewScaleInfo, ViewSizeInfo, Element, ElementType, ElementSize, ViewContext2D, ViewRectVertexes, ViewRectInfo } from '@idraw/types';
2
2
  export declare function calcViewScaleInfo(info: {
3
3
  scale: number;
4
4
  offsetX: number;
@@ -23,11 +23,9 @@ export declare function viewScroll(opts: {
23
23
  }): ViewScaleInfo;
24
24
  export declare function calcViewElementSize(size: ElementSize, opts: {
25
25
  viewScaleInfo: ViewScaleInfo;
26
- viewSizeInfo: ViewSizeInfo;
27
26
  }): ElementSize;
28
27
  export declare function calcViewPointSize(size: PointSize, opts: {
29
28
  viewScaleInfo: ViewScaleInfo;
30
- viewSizeInfo: ViewSizeInfo;
31
29
  }): PointSize;
32
30
  export declare function calcViewVertexes(vertexes: ViewRectVertexes, opts: {
33
31
  viewScaleInfo: ViewScaleInfo;
@@ -39,6 +37,12 @@ export declare function isViewPointInElement(p: Point, opts: {
39
37
  viewScaleInfo: ViewScaleInfo;
40
38
  viewSizeInfo: ViewSizeInfo;
41
39
  }): boolean;
40
+ export declare function isViewPointInElementSize(p: Point, elemSize: ElementSize, opts?: {
41
+ includeBorder?: boolean;
42
+ }): boolean;
43
+ export declare function isViewPointInVertexes(p: Point, vertexes: ViewRectVertexes, opts?: {
44
+ includeBorder?: boolean;
45
+ }): boolean;
42
46
  export declare function getViewPointAtElement(p: Point, opts: {
43
47
  context2d: ViewContext2D;
44
48
  data: Data;
@@ -54,3 +58,12 @@ export declare function isElementInView(elem: ElementSize, opts: {
54
58
  viewScaleInfo: ViewScaleInfo;
55
59
  viewSizeInfo: ViewSizeInfo;
56
60
  }): boolean;
61
+ export declare function calcElementOriginRectInfo(elemSize: ElementSize, opts: {
62
+ groupQueue: Element<'group'>[];
63
+ }): ViewRectInfo;
64
+ export declare function originRectInfoToRangeRectInfo(originRectInfo: ViewRectInfo): ViewRectInfo;
65
+ export declare function calcElementViewRectInfo(elemSize: ElementSize, opts: {
66
+ groupQueue: Element<'group'>[];
67
+ viewScaleInfo: ViewScaleInfo;
68
+ range?: boolean;
69
+ }): ViewRectInfo;
@@ -1,5 +1,7 @@
1
1
  import { rotateElementVertexes } from './rotate';
2
2
  import { checkRectIntersect } from './rect';
3
+ import { calcElementVertexesInGroup, calcElementVertexes } from './vertex';
4
+ import { getCenterFromTwoPoints } from './point';
3
5
  export function calcViewScaleInfo(info, opts) {
4
6
  const { scale, offsetX, offsetY } = info;
5
7
  const { viewSizeInfo } = opts;
@@ -86,9 +88,9 @@ export function calcViewVertexes(vertexes, opts) {
86
88
  ];
87
89
  }
88
90
  export function isViewPointInElement(p, opts) {
89
- const { context2d: ctx, element: elem, viewScaleInfo, viewSizeInfo } = opts;
91
+ const { context2d: ctx, element: elem, viewScaleInfo } = opts;
90
92
  const { angle = 0 } = elem;
91
- const { x, y, w, h } = calcViewElementSize(elem, { viewScaleInfo, viewSizeInfo });
93
+ const { x, y, w, h } = calcViewElementSize(elem, { viewScaleInfo });
92
94
  const vertexes = rotateElementVertexes({ x, y, w, h, angle });
93
95
  if (vertexes.length >= 2) {
94
96
  ctx.beginPath();
@@ -103,6 +105,25 @@ export function isViewPointInElement(p, opts) {
103
105
  }
104
106
  return false;
105
107
  }
108
+ export function isViewPointInElementSize(p, elemSize, opts) {
109
+ const vertexes = calcElementVertexes(elemSize);
110
+ return isViewPointInVertexes(p, vertexes, opts);
111
+ }
112
+ export function isViewPointInVertexes(p, vertexes, opts) {
113
+ const xList = [vertexes[0].x, vertexes[1].x, vertexes[2].x, vertexes[3].x];
114
+ const yList = [vertexes[0].y, vertexes[1].y, vertexes[2].y, vertexes[3].y];
115
+ const mixX = Math.min(...xList);
116
+ const maxX = Math.max(...xList);
117
+ const mixY = Math.min(...yList);
118
+ const maxY = Math.max(...yList);
119
+ if (p.x > mixX && p.x < maxX && p.y > mixY && p.y < maxY) {
120
+ return true;
121
+ }
122
+ if ((opts === null || opts === void 0 ? void 0 : opts.includeBorder) === true && (p.x === mixX || p.x === maxX || p.y === mixY || p.y === maxY)) {
123
+ return true;
124
+ }
125
+ return false;
126
+ }
106
127
  export function getViewPointAtElement(p, opts) {
107
128
  var _a, _b, _c;
108
129
  const { context2d: ctx, data, viewScaleInfo, viewSizeInfo, groupQueue } = opts;
@@ -174,7 +195,7 @@ export function isElementInView(elem, opts) {
174
195
  const { viewSizeInfo, viewScaleInfo } = opts;
175
196
  const { width, height } = viewSizeInfo;
176
197
  const { angle } = elem;
177
- const { x, y, w, h } = calcViewElementSize(elem, { viewScaleInfo, viewSizeInfo });
198
+ const { x, y, w, h } = calcViewElementSize(elem, { viewScaleInfo });
178
199
  const ves = rotateElementVertexes({ x, y, w, h, angle });
179
200
  const viewSize = { x: 0, y: 0, w: width, h: height };
180
201
  const elemStartX = Math.min(ves[0].x, ves[1].x, ves[2].x, ves[3].x);
@@ -184,3 +205,106 @@ export function isElementInView(elem, opts) {
184
205
  const elemSize = { x: elemStartX, y: elemStartY, w: elemEndX - elemStartX, h: elemEndY - elemStartY };
185
206
  return checkRectIntersect(viewSize, elemSize);
186
207
  }
208
+ export function calcElementOriginRectInfo(elemSize, opts) {
209
+ const { groupQueue } = opts;
210
+ const vertexes = calcElementVertexesInGroup(elemSize, { groupQueue });
211
+ const top = getCenterFromTwoPoints(vertexes[0], vertexes[1]);
212
+ const right = getCenterFromTwoPoints(vertexes[1], vertexes[2]);
213
+ const bottom = getCenterFromTwoPoints(vertexes[2], vertexes[3]);
214
+ const left = getCenterFromTwoPoints(vertexes[3], vertexes[0]);
215
+ const topLeft = vertexes[0];
216
+ const topRight = vertexes[1];
217
+ const bottomRight = vertexes[2];
218
+ const bottomLeft = vertexes[3];
219
+ const maxX = Math.max(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x);
220
+ const maxY = Math.max(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y);
221
+ const minX = Math.min(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x);
222
+ const minY = Math.min(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y);
223
+ const center = {
224
+ x: (maxX + minX) / 2,
225
+ y: (maxY + minY) / 2
226
+ };
227
+ const rectInfo = {
228
+ center,
229
+ topLeft,
230
+ topRight,
231
+ bottomLeft,
232
+ bottomRight,
233
+ top,
234
+ right,
235
+ left,
236
+ bottom
237
+ };
238
+ return rectInfo;
239
+ }
240
+ export function originRectInfoToRangeRectInfo(originRectInfo) {
241
+ const rangeMaxX = Math.max(originRectInfo.topLeft.x, originRectInfo.topRight.x, originRectInfo.bottomRight.x, originRectInfo.bottomLeft.x);
242
+ const rangeMaxY = Math.max(originRectInfo.topLeft.y, originRectInfo.topRight.y, originRectInfo.bottomRight.y, originRectInfo.bottomLeft.y);
243
+ const rangeMinX = Math.min(originRectInfo.topLeft.x, originRectInfo.topRight.x, originRectInfo.bottomRight.x, originRectInfo.bottomLeft.x);
244
+ const rangeMinY = Math.min(originRectInfo.topLeft.y, originRectInfo.topRight.y, originRectInfo.bottomRight.y, originRectInfo.bottomLeft.y);
245
+ const rangeCenter = { x: originRectInfo.center.x, y: originRectInfo.center.y };
246
+ const rangeTopLeft = { x: rangeMinX, y: rangeMinY };
247
+ const rangeTopRight = { x: rangeMaxX, y: rangeMinY };
248
+ const rangeBottomRight = { x: rangeMaxX, y: rangeMaxY };
249
+ const rangeBottomLeft = { x: rangeMinX, y: rangeMaxY };
250
+ const rangeTop = getCenterFromTwoPoints(rangeTopLeft, rangeTopRight);
251
+ const rangeBottom = getCenterFromTwoPoints(rangeBottomLeft, rangeBottomRight);
252
+ const rangeLeft = getCenterFromTwoPoints(rangeTopLeft, rangeBottomLeft);
253
+ const rangeRight = getCenterFromTwoPoints(rangeTopRight, rangeBottomRight);
254
+ const rangeRectInfo = {
255
+ center: rangeCenter,
256
+ topLeft: rangeTopLeft,
257
+ topRight: rangeTopRight,
258
+ bottomLeft: rangeBottomLeft,
259
+ bottomRight: rangeBottomRight,
260
+ top: rangeTop,
261
+ right: rangeRight,
262
+ left: rangeLeft,
263
+ bottom: rangeBottom
264
+ };
265
+ return rangeRectInfo;
266
+ }
267
+ export function calcElementViewRectInfo(elemSize, opts) {
268
+ const { groupQueue, viewScaleInfo, range } = opts;
269
+ const originRectInfo = calcElementOriginRectInfo(elemSize, { groupQueue });
270
+ const { center, top, bottom, left, right, topLeft, topRight, bottomLeft, bottomRight } = originRectInfo;
271
+ const viewRectInfo = {
272
+ center: calcViewPointSize(center, { viewScaleInfo }),
273
+ topLeft: calcViewPointSize(topLeft, { viewScaleInfo }),
274
+ topRight: calcViewPointSize(topRight, { viewScaleInfo }),
275
+ bottomLeft: calcViewPointSize(bottomLeft, { viewScaleInfo }),
276
+ bottomRight: calcViewPointSize(bottomRight, { viewScaleInfo }),
277
+ top: calcViewPointSize(top, { viewScaleInfo }),
278
+ right: calcViewPointSize(right, { viewScaleInfo }),
279
+ left: calcViewPointSize(left, { viewScaleInfo }),
280
+ bottom: calcViewPointSize(bottom, { viewScaleInfo })
281
+ };
282
+ if (range === true) {
283
+ const viewMaxX = Math.max(viewRectInfo.topLeft.x, viewRectInfo.topRight.x, viewRectInfo.bottomRight.x, viewRectInfo.bottomLeft.x);
284
+ const viewMaxY = Math.max(viewRectInfo.topLeft.y, viewRectInfo.topRight.y, viewRectInfo.bottomRight.y, viewRectInfo.bottomLeft.y);
285
+ const viewMinX = Math.min(viewRectInfo.topLeft.x, viewRectInfo.topRight.x, viewRectInfo.bottomRight.x, viewRectInfo.bottomLeft.x);
286
+ const viewMinY = Math.min(viewRectInfo.topLeft.y, viewRectInfo.topRight.y, viewRectInfo.bottomRight.y, viewRectInfo.bottomLeft.y);
287
+ const rangeCenter = { x: viewRectInfo.center.x, y: viewRectInfo.center.y };
288
+ const rangeTopLeft = { x: viewMinX, y: viewMinY };
289
+ const rangeTopRight = { x: viewMaxX, y: viewMinY };
290
+ const rangeBottomRight = { x: viewMaxX, y: viewMaxY };
291
+ const rangeBottomLeft = { x: viewMinX, y: viewMaxY };
292
+ const rangeTop = getCenterFromTwoPoints(rangeTopLeft, rangeTopRight);
293
+ const rangeBottom = getCenterFromTwoPoints(rangeBottomLeft, rangeBottomRight);
294
+ const rangeLeft = getCenterFromTwoPoints(rangeTopLeft, rangeBottomLeft);
295
+ const rangeRight = getCenterFromTwoPoints(rangeTopRight, rangeBottomRight);
296
+ const rangeRectInfo = {
297
+ center: rangeCenter,
298
+ topLeft: rangeTopLeft,
299
+ topRight: rangeTopRight,
300
+ bottomLeft: rangeBottomLeft,
301
+ bottomRight: rangeBottomRight,
302
+ top: rangeTop,
303
+ right: rangeRight,
304
+ left: rangeLeft,
305
+ bottom: rangeBottom
306
+ };
307
+ return rangeRectInfo;
308
+ }
309
+ return viewRectInfo;
310
+ }
@@ -0,0 +1,14 @@
1
+ import type { Data, ViewSizeInfo, ViewScaleInfo, PointSize } from '@idraw/types';
2
+ interface ViewCenterContentResult {
3
+ offsetX: number;
4
+ offsetY: number;
5
+ scale: number;
6
+ }
7
+ export declare function calcViewCenterContent(data: Data, opts: {
8
+ viewSizeInfo: ViewSizeInfo;
9
+ }): ViewCenterContentResult;
10
+ export declare function calcViewCenter(opts?: {
11
+ viewScaleInfo: ViewScaleInfo;
12
+ viewSizeInfo: ViewSizeInfo;
13
+ }): PointSize;
14
+ export {};
@@ -0,0 +1,88 @@
1
+ import { rotateElementVertexes } from './rotate';
2
+ import { formatNumber } from '../tool/number';
3
+ import { is } from './is';
4
+ export function calcViewCenterContent(data, opts) {
5
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
6
+ let offsetX = 0;
7
+ let offsetY = 0;
8
+ let scale = 1;
9
+ let contentX = ((_b = (_a = data === null || data === void 0 ? void 0 : data.elements) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.x) || 0;
10
+ let contentY = ((_d = (_c = data === null || data === void 0 ? void 0 : data.elements) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.y) || 0;
11
+ let contentW = ((_f = (_e = data === null || data === void 0 ? void 0 : data.elements) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.w) || 0;
12
+ let contentH = ((_h = (_g = data === null || data === void 0 ? void 0 : data.elements) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.h) || 0;
13
+ const { width, height } = opts.viewSizeInfo;
14
+ if (data.layout && ((_k = (_j = data.layout) === null || _j === void 0 ? void 0 : _j.detail) === null || _k === void 0 ? void 0 : _k.overflow) === 'hidden') {
15
+ contentX = 0;
16
+ contentY = 0;
17
+ contentW = data.layout.w || 0;
18
+ contentH = data.layout.h || 0;
19
+ }
20
+ else {
21
+ data.elements.forEach((elem) => {
22
+ const elemSize = {
23
+ x: elem.x,
24
+ y: elem.y,
25
+ w: elem.w,
26
+ h: elem.h,
27
+ angle: elem.angle
28
+ };
29
+ if (elemSize.angle && (elemSize.angle > 0 || elemSize.angle < 0)) {
30
+ const ves = rotateElementVertexes(elemSize);
31
+ if (ves.length === 4) {
32
+ const xList = [ves[0].x, ves[1].x, ves[2].x, ves[3].x];
33
+ const yList = [ves[0].y, ves[1].y, ves[2].y, ves[3].y];
34
+ elemSize.x = Math.min(...xList);
35
+ elemSize.y = Math.min(...yList);
36
+ elemSize.w = Math.abs(Math.max(...xList) - Math.min(...xList));
37
+ elemSize.h = Math.abs(Math.max(...yList) - Math.min(...yList));
38
+ }
39
+ }
40
+ const areaStartX = Math.min(elemSize.x, contentX);
41
+ const areaStartY = Math.min(elemSize.y, contentY);
42
+ const areaEndX = Math.max(elemSize.x + elemSize.w, contentX + contentW);
43
+ const areaEndY = Math.max(elemSize.y + elemSize.h, contentY + contentH);
44
+ contentX = areaStartX;
45
+ contentY = areaStartY;
46
+ contentW = Math.abs(areaEndX - areaStartX);
47
+ contentH = Math.abs(areaEndY - areaStartY);
48
+ });
49
+ }
50
+ if (data.layout) {
51
+ const { x, y, w, h } = data.layout;
52
+ if (is.x(x) && is.y(y) && is.w(w) && is.h(h)) {
53
+ contentX = Math.min(contentX, x);
54
+ contentY = Math.min(contentY, y);
55
+ contentW = Math.max(contentW, w);
56
+ contentH = Math.max(contentH, h);
57
+ }
58
+ }
59
+ if (contentW > 0 && contentH > 0) {
60
+ const scaleW = formatNumber(width / contentW, { decimalPlaces: 4 });
61
+ const scaleH = formatNumber(height / contentH, { decimalPlaces: 4 });
62
+ scale = Math.min(scaleW, scaleH, 1);
63
+ offsetX = (contentW * scale - width) / 2 / scale + contentX;
64
+ offsetY = (contentH * scale - height) / 2 / scale + contentY;
65
+ }
66
+ const result = {
67
+ offsetX: formatNumber(offsetX, { decimalPlaces: 0 }),
68
+ offsetY: formatNumber(offsetY, { decimalPlaces: 0 }),
69
+ scale
70
+ };
71
+ return result;
72
+ }
73
+ export function calcViewCenter(opts) {
74
+ let x = 0;
75
+ let y = 0;
76
+ if (opts) {
77
+ const { viewScaleInfo, viewSizeInfo } = opts;
78
+ const { offsetLeft, offsetTop, scale } = viewScaleInfo;
79
+ const { width, height } = viewSizeInfo;
80
+ x = 0 - offsetLeft + width / scale / 2;
81
+ y = 0 - offsetTop + height / scale / 2;
82
+ }
83
+ const p = {
84
+ x,
85
+ y
86
+ };
87
+ return p;
88
+ }