EditText.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <div class="edit-text">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. :key="modalForm.id"
  8. label-width="100px"
  9. >
  10. <el-form-item label="字号:">
  11. <size-select
  12. v-model="modalForm.fontSize"
  13. style="width: 100%"
  14. ></size-select>
  15. </el-form-item>
  16. <el-form-item label="颜色:">
  17. <color-select v-model="modalForm.color"></color-select>
  18. </el-form-item>
  19. <el-form-item label="加粗:">
  20. <el-checkbox v-model="isBold" @change="boldChange"
  21. >是否加粗</el-checkbox
  22. >
  23. </el-form-item>
  24. <el-form-item prop="contentStr" label="内容:">
  25. <el-input
  26. type="textarea"
  27. :rows="4"
  28. placeholder="请输入内容"
  29. v-model="modalForm.contentStr"
  30. @change="contentChange"
  31. ref="contentTextarea"
  32. >
  33. </el-input>
  34. </el-form-item>
  35. </el-form>
  36. </div>
  37. </template>
  38. <script>
  39. import SizeSelect from "../../components/common/SizeSelect";
  40. import ColorSelect from "../../components/common/ColorSelect";
  41. const initModalForm = {
  42. id: "",
  43. fontSize: "14px",
  44. color: "",
  45. fontFamily: "",
  46. fontWeight: 400,
  47. rotation: 0,
  48. content: [],
  49. contentStr: "",
  50. };
  51. export default {
  52. name: "edit-text",
  53. components: {
  54. SizeSelect,
  55. ColorSelect,
  56. },
  57. props: {
  58. instance: {
  59. type: Object,
  60. default() {
  61. return {};
  62. },
  63. },
  64. },
  65. data() {
  66. return {
  67. modalForm: { ...initModalForm },
  68. isBold: false,
  69. rules: {
  70. contentStr: [
  71. {
  72. required: true,
  73. message: "请输入文本内容",
  74. trigger: "change",
  75. },
  76. ],
  77. },
  78. };
  79. },
  80. mounted() {
  81. this.initData(this.instance);
  82. },
  83. methods: {
  84. initData(val) {
  85. const contentStr = val.content
  86. .map((item) => {
  87. return item.type === "text"
  88. ? item.content
  89. : "${" + item.content + "}";
  90. })
  91. .join("");
  92. this.modalForm = { ...val, contentStr };
  93. this.isBold = val.fontWeight > 400;
  94. },
  95. boldChange(isBold) {
  96. this.modalForm.fontWeight = isBold ? 700 : 400;
  97. },
  98. contentChange() {
  99. const constentStr = this.modalForm.contentStr;
  100. const rexp = new RegExp(/\$\{.+?\}/, "g");
  101. const variates = constentStr.match(rexp);
  102. const texts = constentStr.split(rexp);
  103. let contents = [];
  104. texts.forEach((text, index) => {
  105. if (text)
  106. contents.push({
  107. type: "text",
  108. content: text,
  109. });
  110. if (variates && variates[index])
  111. contents.push({
  112. type: "variate",
  113. content: variates[index].replace("${", "").replace("}", ""),
  114. });
  115. });
  116. this.modalForm.content = contents;
  117. },
  118. async submit() {
  119. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  120. if (!valid) return;
  121. this.$emit("modified", this.modalForm);
  122. },
  123. },
  124. };
  125. </script>