Skip to content

Commit 5fdd1f6

Browse files
committed
feat: update diff viewer to use new library component
1 parent b83daf0 commit 5fdd1f6

File tree

4 files changed

+532
-14
lines changed

4 files changed

+532
-14
lines changed

custom/AuditLogView.vue

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,62 @@
11
<script setup>
2-
import VueDiff from 'vue-diff';
3-
import 'vue-diff/dist/index.css';
4-
import { app } from '@/main';
52
import { useCoreStore } from '@/stores/core';
6-
import { computed } from 'vue';
7-
app.use(VueDiff);
3+
import { computed, ref, watch } from 'vue';
4+
import "@git-diff-view/vue/styles/diff-view.css";
5+
import { DiffView, DiffModeEnum } from "@git-diff-view/vue";
6+
import { generateDiffFile } from "@git-diff-view/file";
87
98
const props = defineProps(['column', 'record', 'meta', 'resource', 'adminUser']);
109
const coreStore = useCoreStore();
11-
const theme = computed(() => coreStore.theme);;
12-
const isMobile = computed(() => navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone|iPad|iPod/i));
10+
const theme = computed(() => coreStore.theme);
11+
const isMobile = computed(() => /(Android|iPhone|iPad|iPod)/i.test(navigator.userAgent));
12+
const mode = computed(() => isMobile.value ? DiffModeEnum.Unified : DiffModeEnum.Split);
13+
14+
const oldContent = JSON.stringify(props.record[props.meta.resourceColumns.resourceDataColumnName].oldRecord, null, 2)
15+
const newContent = JSON.stringify(props.record[props.meta.resourceColumns.resourceDataColumnName].newRecord, null, 2)
16+
17+
const diffFile = ref();
18+
19+
function initDiffFile() {
20+
const file = generateDiffFile(
21+
'diff.json',
22+
oldContent,
23+
'diff.json',
24+
newContent,
25+
'json',
26+
'json'
27+
);
28+
file.initTheme(theme.value === 'dark' ? 'dark' : 'light');
29+
file.init();
30+
if (mode.value === DiffModeEnum.Split) {
31+
file.buildSplitDiffLines();
32+
} else {
33+
file.buildUnifiedDiffLines();
34+
}
35+
diffFile.value = file;
36+
}
37+
38+
initDiffFile();
39+
40+
watch([mode, theme], ([m, t]) => {
41+
if (!diffFile.value) return;
42+
diffFile.value.initTheme(t === 'dark' ? 'dark' : 'light');
43+
if (m === DiffModeEnum.Split) {
44+
diffFile.value.buildSplitDiffLines();
45+
} else {
46+
diffFile.value.buildUnifiedDiffLines();
47+
}
48+
});
1349
1450
</script>
1551

1652
<template>
17-
<Diff
18-
:mode="isMobile ? 'unified' : 'split'"
19-
:theme="theme"
20-
:language="'JSON'"
21-
:prev="JSON.stringify(props.record[props.meta.resourceColumns.resourceDataColumnName].oldRecord, null, 2)"
22-
:current="JSON.stringify(props.record[props.meta.resourceColumns.resourceDataColumnName].newRecord, null, 2)"
23-
/>
53+
<DiffView
54+
:diff-file="diffFile"
55+
:diff-view-mode="mode"
56+
:diff-view-theme="theme === 'dark' ? 'dark' : 'light'"
57+
:diff-view-highlight="true"
58+
:diff-view-wrap="true"
59+
:diff-view-font-size="14"
60+
/>
2461
</template>
2562

0 commit comments

Comments
 (0)