@@ -6,7 +6,7 @@ import type { RootNode, UpdateAttributesRole } from '../type'
66import { fromYjs , sanitizeSchema , toYjs } from '../utils'
77import { toRaw } from 'vue'
88import type { YjsProvider } from './providerManager'
9- import { IGNORE_OBSERVER_ORIGIN , ROOT_SCHEMA_MAP } from '../config'
9+ import { IGNORE_OBSERVER_ORIGIN , INTERNAL_YJS_KEYS , ROOT_SCHEMA_MAP } from '../config'
1010
1111type DiffPatch =
1212 | { type : 'add' | 'update' | 'delete' ; path : ( string | number ) [ ] ; value ?: any }
@@ -27,11 +27,9 @@ type DiffPatch =
2727 | { type : 'methods-delete' ; path : ( string | number ) [ ] ; nodeId : string ; methodsName : string }
2828 | {
2929 type : 'array-swap'
30- direction : 'down' | 'up'
3130 parentId : string | undefined
3231 schemaId : string
33- targetIndex : number
34- swapIndex : number
32+ swapId : string
3533 }
3634 | {
3735 type : 'methods-add-node'
@@ -126,7 +124,6 @@ export class SchemaManager {
126124 // eslint-disable-next-line no-console
127125 console . log ( `[${ docName } ] Remote has data. Importing remote schema to UI.` )
128126 const rawRemoteSchema = fromYjs ( yMap ! )
129- const INTERNAL_YJS_KEYS = [ 'meta' , '_methods_deleted' , '_node_deleted' , 'newNode' ]
130127 const cleanSchema = sanitizeSchema ( rawRemoteSchema , INTERNAL_YJS_KEYS )
131128 useCanvas ( ) . importSchema ( cleanSchema )
132129 }
@@ -199,13 +196,10 @@ export class SchemaManager {
199196 if ( payload && payload . op === 'move' ) {
200197 const patch : DiffPatch = {
201198 type : 'array-swap' ,
202- direction : payload . direction ,
203199 parentId : payload . parentId ,
204- schemaId : payload . schemeId ,
205- targetIndex : payload . targetIndex ,
206- swapIndex : payload . swapIndex
200+ schemaId : payload . schemaId ,
201+ swapId : payload . swapId
207202 }
208-
209203 this . applyPatches ( docName , [ patch ] )
210204 } else if ( payload && payload . op === 'insert' ) {
211205 const patch : DiffPatch = {
@@ -422,10 +416,15 @@ export class SchemaManager {
422416 const parentNode = patch . parentId ? useCanvas ( ) . getNode ( patch . parentId , false ) : useCanvas ( ) . getPageSchema ( )
423417 const childrenArray : any [ ] = parentNode . children
424418
425- if ( patch . targetIndex > - 1 && patch . swapIndex < childrenArray . length ) {
426- ; [ childrenArray [ patch . targetIndex ] , childrenArray [ patch . swapIndex ] ] = [
427- childrenArray [ patch . swapIndex ] ,
428- childrenArray [ patch . targetIndex ]
419+ // 通过 Id 找到需要交换位置两个节点的索引
420+ const targetIndex = childrenArray . findIndex ( ( node ) => node . id === patch . schemaId )
421+ const swapIndex = childrenArray . findIndex ( ( node ) => node . id === patch . swapId )
422+
423+ // 交换位置
424+ if ( targetIndex > - 1 && swapIndex < childrenArray . length ) {
425+ ; [ childrenArray [ targetIndex ] , childrenArray [ swapIndex ] ] = [
426+ childrenArray [ swapIndex ] ,
427+ childrenArray [ targetIndex ]
429428 ]
430429 }
431430
0 commit comments