Skip to content

Commit 608bab6

Browse files
authored
Merge pull request #25 from MangoTsing/master
升级diff2html到最新版本,增加参数值
2 parents 556b4bd + 419d866 commit 608bab6

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export default {
3535
| new-string| 新的字符串| string |||
3636
| context| 不同地方上下间隔多少行不隐藏 | number |||
3737
| outputFormat| 展示的方式 | string | line-by-line,side-by-side | line-by-line |
38+
| drawFileList | 展示对比文件列表 | boolean | - | false |
39+
| renderNothingWhenEmpty | 当无对比时不渲染 | boolean | - | false |
40+
| diffStyle | 每行中对比差异级别 | string | word, char | word |
41+
| fileName | 文件名 | string | - | |
42+
| isShowNoChange | 当无对比时展示源代码 | boolean | - | false |
3843

3944

4045
## 效果展示

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"dependencies": {
2828
"diff": "^3.5.0",
29-
"diff2html": "^2.3.3",
29+
"diff2html": "^3.1.8",
3030
"highlight.js": "^9.12.0",
3131
"vue": "^2.5.2"
3232
},

src/App.vue

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,43 @@
4444
<el-button type="text" @click="handleClearLocalStorage">清除</el-button>
4545
</el-form-item>
4646
</el-col>
47+
<el-col :span="8">
48+
<el-form-item label="展示对比文件列表">
49+
<el-switch v-model="drawFileList" @click="drawFileList = !drawFileList"></el-switch>
50+
</el-form-item>
51+
</el-col>
52+
<el-col :span="8">
53+
<el-form-item label="noDiff不渲染">
54+
<el-switch v-model="renderNothingWhenEmpty" @click="renderNothingWhenEmpty = !renderNothingWhenEmpty"></el-switch>
55+
</el-form-item>
56+
</el-col>
57+
<el-col :span="8">
58+
<el-form-item label="noDiff显示源代码">
59+
<el-switch v-model="isShowNoChange" @click="isShowNoChange = !isShowNoChange"></el-switch>
60+
</el-form-item>
61+
</el-col>
62+
<el-col :span="8">
63+
<el-form-item label="差异级别">
64+
<el-button v-model="diffStyle" @click="diffStyle = diffStyle === 'char' ? 'word' : 'char'">{{diffStyle}}</el-button>
65+
</el-form-item>
66+
</el-col>
67+
<el-col :span="8">
68+
<el-form-item label="文件名">
69+
<el-input v-model="fileName"></el-input>
70+
</el-form-item>
71+
</el-col>
4772
</el-row>
4873
</el-form>
4974
<code-diff
5075
:old-string="oldStr"
5176
:new-string="newStr"
5277
:context="context"
5378
:output-format="outputFormat"
79+
:drawFileList="drawFileList"
80+
:renderNothingWhenEmpty="renderNothingWhenEmpty"
81+
:diffStyle="diffStyle"
82+
:fileName="fileName"
83+
:isShowNoChange="isShowNoChange"
5484
/>
5585
</div>
5686
</template>
@@ -67,7 +97,12 @@ export default {
6797
oldStr: '',
6898
newStr: '',
6999
fotmat: false,
70-
context: 10
100+
context: 10,
101+
diffStyle: 'word',
102+
fileName: '',
103+
isShowNoChange: true,
104+
drawFileList: true,
105+
renderNothingWhenEmpty: false
71106
}
72107
},
73108
computed: {

src/lib/code-diff/index.vue

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
<script>
88
import {createPatch} from 'diff'
9-
import {Diff2Html} from 'diff2html'
9+
import * as Diff2Html from 'diff2html'
1010
import hljs from 'highlight.js'
1111
import 'highlight.js/styles/googlecode.css'
12-
import 'diff2html/dist/diff2html.css'
12+
import 'diff2html/bundles/css/diff2html.min.css'
1313
export default {
1414
name: 'code-diff',
1515
props: {
@@ -28,6 +28,26 @@ export default {
2828
outputFormat: {
2929
type: String,
3030
default: 'line-by-line'
31+
},
32+
drawFileList: {
33+
type: Boolean,
34+
defalut: false
35+
},
36+
renderNothingWhenEmpty: {
37+
type: Boolean,
38+
default: false
39+
},
40+
diffStyle: {
41+
type: String,
42+
default: 'word'
43+
},
44+
fileName: {
45+
type: String,
46+
default: ''
47+
},
48+
isShowNoChange: {
49+
type: Boolean,
50+
default: false
3151
}
3252
},
3353
directives: {
@@ -40,18 +60,32 @@ export default {
4060
},
4161
computed: {
4262
html () {
43-
return this.createdHtml(this.oldString, this.newString, this.context, this.outputFormat)
63+
return this.createdHtml(this.oldString, this.newString, this.context, this.outputFormat, this.drawFileList, this.renderNothingWhenEmpty, this.fileName, this.isShowNoChange)
4464
}
4565
},
4666
methods: {
47-
createdHtml (oldString, newString, context, outputFormat) {
67+
createdHtml (oldString, newString, context, outputFormat, drawFileList, renderNothingWhenEmpty, fileName, isShowNoChange) {
4868
function hljs (html) {
4969
return html.replace(/<span class="d2h-code-line-ctn">(.+?)<\/span>/g, '<span class="d2h-code-line-ctn"><code>$1</code></span>')
5070
}
51-
let args = ['', oldString, newString, '', '', {context: context}]
71+
if (isShowNoChange) {
72+
oldString = 'File Without Change\tOldString: ======================== \n' + oldString
73+
newString = 'File Without Change\tNewString: ======================== \n' + newString
74+
}
75+
let args = [fileName, oldString, newString, '', '', {context: context}]
5276
let dd = createPatch(...args)
53-
let outStr = Diff2Html.getJsonFromDiff(dd, {inputFormat: 'diff', outputFormat: outputFormat, showFiles: false, matching: 'lines'})
54-
let html = Diff2Html.getPrettyHtml(outStr, {inputFormat: 'json', outputFormat: outputFormat, showFiles: false, matching: 'lines'})
77+
let outStr = Diff2Html.parse(dd, {
78+
inputFormat: 'diff',
79+
outputFormat: outputFormat,
80+
drawFileList: drawFileList,
81+
matching: 'lines',
82+
renderNothingWhenEmpty: renderNothingWhenEmpty})
83+
let html = Diff2Html.html(outStr, {
84+
inputFormat: 'json',
85+
outputFormat: outputFormat,
86+
drawFileList: drawFileList,
87+
matching: 'lines',
88+
renderNothingWhenEmpty: renderNothingWhenEmpty})
5589
return hljs(html)
5690
}
5791
}

0 commit comments

Comments
 (0)