提交 43b80ef7 编写于 作者: B baiy 提交者: ninecents

diff 重构

上级 7e6b93ba
{ {
"name": "c-tool", "name": "c-tool",
"version": "1.7.10", "version": "1.8.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
...@@ -4505,11 +4505,6 @@ ...@@ -4505,11 +4505,6 @@
"integrity": "sha512-qi86tE6hRcFHy8jI1m2VG+LaPUR1LhqDa5G8tVjuUXmOrpuAgqsA1pN0+ldgr3aKUH+QLI9hCY/OcRYisERejw==", "integrity": "sha512-qi86tE6hRcFHy8jI1m2VG+LaPUR1LhqDa5G8tVjuUXmOrpuAgqsA1pN0+ldgr3aKUH+QLI9hCY/OcRYisERejw==",
"dev": true "dev": true
}, },
"diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A=="
},
"diffie-hellman": { "diffie-hellman": {
"version": "5.0.3", "version": "5.0.3",
"resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz",
......
<template>
<div ref="container" class="diff-editor" :style="`height:${containerHeight};width:${width}`"></div>
</template>
<script>
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
export default {
name: 'diffEditor',
props: {
value: {
type: Object
},
language: {
type: String,
default: ""
},
autoHeight: {
type: Number,
default: 0
},
theme: {
type: String,
default: 'vs'
},
roundedSelection: {
type: Boolean,
default: true
},
height: {
type: String,
default: "350px"
},
width: {
type: String,
default: "100%"
},
},
watch: {
value(newValue) {
if (this.editor !== null
&& (
this.original.getValue() !== newValue['original']
|| this.modified.getValue() !== newValue['modified']
)) {
this.original.setValue(newValue['original'])
this.modified.setValue(newValue['modified'])
}
},
language(newValue) {
if (this.editor !== null) {
monaco.editor.setModelLanguage(this.editor.getOriginalEditor().getModel(), newValue)
monaco.editor.setModelLanguage(this.editor.getModifiedEditor().getModel(), newValue)
}
},
theme(newValue) {
if (this.editor !== null) {
monaco.editor.setTheme(newValue)
}
}
},
created() {
if (this.autoHeight > 0) {
this.containerHeight = (window.innerHeight - this.autoHeight) + "px"
} else {
this.containerHeight = this.height
}
},
mounted() {
this.initEditor()
},
data() {
return {
editor: null,
original: null,
modified: null,
containerHeight: ""
}
},
methods: {
initEditor() {
this.$refs.container.innerHTML = ''
this.editor = monaco.editor.createDiffEditor(this.$refs.container, {
theme: this.theme,
roundedSelection: this.roundedSelection,
automaticLayout: true,
renderSideBySide: true
})
this.original = monaco.editor.createModel(this.value['original'], this.language)
this.modified = monaco.editor.createModel(this.value['modified'], this.language)
this.editor.setModel({original: this.original, modified: this.modified})
this.editor.getOriginalEditor().updateOptions({readOnly: false})
this.original.onDidChangeContent(() => {
if (this.value.original !== this.original.getValue()) {
this.value.original = this.original.getValue()
this.$emit('input', this.value)
}
})
this.modified.onDidChangeContent(() => {
if (this.value.modified !== this.modified.getValue()) {
this.value.modified = this.modified.getValue()
this.$emit('input', this.value)
}
})
},
// 行内模式
inline(value) {
if (this.editor) {
value = !!value;
this.editor.updateOptions({renderSideBySide: !!value})
this.editor.getOriginalEditor().updateOptions({readOnly: !value})
this.editor.getModifiedEditor().updateOptions({readOnly: !value})
}
}
}
};
</script>
<template> <template>
<div> <div>
<Tabs v-model="current.operation"> <div style="border: 1px solid #dcdee2; border-radius: 4px">
<TabPane label="对比内容" name="input"> <diffEditor ref="editor" v-model="current.diff" :language="current.language" :auto-height="220" />
<Row :gutter="16"> </div>
<Col span="12"> <option-block>
<Input v-model="current.source" :rows="14" type="textarea" placeholder="内容"></Input> <FormItem>
</Col> <ButtonGroup>
<Col span="12"> <Button
<Input v-model="current.target" :rows="14" type="textarea" placeholder="内容"></Input> :type="current.language !== item.id ? 'primary' : 'warning'"
</Col> @click="setLanguage(item.id)"
</Row> v-for="item in buttonLang"
</TabPane> :key="item.id"
<TabPane label="对比结果" name="result"> >{{ item.name }}
<RadioGroup v-model="current.type" type="button"> </Button>
<Radio :label="k" v-for="(v,k) in type" :key="k"> </ButtonGroup>
<span>{{v}}</span> </FormItem>
</Radio> <FormItem>
</RadioGroup> <Select placeholder="更多语言" @on-change="(value)=>{setLanguage(value)}">
<div class="diff-block"> <Option v-for="item in allLang" :value="item.id" :key="item.id">{{ item.name }}</Option>
<diff-block :diff="diff"></diff-block> </Select>
</div> </FormItem>
</TabPane> <FormItem>
</Tabs> <Checkbox @on-change="(value)=>inline(value)">行内对比</Checkbox>
</FormItem>
</option-block>
</div> </div>
</template> </template>
<script> <script>
const jsDiff = require('diff'); import diffEditor from "./components/diffEditor";
export default { import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
components: {
"diff-block": {
render: function (createElement) {
let e = [];
let diff = this.diff;
for (let i = 0; i < diff.length; i++) { let allLang = {}
if (diff[i].added && diff[i + 1] && diff[i + 1].removed) { for (let lang of monaco.languages.getLanguages()) {
let swap = diff[i]; allLang[lang.id] = {
diff[i] = diff[i + 1]; id: lang.id,
diff[i + 1] = swap; name: lang.id === "plaintext" ? "纯文本" : lang.aliases[0]
} }
if (diff[i].removed){ }
e.push(createElement(
'del', const COMMON_LANG = [
diff[i].value "plaintext",
)) "javascript",
} "html",
else if(diff[i].added){ "css",
e.push(createElement( "json",
'ins', "python",
diff[i].value "java",
)) "php"
} ]
else{
e.push(diff[i].value) export default {
} components: {
} diffEditor,
return createElement( },
'pre', computed: {
e allLang() {
) return Object.values(allLang)
},
props: {
diff: {
type: Array,
default: []
}
}
},
}, },
computed: { buttonLang() {
diff() { let data = COMMON_LANG.map((item) => {
let beginTime = new Date(); return allLang[item]
console.log("开始对比"+this.current.type); });
let diff = jsDiff[this.current.type](this.current.source, this.current.target); if (this.current.language && !COMMON_LANG.includes(this.current.language)) {
this.$saveToolData(this.current); data.push(allLang[this.current.language])
console.log("结束对比 用时:"+((new Date())-beginTime)+"ms "+this.current.type);
return diff;
} }
return data;
}
},
created() {
this.current = Object.assign(this.current, this.$getToolData())
},
methods: {
setLanguage(lang) {
this.current.language = lang;
}, },
created() { inline(value){
this.current = Object.assign(this.current, this.$getToolData()) this.$refs.editor.inline(!value)
}, }
methods: { },
// handle(type) { watch: {
// this.current.diff = JsDiff[type](this.current.source, this.current.target) current:{
// this.current.operation = "result"; handler(newVal){
// this.$saveToolData(this.current); if (newVal.diff.original && newVal.diff.modified){
// }, this.$saveToolData(this.current);
},
data() {
return {
current: {
source: "",
target: "",
type: "diffLines",
operation: "input",
},
type: {
"diffLines": "",
"diffWords": "单词",
"diffCss": "css",
"diffJson": "json",
"diffArrays": "js数组(性能不好)",
"diffChars": "字符(性能不好)",
} }
},
deep:true
}
},
data() {
return {
current: {
diff: {original: "", modified: ""},
language: ""
} }
} }
} }
}
</script> </script>
<style>
.diff-block del {
text-decoration: none;
color: #b30000;
background: #fadad7;
}
.diff-block ins {
background: #eaf2c2;
color: #406619;
text-decoration: none;
}
.diff-block pre{
background: #f5f2f0;
padding: 1em;
margin: .5em 0;
overflow: auto;
}
</style>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册