md/src/components/codeMirror/insertForm.vue

71 lines
2.1 KiB
Vue
Raw Normal View History

2020-05-02 11:50:26 +08:00
<template>
<el-dialog title="插入表格" :visible="dialogFormVisible" @close="$emit('close')">
<el-form :model="config.form">
<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">
<el-button type="success" plain @click="$emit('close')"> </el-button>
<el-button type="success" @click="insertTable"> </el-button>
</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: {
...mapState({
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>