提交 c180922f 编写于 作者: B baiy 提交者: ninecents

diff 重构

上级 6fd03f8e
{ {
"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> <template>
<div ref="container" class="diff-editor" :style="`height:${height};width:${width}`"></div> <div ref="container" class="diff-editor" :style="`height:${containerHeight};width:${width}`"></div>
</template> </template>
<script> <script>
import {createMerge} from "../library/editor"; import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
export default { export default {
name: 'diffEditor', name: 'diffEditor',
...@@ -14,13 +14,21 @@ export default { ...@@ -14,13 +14,21 @@ export default {
type: String, type: String,
default: "" default: ""
}, },
collapse: { autoHeight: {
type: Number,
default: 0
},
theme: {
type: String,
default: 'vs'
},
roundedSelection: {
type: Boolean, type: Boolean,
default: false default: true
}, },
height: { height: {
type: String, type: String,
default: "100%" default: "350px"
}, },
width: { width: {
type: String, type: String,
...@@ -29,51 +37,83 @@ export default { ...@@ -29,51 +37,83 @@ export default {
}, },
watch: { watch: {
value() { value(newValue) {
this.reset() 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) { language(newValue) {
if (this.editor !== null) { if (this.editor !== null) {
this.editor.customSetEditorLanguage(newValue); monaco.editor.setModelLanguage(this.editor.getOriginalEditor().getModel(), newValue)
monaco.editor.setModelLanguage(this.editor.getModifiedEditor().getModel(), newValue)
} }
}, },
collapse() { theme(newValue) {
this.reset() 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() { mounted() {
this.reset() this.initEditor()
}, },
data() { data() {
return { return {
editor: null, editor: null,
original: null,
modified: null,
containerHeight: ""
} }
}, },
methods: { methods: {
reset() {
this.$refs.container.innerHTML = "";
this.initEditor();
},
initEditor() { initEditor() {
this.editor = createMerge(this.value, this.$refs.container, this.language, {collapseIdentical: this.collapse ? 2 : false}) this.$refs.container.innerHTML = ''
this.editor.customChange((original, modified) => { this.editor = monaco.editor.createDiffEditor(this.$refs.container, {
if (original !== this.value.original || modified !== this.value.modified) { theme: this.theme,
this.value.original = original roundedSelection: this.roundedSelection,
this.value.modified = modified 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) 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> </script>
<style>
.diff-editor .CodeMirror-merge{
border: none;
}
.diff-editor .CodeMirror-merge,.diff-editor .CodeMirror-merge-pane, .diff-editor .CodeMirror-merge .CodeMirror {
height: 100%;
}
</style>
<template> <template>
<div> <div>
<heightResize :append="['.page-option-block']"> <div style="border: 1px solid #dcdee2; border-radius: 4px">
<div style="border: 1px solid #dcdee2; border-radius: 4px;height: 100%;"> <diffEditor ref="editor" v-model="current.diff" :language="current.language" :auto-height="220" />
<diff-editor ref="editor" :collapse="current.collapse" v-model="current.diff" :language="current.language" />
</div> </div>
</heightResize> <option-block>
<option-block class="page-option-block">
<FormItem> <FormItem>
<ButtonGroup> <ButtonGroup>
<Button <Button
:type="current.language !== item ? 'primary' : 'warning'" :type="current.language !== item.id ? 'primary' : 'warning'"
@click="setLanguage(item)" @click="setLanguage(item.id)"
v-for="item in buttonLang" v-for="item in buttonLang"
:key="item" :key="item.id"
>{{ item }} >{{ item.name }}
</Button> </Button>
</ButtonGroup> </ButtonGroup>
</FormItem> </FormItem>
<FormItem> <FormItem>
<Select :placeholder="$t('diffs_more')" @on-change="(value)=>{setLanguage(value)}"> <Select placeholder="更多语言" @on-change="(value)=>{setLanguage(value)}">
<Option v-for="item in allLang" :value="item" :key="item">{{ item }}</Option> <Option v-for="item in allLang" :value="item.id" :key="item.id">{{ item.name }}</Option>
</Select> </Select>
</FormItem> </FormItem>
<FormItem> <FormItem>
<Checkbox v-model="current.collapse">{{ $t('diffs_collapse') }}</Checkbox> <Checkbox @on-change="(value)=>inline(value)">行内对比</Checkbox>
</FormItem> </FormItem>
</option-block> </option-block>
</div> </div>
</template> </template>
<script> <script>
import diffEditor from "./components/diffEditor"; import diffEditor from "./components/diffEditor";
import {allLang} from "./library/editor"; import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import _ from "lodash";
import heightResize from "./components/heightResize"; let allLang = {}
for (let lang of monaco.languages.getLanguages()) {
allLang[lang.id] = {
id: lang.id,
name: lang.id === "plaintext" ? "纯文本" : lang.aliases[0]
}
}
const COMMON_LANG = [ const COMMON_LANG = [
"text", "plaintext",
"javascript",
"html", "html",
"js",
"css", "css",
"json", "json",
"python", "python",
...@@ -48,44 +52,47 @@ const COMMON_LANG = [ ...@@ -48,44 +52,47 @@ const COMMON_LANG = [
export default { export default {
components: { components: {
diffEditor, diffEditor,
heightResize
}, },
computed: { computed: {
allLang() { allLang() {
return allLang return Object.values(allLang)
}, },
buttonLang() { buttonLang() {
let data = _.cloneDeep(COMMON_LANG); let data = COMMON_LANG.map((item) => {
if (this.current.language && !data.includes(this.current.language)) { return allLang[item]
data.push(this.current.language) });
if (this.current.language && !COMMON_LANG.includes(this.current.language)) {
data.push(allLang[this.current.language])
} }
return data; return data;
} }
}, },
created() { created() {
this.$initToolData() this.current = Object.assign(this.current, this.$getToolData())
}, },
methods: { methods: {
setLanguage(lang) { setLanguage(lang) {
this.current.language = lang; this.current.language = lang;
},
inline(value){
this.$refs.editor.inline(!value)
} }
}, },
watch: { watch: {
current: { current:{
handler(newVal) { handler(newVal){
if (newVal.diff.original && newVal.diff.modified) { if (newVal.diff.original && newVal.diff.modified){
this.$saveToolData(this.current); this.$saveToolData(this.current);
} }
}, },
deep: true deep:true
} }
}, },
data() { data() {
return { return {
current: { current: {
diff: {original: "", modified: ""}, diff: {original: "", modified: ""},
language: "text", language: ""
collapse: false
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册