8
8
isEqual ,
9
9
isSame ,
10
10
newWizardEvent ,
11
- SimpleAction ,
12
11
Wizard ,
13
12
WizardActor
14
13
} from "../foundation.js" ;
@@ -19,13 +18,13 @@ import {ListItem} from "@material/mwc-list/mwc-list-item";
19
18
interface MergeOptions {
20
19
title ?: string ;
21
20
selected ?: ( diff : Diff < Element | string > ) => boolean ;
22
- auto ?: ( sink : Element , source : Element ) => boolean ;
21
+ auto ?: ( oldValue : Element , newValue : Element ) => boolean ;
23
22
}
24
23
25
24
export type Diff < T > =
26
- | { ours : T ; theirs : null }
27
- | { ours : null ; theirs : T }
28
- | { ours : T ; theirs : T } ;
25
+ | { oldValue : T ; newValue : null }
26
+ | { oldValue : null ; newValue : T }
27
+ | { oldValue : T ; newValue : T } ;
29
28
30
29
function describe ( element : Element ) : string {
31
30
const id = identity ( element ) ;
@@ -36,8 +35,8 @@ function describe(element: Element): string {
36
35
function compareWizardAction (
37
36
attrDiffs : [ string , Diff < string > ] [ ] ,
38
37
childDiffs : Diff < Element > [ ] ,
39
- sink : Element ,
40
- source : Element ,
38
+ oldValue : Element ,
39
+ newValue : Element ,
41
40
options ?: MergeOptions
42
41
) : WizardActor {
43
42
return ( _ , wizard : Element ) : EditorAction [ ] => {
@@ -49,11 +48,11 @@ function compareWizardAction(
49
48
. map ( item => childDiffs [ item . value as unknown as number ] ) ;
50
49
if ( selectedChildDiffs . length ) {
51
50
for ( const diff of selectedChildDiffs )
52
- if ( diff . ours && diff . theirs ) {
51
+ if ( diff . oldValue && diff . newValue ) {
53
52
acted = true ;
54
53
wizard . dispatchEvent (
55
54
newWizardEvent (
56
- compareWizard ( diff . ours , diff . theirs , {
55
+ compareWizard ( diff . oldValue , diff . newValue , {
57
56
...options ,
58
57
title : undefined ,
59
58
} )
@@ -70,75 +69,75 @@ function compareWizardAction(
70
69
{
71
70
actions : [ ] ,
72
71
title : get ( 'compas.compare.elementTitle' , {
73
- sink : describe ( sink ) ,
74
- source : describe ( source ) ,
75
- tag : sink . tagName ,
72
+ oldValue : describe ( oldValue ) ,
73
+ newValue : describe ( newValue ) ,
74
+ tag : oldValue . tagName ,
76
75
} ) ,
77
76
} ,
78
77
] ;
79
78
} ;
80
79
}
81
80
82
81
export function compareWizard (
83
- sink : Element ,
84
- source : Element ,
82
+ oldElement : Element ,
83
+ newElement : Element ,
85
84
options ?: MergeOptions
86
85
) : Wizard {
87
86
const attrDiffs : [ string , Diff < string > ] [ ] = [ ] ;
88
- const ourText = sink . textContent ?? '' ;
89
- const theirText = source . textContent ?? '' ;
87
+ const oldText = oldElement . textContent ?? '' ;
88
+ const newText = newElement . textContent ?? '' ;
90
89
91
- if ( sink . childElementCount === 0 &&
92
- source . childElementCount === 0 &&
93
- theirText !== ourText
90
+ if ( oldElement . childElementCount === 0 &&
91
+ newElement . childElementCount === 0 &&
92
+ newText !== oldText
94
93
) {
95
- attrDiffs . push ( [ 'value' , { ours : ourText , theirs : theirText } ] ) ;
94
+ attrDiffs . push ( [ 'value' , { oldValue : oldText , newValue : newText } ] ) ;
96
95
}
97
96
98
- const attributeNames = new Set ( source . getAttributeNames ( ) . concat ( sink . getAttributeNames ( ) ) ) ;
97
+ const attributeNames = new Set ( newElement . getAttributeNames ( ) . concat ( oldElement . getAttributeNames ( ) ) ) ;
99
98
100
99
for ( const name of attributeNames )
101
- if ( source . getAttribute ( name ) !== sink . getAttribute ( name ) )
100
+ if ( newElement . getAttribute ( name ) !== oldElement . getAttribute ( name ) )
102
101
attrDiffs . push ( [
103
102
name ,
104
103
< Diff < string > > {
105
- theirs : source . getAttribute ( name ) ,
106
- ours : sink . getAttribute ( name ) ,
104
+ newValue : newElement . getAttribute ( name ) ,
105
+ oldValue : oldElement . getAttribute ( name ) ,
107
106
} ,
108
107
] ) ;
109
108
110
109
const childDiffs : Diff < Element > [ ] = [ ] ;
111
- const ourChildren = Array . from ( sink . children ) ;
112
- const theirChildren = Array . from ( source . children ) ;
110
+ const ourChildren = Array . from ( oldElement . children ) ;
111
+ const theirChildren = Array . from ( newElement . children ) ;
113
112
114
- theirChildren . forEach ( theirs => {
113
+ theirChildren . forEach ( newValue => {
115
114
const twinIndex = ourChildren . findIndex ( ourChild =>
116
- isSame ( theirs , ourChild )
115
+ isSame ( newValue , ourChild )
117
116
) ;
118
- const ours = twinIndex > - 1 ? ourChildren [ twinIndex ] : null ;
117
+ const oldValue = twinIndex > - 1 ? ourChildren [ twinIndex ] : null ;
119
118
120
- if ( ours ) ourChildren . splice ( twinIndex , 1 ) ;
121
- if ( ours && isEqual ( theirs , ours ) ) return ;
119
+ if ( oldValue ) ourChildren . splice ( twinIndex , 1 ) ;
120
+ if ( oldValue && isEqual ( newValue , oldValue ) ) return ;
122
121
123
- if ( ! ours || ! isEqual ( theirs , ours ) ) childDiffs . push ( { theirs , ours } ) ;
122
+ if ( ! oldValue || ! isEqual ( newValue , oldValue ) ) childDiffs . push ( { newValue , oldValue } ) ;
124
123
} ) ;
125
124
126
- ourChildren . forEach ( ours => childDiffs . push ( { theirs : null , ours } ) ) ;
125
+ ourChildren . forEach ( oldValue => childDiffs . push ( { newValue : null , oldValue } ) ) ;
127
126
128
127
return [
129
128
{
130
129
title :
131
130
options ?. title ??
132
131
get ( 'compas.compare.elementTitle' , {
133
- sink : describe ( sink ) ,
134
- source : describe ( source ) ,
135
- tag : sink . tagName ,
132
+ oldValue : describe ( oldElement ) ,
133
+ newValue : describe ( newElement ) ,
134
+ tag : oldElement . tagName ,
136
135
} ) ,
137
136
primary : {
138
137
label : get ( 'compas.compare.primaryButton' ) ,
139
138
icon : 'compare' ,
140
- action : compareWizardAction ( attrDiffs , childDiffs , sink , source , options ) ,
141
- auto : options ?. auto ?.( sink , source ) ?? false ,
139
+ action : compareWizardAction ( attrDiffs , childDiffs , oldElement , newElement , options ) ,
140
+ auto : options ?. auto ?.( oldElement , newElement ) ?? false ,
142
141
} ,
143
142
content : [
144
143
html `
@@ -151,12 +150,12 @@ export function compareWizard(
151
150
left
152
151
hasMeta >
153
152
< span > ${ name } </ span >
154
- < span slot ="secondary "> ${ diff . ours ?? '' }
155
- ${ diff . ours && diff . theirs ? html `↶` : ' ' }
156
- ${ diff . theirs ?? '' } </ span >
153
+ < span slot ="secondary "> ${ diff . oldValue ?? '' }
154
+ ${ diff . oldValue && diff . newValue ? html `↶` : ' ' }
155
+ ${ diff . newValue ?? '' } </ span >
157
156
< mwc-icon slot ="meta ">
158
- ${ diff . ours
159
- ? diff . theirs
157
+ ${ diff . oldValue
158
+ ? diff . newValue
160
159
? 'edit'
161
160
: 'delete'
162
161
: 'add' } </ mwc-icon >
@@ -171,7 +170,7 @@ export function compareWizard(
171
170
childDiffs ,
172
171
e => e ,
173
172
( diff , index ) => {
174
- if ( diff . ours && diff . theirs ) {
173
+ if ( diff . oldValue && diff . newValue ) {
175
174
return html `
176
175
< mwc-check-list-item value =${ index }
177
176
class ="child"
@@ -180,14 +179,14 @@ export function compareWizard(
180
179
hasMeta
181
180
.selected=${ options ?. selected ?.( diff ) ?? false }
182
181
style="--mdc-checkbox-checked-color: var(--mdc-theme-
183
- ${ diff . ours
184
- ? diff . theirs
182
+ ${ diff . oldValue
183
+ ? diff . newValue
185
184
? 'secondary'
186
185
: 'error'
187
186
: 'primary' } );">
188
- < span > ${ diff . ours ?. tagName ?? diff . theirs ?. tagName } </ span >
187
+ < span > ${ diff . oldValue ?. tagName ?? diff . newValue ?. tagName } </ span >
189
188
< span slot ="secondary ">
190
- ${ describe ( diff . ours ) } ↶${ describe ( diff . theirs ) }
189
+ ${ describe ( diff . oldValue ) } ↶${ describe ( diff . newValue ) }
191
190
</ span >
192
191
< mwc-icon slot ="meta "> compare</ mwc-icon >
193
192
</ mwc-check-list-item > `
@@ -196,12 +195,12 @@ export function compareWizard(
196
195
< mwc-list-item twoline
197
196
left
198
197
hasMeta >
199
- < span > ${ diff . ours ?. tagName ?? diff . theirs ?. tagName } </ span >
198
+ < span > ${ diff . oldValue ?. tagName ?? diff . newValue ?. tagName } </ span >
200
199
< span slot ="secondary ">
201
- ${ diff . ours ? describe ( diff . ours ) : describe ( diff . theirs ) }
200
+ ${ diff . oldValue ? describe ( diff . oldValue ) : describe ( diff . newValue ) }
202
201
</ span >
203
202
< mwc-icon slot ="meta ">
204
- ${ diff . ours ? 'delete' : 'add' }
203
+ ${ diff . oldValue ? 'delete' : 'add' }
205
204
</ mwc-icon >
206
205
</ mwc-list-item > `
207
206
}
0 commit comments