兰宝车间质量管理系统-前端
疯狂的狮子Li
2023-11-30 e1023bb3313282b00d6dcdefd5d7e30d7aaa1bfc
src/components/Editor/index.vue
@@ -1,48 +1,59 @@
<template>
  <div>
    <el-upload
      :action="upload.url"
      :before-upload="handleBeforeUpload"
      :on-success="handleUploadSuccess"
      :on-error="handleUploadError"
      class="editor-img-uploader"
      name="file"
      :show-file-list="false"
      :headers="upload.headers"
      ref="uploadRef"
      v-if="type === 'url'"
    >
    </el-upload>
    <div class="editor">
      <quill-editor
        ref="quillEditorRef"
        v-model:content="content"
        contentType="html"
        @textChange="(e: any) => $emit('update:modelValue', content)"
        :options="options"
        :style="styles"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
import { QuillEditor, Quill } from '@vueup/vue-quill';
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import { getToken } from "@/utils/auth";
import { ComponentInternalInstance } from "vue";
import { propTypes } from '@/utils/propTypes';
import { globalHeaders } from "@/utils/request";
const props = defineProps({
  /* 编辑器的内容 */
  modelValue: {
    type: String,
  },
  modelValue: propTypes.string,
  /* 高度 */
  height: {
    type: Number,
    default: null,
  },
  height: propTypes.number.def(400),
  /* 最小高度 */
  minHeight: {
    type: Number,
    default: null,
  },
  minHeight: propTypes.number.def(400),
  /* 只读 */
  readOnly: {
    type: Boolean,
    default: false,
  },
  readOnly: propTypes.bool.def(false),
  /* 上传文件大小限制(MB) */
  fileSize: {
    type: Number,
    default: 5,
  },
  fileSize: propTypes.number.def(5),
  /* 类型(base64格式、url格式) */
  type: {
    type: String,
    default: "url",
  }
  type: propTypes.string.def('url')
});
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const upload = reactive<UploadOption>({
  headers: { Authorization: "Bearer " + getToken() },
  url: import.meta.env.VITE_APP_BASE_API + '/system/oss/upload'
  headers: globalHeaders(),
  url: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload'
})
const myQuillEditor = ref();
const quillEditorRef = ref();
const options = ref({
  theme: "snow",
@@ -54,7 +65,7 @@
      container: [
        ["bold", "italic", "underline", "strike"],       // 加粗 斜体 下划线 删除线
        ["blockquote", "code-block"],                    // 引用  代码块
        [{ list: "ordered" }, { list: "bullet"} ],       // 有序、无序列表
        [{ list: "ordered" }, { list: "bullet" }],       // 有序、无序列表
        [{ indent: "-1" }, { indent: "+1" }],            // 缩进
        [{ size: ["small", false, "large", "huge"] }],   // 字体大小
        [{ header: [1, 2, 3, 4, 5, 6, false] }],         // 标题
@@ -75,7 +86,7 @@
      },
    }
  },
  placeholder: '请输入内容',
  placeholder: "请输入内容",
  readOnly: props.readOnly,
});
@@ -99,10 +110,10 @@
// 图片上传成功返回图片地址
const handleUploadSuccess = (res: any) => {
  // 获取富文本实例
  let quill = toRaw(myQuillEditor.value).getQuill();
  // 如果上传成功
  if (res.code === 200) {
    // 获取富文本实例
    let quill = toRaw(quillEditorRef.value).getQuill();
    // 获取光标位置
    let length = quill.selection.savedRange.index;
    // 插入图片,res为服务器返回的图片链接地址
@@ -118,6 +129,13 @@
// 图片上传前拦截
const handleBeforeUpload = (file: any) => {
  const type = ["image/jpeg", "image/jpg", "image/png", "image/svg"];
  const isJPG = type.includes(file.type);
  //检验文件格式
  if (!isJPG) {
    proxy?.$modal.msgError(`图片格式错误!`);
    return false;
  }
  // 校检文件大小
  if (props.fileSize) {
    const isLt = file.size / 1024 / 1024 < props.fileSize;
@@ -137,36 +155,12 @@
}
</script>
<template>
   <div>
      <el-upload
         :action="upload.url"
         :before-upload="handleBeforeUpload"
         :on-success="handleUploadSuccess"
         :on-error="handleUploadError"
         class="editor-img-uploader"
         name="file"
         :show-file-list="false"
         :headers="upload.headers"
         style="display: none"
         v-if="type === 'url'"
      >
      </el-upload>
      <div class="editor">
         <quill-editor
            ref="myQuillEditor"
            v-model:content="content"
            contentType="html"
            @textChange="(e: any) => $emit('update:modelValue', content)"
            :options="options"
            :style="styles"
         />
      </div>
   </div>
</template>
<style>
.editor, .ql-toolbar {
.editor-img-uploader {
  display: none;
}
.editor,
.ql-toolbar {
  white-space: pre-wrap !important;
  line-height: normal !important;
}
@@ -181,11 +175,9 @@
  content: "保存";
  padding-right: 0;
}
.ql-snow .ql-tooltip[data-mode="video"]::before {
  content: "请输入视频地址:";
}
.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before {
  content: "14px";
@@ -202,7 +194,6 @@
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  content: "32px";
}
.ql-snow .ql-picker.ql-header .ql-picker-label::before,
.ql-snow .ql-picker.ql-header .ql-picker-item::before {
  content: "文本";
@@ -231,7 +222,6 @@
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  content: "标题6";
}
.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
  content: "标准字体";