1- import type { DeleteOperation , MoveOperation , Node , NodeOperation , PageSchema } from '../type'
1+ import type {
2+ DeleteOperation ,
3+ MoveOperation ,
4+ Node ,
5+ NodeOperation ,
6+ PageSchema ,
7+ UpdatePropsOperation ,
8+ UpdateStyleOperation
9+ } from '../type'
210import * as Y from 'yjs'
311import { toYjs } from '../utils'
412import { DocManager } from '../services/docManager'
@@ -154,7 +162,8 @@ export class OperationHandler {
154162 }
155163
156164 // 修改节点样式
157- public updatedStyle ( strStyle : string , nodeId : string , className : string ) {
165+ public updatedStyle ( operation : UpdateStyleOperation ) {
166+ const { strStyle, nodeId, className } = operation
158167 // 添加样式
159168 this . yMap . set ( 'css' , strStyle )
160169
@@ -165,6 +174,46 @@ export class OperationHandler {
165174 Object . assign ( this . rootSchema , { css : strStyle } )
166175 }
167176
177+ // 修改节点属性
178+ public updatedProps ( operation : UpdatePropsOperation ) {
179+ const { newProps, nodeId, overwrite } = operation
180+ let node = this . getYNode ( nodeId )
181+
182+ if ( ! node ) {
183+ node = this . yMap
184+ }
185+
186+ const yNewProps = new Y . Map < any > ( ) // 新的 props
187+ const propsMap = node . get ( 'props' ) as Y . Map < any > // 旧的 props
188+
189+ if ( overwrite ) {
190+ // 覆盖模式
191+ for ( const [ k , v ] of Object . entries ( newProps || { } ) ) {
192+ yNewProps . set ( k , v )
193+ }
194+ } else {
195+ // 先复制旧的
196+ if ( propsMap ) {
197+ propsMap . forEach ( ( val , key ) => {
198+ yNewProps . set ( key , val )
199+ } )
200+ }
201+
202+ // 再合并新的
203+ for ( const [ k , v ] of Object . entries ( newProps ) || { } ) {
204+ yNewProps . set ( k , v )
205+ }
206+ }
207+
208+ // 元数据,用于补丁操作
209+ const meta = new Y . Map < any > ( )
210+ meta . set ( 'nodeId' , nodeId )
211+ meta . set ( 'overwrite' , overwrite )
212+
213+ yNewProps . set ( 'meta' , meta )
214+ node . set ( 'props' , yNewProps )
215+ }
216+
168217 // 重建整个映射(刷新后可以手动调用)
169218 public rebuildYNodeMap ( rootSchema : PageSchema ) {
170219 this . yNodeMap . clear ( )
0 commit comments