index.vue
2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<template>
<div class="variable-textarea-wrapper">
<el-input ref="textareaRef"
placeholder="请输入消息内容"
resize="none"
rows="8"
type="textarea"
show-word-limit
:maxlength="maxLength"
:value="value"
@input="handleInput">
</el-input>
<div class="foot-wrapper">
<el-dropdown placement="bottom-start"
style="width:100%"
trigger="click"
@command="handleCommand">
<div class="dropdown-wrapper">
<i class="el-icon-plus "></i>
<span class="el-dropdown-link" style="margin-left:8px">
插入变量
</span>
</div>
<el-dropdown-menu slot="dropdown"
class="marketing-dropdown-menu"
style="max-height: 400px; overflow: auto;">
<el-dropdown-item v-for="variable in variableList"
:key="variable.code"
:command="variable.name">
{{ variable.name }}
</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div>
</div>
</template>
<script>
export default {
name: "VariableTextarea",
props: {
maxLength: {
type: Number,
default: 1000,
},
value: {
type: String,
required: true,
},
variableList: {
type: Array,
default: () => ([]),
},
},
data() {
return {
autosize: {
minRows: 6,
maxRows: 60,
},
};
},
computed: {
},
mounted() {
},
methods: {
handleInput(e) {
this.$emit("input", e);
},
handleCommand(command) {
const textarea = this.$refs.textareaRef.$el.querySelector("textarea");
const text = this.value || "";
const insertion = `{{${command}}}`;
if (textarea) {
// 在光标位置插入
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const newValue = text.substring(0, start) + insertion + text.substring(end);
this.$emit("input", newValue);
this.$nextTick(() => {
const newPos = start + insertion.length;
textarea.setSelectionRange(newPos, newPos);
textarea.focus();
});
} else {
this.$emit("input", text + insertion);
}
},
},
};
</script>
<style scoped lang="scss">
.variable-textarea-wrapper {
border-radius: 4px;
border: 1px solid #e5e5e5;
::v-deep .el-textarea {
.el-textarea__inner {
border: none;
border-radius: 0;
box-shadow: none;
background-color: #f5f5f5;
color: #666;
}
.el-input__count {
right: 10px;
bottom: -42px;
background-color: transparent;
}
}
.foot-wrapper {
background-color: #f5f6fa;
border-top: 1px solid #e5e5e5;
.dropdown-wrapper {
width: 100%;
color: #2a58ad;
cursor: pointer;
display: flex;
align-items: center;
padding: 8px 16px;
}
}
}
</style>