v-code-diff 1.2.0 → 1.3.0

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.0",
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/utils.ts CHANGED
@@ -1,5 +1,6 @@
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
6
  import type { DiffLine, DiffStat, SplitDiffLine, SplitViewerChange, UnifiedLine, UnifiedViewerChange } from './types'
@@ -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,8 +159,7 @@ 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
@@ -301,9 +322,9 @@ 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