Skip to content

Commit 284121f

Browse files
committed
chore: first draft working
1 parent db08e84 commit 284121f

File tree

7 files changed

+115
-17
lines changed

7 files changed

+115
-17
lines changed

helpers/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function urlEncode(unencoded: string): string {
2+
var encoded = globalThis.btoa(unencoded)
3+
return encoded.replace('+', '-').replace('/', '_').replace(/=+$/, '')
4+
}
5+
6+
export function urlDecode(encoded: string): string {
7+
encoded = encoded.replace('-', '+').replace('_', '/')
8+
while (encoded.length % 4) encoded += '='
9+
return globalThis.atob(encoded)
10+
}

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"dependencies": {
2424
"@nuxtjs/pwa": "^3.3.5",
2525
"core-js": "^3.15.1",
26+
"diff": "^5.0.0",
27+
"diff-match-patch": "^1.0.5",
2628
"nuxt": "^2.15.7"
2729
},
2830
"devDependencies": {

pages/_diff.vue

Lines changed: 0 additions & 14 deletions
This file was deleted.

pages/diff/_lhs/_rhs.vue

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<template>
2+
<div>
3+
<p v-for="(lineDiff, index) in lhsDiff" :key="index">
4+
<span v-html="lineDiff"></span>
5+
</p>
6+
<p v-for="(lineDiff, index) in rhsDiff" :key="index">
7+
<span v-html="lineDiff"></span>
8+
</p>
9+
</div>
10+
</template>
11+
12+
<script>
13+
import DiffMatchPatch from 'diff-match-patch'
14+
import { urlDecode } from '../../../helpers/utils'
15+
const dmp = new DiffMatchPatch()
16+
export default {
17+
lhs: '',
18+
rhs: '',
19+
diff: [],
20+
mounted() {
21+
const { lhs, rhs } = this.$route.params
22+
this.originalLhs = urlDecode(lhs).split('\n')
23+
this.originalRhs = urlDecode(rhs).split('\n')
24+
const diff = this.originalLhs.map((x, i) => {
25+
if (!this.originalRhs[i]) {
26+
this.originalRhs[i] = ''
27+
}
28+
return dmp.diff_main(x, this.originalRhs[i])
29+
})
30+
this.lhsDiff = diff.map((lineDiff) => {
31+
return lineDiff
32+
.map((item) => {
33+
if (item[0] === -1 || item[0] === 0) {
34+
const className = item[0] === -1 ? 'bg-red-300' : ''
35+
return `<pre class="inline mr-[-4px] p-0 m-0 ${className}">${item[1]}</pre>`
36+
}
37+
return ''
38+
})
39+
.filter(Boolean)
40+
.join('\n')
41+
})
42+
this.rhsDiff = diff.map((lineDiff) => {
43+
return lineDiff
44+
.map((item) => {
45+
if (item[0] === 1 || item[0] === 0) {
46+
const className = item[0] === 1 ? 'bg-green-300' : ''
47+
return `<pre class="inline mr-[-4px] p-0 m-0 ${className}">${item[1]}</pre>`
48+
}
49+
return ''
50+
})
51+
.filter(Boolean)
52+
.join('\n')
53+
})
54+
console.log(this.originalLhs, this.originalRhs, diff)
55+
},
56+
data() {
57+
return {
58+
diff: this.diff,
59+
originalRhs: this.originalRhs,
60+
originalLhs: this.originalLhs,
61+
lhsDiff: this.lhsDiff,
62+
rhsDiff: this.rhsDiff,
63+
}
64+
},
65+
}
66+
</script>

pages/index.vue

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
<template>
2-
<h1 class="text-7xl">index</h1>
2+
<form @submit="checkForm">
3+
<textarea id="lhs" cols="30" rows="10"></textarea>
4+
<textarea id="rhs" cols="30" rows="10"></textarea>
5+
<button>submit</button>
6+
</form>
37
</template>
48

59
<script lang="ts">
610
import Vue from 'vue'
7-
8-
export default Vue.extend({})
11+
import { urlEncode } from '../helpers/utils'
12+
export default Vue.extend({
13+
methods: {
14+
checkForm: (e: any) => {
15+
e.preventDefault()
16+
const lhsTextArea: HTMLTextAreaElement = document.getElementById(
17+
'lhs'
18+
) as HTMLTextAreaElement
19+
const lhs: string = lhsTextArea?.value || ''
20+
const rhsTextArea: HTMLTextAreaElement = document.getElementById(
21+
'rhs'
22+
) as HTMLTextAreaElement
23+
const rhs: string = rhsTextArea?.value || ''
24+
this.$router.push({
25+
path: `/diff/${urlEncode(lhs)}/${urlEncode(rhs)}`,
26+
})
27+
},
28+
},
29+
})
930
</script>

tailwind.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
mode: 'jit'
3+
}

0 commit comments

Comments
 (0)