EditImage.vue 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div class="edit-image">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :key="modalForm.id"
  7. label-width="100px"
  8. >
  9. <el-form-item label="边框颜色:">
  10. <color-select v-model="modalForm.borderColor" show-empty></color-select>
  11. </el-form-item>
  12. <el-form-item label="边框线形:">
  13. <line-style-select v-model="modalForm.borderStyle"></line-style-select>
  14. </el-form-item>
  15. <el-form-item>
  16. <el-button type="primary" @click="addImage">插入图片</el-button>
  17. <input
  18. ref="fileInput"
  19. type="file"
  20. style="display:none;"
  21. @change="imageChange"
  22. accept=".jpeg,.jpg,.png"
  23. />
  24. </el-form-item>
  25. <div style="text-align: center;">
  26. <img style="width: 150px;" :src="imageSrc" alt="图片" v-if="imageSrc" />
  27. </div>
  28. </el-form>
  29. </div>
  30. </template>
  31. <script>
  32. import ColorSelect from "../../components/common/ColorSelect";
  33. import LineStyleSelect from "../../components/common/LineStyleSelect";
  34. const initModalForm = {
  35. id: "",
  36. borderColor: "",
  37. borderStyle: "",
  38. content: []
  39. };
  40. export default {
  41. name: "edit-image",
  42. components: { ColorSelect, LineStyleSelect },
  43. props: {
  44. instance: {
  45. type: Object,
  46. default() {
  47. return {};
  48. }
  49. }
  50. },
  51. data() {
  52. return {
  53. imageSrc: "",
  54. modalForm: { ...initModalForm }
  55. };
  56. },
  57. mounted() {
  58. this.initData(this.instance);
  59. },
  60. methods: {
  61. initData(val) {
  62. this.modalForm = { ...val };
  63. const content = val.content && val.content[0] && val.content[0].content;
  64. this.imageSrc =
  65. content && content.indexOf("base64") !== -1 ? content : "";
  66. },
  67. addImage() {
  68. this.$refs.fileInput.click();
  69. },
  70. imageChange(e) {
  71. const file = e.target.files[0];
  72. if (file.size > 100 * 1024) {
  73. this.$message.error("图片大小不要超过100kb");
  74. return;
  75. }
  76. const reader = new FileReader();
  77. reader.readAsDataURL(file);
  78. reader.onload = e => {
  79. this.modalForm.content = [
  80. {
  81. type: "text",
  82. content: e.target.result
  83. }
  84. ];
  85. this.imageSrc = e.target.result;
  86. this.$refs.fileInput.value = null;
  87. };
  88. },
  89. submit() {
  90. this.$emit("modified", this.modalForm);
  91. }
  92. }
  93. };
  94. </script>