md/src/components/codeMirror/insertForm.vue

75 lines
2.3 KiB
Vue
Raw Normal View History

2020-05-02 11:50:26 +08:00
<template>
2020-05-17 18:16:26 +08:00
<el-dialog title="插入表格" class="insert__dialog" :visible="dialogFormVisible" @close="$emit('close')">
<el-form class="insert__form" :model="config.form">
2020-05-02 11:50:26 +08:00
<el-form-item label="行数(表头不计入行数)">
<el-input v-model="config.form.rows"></el-input>
</el-form-item>
<el-form-item label="列数">
<el-input v-model="config.form.cols"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
2020-05-17 18:16:26 +08:00
<el-button :type="btnType" plain @click="$emit('close')"> </el-button>
<el-button :type="btnType" @click="insertTable" plain> </el-button>
2020-05-02 11:50:26 +08:00
</div>
</el-dialog>
</template>
<script>
import config from '../../scripts/config'
import {mapState, mapMutations} from 'vuex';
export default {
props: {
dialogFormVisible: {
type: Boolean,
default: false
}
},
data() {
return {
config: config
}
},
computed: {
2020-05-17 18:16:26 +08:00
btnType() {
return !this.nightMode ? 'success' : 'default';
},
2020-05-02 11:50:26 +08:00
...mapState({
2020-05-17 18:16:26 +08:00
nightMode: state=> state.nightMode,
2020-05-02 11:50:26 +08:00
editor: state=> state.editor
})
},
methods: {
// 插入表格
insertTable() {
const cursor = this.editor.getCursor()
const rows = parseInt(this.config.form.rows)
const cols = parseInt(this.config.form.cols)
if (isNaN(rows) || isNaN(cols) || rows < 1 || cols < 1) {
this.$message({
showClose: true,
message: '输入的行/列数无效,请重新输入',
type: 'error'
})
return
}
let table = ''
for (let i = 0; i < rows + 2; ++i) {
2020-05-02 15:59:06 +08:00
for (let j = 0; j < cols + 1; ++j) {
table += (j === 0 ? '|' : (i !== 1 ? ' |' : ' --- |'))
}
2020-05-02 11:50:26 +08:00
table += '\n'
}
this.editor.replaceSelection(`\n${table}\n`, cursor)
this.$emit('close')
this.editorRefresh()
},
...mapMutations(['editorRefresh'])
}
}
</script>
<style lang="less" scoped>
</style>