Skip to content

升级diff2html到最新版本,增加参数值 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default {
| new-string| 新的字符串| string | — | — |
| context| 不同地方上下间隔多少行不隐藏 | number | — | — |
| outputFormat| 展示的方式 | string | line-by-line,side-by-side | line-by-line |
| drawFileList | 展示对比文件列表 | boolean | - | false |
| renderNothingWhenEmpty | 当无对比时不渲染 | boolean | - | false |
| diffStyle | 每行中对比差异级别 | string | word, char | word |
| fileName | 文件名 | string | - | |
| isShowNoChange | 当无对比时展示源代码 | boolean | - | false |


## 效果展示
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"diff": "^3.5.0",
"diff2html": "^2.3.3",
"diff2html": "^3.1.8",
"highlight.js": "^9.12.0",
"vue": "^2.5.2"
},
Expand Down
37 changes: 36 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,43 @@
<el-button type="text" @click="handleClearLocalStorage">清除</el-button>
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="展示对比文件列表">
<el-switch v-model="drawFileList" @click="drawFileList = !drawFileList"></el-switch>
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="noDiff不渲染">
<el-switch v-model="renderNothingWhenEmpty" @click="renderNothingWhenEmpty = !renderNothingWhenEmpty"></el-switch>
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="noDiff显示源代码">
<el-switch v-model="isShowNoChange" @click="isShowNoChange = !isShowNoChange"></el-switch>
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="差异级别">
<el-button v-model="diffStyle" @click="diffStyle = diffStyle === 'char' ? 'word' : 'char'">{{diffStyle}}</el-button>
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="文件名">
<el-input v-model="fileName"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<code-diff
:old-string="oldStr"
:new-string="newStr"
:context="context"
:output-format="outputFormat"
:drawFileList="drawFileList"
:renderNothingWhenEmpty="renderNothingWhenEmpty"
:diffStyle="diffStyle"
:fileName="fileName"
:isShowNoChange="isShowNoChange"
/>
</div>
</template>
Expand All @@ -67,7 +97,12 @@ export default {
oldStr: '',
newStr: '',
fotmat: false,
context: 10
context: 10,
diffStyle: 'word',
fileName: '',
isShowNoChange: true,
drawFileList: true,
renderNothingWhenEmpty: false
}
},
computed: {
Expand Down
48 changes: 41 additions & 7 deletions src/lib/code-diff/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

<script>
import {createPatch} from 'diff'
import {Diff2Html} from 'diff2html'
import * as Diff2Html from 'diff2html'
import hljs from 'highlight.js'
import 'highlight.js/styles/googlecode.css'
import 'diff2html/dist/diff2html.css'
import 'diff2html/bundles/css/diff2html.min.css'
export default {
name: 'code-diff',
props: {
Expand All @@ -28,6 +28,26 @@ export default {
outputFormat: {
type: String,
default: 'line-by-line'
},
drawFileList: {
type: Boolean,
defalut: false
},
renderNothingWhenEmpty: {
type: Boolean,
default: false
},
diffStyle: {
type: String,
default: 'word'
},
fileName: {
type: String,
default: ''
},
isShowNoChange: {
type: Boolean,
default: false
}
},
directives: {
Expand All @@ -40,18 +60,32 @@ export default {
},
computed: {
html () {
return this.createdHtml(this.oldString, this.newString, this.context, this.outputFormat)
return this.createdHtml(this.oldString, this.newString, this.context, this.outputFormat, this.drawFileList, this.renderNothingWhenEmpty, this.fileName, this.isShowNoChange)
}
},
methods: {
createdHtml (oldString, newString, context, outputFormat) {
createdHtml (oldString, newString, context, outputFormat, drawFileList, renderNothingWhenEmpty, fileName, isShowNoChange) {
function hljs (html) {
return html.replace(/<span class="d2h-code-line-ctn">(.+?)<\/span>/g, '<span class="d2h-code-line-ctn"><code>$1</code></span>')
}
let args = ['', oldString, newString, '', '', {context: context}]
if (isShowNoChange) {
oldString = 'File Without Change\tOldString: ======================== \n' + oldString
newString = 'File Without Change\tNewString: ======================== \n' + newString
}
let args = [fileName, oldString, newString, '', '', {context: context}]
let dd = createPatch(...args)
let outStr = Diff2Html.getJsonFromDiff(dd, {inputFormat: 'diff', outputFormat: outputFormat, showFiles: false, matching: 'lines'})
let html = Diff2Html.getPrettyHtml(outStr, {inputFormat: 'json', outputFormat: outputFormat, showFiles: false, matching: 'lines'})
let outStr = Diff2Html.parse(dd, {
inputFormat: 'diff',
outputFormat: outputFormat,
drawFileList: drawFileList,
matching: 'lines',
renderNothingWhenEmpty: renderNothingWhenEmpty})
let html = Diff2Html.html(outStr, {
inputFormat: 'json',
outputFormat: outputFormat,
drawFileList: drawFileList,
matching: 'lines',
renderNothingWhenEmpty: renderNothingWhenEmpty})
return hljs(html)
}
}
Expand Down