-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathCodeDiff.vue
More file actions
176 lines (159 loc) · 5.58 KB
/
CodeDiff.vue
File metadata and controls
176 lines (159 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<script setup lang="ts">
import { computed, ref, watch } from 'vue-demi'
import { createSplitDiff, createUnifiedDiff } from './utils'
import UnifiedViewer from './unified/UnifiedViewer.vue'
import SplitViewer from './split/SplitViewer.vue'
import DownArrowIcon from './icons/DownArrowIcon.vue'
import UpArrowIcon from './icons/UpArrowIcon.vue'
import './style.scss'
interface Props {
newString: string
oldString: string
language?: string
context?: number
diffStyle?: 'word' | 'char'
forceInlineComparison?: boolean
outputFormat?: 'line-by-line' | 'side-by-side'
trim?: boolean
noDiffLineFeed?: boolean
maxHeight?: string
filename?: string
newFilename?: string
hideHeader?: boolean
hideStat?: boolean
theme?: 'light' | 'dark'
// Give a pattern to ignore matching lines eg: '(time|token)'
ignoreMatchingLines?: string
}
const props = withDefaults(defineProps<Props>(), {
language: 'plaintext',
context: 10,
diffStyle: 'word',
forceInlineComparison: false,
outputFormat: 'line-by-line',
trim: false,
noDiffLineFeed: false,
maxHeight: undefined,
filename: undefined,
newFilename: undefined,
hideHeader: false,
hideStat: false,
theme: 'light',
ignoreMatchingLines: undefined,
})
const emits = defineEmits<{
(e: 'diff', diffResult: DiffResult): void
}>()
interface DiffResult {
stat: {
isChanged: boolean
addNum: number
delNum: number
}
}
const isUnifiedViewer = computed(() => props.outputFormat === 'line-by-line')
const oldString = computed(() => {
let value = props.oldString || ''
value = props.trim ? value.trim() : value
value = props.noDiffLineFeed ? value.replace(/(\r\n)/g, '\n') : value
return value
})
const newString = computed(() => {
let value = props.newString || ''
value = props.trim ? value.trim() : value
value = props.noDiffLineFeed ? value.replace(/(\r\n)/g, '\n') : value
return value
})
const raw = computed(() =>
isUnifiedViewer.value
? createUnifiedDiff(oldString.value, newString.value, props.language, props.diffStyle, props.forceInlineComparison, props.context, props.ignoreMatchingLines)
: createSplitDiff(oldString.value, newString.value, props.language, props.diffStyle, props.forceInlineComparison, props.context, props.ignoreMatchingLines),
)
const diffChange = ref(raw.value)
const isNotChanged = computed(() => diffChange.value.stat.additionsNum === 0 && diffChange.value.stat.deletionsNum === 0)
const currentDiffIndex = ref(-1)
function goToNextDiff() {
const diffs = document.querySelectorAll('.blob-code-addition')
if (currentDiffIndex.value < diffs.length - 1) {
currentDiffIndex.value++
updateCurrentDiffHighlight(diffs)
}
}
function goToPrevDiff() {
const diffs = document.querySelectorAll('.blob-code-addition')
if (currentDiffIndex.value > 0) {
currentDiffIndex.value--
updateCurrentDiffHighlight(diffs)
}
}
function updateCurrentDiffHighlight(diffs: NodeListOf<Element>) {
diffs.forEach((diff: { classList: { remove: (arg0: string) => any } }) => diff.classList.remove('current-diff'))
const currentDiff = diffs[currentDiffIndex.value]
if (currentDiff) {
currentDiff.classList.add('current-diff')
currentDiff.scrollIntoView({ behavior: 'smooth', block: 'center' })
}
}
watch(() => props, () => {
diffChange.value = raw.value
emits('diff', {
stat: {
isChanged: !isNotChanged.value,
addNum: diffChange.value.stat.additionsNum,
delNum: diffChange.value.stat.deletionsNum,
},
})
}, { deep: true, immediate: true })
</script>
<template>
<div class="code-diff-view" :theme="theme" :style="{ maxHeight }">
<div v-if="!hideHeader" class="file-header">
<!-- line by line -->
<div v-if="isUnifiedViewer" class="file-info">
<span>
<div class="info-left">{{ filename }}</div>
<div class="info-left">{{ newFilename }}</div>
</span>
<span class="diff-commandbar">
<button class="command-item-button" title="Next Change" @click="goToNextDiff">
<DownArrowIcon />
</button>
<button class="command-item-button" title="Previous Change" @click="goToPrevDiff">
<UpArrowIcon />
</button>
</span>
<span v-if="!hideStat" class="diff-stat">
<slot name="stat" :stat="diffChange.stat">
<span class="diff-stat-added">+{{ diffChange.stat.additionsNum }} additions</span>
<span class="diff-stat-deleted">-{{ diffChange.stat.deletionsNum }} deletions</span>
</slot>
</span>
</div>
<!-- side by side -->
<div v-else class="file-info">
<span class="info-left">{{ filename }}</span>
<span class="info-right">
<span style="margin-left: 20px;">{{ newFilename }}</span>
<span class="diff-commandbar">
<button class="command-item-button" title="Next Change" @click="goToNextDiff">
<DownArrowIcon />
</button>
<button class="command-item-button" title="Previous Change" @click="goToPrevDiff">
<UpArrowIcon />
</button>
</span>
<span v-if="!hideStat" class="diff-stat">
<slot name="stat" :stat="diffChange.stat">
<span class="diff-stat-added">+{{ diffChange.stat.additionsNum }} additions</span>
<span class="diff-stat-deleted">-{{ diffChange.stat.deletionsNum }} deletions</span>
</slot>
</span>
</span>
</div>
</div>
<UnifiedViewer v-if="isUnifiedViewer" :diff-change="diffChange" />
<SplitViewer v-else :diff-change="diffChange" />
</div>
</template>
<style lang="scss">
</style>