@@ -37,16 +37,22 @@ import ToggleInSync from './buttons/toggleInSync.vue'
3737import NextDiff from ' ./buttons/nextDiff.vue'
3838import CopyLink from ' ./buttons/copyLink.vue'
3939import { putToClipboard } from ' ~/helpers/utils'
40+ interface Data {
41+ comparator: HTMLElement | null
42+ comparer: HTMLElement | null
43+ copied: Boolean
44+ treeWalker: TreeWalker | null
45+ }
4046export 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 },
0 commit comments