Skip to content

Commit 5d5c7c7

Browse files
committed
refactor: fixed all TS errors
1 parent 458c8dd commit 5d5c7c7

File tree

4 files changed

+65
-43
lines changed

4 files changed

+65
-43
lines changed

components/buttons/stickyCopy.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default Vue.extend({
4949
props: {
5050
clickHandler: {
5151
type: Function,
52-
default: () => {},
52+
required: true,
5353
},
5454
ariaLabel: {
5555
type: String,
@@ -62,7 +62,7 @@ export default Vue.extend({
6262
}
6363
},
6464
methods: {
65-
handleClick(e) {
65+
handleClick(e: MouseEvent) {
6666
this.copied = true
6767
this.clickHandler(e)
6868
setTimeout(() => {

components/diffActionBar.vue

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,22 @@ import ToggleInSync from './buttons/toggleInSync.vue'
3737
import NextDiff from './buttons/nextDiff.vue'
3838
import CopyLink from './buttons/copyLink.vue'
3939
import { putToClipboard } from '~/helpers/utils'
40+
interface Data {
41+
comparator: HTMLElement | null
42+
comparer: HTMLElement | null
43+
copied: Boolean
44+
treeWalker: TreeWalker | null
45+
}
4046
export default Vue.extend({
4147
components: { PrevDiff, NextDiff, ToggleInSync, CopyLink },
42-
data() {
48+
data(): Data {
4349
return {
44-
copied: this.copied,
50+
copied: false,
51+
comparator: null,
52+
comparer: null,
53+
treeWalker: null,
4554
}
4655
},
47-
beforeCreate() {
48-
this.copied = false
49-
},
5056
mounted() {
5157
const lhsDiffNode = document.getElementById('lhsDiff')
5258
const rhsDiffNode = document.getElementById('rhsDiff')
@@ -66,11 +72,14 @@ export default Vue.extend({
6672
comparer,
6773
NodeFilter.SHOW_ELEMENT,
6874
{
69-
acceptNode: (node) => {
70-
return (
71-
node.classList.contains('bg-red-200') ||
72-
node.classList.contains('bg-green-200')
73-
)
75+
acceptNode: (node: Node) => {
76+
if (
77+
(node as HTMLDivElement).classList.contains('bg-red-200') ||
78+
(node as HTMLDivElement).classList.contains('bg-green-200')
79+
) {
80+
return NodeFilter.FILTER_ACCEPT
81+
}
82+
return NodeFilter.FILTER_REJECT
7483
},
7584
}
7685
)
@@ -92,52 +101,63 @@ export default Vue.extend({
92101
this.$store.commit('scrollInSync/toggle')
93102
},
94103
goToNextDiff() {
95-
const currentNode = this.treeWalker.currentNode
96-
const nextNode = this.treeWalker.nextNode()
104+
const currentNode = this.treeWalker?.currentNode
105+
const nextNode = this.treeWalker?.nextNode()
97106
if (nextNode) {
98107
const currentNodeIndex = Array.prototype.indexOf.call(
99-
currentNode.parentElement.children,
108+
currentNode?.parentElement?.children,
100109
currentNode
101110
)
102111
const nextNodeIndex = Array.prototype.indexOf.call(
103-
nextNode.parentElement.children,
112+
nextNode.parentElement?.children,
104113
nextNode
105114
)
106-
const comparatorCurrentNode = this.comparator.children[currentNodeIndex]
107-
const comparatorNextNode = this.comparator.children[nextNodeIndex]
115+
const comparatorCurrentNode =
116+
this.comparator?.children[currentNodeIndex]
117+
const comparatorNextNode = this.comparator?.children[nextNodeIndex]
108118
this.toggleDiffHunkAndScrollIntoView(
109-
[currentNode, comparatorCurrentNode],
110-
[nextNode, comparatorNextNode]
119+
[
120+
currentNode as HTMLDivElement,
121+
comparatorCurrentNode as HTMLDivElement,
122+
],
123+
[nextNode as HTMLDivElement, comparatorNextNode as HTMLDivElement]
111124
)
112125
}
113126
},
114127
goToPreviousDiff() {
115-
const currentNode = this.treeWalker.currentNode
116-
const prevNode = this.treeWalker.previousNode()
128+
const currentNode = this.treeWalker?.currentNode
129+
const prevNode = this.treeWalker?.previousNode()
117130
if (prevNode) {
118131
const currentNodeIndex = Array.prototype.indexOf.call(
119-
currentNode.parentElement.children,
132+
currentNode?.parentElement?.children,
120133
currentNode
121134
)
122135
const prevNodeIndex = Array.prototype.indexOf.call(
123-
prevNode.parentElement.children,
136+
prevNode.parentElement?.children,
124137
prevNode
125138
)
126-
const comparatorCurrentNode = this.comparator.children[currentNodeIndex]
127-
const comparatorPrevNode = this.comparator.children[prevNodeIndex]
139+
const comparatorCurrentNode =
140+
this.comparator?.children[currentNodeIndex]
141+
const comparatorPrevNode = this.comparator?.children[prevNodeIndex]
128142
this.toggleDiffHunkAndScrollIntoView(
129-
[currentNode, comparatorCurrentNode],
130-
[prevNode, comparatorPrevNode]
143+
[
144+
currentNode as HTMLDivElement,
145+
comparatorCurrentNode as HTMLDivElement,
146+
],
147+
[prevNode as HTMLDivElement, comparatorPrevNode as HTMLDivElement]
131148
)
132149
}
133150
},
134-
toggleDiffHunkAndScrollIntoView(unselectedNodes, selectedNodes) {
151+
toggleDiffHunkAndScrollIntoView(
152+
unselectedNodes: Array<HTMLDivElement | undefined> = [],
153+
selectedNodes: Array<HTMLDivElement | undefined> = []
154+
) {
135155
unselectedNodes.forEach((element) => {
136-
element.querySelector('p').classList.remove('selected')
156+
element?.querySelector('p')?.classList.remove('selected')
137157
})
138158
selectedNodes.forEach((element) => {
139-
element.querySelector('p').classList.add('selected')
140-
element.scrollIntoView()
159+
element?.querySelector('p')?.classList.add('selected')
160+
element?.scrollIntoView()
141161
})
142162
},
143163
},

components/singleDiff.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ export default Vue.extend({
5858
},
5959
},
6060
methods: {
61-
copyTextToClipboard(e) {
61+
copyTextToClipboard(e: Event) {
62+
const copyTextButton = e.currentTarget as HTMLButtonElement
6263
putToClipboard(
63-
e.currentTarget.parentNode.parentNode.innerText
64+
(copyTextButton.parentNode?.parentNode as HTMLElement).innerText
6465
.split('\n\n')
6566
.join('\n'),
6667
'Text copied to your clipboard',

pages/diff.vue

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ export default Vue.extend({
9393
layout: 'main',
9494
data() {
9595
return {
96-
lhsDiff: this.lhsDiff,
97-
rhsDiff: this.rhsDiff,
96+
lhsDiff: [],
97+
rhsDiff: [],
9898
copied: false,
9999
}
100100
},
@@ -103,13 +103,13 @@ export default Vue.extend({
103103
const gunzip = pako.ungzip(Buffer.from(undoUrlSafeBase64(_diff), 'base64'))
104104
const diff = JSON.parse(Buffer.from(gunzip).toString('utf8'))
105105
this.lhsDiff = diff
106-
.map((item) => {
106+
.map((item: Array<string | number>) => {
107107
const hunkState = item[0]
108108
if (hunkState === -1 || hunkState === 0) {
109109
const className =
110110
hunkState === -1 ? 'isModified bg-red-300 dark:bg-red-500' : ''
111111
return `<span class="break-all inline p-0 m-0 ${className}">${escapeHtml(
112-
item[1]
112+
item[1] as string
113113
)}</span>`
114114
}
115115
return false
@@ -118,13 +118,13 @@ export default Vue.extend({
118118
.join('')
119119
.split('\n')
120120
this.rhsDiff = diff
121-
.map((item) => {
121+
.map((item: Array<string | number>) => {
122122
const hunkState = item[0]
123123
if (hunkState === 1 || hunkState === 0) {
124124
const className =
125125
hunkState === 1 ? 'isModified bg-green-400 dark:bg-green-900' : ''
126126
return `<span class="break-all inline p-0 m-0 ${className}">${escapeHtml(
127-
item[1]
127+
item[1] as string
128128
)}</span>`
129129
}
130130
return false
@@ -142,7 +142,7 @@ export default Vue.extend({
142142
)
143143
},
144144
methods: {
145-
putToClipboard(textToPut, toastContent) {
145+
putToClipboard(textToPut: string, toastContent: string) {
146146
navigator.clipboard.writeText(textToPut)
147147
this.$store.commit('toast/show', {
148148
show: true,
@@ -173,9 +173,10 @@ export default Vue.extend({
173173
this.copied = false
174174
}, 5000)
175175
},
176-
copyTextToClipboard(e) {
176+
copyTextToClipboard(e: Event) {
177+
const copyTextButton = e.currentTarget as HTMLButtonElement
177178
this.putToClipboard(
178-
e.currentTarget.parentNode.parentNode.innerText
179+
(copyTextButton.parentNode?.parentNode as HTMLElement).innerText
179180
.split('\n\n')
180181
.join('\n'),
181182
'Text copied to your clipboard'

0 commit comments

Comments
 (0)