1+ import { Empty } from '@bufbuild/protobuf'
12import { EditorBlock } from '@seed-hypermedia/client/editor-types'
23import { editorBlockToHMBlock } from '@seed-hypermedia/client/editorblock-to-hmblock'
34import { HMBlock , HMBlockNode , HMMetadata , HMQuery } from '@seed-hypermedia/client/hm-types'
@@ -16,35 +17,57 @@ export type BlocksMapItem = {
1617 block : HMBlock
1718}
1819
19- export function getDocAttributeChanges ( metadata : HMMetadata ) {
20- const changes = [ ]
21- if ( metadata . name !== undefined ) changes . push ( docAttributeChangeString ( [ 'name' ] , metadata . name ) )
22- if ( metadata . summary !== undefined ) changes . push ( docAttributeChangeString ( [ 'summary' ] , metadata . summary ) )
23- if ( metadata . icon !== undefined ) changes . push ( docAttributeChangeString ( [ 'icon' ] , metadata . icon ) )
24- if ( metadata . thumbnail !== undefined ) changes . push ( docAttributeChangeString ( [ 'thumbnail' ] , metadata . thumbnail ) )
25- if ( metadata . cover !== undefined ) changes . push ( docAttributeChangeString ( [ 'cover' ] , metadata . cover ) )
26- if ( metadata . siteUrl !== undefined ) changes . push ( docAttributeChangeString ( [ 'siteUrl' ] , metadata . siteUrl ) )
27- if ( metadata . layout !== undefined ) changes . push ( docAttributeChangeString ( [ 'layout' ] , metadata . layout ) )
28- if ( metadata . displayAuthor !== undefined )
29- changes . push ( docAttributeChangeString ( [ 'displayAuthor' ] , metadata . displayAuthor ) )
30- if ( metadata . displayPublishTime !== undefined )
31- changes . push ( docAttributeChangeString ( [ 'displayPublishTime' ] , metadata . displayPublishTime ) )
32- if ( metadata . seedExperimentalLogo !== undefined )
33- changes . push ( docAttributeChangeString ( [ 'seedExperimentalLogo' ] , metadata . seedExperimentalLogo ) )
34- if ( metadata . seedExperimentalHomeOrder !== undefined )
35- changes . push ( docAttributeChangeString ( [ 'seedExperimentalHomeOrder' ] , metadata . seedExperimentalHomeOrder ) )
36- if ( metadata . showOutline !== undefined ) changes . push ( docAttributeChangeBool ( [ 'showOutline' ] , metadata . showOutline ) )
37- if ( metadata . theme !== undefined ) {
38- if ( metadata . theme . headerLayout !== undefined )
39- changes . push ( docAttributeChangeString ( [ 'theme' , 'headerLayout' ] , metadata . theme . headerLayout ) )
20+ export function getDocAttributeChanges ( metadata : HMMetadata , baseMetadata ?: HMMetadata ) {
21+ return getAttributeChangesForObject (
22+ metadata as Record < string , unknown > ,
23+ baseMetadata as Record < string , unknown > | undefined ,
24+ )
25+ }
26+
27+ function getAttributeChangesForObject (
28+ metadata : Record < string , unknown > ,
29+ baseMetadata : Record < string , unknown > | undefined ,
30+ ) {
31+ const changes : DocumentChange [ ] = [ ]
32+ const keys = new Set ( [ ...Object . keys ( baseMetadata ?? { } ) , ...Object . keys ( metadata ?? { } ) ] )
33+ for ( const key of Array . from ( keys ) ) {
34+ pushAttributeChanges ( changes , [ key ] , metadata ?. [ key ] , baseMetadata ?. [ key ] )
35+ }
36+ return changes
37+ }
38+
39+ function pushAttributeChanges ( changes : DocumentChange [ ] , key : string [ ] , value : unknown , baseValue : unknown ) {
40+ if ( isPlainObject ( value ) || isPlainObject ( baseValue ) ) {
41+ const valueObj = isPlainObject ( value ) ? value : undefined
42+ const baseObj = isPlainObject ( baseValue ) ? baseValue : undefined
43+ const keys = new Set ( [ ...Object . keys ( baseObj ?? { } ) , ...Object . keys ( valueObj ?? { } ) ] )
44+ for ( const childKey of Array . from ( keys ) ) {
45+ pushAttributeChanges ( changes , [ ...key , childKey ] , valueObj ?. [ childKey ] , baseObj ?. [ childKey ] )
46+ }
47+ return
4048 }
41- if ( metadata . contentWidth !== undefined ) {
42- changes . push ( docAttributeChangeString ( [ 'contentWidth' ] , metadata . contentWidth ) )
49+
50+ if ( baseValue !== undefined && value === undefined ) {
51+ changes . push ( docAttributeChangeNull ( key ) )
52+ return
4353 }
44- if ( metadata . showActivity !== undefined ) {
45- changes . push ( docAttributeChangeBool ( [ 'showActivity' ] , metadata . showActivity ) )
54+ if ( baseValue !== undefined && value === baseValue ) return
55+
56+ if ( typeof value === 'string' ) {
57+ changes . push ( docAttributeChangeString ( key , value ) )
58+ } else if ( typeof value === 'boolean' ) {
59+ changes . push ( docAttributeChangeBool ( key , value ) )
60+ } else if ( typeof value === 'number' && Number . isInteger ( value ) ) {
61+ changes . push ( docAttributeChangeInt ( key , value ) )
62+ } else if ( typeof value === 'bigint' ) {
63+ changes . push ( docAttributeChangeInt ( key , value ) )
64+ } else if ( value === null ) {
65+ changes . push ( docAttributeChangeNull ( key ) )
4666 }
47- return changes
67+ }
68+
69+ function isPlainObject ( value : unknown ) : value is Record < string , unknown > {
70+ return ! ! value && typeof value === 'object' && ! Array . isArray ( value )
4871}
4972
5073type PrimitiveValue = string | number | boolean | null | undefined
@@ -75,21 +98,37 @@ function docAttributeChangeString(key: string[], value: string) {
7598 } ,
7699 } )
77100}
78- // function docAttributeChangeInt(key: string[], value: number) {
79- // return new DocumentChange({
80- // op: {
81- // case: 'setAttribute',
82- // value: new DocumentChange_SetAttribute({
83- // blockId: '',
84- // key,
85- // value: {
86- // case: 'intValue',
87- // value: BigInt(value),
88- // },
89- // }),
90- // },
91- // })
92- // }
101+ function docAttributeChangeInt ( key : string [ ] , value : number | bigint ) {
102+ return new DocumentChange ( {
103+ op : {
104+ case : 'setAttribute' ,
105+ value : new DocumentChange_SetAttribute ( {
106+ blockId : '' ,
107+ key,
108+ value : {
109+ case : 'intValue' ,
110+ value : BigInt ( value ) ,
111+ } ,
112+ } ) ,
113+ } ,
114+ } )
115+ }
116+
117+ function docAttributeChangeNull ( key : string [ ] ) {
118+ return new DocumentChange ( {
119+ op : {
120+ case : 'setAttribute' ,
121+ value : new DocumentChange_SetAttribute ( {
122+ blockId : '' ,
123+ key,
124+ value : {
125+ case : 'nullValue' ,
126+ value : new Empty ( ) ,
127+ } ,
128+ } ) ,
129+ } ,
130+ } )
131+ }
93132function docAttributeChangeBool ( key : string [ ] , value : boolean ) {
94133 return new DocumentChange ( {
95134 op : {
0 commit comments