@@ -37,16 +37,22 @@ import ToggleInSync from './buttons/toggleInSync.vue'
37
37
import NextDiff from ' ./buttons/nextDiff.vue'
38
38
import CopyLink from ' ./buttons/copyLink.vue'
39
39
import { putToClipboard } from ' ~/helpers/utils'
40
+ interface Data {
41
+ comparator: HTMLElement | null
42
+ comparer: HTMLElement | null
43
+ copied: Boolean
44
+ treeWalker: TreeWalker | null
45
+ }
40
46
export default Vue .extend ({
41
47
components: { PrevDiff , NextDiff , ToggleInSync , CopyLink },
42
- data() {
48
+ data(): Data {
43
49
return {
44
- copied: this .copied ,
50
+ copied: false ,
51
+ comparator: null ,
52
+ comparer: null ,
53
+ treeWalker: null ,
45
54
}
46
55
},
47
- beforeCreate() {
48
- this .copied = false
49
- },
50
56
mounted() {
51
57
const lhsDiffNode = document .getElementById (' lhsDiff' )
52
58
const rhsDiffNode = document .getElementById (' rhsDiff' )
@@ -66,11 +72,14 @@ export default Vue.extend({
66
72
comparer ,
67
73
NodeFilter .SHOW_ELEMENT ,
68
74
{
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
74
83
},
75
84
}
76
85
)
@@ -92,52 +101,63 @@ export default Vue.extend({
92
101
this .$store .commit (' scrollInSync/toggle' )
93
102
},
94
103
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 ()
97
106
if (nextNode ) {
98
107
const currentNodeIndex = Array .prototype .indexOf .call (
99
- currentNode .parentElement .children ,
108
+ currentNode ? .parentElement ? .children ,
100
109
currentNode
101
110
)
102
111
const nextNodeIndex = Array .prototype .indexOf .call (
103
- nextNode .parentElement .children ,
112
+ nextNode .parentElement ? .children ,
104
113
nextNode
105
114
)
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 ]
108
118
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 ]
111
124
)
112
125
}
113
126
},
114
127
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 ()
117
130
if (prevNode ) {
118
131
const currentNodeIndex = Array .prototype .indexOf .call (
119
- currentNode .parentElement .children ,
132
+ currentNode ? .parentElement ? .children ,
120
133
currentNode
121
134
)
122
135
const prevNodeIndex = Array .prototype .indexOf .call (
123
- prevNode .parentElement .children ,
136
+ prevNode .parentElement ? .children ,
124
137
prevNode
125
138
)
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 ]
128
142
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 ]
131
148
)
132
149
}
133
150
},
134
- toggleDiffHunkAndScrollIntoView(unselectedNodes , selectedNodes ) {
151
+ toggleDiffHunkAndScrollIntoView(
152
+ unselectedNodes : Array <HTMLDivElement | undefined > = [],
153
+ selectedNodes : Array <HTMLDivElement | undefined > = []
154
+ ) {
135
155
unselectedNodes .forEach ((element ) => {
136
- element .querySelector (' p' ).classList .remove (' selected' )
156
+ element ? .querySelector (' p' )? .classList .remove (' selected' )
137
157
})
138
158
selectedNodes .forEach ((element ) => {
139
- element .querySelector (' p' ).classList .add (' selected' )
140
- element .scrollIntoView ()
159
+ element ? .querySelector (' p' )? .classList .add (' selected' )
160
+ element ? .scrollIntoView ()
141
161
})
142
162
},
143
163
},
0 commit comments