Skip to content

Commit e5f4234

Browse files
committed
feat: optimize rendering performance for large text
1 parent af235f1 commit e5f4234

3 files changed

Lines changed: 42 additions & 8 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@
5353
},
5454
"dependencies": {
5555
"diff": "^5.0.0",
56+
"diff-match-patch": "^1.0.5",
5657
"highlight.js": "^11.6.0",
5758
"vue-demi": "^0.13.11"
5859
},
5960
"devDependencies": {
6061
"@antfu/eslint-config": "^0.34.0",
6162
"@types/diff": "^5.0.0",
63+
"@types/diff-match-patch": "^1.0.32",
6264
"@types/node": "^18.11.18",
6365
"bumpp": "^8.2.1",
6466
"eslint": "^8.31.0",

pnpm-lock.yaml

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

src/utils.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Diff from 'diff'
22
import type { Change } from 'diff'
3+
import { DIFF_DELETE, DIFF_INSERT, diff_match_patch as DiffMatchPatch } from 'diff-match-patch'
34
import hljs from './highlight'
45
import { DiffType } from './types'
56
import type { DiffLine, DiffStat, SplitDiffLine, SplitViewerChange, UnifiedLine, UnifiedViewerChange } from './types'
@@ -36,6 +37,27 @@ const renderWords = (prev?: string, current?: string, diffStyle = 'word'): strin
3637
.join('')
3738
}
3839

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+
3961
const getHighlightCode = (language: string, code: string) => {
4062
const hasModifiedTags = code.match(new RegExp(`(${MODIFIED_START_TAG}|${MODIFIED_CLOSE_TAG})`, 'g'))
4163

@@ -137,8 +159,7 @@ export function createSplitDiff(
137159
): SplitViewerChange {
138160
const newEmptySplitDiff = (): DiffLine => ({ type: DiffType.EMPTY })
139161
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)
142163

143164
let delNum = 0
144165
let addNum = 0
@@ -301,9 +322,9 @@ export function createUnifiedDiff(
301322
newString: string,
302323
language = 'plaintext',
303324
diffStyle = 'word',
304-
context = 2,
325+
context = 10,
305326
): UnifiedViewerChange {
306-
const changes = Diff.diffLines(oldString, newString)
327+
const changes = diffLines(oldString, newString)
307328

308329
let delNum = 0
309330
let addNum = 0

0 commit comments

Comments
 (0)