1818 * 缺陷:
1919 * 1. 如果画布无法挂载 data-uid 属性到 DOM 节点上,那么该节点无法反查到对应的 node 节点,导致 hover 、选中等逻辑无法生效。
2020 */
21- import { nextTick , ref } from 'vue'
21+ import { ref } from 'vue'
2222import { useCanvas } from '@opentiny/tiny-engine-meta-register'
2323import { NODE_TAG , NODE_UID , NODE_INACTIVE_UID } from '../../../common'
2424import { getConfigure , scrollToNode , canvasState , getDocument , querySelectById } from '../container'
@@ -27,7 +27,8 @@ import {
2727 clearHover as commonClearHover ,
2828 getClosedElementHasUid ,
2929 getWindowRect ,
30- hoverNodeById as commonHoverNodeById
30+ hoverNodeById as commonHoverNodeById ,
31+ selectNodeById as commonSelectNodeById
3132} from './common'
3233import type { HoverOrSelectState } from './common'
3334
@@ -112,7 +113,7 @@ export const useHoverNode = () => {
112113 }
113114}
114115
115- const updateSelectedNode = async ( e : MouseEvent , type : string ) => {
116+ const updateSelectedNode = async ( e : MouseEvent , type : string , isMultipleSelect = false ) => {
116117 let res = getRectAndNode ( e )
117118
118119 if ( ! res ) {
@@ -121,8 +122,6 @@ const updateSelectedNode = async (e: MouseEvent, type: string) => {
121122 return
122123 }
123124
124- const isMultipleSelect = e . ctrlKey || e . metaKey
125-
126125 // 选中的是非当前编辑页的节点,改为选中顶层节点
127126 if ( ! res . node && res . isInactiveNode ) {
128127 // 多选选中非激活节点,忽略
@@ -171,44 +170,8 @@ const updateSelectedNode = async (e: MouseEvent, type: string) => {
171170 }
172171}
173172
174- // TODO: 支持多选
175- const selectNodeById = async ( id : string , type : string ) => {
176- // commonSelectNodeById(updateSelectedNode, id, type)
177- const element = querySelectById ( id )
178- const { node, parent } = useCanvas ( ) . getNodeWithParentById ( id ) || { }
179-
180- if ( ! element || ! node ) {
181- clearSelect ( )
182-
183- return
184- }
185-
186- const rect = element . getBoundingClientRect ( )
187- const componentName = node . componentName
188- const configure = getConfigure ( componentName )
189-
190- canvasState . current = node
191- canvasState . parent = parent
192- selectState . value = [
193- {
194- rect : {
195- top : rect . top ,
196- left : rect . left ,
197- width : rect . width ,
198- height : rect . height
199- } ,
200- node,
201- configure,
202- element,
203- componentName,
204- isInactiveNode : false
205- }
206- ]
207-
208- // TODO: 改成事件通知
209- canvasState . emit ( 'selected' , node , parent , type , node ?. id )
210- await nextTick ( )
211- await scrollToNode ( element )
173+ const selectNodeById = async ( id : string , type : string , isMultipleSelect = false ) => {
174+ commonSelectNodeById ( updateSelectedNode , id , type , isMultipleSelect )
212175}
213176
214177const updateSelectedRect = ( ) => {
@@ -218,31 +181,29 @@ const updateSelectedRect = () => {
218181 }
219182
220183 selectState . value = selectState . value . map ( ( stateItem ) => {
184+ // 优先需要尝试使用 querySelectById 计算 位置
185+ const element = querySelectById ( stateItem . node . id )
186+
187+ if ( element ) {
188+ const rect = element . getBoundingClientRect ( )
189+
190+ return {
191+ ...stateItem ,
192+ rect : {
193+ top : rect . top ,
194+ left : rect . left ,
195+ width : rect . width ,
196+ height : rect . height
197+ }
198+ }
199+ }
200+
221201 const res = getRectAndNode ( { target : stateItem . element } as unknown as MouseEvent )
222202
223203 if ( res ?. node ) {
224204 return res
225205 }
226206
227- // 通过节点没法直接计算到 rect,可能是没法挂载 data-uid 属性,需要尝试使用 querySelectById
228- if ( ! res ?. node ) {
229- const element = querySelectById ( stateItem . node . id )
230-
231- if ( element ) {
232- const rect = element . getBoundingClientRect ( )
233-
234- return {
235- ...stateItem ,
236- rect : {
237- top : rect . top ,
238- left : rect . left ,
239- width : rect . width ,
240- height : rect . height
241- }
242- }
243- }
244- }
245-
246207 return stateItem
247208 } )
248209 } , 0 )
0 commit comments