EditText.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <el-dialog
  3. class="edit-text edit-dialog"
  4. :visible.sync="dialogIsShow"
  5. title="文本编辑"
  6. top="10vh"
  7. width="640px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. @open="opened"
  12. @close="closed"
  13. >
  14. <el-form
  15. ref="modalFormComp"
  16. :model="modalForm"
  17. :rules="rules"
  18. :key="modalForm.id"
  19. label-width="100px"
  20. >
  21. <el-form-item label="字号:">
  22. <size-select
  23. v-model="modalForm.fontSize"
  24. style="width:100%;"
  25. ></size-select>
  26. </el-form-item>
  27. <el-form-item label="字体:">
  28. <font-family-select
  29. v-model="modalForm.fontFamily"
  30. style="width:100%;"
  31. ></font-family-select>
  32. </el-form-item>
  33. <el-form-item label="颜色:">
  34. <color-select v-model="modalForm.color"></color-select>
  35. </el-form-item>
  36. <el-form-item label="加粗:">
  37. <el-checkbox v-model="isBold" @change="boldChange"
  38. >是否加粗</el-checkbox
  39. >
  40. </el-form-item>
  41. <el-form-item prop="contentStr" label="内容:">
  42. <el-input
  43. type="textarea"
  44. :rows="4"
  45. placeholder="请输入内容"
  46. v-model="modalForm.contentStr"
  47. @change="contentChange"
  48. ref="contentTextarea"
  49. >
  50. </el-input>
  51. </el-form-item>
  52. </el-form>
  53. <div slot="footer">
  54. <el-button type="primary" @click="submit">确认</el-button>
  55. <el-button @click="cancel" plain>取消</el-button>
  56. </div>
  57. </el-dialog>
  58. </template>
  59. <script>
  60. import SizeSelect from "../common/SizeSelect";
  61. import ColorSelect from "../common/ColorSelect";
  62. import FontFamilySelect from "../common/FontFamilySelect";
  63. const initModalForm = {
  64. id: "",
  65. fontSize: "10.5pt",
  66. color: "",
  67. fontFamily: "",
  68. fontWeight: 400,
  69. rotation: 0,
  70. content: [],
  71. contentStr: ""
  72. };
  73. export default {
  74. name: "edit-text",
  75. components: {
  76. SizeSelect,
  77. ColorSelect,
  78. FontFamilySelect
  79. },
  80. props: {
  81. instance: {
  82. type: Object,
  83. default() {
  84. return {};
  85. }
  86. }
  87. },
  88. data() {
  89. return {
  90. dialogIsShow: false,
  91. modalForm: { ...initModalForm },
  92. isBold: false,
  93. rules: {
  94. contentStr: [
  95. {
  96. required: true,
  97. message: "请输入文本内容",
  98. trigger: "change"
  99. }
  100. ]
  101. }
  102. };
  103. },
  104. methods: {
  105. initData(val) {
  106. const contentStr = val.content
  107. .map(item => {
  108. return item.type === "text"
  109. ? item.content
  110. : "${" + item.content + "}";
  111. })
  112. .join("");
  113. this.modalForm = { ...val, contentStr };
  114. this.isBold = val.fontWeight > 400;
  115. },
  116. boldChange(isBold) {
  117. this.modalForm.fontWeight = isBold ? 700 : 400;
  118. },
  119. contentChange() {
  120. const constentStr = this.modalForm.contentStr;
  121. const rexp = new RegExp(/\$\{.+?\}/, "g");
  122. const variates = constentStr.match(rexp);
  123. const texts = constentStr.split(rexp);
  124. let contents = [];
  125. texts.forEach((text, index) => {
  126. if (text)
  127. contents.push({
  128. type: "text",
  129. content: text
  130. });
  131. if (variates && variates[index])
  132. contents.push({
  133. type: "variate",
  134. content: variates[index].replace("${", "").replace("}", "")
  135. });
  136. });
  137. this.modalForm.content = contents;
  138. },
  139. opened() {
  140. this.initData(this.instance);
  141. },
  142. closed() {
  143. this.$emit("closed");
  144. },
  145. cancel() {
  146. this.dialogIsShow = false;
  147. },
  148. open() {
  149. this.dialogIsShow = true;
  150. },
  151. async submit() {
  152. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  153. if (!valid) return;
  154. this.$emit("modified", this.modalForm);
  155. this.cancel();
  156. }
  157. }
  158. };
  159. </script>