index.vue 2.85 KB
<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>