@@ -3,7 +3,7 @@ import * as Y from 'yjs'
33import { NodeSchemaModel } from '../models/NodeSchemaModel'
44import { DocManager } from './docManager'
55import type { RootNode } from '../type'
6- import { fromYjs , toYjs } from '../utils'
6+ import { fromYjs , sanitizeSchema , toYjs } from '../utils'
77import { toRaw } from 'vue'
88import type { YjsProvider } from './providerManager'
99import { IGNORE_OBSERVER_ORIGIN , ROOT_SCHEMA_MAP } from '../config'
@@ -25,6 +25,14 @@ type DiffPatch =
2525 | { type : 'style-update' ; path : ( string | number ) [ ] ; css : string }
2626 | { type : 'class-add' ; path : ( string | number ) [ ] ; nodeId : string ; className : string }
2727 | { 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 > }
29+ | {
30+ type : 'methods-add-node'
31+ path : ( string | number ) [ ]
32+ methods : Record < string , any >
33+ methodsName : string
34+ nodeId : string
35+ }
2836
2937/**
3038 * SchemaManager 类,负责管理 Yjs 中的 NodeSchema 文档
@@ -90,8 +98,14 @@ export class SchemaManager {
9098 console . log ( `[${ docName } ] Initial sync complete. Applying full schema to UI.` )
9199
92100 // 安全时间点,用 Yjs 的权威数据完全覆盖 UI
93- const remoteSchema = fromYjs ( yMap )
94- useCanvas ( ) . importSchema ( remoteSchema )
101+ const rawRemoteSchema = fromYjs ( yMap )
102+
103+ // 净化 schema
104+ const INTERNAL_YJS_KEYS = [ 'meta' ] // 定义需要过滤的键
105+ const cleanSchema = sanitizeSchema ( rawRemoteSchema , INTERNAL_YJS_KEYS )
106+
107+ // 使用干净的 schema 来覆盖 UI
108+ useCanvas ( ) . importSchema ( cleanSchema )
95109
96110 // 标记初始同步已完成
97111 this . initialSyncDone . set ( docName , true )
@@ -102,7 +116,7 @@ export class SchemaManager {
102116 // 冷启动:第一次启动,远端无数据 -> 用本地初始化
103117 if ( yMap . size === 0 ) {
104118 ydoc . transact ( ( ) => {
105- toYjs ( yMap ! , pageSchema )
119+ toYjs ( yMap ! , toRaw ( useCanvas ( ) . getPageSchema ( ) ) )
106120 } , IGNORE_OBSERVER_ORIGIN )
107121 }
108122 } else {
@@ -191,7 +205,6 @@ export class SchemaManager {
191205 | { yRoot : Y . Map < any > ; cb : ( events : Y . YEvent < any > [ ] , tr : Y . Transaction ) => void }
192206 | undefined
193207
194- // 判断时也使用正确的大小写
195208 if ( prev ?. yRoot && prev ?. cb ) {
196209 try {
197210 prev . yRoot . unobserveDeep ( prev . cb )
@@ -239,6 +252,7 @@ export class SchemaManager {
239252 // Map 变更
240253 if ( event . target instanceof Y . Map ) {
241254 const yMapNode = event . target
255+ const regex = / ^ o n [ A - Z ] [ A - Z a - z ] * $ /
242256
243257 event . changes . keys . forEach ( ( change , key ) => {
244258 // 软删除
@@ -283,16 +297,37 @@ export class SchemaManager {
283297 // Props 属性更新同步逻辑
284298 if ( change . action === 'add' || change . action === 'update' ) {
285299 const newProps = yMapNode . get ( 'props' ) . toJSON ( )
286- const meta = newProps . meta // meta 包含操作的 nodeId 和 overwrite
287-
288- delete newProps . meta // 删除元数据,保持 props 不被污染
300+ const { meta, ...cleanProps } = newProps
289301 patches . push ( {
290302 type : 'props-update' ,
291303 path : event . path ,
292- props : newProps ,
304+ props : cleanProps ,
293305 meta
294306 } )
295307 }
308+ } else if ( key === 'methods' ) {
309+ // 根节点添加 事件函数
310+ if ( change . action === 'add' || change . action === 'update' ) {
311+ const newMethods = yMapNode . get ( 'methods' )
312+ patches . push ( {
313+ type : 'methods-add-root' ,
314+ path : event . path ,
315+ methods : newMethods
316+ } )
317+ }
318+ } else if ( regex . test ( key ) ) {
319+ // 先判断非根节点 添加时间函数
320+ if ( change . action === 'add' || change . action === 'update' ) {
321+ const newObj = yMapNode . get ( key )
322+ const { meta, methods } = newObj
323+ patches . push ( {
324+ type : 'methods-add-node' ,
325+ path : event . path ,
326+ methods,
327+ methodsName : key ,
328+ nodeId : meta . nodeId
329+ } )
330+ }
296331 }
297332 } )
298333 }
@@ -413,10 +448,9 @@ export class SchemaManager {
413448 break
414449 }
415450 case 'style-update' : {
416- const { updateSchema } = useCanvas ( )
417451 const strStyle = patch . css
418452
419- updateSchema ( { css : strStyle } )
453+ useCanvas ( ) . updateSchema ( { css : strStyle } )
420454 break
421455 }
422456 case 'props-update' : {
@@ -431,6 +465,19 @@ export class SchemaManager {
431465 } )
432466 break
433467 }
468+ case 'methods-add-root' : {
469+ const { methods } = patch
470+ useCanvas ( ) . updateSchema ( { methods } )
471+ break
472+ }
473+ case 'methods-add-node' : {
474+ const { methods, methodsName, nodeId } = patch
475+ const targetNode = useCanvas ( ) . getNode ( nodeId , false )
476+
477+ targetNode [ methodsName ] = methods
478+ useMessage ( ) . publish ( { topic : 'schemaChange' , data : { } } )
479+ break
480+ }
434481 default :
435482 break
436483 }
0 commit comments