Skip to content

Commit 5797628

Browse files
committed
fix: optimize code according to review comment
1 parent 736f995 commit 5797628

File tree

3 files changed

+8
-28
lines changed

3 files changed

+8
-28
lines changed

packages/canvas/container/src/interactions/common.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ export const initialHoverState = {
6262
}
6363

6464
export const clearHover = (hoverState: Ref<HoverOrSelectState>) => {
65-
hoverState.value = {
66-
...initialHoverState,
67-
rect: { ...initialHoverState.rect }
68-
}
65+
hoverState.value = structuredClone(initialHoverState)
6966
}
7067

7168
export const getClosedElementHasUid = (element: Element | null): Element | undefined => {

packages/canvas/container/src/interactions/default-interactions.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,21 @@ import {
3131
} from './common'
3232
import type { HoverOrSelectState } from './common'
3333

34-
const curHoverState = ref<HoverOrSelectState>({
35-
...initialHoverState,
36-
rect: { ...initialHoverState.rect }
37-
})
38-
34+
const curHoverState = ref<HoverOrSelectState>(structuredClone(initialHoverState))
3935
const selectState = ref<HoverOrSelectState[]>([])
40-
4136
const clearHover = () => commonClearHover(curHoverState)
4237

4338
const getRectAndNode = (e: MouseEvent): HoverOrSelectState | undefined => {
4439
const element = getClosedElementHasUid(e.target as Element)
45-
46-
let res: HoverOrSelectState = {
47-
...initialHoverState,
48-
rect: { ...initialHoverState.rect }
49-
}
40+
let res: HoverOrSelectState = structuredClone(initialHoverState)
5041

5142
if (!element) {
5243
return res
5344
}
5445

5546
// hover 整个页面
5647
if (element === element?.ownerDocument?.body) {
57-
res.rect = { ...getWindowRect() }
48+
res.rect = getWindowRect()
5849

5950
return res
6051
}
@@ -140,7 +131,7 @@ const updateSelectedNode = async (e: MouseEvent, type: string) => {
140131
}
141132

142133
res = {
143-
rect: { ...getWindowRect() },
134+
rect: getWindowRect(),
144135
node: null,
145136
configure: null,
146137
element: getDocument().body,

packages/canvas/container/src/interactions/vue-interactions.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ export const getClosedVueElement = (element: HTMLElementWithVue | null): VueInst
5555
return null
5656
}
5757

58-
const curHoverState = ref<HoverOrSelectState>({
59-
...initialHoverState,
60-
rect: { ...initialHoverState.rect }
61-
})
58+
const curHoverState = ref<HoverOrSelectState>(structuredClone(initialHoverState))
6259

6360
const selectState = ref<HoverOrSelectState[]>([])
6461

@@ -67,12 +64,7 @@ const clearHover = () => commonClearHover(curHoverState)
6764
const getRectAndNode = (e: { target: HTMLElementWithVue }): HoverOrSelectState => {
6865
// 拿到最近的带有 __vueComponent 的vue 实例
6966
let instance = getClosedVueElement(e.target)
70-
71-
const res: HoverOrSelectState = {
72-
...initialHoverState,
73-
rect: { ...initialHoverState.rect }
74-
}
75-
67+
const res: HoverOrSelectState = structuredClone(initialHoverState)
7668
const windowRect = getWindowRect()
7769

7870
if (!instance) {
@@ -175,7 +167,7 @@ const updateSelectedNode = async (e: MouseEvent, type: string): Promise<void> =>
175167

176168
// 非多选选中非激活节点,则选中整个画布
177169
res = {
178-
rect: { ...getWindowRect() },
170+
rect: getWindowRect(),
179171
node: null,
180172
configure: null,
181173
element: getDocument().body,

0 commit comments

Comments
 (0)