md/src/components/CodemirrorEditor/insertForm.vue

127 lines
3.4 KiB
Vue
Raw Normal View History

2020-05-02 11:50:26 +08:00
<template>
2020-07-12 20:51:06 +08:00
<el-dialog
title="插入表格"
class="insert__dialog"
2020-07-13 12:05:09 +08:00
:visible="value"
@close="$emit('input', false)"
2020-07-12 20:51:06 +08:00
border
>
<el-row class="tb-options" type="flex" align="middle" :gutter="10">
<el-col :span="6">
<el-input-number
v-model="rowNum"
controls-position="right"
@change="handleChange($event,'row')"
:min="1"
:max="100"
size="small"
></el-input-number>
</el-col>
<el-col :span="6">
<el-input-number
v-model="colNum"
controls-position="right"
@change="handleChange($event,'col')"
:min="1"
:max="100"
size="small"
></el-input-number>
</el-col>
</el-row>
<!-- -->
<table style="border-collapse: collapse">
<tr :class="{ 'head-style': row===1 }" v-for="row in rowNum+1" :key="row">
<td v-for="col in colNum" :key="col">
<el-input
align="center"
v-model="tableData[`k_${row-1}_${col-1}`]"
:placeholder="row===1?'表头':''"
/>
</td>
</tr>
</table>
<!-- -->
<div slot="footer" class="dialog-footer">
2020-07-13 12:05:09 +08:00
<el-button :type="btnType" plain @click="$emit('input', false)"> </el-button>
2020-07-12 20:51:06 +08:00
<el-button :type="btnType" @click="insertTable" plain> </el-button>
</div>
</el-dialog>
2020-05-02 11:50:26 +08:00
</template>
<script>
2020-07-12 22:55:41 +08:00
import config from "../../assets/scripts/config";
import {
mapState,
mapMutations
} from "vuex";
export default {
props: {
2020-07-13 12:05:09 +08:00
value: {
2020-07-12 22:55:41 +08:00
type: Boolean,
default: false
}
},
data() {
return {
config: config,
rowNum: 1,
colNum: 1,
tableData: {}
};
},
computed: {
btnType() {
2020-08-31 10:15:21 +08:00
return this.nightMode ? "default" : "primary";
2020-07-12 22:55:41 +08:00
},
...mapState({
nightMode: state => state.nightMode,
editor: state => state.editor
})
},
methods: {
// 插入表格
insertTable() {
const cursor = this.editor.getCursor();
const rows = this.rowNum;
const cols = this.colNum;
2020-07-12 20:51:06 +08:00
2020-07-12 22:55:41 +08:00
let table = "";
let currRow = [];
for (let i = 0; i < rows + 2; ++i) {
table += "|\t";
currRow = [];
for (let j = 0; j < cols; ++j) {
const rowIdx = i > 1 ? i - 1 : i;
2020-07-13 12:05:09 +08:00
i === 1 ?
2020-07-12 22:55:41 +08:00
currRow.push("---\t") :
currRow.push(this.tableData[`k_${rowIdx}_${j}`] || "");
}
table += currRow.join("\t|\t");
table += "\t|\n";
}
this.tableData = {};
this.rowNum = 1;
this.colNum = 1;
this.editor.replaceSelection(`\n${table}\n`, "end");
2020-07-13 12:05:09 +08:00
this.$emit('input', false);
2020-07-12 22:55:41 +08:00
this.editorRefresh();
},
...mapMutations(["editorRefresh"]),
handleChange(val, type) {}
2020-05-02 11:50:26 +08:00
}
2020-07-12 22:55:41 +08:00
};
2020-07-12 20:51:06 +08:00
</script>
2020-05-02 11:50:26 +08:00
2020-07-12 20:51:06 +08:00
<style lang="less">
.tb-options {
2020-07-12 22:55:41 +08:00
margin-bottom: 20px;
2020-05-02 11:50:26 +08:00
}
2020-07-12 20:51:06 +08:00
.head-style .el-input__inner {
2020-07-12 22:55:41 +08:00
background-color: #f2f2f2;
2020-07-12 20:51:06 +08:00
}
</style>