@@ -2,7 +2,7 @@ import { useCanvas, useMessage } from '@opentiny/tiny-engine-meta-register'
22import * as Y from 'yjs'
33import { NodeSchemaModel } from '../models/NodeSchemaModel'
44import { DocManager } from './docManager'
5- import type { RootNode } from '../type'
5+ import type { RootNode , UpdateAttributesRole } from '../type'
66import { fromYjs , sanitizeSchema , toYjs } from '../utils'
77import { toRaw } from 'vue'
88import type { YjsProvider } from './providerManager'
@@ -14,6 +14,10 @@ type DiffPatch =
1414 | { type : 'array-delete' ; path : ( string | number ) [ ] ; count : number ; deletedIds : string [ ] }
1515 | { type : 'text-insert' ; path : ( string | number ) [ ] ; index : number ; text : string }
1616 | { type : 'text-delete' ; path : ( string | number ) [ ] ; index : number ; length : number }
17+ | { type : 'style-update' ; path : ( string | number ) [ ] ; css : string }
18+ | { type : 'class-add' ; path : ( string | number ) [ ] ; nodeId : string ; className : string }
19+ | { type : 'props-update' ; path : ( string | number ) [ ] ; props : Record < any , any > ; meta : Record < any , any > }
20+ | { type : 'methods-add-root' ; path : ( string | number ) [ ] ; methods : Record < string , any > }
1721 | {
1822 type : 'array-swap'
1923 direction : 'down' | 'up'
@@ -22,17 +26,20 @@ type DiffPatch =
2226 targetIndex : number
2327 swapIndex : number
2428 }
25- | { type : 'style-update' ; path : ( string | number ) [ ] ; css : string }
26- | { type : 'class-add' ; path : ( string | number ) [ ] ; nodeId : string ; className : string }
27- | { type : 'props-update' ; path : ( string | number ) [ ] ; props : Record < any , any > ; meta : Record < any , any > }
28- | { type : 'methods-add-root' ; path : ( string | number ) [ ] ; methods : Record < string , any > }
2929 | {
3030 type : 'methods-add-node'
3131 path : ( string | number ) [ ]
3232 methods : Record < string , any >
3333 methodsName : string
3434 nodeId : string
3535 }
36+ | {
37+ type : 'attributes-update'
38+ role : UpdateAttributesRole
39+ path : ( string | number ) [ ]
40+ value : boolean | Record < string , any > | undefined
41+ nodeId : string
42+ }
3643
3744/**
3845 * SchemaManager 类,负责管理 Yjs 中的 NodeSchema 文档
@@ -253,6 +260,7 @@ export class SchemaManager {
253260 if ( event . target instanceof Y . Map ) {
254261 const yMapNode = event . target
255262 const regex = / ^ o n [ A - Z ] [ A - Z a - z ] * $ /
263+ const attributesKey = [ 'condition' , 'loop' , 'loopArgs' , 'clean' ]
256264
257265 event . changes . keys . forEach ( ( change , key ) => {
258266 // 软删除
@@ -315,6 +323,29 @@ export class SchemaManager {
315323 methods : newMethods
316324 } )
317325 }
326+ } else if ( attributesKey . includes ( key ) ) {
327+ if ( key === 'condition' || key === 'loop' || key === 'loopArgs' ) {
328+ if ( change . action === 'add' || change . action === 'update' ) {
329+ const targetNodeId = yMapNode . get ( 'id' )
330+ const value = yMapNode . get ( key )
331+ patches . push ( {
332+ type : 'attributes-update' ,
333+ role : key ,
334+ path : event . path ,
335+ value,
336+ nodeId : targetNodeId
337+ } )
338+ } else if ( change . action === 'delete' && ( key === 'loop' || key === 'loopArgs' ) ) {
339+ const targetNodeId = yMapNode . get ( 'id' )
340+ patches . push ( {
341+ type : 'attributes-update' ,
342+ role : 'clean' ,
343+ path : event . path ,
344+ value : undefined ,
345+ nodeId : targetNodeId
346+ } )
347+ }
348+ }
318349 } else if ( regex . test ( key ) ) {
319350 // 先判断非根节点 添加时间函数
320351 if ( change . action === 'add' || change . action === 'update' ) {
@@ -478,6 +509,27 @@ export class SchemaManager {
478509 useMessage ( ) . publish ( { topic : 'schemaChange' , data : { } } )
479510 break
480511 }
512+ case 'attributes-update' : {
513+ const { role, value, nodeId } = patch
514+ const targetNode = useCanvas ( ) . getNode ( nodeId , false )
515+
516+ if ( role === 'condition' ) {
517+ if ( ! ( value as boolean ) ) {
518+ useCanvas ( ) . operateNode ( { type : 'updateAttributes' , id : nodeId , value : { condition : value } } )
519+ } else {
520+ const { condition : _schemaCondition , children, ...rest } = targetNode
521+ useCanvas ( ) . operateNode ( { type : 'updateAttributes' , id : nodeId , value : { ...rest } , overwrite : true } )
522+ }
523+ } else if ( role === 'loop' ) {
524+ useCanvas ( ) . operateNode ( { type : 'updateAttributes' , id : nodeId , value : { loop : value } } )
525+ } else if ( role === 'loopArgs' ) {
526+ useCanvas ( ) . operateNode ( { type : 'updateAttributes' , id : nodeId , value : { loopArgs : value } } )
527+ } else if ( role === 'clean' ) {
528+ const { loop : _loop , loopArgs : _loopArgs , children : _children , ...rest } = targetNode
529+ useCanvas ( ) . operateNode ( { type : 'updateAttributes' , id : nodeId , value : rest , overwrite : true } )
530+ }
531+ break
532+ }
481533 default :
482534 break
483535 }
0 commit comments