chore:修改保存操作时的逻辑

This commit is contained in:
zhaoxuhui 2018-08-23 16:24:03 +08:00
parent 41c04ba5aa
commit ef15e51666
2 changed files with 36 additions and 27 deletions

View File

@ -104,7 +104,6 @@
<textarea <textarea
v-model="value" v-model="value"
@keydown.tab="tab" @keydown.tab="tab"
@keydown.meta.83="save"
@keyup.enter="enter" @keyup.enter="enter"
@keyup.delete="onDelete" @keyup.delete="onDelete"
ref="textarea" ref="textarea"

View File

@ -25,9 +25,9 @@ export default {
type : String, type : String,
default : '' default : ''
}, },
titleStyle:{ titleStyle : {
type:Object, type : Object,
default(){ default() {
return {} return {}
} }
}, },
@ -36,8 +36,8 @@ export default {
default : 'Light' default : 'Light'
}, },
width : {// 宽度 width : {// 宽度
type:[Number,String], type : [Number, String],
default:'auto' default : 'auto'
}, },
height : { // 高度 height : { // 高度
type : Number, type : Number,
@ -57,9 +57,9 @@ export default {
type : Number, type : Number,
default : 10000 default : 10000
}, },
initialValue:{ // 初始化值 initialValue : { // 初始化值
type:String, type : String,
default:'' default : ''
} }
}, },
data() { data() {
@ -96,7 +96,7 @@ export default {
shift : true, shift : true,
fullscreen : true, fullscreen : true,
print : true, print : true,
theme:true theme : true
}, },
slideDown : false, slideDown : false,
themeName : 'Light',// 主题名称 themeName : 'Light',// 主题名称
@ -114,31 +114,28 @@ export default {
} }
}, },
tools() { tools() {
const {allTools,toolbars} = this ; const {allTools, toolbars} = this;
return Object.assign(allTools,toolbars) return Object.assign(allTools, toolbars)
} }
}, },
mounted() { mounted() {
this.$nextTick(()=>{ this.$nextTick(() => {
this.$refs.textarea.focus(); this.$refs.textarea.focus();
}) })
this.init(); this.init();
this.addListener();
}, },
methods : { methods : {
init() { init() {
this.themeName = this.theme; this.themeName = this.theme;
const {autoSave, interval,theme,initialValue} = this; const {autoSave, interval, theme, initialValue} = this;
this.value = initialValue ; this.value = initialValue;
this.previewMarkdown = marked(initialValue, { this.previewMarkdown = marked(initialValue, {
sanitize : true sanitize : true
}); });
if (autoSave) { if (autoSave) {
this.timerId = setInterval(() => { this.timerId = setInterval(() => {
this.$emit('on-save', { this.handleSave();
markdownValue : this.value,
htmlValue : this.previewMarkdown,
theme
});
}, interval) }, interval)
} }
@ -167,9 +164,9 @@ export default {
this.scroll = side; this.scroll = side;
}, },
insertContent(str) { // 插入文本 insertContent(str) { // 插入文本
const {preview} = this ; const {preview} = this;
if(preview===2){ if (preview === 2) {
return ; return;
} }
this.lastInsert = str; this.lastInsert = str;
const textareaDom = this.$refs.textarea; const textareaDom = this.$refs.textarea;
@ -333,10 +330,13 @@ export default {
}, },
save(e) { // 保存 save(e) { // 保存
e.preventDefault(); e.preventDefault();
this.handleSave();
},
handleSave() {
this.$emit('on-save', { this.$emit('on-save', {
markdownValue : this.value, markdownValue : this.value,
htmlValue : this.previewMarkdown, htmlValue : this.previewMarkdown,
theme:this.theme theme : this.theme
}); });
}, },
insertLine() { // 插入分割线 insertLine() { // 插入分割线
@ -365,14 +365,24 @@ export default {
print() { // 打印文件 print() { // 打印文件
const dom = this.$refs.preview; const dom = this.$refs.preview;
Print(dom); Print(dom);
},
addListener() { // 事件监听,阻止保存
document.addEventListener('keydown', e => {
if (e.keyCode === 83 && e.metaKey) {
e.preventDefault();
this.handleSave();
}
})
} }
}, },
watch : { watch : {
value() { value() {
clearTimeout(this.timeoutId); clearTimeout(this.timeoutId);
this.previewMarkdown = marked(this.value, { this.timeoutId = setTimeout(() => {
sanitize : true this.previewMarkdown = marked(this.value, {
}); sanitize : true
});
}, 30)
this.indexLenth = this.value.split('\n').length; this.indexLenth = this.value.split('\n').length;
const height_1 = this.indexLenth * 22; const height_1 = this.indexLenth * 22;
const height_2 = this.$refs.textarea.scrollHeight; const height_2 = this.$refs.textarea.scrollHeight;