v-code-diff 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "v-code-diff",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "template component for vue-demi, can dev and build",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.es.js",
@@ -53,12 +53,14 @@
53
53
  },
54
54
  "dependencies": {
55
55
  "diff": "^5.0.0",
56
+ "diff-match-patch": "^1.0.5",
56
57
  "highlight.js": "^11.6.0",
57
58
  "vue-demi": "^0.13.11"
58
59
  },
59
60
  "devDependencies": {
60
61
  "@antfu/eslint-config": "^0.34.0",
61
62
  "@types/diff": "^5.0.0",
63
+ "@types/diff-match-patch": "^1.0.32",
62
64
  "@types/node": "^18.11.18",
63
65
  "bumpp": "^8.2.1",
64
66
  "eslint": "^8.31.0",
package/src/CodeDiff.vue CHANGED
@@ -16,7 +16,7 @@ interface Props {
16
16
  outputFormat?: 'line-by-line' | 'side-by-side'
17
17
  trim?: boolean
18
18
  noDiffLineFeed?: boolean
19
- maxHeight?: number
19
+ maxHeight?: string
20
20
  filename?: string
21
21
  }
22
22
 
@@ -1,9 +1,9 @@
1
1
  <script setup lang="ts">
2
2
  import { DiffType } from '../types'
3
- import type { SplitDiffLine } from '../types'
3
+ import type { SplitLineChange } from '../types'
4
4
 
5
5
  const props = defineProps<{
6
- splitLine: SplitDiffLine
6
+ splitLine: SplitLineChange
7
7
  }>()
8
8
 
9
9
  const getCodeMarker = (type: DiffType) => {
package/src/types.ts CHANGED
@@ -11,13 +11,13 @@ export interface DiffLine {
11
11
  num?: number
12
12
  }
13
13
 
14
- export interface SplitDiffLine {
14
+ export interface SplitLineChange {
15
15
  fold?: boolean
16
16
  left: DiffLine
17
17
  right: DiffLine
18
18
  }
19
19
 
20
- export interface UnifiedLine {
20
+ export interface UnifiedLineChange {
21
21
  fold?: boolean
22
22
  type: DiffType
23
23
  code: string
@@ -30,11 +30,11 @@ export interface DiffStat {
30
30
  deletionsNum: number
31
31
  }
32
32
  export interface SplitViewerChange {
33
- changes: SplitDiffLine[]
33
+ changes: SplitLineChange[]
34
34
  stat: DiffStat
35
35
  }
36
36
 
37
37
  export interface UnifiedViewerChange {
38
- changes: UnifiedLine[]
38
+ changes: UnifiedLineChange[]
39
39
  stat: DiffStat
40
40
  }
@@ -1,9 +1,9 @@
1
1
  <script lang="ts" setup>
2
- import type { UnifiedLine } from '../types'
2
+ import type { UnifiedLineChange } from '../types'
3
3
  import { DiffType } from '../types'
4
4
 
5
5
  const props = defineProps<{
6
- line: UnifiedLine
6
+ line: UnifiedLineChange
7
7
  }>()
8
8
 
9
9
  const getCodeMarker = (type: DiffType) => {
@@ -1,9 +1,9 @@
1
1
  <script setup lang="ts">
2
- import type { UnifiedViewerChange } from '../types'
2
+ import type { UnifiedLineChange } from '../types'
3
3
  import UnifiedLine from './UnifiedLine.vue'
4
4
 
5
5
  const props = defineProps<{
6
- diffChange: UnifiedViewerChange
6
+ diffChange: UnifiedLineChange[]
7
7
  }>()
8
8
  </script>
9
9
 
package/src/utils.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import * as Diff from 'diff'
2
2
  import type { Change } from 'diff'
3
+ import { DIFF_DELETE, DIFF_INSERT, diff_match_patch as DiffMatchPatch } from 'diff-match-patch'
3
4
  import hljs from './highlight'
4
5
  import { DiffType } from './types'
5
- import type { DiffLine, DiffStat, SplitDiffLine, SplitViewerChange, UnifiedLine, UnifiedViewerChange } from './types'
6
+ import type { DiffLine, DiffStat, SplitLineChange, SplitViewerChange, UnifiedLineChange, UnifiedViewerChange } from './types'
6
7
 
7
8
  const MODIFIED_START_TAG = '<code-diff-modified>'
8
9
  const MODIFIED_CLOSE_TAG = '</code-diff-modified>'
@@ -36,6 +37,27 @@ const renderWords = (prev?: string, current?: string, diffStyle = 'word'): strin
36
37
  .join('')
37
38
  }
38
39
 
40
+ function diffLines(prev: string, current: string) {
41
+ const dmp = new DiffMatchPatch()
42
+ const a = dmp.diff_linesToChars_(prev, current)
43
+ const linePrev = a.chars1
44
+ const lineCurrent = a.chars2
45
+ const lineArray = a.lineArray
46
+ const diffs: any[] = dmp.diff_main(linePrev, lineCurrent, false)
47
+ dmp.diff_charsToLines_(diffs, lineArray)
48
+ return diffs.map((x) => {
49
+ const [type, text] = x
50
+ const count = text.trim().split('\n').length
51
+ const change: Diff.Change = {
52
+ count,
53
+ value: text,
54
+ removed: type === DIFF_DELETE,
55
+ added: type === DIFF_INSERT,
56
+ }
57
+ return change
58
+ })
59
+ }
60
+
39
61
  const getHighlightCode = (language: string, code: string) => {
40
62
  const hasModifiedTags = code.match(new RegExp(`(${MODIFIED_START_TAG}|${MODIFIED_CLOSE_TAG})`, 'g'))
41
63
 
@@ -137,14 +159,13 @@ export function createSplitDiff(
137
159
  ): SplitViewerChange {
138
160
  const newEmptySplitDiff = (): DiffLine => ({ type: DiffType.EMPTY })
139
161
  const newSplitDiff = (type: DiffType, num: number, code: string): DiffLine => ({ type, num, code })
140
-
141
- const changes = Diff.diffLines(oldString, newString)
162
+ const changes = diffLines(oldString, newString)
142
163
 
143
164
  let delNum = 0
144
165
  let addNum = 0
145
166
  let skip = false
146
167
 
147
- const rawChanges: SplitDiffLine[] = []
168
+ const rawChanges: SplitLineChange[] = []
148
169
  const result: SplitViewerChange = {
149
170
  changes: rawChanges,
150
171
  stat: calcDiffStat(changes),
@@ -301,15 +322,15 @@ export function createUnifiedDiff(
301
322
  newString: string,
302
323
  language = 'plaintext',
303
324
  diffStyle = 'word',
304
- context = 2,
325
+ context = 10,
305
326
  ): UnifiedViewerChange {
306
- const changes = Diff.diffLines(oldString, newString)
327
+ const changes = diffLines(oldString, newString)
307
328
 
308
329
  let delNum = 0
309
330
  let addNum = 0
310
331
  let skip = false
311
332
 
312
- const rawChanges: UnifiedLine[] = []
333
+ const rawChanges: UnifiedLineChange[] = []
313
334
  const result: UnifiedViewerChange = {
314
335
  changes: rawChanges,
315
336
  stat: calcDiffStat(changes),