@flowgram-vue/free-snap-plugin 0.2.0

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.
package/src/type.ts ADDED
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import type { WorkflowNodeEntity } from '@flowgram-vue/free-layout-core';
7
+ import type { Rectangle } from '@flowgram-vue/utils';
8
+
9
+ export interface SnapNodeRect {
10
+ id: string;
11
+ rect: Rectangle;
12
+ entity: WorkflowNodeEntity;
13
+ }
14
+
15
+ export interface SnapLine {
16
+ x?: number;
17
+ y?: number;
18
+ sourceNodeId: string;
19
+ }
20
+
21
+ export interface SnapHorizontalLine extends SnapLine {
22
+ y: number;
23
+ }
24
+
25
+ export interface SnapVerticalLine extends SnapLine {
26
+ x: number;
27
+ }
28
+
29
+ export interface SnapMidHorizontalLine extends SnapLine {
30
+ y: number;
31
+ }
32
+
33
+ export interface SnapMidVerticalLine extends SnapLine {
34
+ x: number;
35
+ }
36
+
37
+ export interface SnapLines {
38
+ horizontal: SnapHorizontalLine[];
39
+ vertical: SnapVerticalLine[];
40
+ midHorizontal: SnapMidHorizontalLine[];
41
+ midVertical: SnapMidVerticalLine[];
42
+ }
43
+
44
+ export interface SnapEdgeLines {
45
+ top?: SnapHorizontalLine;
46
+ bottom?: SnapHorizontalLine;
47
+ left?: SnapVerticalLine;
48
+ right?: SnapVerticalLine;
49
+ midHorizontal?: SnapMidHorizontalLine;
50
+ midVertical?: SnapMidVerticalLine;
51
+ }
52
+
53
+ export interface AlignRect {
54
+ rect: Rectangle;
55
+ sourceNodeId: string;
56
+ }
57
+
58
+ export interface AlignRects {
59
+ top: AlignRect[];
60
+ bottom: AlignRect[];
61
+ left: AlignRect[];
62
+ right: AlignRect[];
63
+ }
64
+
65
+ export interface AlignSpacing {
66
+ top?: number;
67
+ bottom?: number;
68
+ left?: number;
69
+ right?: number;
70
+ midHorizontal?: number;
71
+ midVertical?: number;
72
+ }
73
+
74
+ export interface SnapEvent {
75
+ snapEdgeLines: SnapEdgeLines;
76
+ snapRect: Rectangle;
77
+ alignRects: AlignRects;
78
+ alignSpacing: AlignSpacing;
79
+ }
80
+
81
+ export interface WorkflowSnapServiceOptions {
82
+ edgeThreshold: number;
83
+ gridSize: number;
84
+ enableGridSnapping: boolean;
85
+ enableEdgeSnapping: boolean;
86
+ enableMultiSnapping: boolean;
87
+ enableOnlyViewportSnapping: boolean;
88
+ }
89
+
90
+ export interface WorkflowSnapLayerOptions {
91
+ edgeColor: string;
92
+ alignColor: string;
93
+ edgeLineWidth: number;
94
+ alignLineWidth: number;
95
+ alignCrossWidth: number;
96
+ }
97
+
98
+ export type FreeSnapPluginOptions = Partial<WorkflowSnapServiceOptions & WorkflowSnapLayerOptions>;
package/src/utils.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { Epsilon } from './constant';
7
+
8
+ /** 检查浮点数 a 是否等于 b */
9
+ export const isEqual = (a: number | undefined, b: number | undefined): boolean => {
10
+ if (a === undefined || b === undefined) {
11
+ return false;
12
+ }
13
+ // 检查 a 和 b 的差的绝对值是否小于 Epsilon
14
+ return Math.abs(a - b) < Epsilon;
15
+ };
16
+
17
+ /** 检查浮点数 a 是否小于 b */
18
+ export const isLessThan = (a: number | undefined, b: number | undefined): boolean => {
19
+ if (a === undefined || b === undefined) {
20
+ return false;
21
+ }
22
+ // 检查 a 是否显著小于 b
23
+ return b - a > Epsilon;
24
+ };
25
+
26
+ /** 检查浮点数 a 是否大于 b */
27
+ export const isGreaterThan = (a: number | undefined, b: number | undefined): boolean => {
28
+ if (a === undefined || b === undefined) {
29
+ return false;
30
+ }
31
+ return a - b > Epsilon;
32
+ };
33
+
34
+ /** 检查浮点数 a 是否小于等于 b */
35
+ export const isLessThanOrEqual = (a: number | undefined, b: number | undefined): boolean =>
36
+ isEqual(a, b) || isLessThan(a, b);
37
+
38
+ /** 检查浮点数 a 是否大于等于 b */
39
+ export const isGreaterThanOrEqual = (a: number | undefined, b: number | undefined): boolean =>
40
+ isEqual(a, b) || isGreaterThan(a, b);
41
+
42
+ /** 检查值是否是数字类型 */
43
+ export const isNumber = (value: unknown): value is number =>
44
+ typeof value === 'number' && !isNaN(value);