Skip to content

Commit 601bf70

Browse files
committed
add typescript to panel.vue
1 parent f311c94 commit 601bf70

File tree

6 files changed

+70
-47
lines changed

6 files changed

+70
-47
lines changed

src/js/bundle/jsdiff-panel.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/view/api/html-helper.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ function eachChildren(
2626
}
2727
}
2828

29-
export const postDiffRender = () => {
30-
setTimeout(function jsondiffpatchHtmlFormatterAdjustArrows(
31-
nodeArg: HTMLElement
32-
) {
29+
export const postDiffRender = (nodeArg: HTMLElement | null) => {
30+
setTimeout(function jsondiffpatchHtmlFormatterAdjustArrows() {
3331
const node = nodeArg || document;
3432

35-
eachByQuery(node, '.jsondiffpatch-arrow', function (_ref3) {
33+
eachByQuery(<HTMLElement>node, '.jsondiffpatch-arrow', function (_ref3) {
3634
const parentNode = _ref3.parentNode;
3735
const children = _ref3.children;
3836
const style = _ref3.style;
@@ -77,6 +75,5 @@ export const postDiffRender = () => {
7775
}
7876
}
7977
});
80-
},
81-
10);
78+
}, 10);
8279
};

src/js/view/api/time.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' });
2222
* @param [now] {number}
2323
* @return {string}
2424
*/
25-
export function timeFromNow(test: number, now: number) {
25+
export function timeFromNow(test: number, now: number): string {
2626
const delta = now - test;
2727
const absDelta = Math.abs(delta);
28+
let rv = '';
2829

2930
for (const interval of intervals) {
3031
if (absDelta >= interval.ge) {
3132
const time = Math.trunc(delta / interval.divisor);
32-
return interval.unit
33+
rv = interval.unit
3334
? rtf.format(-time, interval.unit as Intl.RelativeTimeFormatUnit)
34-
: interval.text;
35+
: interval.text || '';
36+
break;
3537
}
3638
}
39+
40+
return rv;
3741
}

src/js/view/panel.vue

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
</div>
3636
</section>
3737

38-
<section v-if="hasBothSides && !deltaHtml" class="-match">
38+
<section v-if="hasBothSides && !deltaObj" class="-match">
3939
<div ref="deltaEl" class="-center">match</div>
4040
</section>
41-
<section v-else-if="hasBothSides && deltaHtml">
41+
<section v-else-if="hasBothSides && deltaObj">
4242
<div ref="deltaEl" class="-delta" v-html="deltaHtml" />
4343
</section>
4444
<section v-if="!hasBothSides" class="-empty">
@@ -57,16 +57,17 @@
5757
</section>
5858
</template>
5959

60-
<script setup>
60+
<script setup lang="ts">
6161
import { computed, onMounted, reactive, ref } from 'vue';
6262
import packageJson from '../../../package.json';
6363
import * as jsondiffpatch from 'jsondiffpatch';
6464
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';
6768
6869
const formatters = jsondiffpatch.formatters;
69-
const deltaEl = ref(null);
70+
const deltaEl = ref<HTMLElement | null>(null);
7071
const appStartTimestamp = Date.now();
7172
const state = reactive({
7273
version: packageJson.version,
@@ -76,30 +77,39 @@ const state = reactive({
7677
},
7778
codeExample: 'console.diff({a:1,b:1,c:3}, {a:1,b:2,d:3});',
7879
showUnchanged: true,
79-
compare: {
80-
timestamp: appStartTimestamp,
81-
left: null,
82-
right: null,
83-
},
8480
now: appStartTimestamp,
85-
timer: null,
8681
});
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;
8793
8894
const lastUpdated = computed(() =>
89-
timeFromNow(state.compare.timestamp, state.now)
95+
timeFromNow(compare.value.timestamp, state.now)
9096
);
9197
9298
const hasBothSides = computed(
93-
() => $_hasData(state.compare.left) && $_hasData(state.compare.right)
99+
() => $_hasData(compare.value.left) && $_hasData(compare.value.right)
94100
);
95101
96102
const deltaObj = computed(() =>
97-
jsondiffpatch.diff(state.compare.left, state.compare.right)
103+
jsondiffpatch.diff(compare.value.left, compare.value.right)
98104
);
99105
100106
const deltaHtml = computed(() => {
101107
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+
}
103113
} catch (bug) {
104114
return JSON.stringify(bug);
105115
}
@@ -126,20 +136,22 @@ onMounted(() => {
126136
});
127137
128138
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+
}
132144
};
133145
134146
const onCopyDelta = () => {
135-
const diff = jsondiffpatch.diff(state.compare.left, state.compare.right);
147+
const diff = jsondiffpatch.diff(compare.value.left, compare.value.right);
136148
const sDiff = JSON.stringify(diff, null, 2);
137149
138150
document.oncopy = function (e) {
139-
e.clipboardData.setData('text', sDiff);
151+
e.clipboardData?.setData('text', sDiff);
140152
e.preventDefault();
141153
};
142-
document.execCommand('copy', false, null);
154+
document.execCommand('copy', false);
143155
// modern alternative, but don't have permission [?]
144156
// navigator.clipboard.writeText(sDiff).then(
145157
// () => {
@@ -152,32 +164,40 @@ const onCopyDelta = () => {
152164
};
153165
154166
function $_restartLastUpdated() {
155-
state.compare.timestamp = Date.now();
167+
compare.value.timestamp = Date.now();
156168
157-
clearInterval(state.timer);
158-
state.timer = window.setInterval(() => {
169+
window.clearInterval(timer);
170+
timer = window.setInterval(() => {
159171
state.now = Date.now();
160172
}, 1e3);
161173
}
162174
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+
}) {
164184
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;
167187
} else {
168188
if (left) {
169-
state.compare.left = left;
189+
compare.value.left = left;
170190
}
171191
if (right) {
172-
state.compare.right = right;
192+
compare.value.right = right;
173193
}
174194
}
175195
176196
$_restartLastUpdated();
177197
postDiffRender(deltaEl.value);
178198
}
179199
180-
function $_hasData(o) {
200+
function $_hasData(o: unknown): boolean {
181201
return undefined !== o && null !== o;
182202
}
183203
</script>

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"module": "ESNext",
1313
"lib": ["ESNext", "DOM"],
1414
"resolveJsonModule": true,
15-
"allowSyntheticDefaultImports": true
15+
"allowSyntheticDefaultImports": true,
16+
"esModuleInterop": true
1617
},
1718
"include": ["./**/*"],
1819
"exclude": ["node_modules", ".ts-built", "src/js/bundle"]

webpack.config.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function (env, op) {
2525
},
2626

2727
resolve: {
28+
extensions: ['.ts', '.js'],
2829
modules: [path.resolve(__dirname, 'src/js'), 'node_modules'],
2930
alias: {},
3031
},
@@ -49,15 +50,15 @@ export default function (env, op) {
4950
test: /\.vue$/,
5051
loader: 'vue-loader',
5152
},
52-
{
53-
test: /\.(scss|css)$/,
54-
use: ['style-loader', 'css-loader', 'sass-loader'],
55-
},
5653
{
5754
test: /\.tsx?$/,
5855
loader: 'ts-loader',
5956
options: { appendTsSuffixTo: [/\.vue$/] },
6057
},
58+
{
59+
test: /\.(scss|css)$/,
60+
use: ['style-loader', 'css-loader', 'sass-loader'],
61+
},
6162
],
6263
},
6364

0 commit comments

Comments
 (0)