35
35
</div >
36
36
</section >
37
37
38
- <section v-if =" hasBothSides && !deltaHtml " class =" -match" >
38
+ <section v-if =" hasBothSides && !deltaObj " class =" -match" >
39
39
<div ref =" deltaEl" class =" -center" >match</div >
40
40
</section >
41
- <section v-else-if =" hasBothSides && deltaHtml " >
41
+ <section v-else-if =" hasBothSides && deltaObj " >
42
42
<div ref =" deltaEl" class =" -delta" v-html =" deltaHtml" />
43
43
</section >
44
44
<section v-if =" !hasBothSides" class =" -empty" >
57
57
</section >
58
58
</template >
59
59
60
- <script setup>
60
+ <script setup lang="ts" >
61
61
import { computed , onMounted , reactive , ref } from ' vue' ;
62
62
import packageJson from ' ../../../package.json' ;
63
63
import * as jsondiffpatch from ' jsondiffpatch' ;
64
64
import ' jsondiffpatch/dist/formatters-styles/html.css' ;
65
- import { timeFromNow } from ' ./api/time.ts' ;
66
- import { postDiffRender } from ' ./api/html-helper.ts' ;
65
+ import { timeFromNow } from ' ./api/time' ;
66
+ import { postDiffRender } from ' ./api/html-helper' ;
67
+ import { Delta } from ' jsondiffpatch' ;
67
68
68
69
const formatters = jsondiffpatch .formatters ;
69
- const deltaEl = ref (null );
70
+ const deltaEl = ref < HTMLElement | null > (null );
70
71
const appStartTimestamp = Date .now ();
71
72
const state = reactive ({
72
73
version: packageJson .version ,
@@ -76,30 +77,39 @@ const state = reactive({
76
77
},
77
78
codeExample: ' console.diff({a:1,b:1,c:3}, {a:1,b:2,d:3});' ,
78
79
showUnchanged: true ,
79
- compare: {
80
- timestamp: appStartTimestamp,
81
- left: null ,
82
- right: null ,
83
- },
84
80
now: appStartTimestamp ,
85
- timer: null ,
86
81
});
82
+ interface CompareState {
83
+ timestamp: number ;
84
+ left? : unknown ;
85
+ right? : unknown ;
86
+ }
87
+ const compare = ref <CompareState >({
88
+ timestamp: appStartTimestamp ,
89
+ left: undefined ,
90
+ right: undefined ,
91
+ });
92
+ let timer: number ;
87
93
88
94
const lastUpdated = computed (() =>
89
- timeFromNow (state . compare .timestamp , state .now )
95
+ timeFromNow (compare . value .timestamp , state .now )
90
96
);
91
97
92
98
const hasBothSides = computed (
93
- () => $_hasData (state . compare .left ) && $_hasData (state . compare .right )
99
+ () => $_hasData (compare .value . left ) && $_hasData (compare . value .right )
94
100
);
95
101
96
102
const deltaObj = computed (() =>
97
- jsondiffpatch .diff (state . compare .left , state . compare .right )
103
+ jsondiffpatch .diff (compare .value . left , compare . value .right )
98
104
);
99
105
100
106
const deltaHtml = computed (() => {
101
107
try {
102
- return formatters .html .format (deltaObj .value , state .compare .left );
108
+ if (deltaObj .value ) {
109
+ return formatters .html .format (<Delta >deltaObj .value , compare .value .left );
110
+ } else {
111
+ return ' ' ;
112
+ }
103
113
} catch (bug ) {
104
114
return JSON .stringify (bug );
105
115
}
@@ -126,20 +136,22 @@ onMounted(() => {
126
136
});
127
137
128
138
const onToggleUnchanged = () => {
129
- state .showUnchanged = ! state .showUnchanged ;
130
- formatters .html .showUnchanged (state .showUnchanged , deltaEl .value );
131
- postDiffRender (deltaEl .value );
139
+ if (deltaEl .value ) {
140
+ state .showUnchanged = ! state .showUnchanged ;
141
+ formatters .html .showUnchanged (state .showUnchanged , deltaEl .value );
142
+ postDiffRender (deltaEl .value );
143
+ }
132
144
};
133
145
134
146
const onCopyDelta = () => {
135
- const diff = jsondiffpatch .diff (state . compare .left , state . compare .right );
147
+ const diff = jsondiffpatch .diff (compare .value . left , compare . value .right );
136
148
const sDiff = JSON .stringify (diff , null , 2 );
137
149
138
150
document .oncopy = function (e ) {
139
- e .clipboardData .setData (' text' , sDiff);
151
+ e .clipboardData ? .setData (' text' , sDiff );
140
152
e .preventDefault ();
141
153
};
142
- document .execCommand (' copy' , false , null );
154
+ document .execCommand (' copy' , false );
143
155
// modern alternative, but don't have permission [?]
144
156
// navigator.clipboard.writeText(sDiff).then(
145
157
// () => {
@@ -152,32 +164,40 @@ const onCopyDelta = () => {
152
164
};
153
165
154
166
function $_restartLastUpdated() {
155
- state . compare .timestamp = Date .now ();
167
+ compare . value .timestamp = Date .now ();
156
168
157
- clearInterval (state . timer );
158
- state . timer = window .setInterval (() => {
169
+ window . clearInterval (timer );
170
+ timer = window .setInterval (() => {
159
171
state .now = Date .now ();
160
172
}, 1e3 );
161
173
}
162
174
163
- function $_onDiffRequest ({ left, right, push }) {
175
+ function $_onDiffRequest({
176
+ left ,
177
+ right ,
178
+ push ,
179
+ }: {
180
+ left? : unknown ;
181
+ right? : unknown ;
182
+ push? : unknown ;
183
+ }) {
164
184
if (push ) {
165
- state . compare .left = state . compare .right ;
166
- state . compare .right = push;
185
+ compare .value . left = compare . value .right ;
186
+ compare . value .right = push ;
167
187
} else {
168
188
if (left ) {
169
- state . compare .left = left;
189
+ compare . value .left = left ;
170
190
}
171
191
if (right ) {
172
- state . compare .right = right;
192
+ compare . value .right = right ;
173
193
}
174
194
}
175
195
176
196
$_restartLastUpdated ();
177
197
postDiffRender (deltaEl .value );
178
198
}
179
199
180
- function $_hasData (o ) {
200
+ function $_hasData(o : unknown ) : boolean {
181
201
return undefined !== o && null !== o ;
182
202
}
183
203
</script >
0 commit comments