Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit e6cfd29

Browse files
committed
chore: development
1 parent c84f3b0 commit e6cfd29

File tree

11 files changed

+195
-50
lines changed

11 files changed

+195
-50
lines changed

dev/App.vue

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
11
<template>
2+
<link rel="stylesheet" :href="`/src/themes/${theme}.css`">
23
<h1>Vue Diff</h1>
3-
<section>
4-
<h2>Split</h2>
5-
<Diff
6-
type="split"
7-
language="javascript"
8-
:prev="prev"
9-
:current="current"
10-
/>
11-
</section>
12-
<section>
13-
<h2>Unified</h2>
14-
<Diff
15-
type="unified"
16-
language="javascript"
17-
:prev="prev"
18-
:current="current"
19-
/>
20-
</section>
4+
language
5+
type
6+
theme
7+
<div class="editor">
8+
<section>
9+
<h2>Editor</h2>
10+
<div>
11+
<h3>Prev</h3>
12+
<textarea v-model="prev"></textarea>
13+
</div>
14+
<div>
15+
<h3>Current</h3>
16+
<textarea v-model="current"></textarea>
17+
</div>
18+
</section>
19+
</div>
20+
<div class="viewer">
21+
<section>
22+
<h2>Split</h2>
23+
<Diff
24+
type="split"
25+
language="javascript"
26+
:prev="prev"
27+
:current="current"
28+
/>
29+
</section>
30+
<section>
31+
<h2>Unified</h2>
32+
<Diff
33+
type="unified"
34+
language="javascript"
35+
:prev="prev"
36+
:current="current"
37+
/>
38+
</section>
39+
</div>
2140
</template>
2241

2342
<script lang="ts">
2443
import { defineComponent, ref } from 'vue'
2544
2645
export default defineComponent({
2746
setup () {
47+
const theme = 'okaidia'
48+
2849
const prev = ref(
2950
`var a = {
3051
"name": "vue-diff",
@@ -47,7 +68,29 @@ export default defineComponent({
4768
}
4869
}`)
4970
50-
return { prev, current }
71+
return { prev, current, theme }
5172
}
5273
})
5374
</script>
75+
76+
<style scoped lang="scss">
77+
.editor {
78+
section {
79+
display: flex;
80+
flex-wrap: wrap;
81+
82+
h2 {
83+
width: 100%;
84+
}
85+
86+
div {
87+
width: 50%;
88+
}
89+
90+
textarea {
91+
width: 100%;
92+
height: 100px;
93+
}
94+
}
95+
}
96+
</style>

dev/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createApp } from 'vue'
22
import App from './App.vue'
33
import VueDiff from '../src'
4-
import '../src/themes/okaidia.css'
54

65
const app = createApp(App)
76
app.use(VueDiff)

package-lock.json

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"core-js": "^3.6.5",
2020
"diff": "^5.0.0",
21+
"html-entities": "^2.0.2",
2122
"prismjs": "^1.22.0",
2223
"vue": "^3.0.0"
2324
},

src/Code.vue

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
<td class="lineNum">
44
{{ data.lineNum }}
55
</td>
6-
<td ref="code" class="code">
7-
<pre><code :class="`language-${language}`" v-html="data.value"></code></pre>
6+
<td class="code">
7+
<pre><code ref="codeRef" :class="`language-${language}`" v-html="code"></code></pre>
88
</td>
99
</tr>
1010
</template>
1111

1212
<script lang="ts">
13-
import { defineComponent, PropType, ref, onMounted } from 'vue'
13+
// @ts-ignore
14+
import { encode } from 'html-entities'
15+
import { defineComponent, PropType, ref, onMounted, watch, nextTick, computed } from 'vue'
1416
import { Prism } from './index'
15-
import { Line } from './utils'
17+
import { MODIFIED_TAG, Line } from './utils'
1618
1719
export default defineComponent({
1820
props: {
@@ -25,34 +27,52 @@ export default defineComponent({
2527
required: true
2628
}
2729
},
28-
setup () {
29-
const code = ref(null)
30+
setup (props) {
31+
const codeRef = ref(null)
32+
const code = computed(() => {
33+
return encode(props.data.value)
34+
.replace(new RegExp(`&lt;${MODIFIED_TAG}&gt;`, 'gi'), '<span class="token modified">')
35+
.replace(new RegExp(`&lt;/${MODIFIED_TAG}&gt;`, 'gi'), '</span>')
36+
})
3037
3138
onMounted(() => {
32-
Prism.highlightAllUnder(code.value as unknown as HTMLElement)
39+
watch(() => props.data.value, () => {
40+
nextTick(() => Prism.highlightElement(codeRef.value as unknown as HTMLElement))
41+
}, { immediate: true })
3342
})
3443
35-
return { code }
44+
return { codeRef, code }
3645
}
3746
})
3847
</script>
3948

4049
<style scoped lang="scss">
4150
td {
42-
padding: 0 10px;
51+
padding: 0 0.5em;
4352
4453
&.lineNum {
4554
width: 2em;
4655
text-align: right;
4756
color: #999;
4857
font-size: 0.9em;
4958
}
59+
60+
pre[class*="language-"]:before {
61+
display: inline-block;
62+
position: absolute;
63+
left: 0;
64+
top: 0;
65+
opacity: 0.8;
66+
}
5067
}
5168
5269
pre[class*="language-"] {
53-
padding: 0;
70+
display: block;
71+
position: relative;
5472
margin: 0;
55-
overflow: auto;
73+
padding: 0;
74+
padding-left: 1.5em;
75+
overflow: visible;
5676
background: none;
5777
border-radius: 0;
5878
}
@@ -63,6 +83,21 @@ tr.vue-diff-row-removed td {
6383
:deep(.token.modified) {
6484
background-color: rgba(255, 0, 0, .3);
6585
}
86+
87+
pre[class*="language-"]:before {
88+
content: "-";
89+
}
90+
91+
}
92+
93+
.vue-diff-viewer-prev {
94+
tr.vue-diff-row-added td {
95+
background-color: rgba(128, 128, 128, 0.1);
96+
97+
pre[class*="language-"]:before {
98+
display: none;
99+
}
100+
}
66101
}
67102
68103
tr.vue-diff-row-added td {
@@ -71,5 +106,19 @@ tr.vue-diff-row-added td {
71106
:deep(.token.modified) {
72107
background-color: rgba(0, 255, 128, .3);
73108
}
109+
110+
pre[class*="language-"]:before {
111+
content: "+";
112+
}
113+
}
114+
115+
.vue-diff-viewer-current {
116+
tr.vue-diff-row-removed td {
117+
background-color: rgba(128, 128, 128, 0.1);
118+
119+
pre[class*="language-"]:before {
120+
display: none;
121+
}
122+
}
74123
}
75124
</style>

src/Diff.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,4 @@ export default defineComponent({
4747
justify-content: space-between;
4848
width: 100%;
4949
}
50-
51-
.vue-diff-wrapper-split .vue-diff-viewer {
52-
width: calc(50% - 10px);
53-
}
5450
</style>

src/Viewer.vue

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div ref="viewer" class="vue-diff-viewer">
2+
<div ref="viewer" class="vue-diff-viewer" :class="`vue-diff-viewer-${role}`">
33
<table>
44
<tbody>
55
<Code
@@ -15,11 +15,11 @@
1515

1616
<script lang="ts">
1717
import * as Diff from 'diff'
18-
import { defineComponent, ref, PropType } from 'vue'
18+
import { defineComponent, ref, PropType, watch } from 'vue'
1919
import { renderLines } from './utils'
2020
import Code from './Code.vue'
2121
22-
import type { Role } from './utils'
22+
import type { Role, Line } from './utils'
2323
2424
export default defineComponent({
2525
components: {
@@ -44,23 +44,34 @@ export default defineComponent({
4444
}
4545
},
4646
setup (props) {
47-
const diff = Diff.diffLines(props.prev, props.current)
48-
const lines = ref(renderLines(props.role, diff))
47+
const lines = ref<Array<Line>>([])
48+
49+
watch([() => props.prev, () => props.current], () => {
50+
const diff = Diff.diffLines(props.prev, props.current)
51+
lines.value = renderLines(props.role, diff)
52+
}, { immediate: true })
4953
5054
return { lines }
5155
}
5256
})
5357
</script>
5458

5559
<style scoped lang="scss">
60+
.vue-diff-wrapper-split {
61+
.vue-diff-viewer {
62+
width: calc(50% - 10px);
63+
}
64+
}
65+
5666
.vue-diff-viewer {
57-
overflow: hidden;
67+
overflow: auto;
68+
width: 100%;
5869
padding: 1em 0;
5970
border-radius: 0.3em;
6071
}
6172
6273
table {
63-
width: 100%;
74+
min-width: 100%;
6475
table-layout: fixed;
6576
border-collapse: collapse;
6677
}

src/shims-vue.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ declare module '*.vue' {
33
const component: DefineComponent
44
export default component
55
}
6+
7+
declare module '*.css'

src/style.scss

Whitespace-only changes.

src/themes/okaidia.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import 'prismjs/themes/prism-okaidia.css';
1+
@import '../../node_modules/prismjs/themes/prism-okaidia.css';
22

33
.vue-diff-viewer {
44
background-color: #272822;

0 commit comments

Comments
 (0)