点击span标签,使span标签的内容插入到input光标处
<el-input
ref="formulaTextareaRef"
v-model="expression"
type="textarea"
placeholder="示例:(月度设备可利用率-98%)/0.1*10"
autosize
/>
span内容通过for循环获取
<span
v-for="op in visibleOperators"
:key="op.value"
:value="op.value"
:label="op.value"
@click="insertTextAtCursor(op.value)"
>{{ op.value }}</span>
光标处插入文字
async insertTextAtCursor(value) {
// 获取input标签
const textarea = this.$refs.formulaTextareaRef.$el.children[0]
const startPos = textarea.selectionStart
const endPos = textarea.selectionEnd
this.expression = textarea.value.slice(0, startPos) + value + textarea.value.slice(endPos, textarea.value.length)
await nextTick()
textarea.focus()
if (value.slice(-2) === '()') {
textarea.setSelectionRange(startPos + value.length - 1, startPos + value.length - 1)
} else {
textarea.setSelectionRange(startPos + value.length, startPos + value.length)
}
},