曹睿
2 天以前 3b73c6d15fc65c777e63eb06cf128f7799120bc5
src/components/Editor/index.vue
@@ -27,7 +27,7 @@
</template>
<script setup>
import axios from 'axios';
import axios from "axios";
import { QuillEditor } from "@vueup/vue-quill";
import "@vueup/vue-quill/dist/vue-quill.snow.css";
import { getToken } from "@/utils/auth";
@@ -37,7 +37,7 @@
const quillEditorRef = ref();
const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload"); // 上传的图片服务器地址
const headers = ref({
  Authorization: "Bearer " + getToken()
  Authorization: "Bearer " + getToken(),
});
const props = defineProps({
@@ -69,7 +69,7 @@
  type: {
    type: String,
    default: "url",
  }
  },
});
const options = ref({
@@ -79,20 +79,20 @@
  modules: {
    // 工具栏配置
    toolbar: [
      ["bold", "italic", "underline", "strike"],      // 加粗 斜体 下划线 删除线
      ["blockquote", "code-block"],                   // 引用  代码块
      [{ list: "ordered" }, { list: "bullet" }],      // 有序、无序列表
      [{ indent: "-1" }, { indent: "+1" }],           // 缩进
      [{ size: ["small", false, "large", "huge"] }],  // 字体大小
      [{ header: [1, 2, 3, 4, 5, 6, false] }],        // 标题
      [{ color: [] }, { background: [] }],            // 字体颜色、字体背景颜色
      [{ align: [] }],                                // 对齐方式
      ["clean"],                                      // 清除文本格式
      ["link", "image", "video"]                      // 链接、图片、视频
      ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
      ["blockquote", "code-block"], // 引用  代码块
      [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
      [{ indent: "-1" }, { indent: "+1" }], // 缩进
      [{ size: ["small", false, "large", "huge"] }], // 字体大小
      [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
      [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
      [{ align: [] }], // 对齐方式
      ["clean"], // 清除文本格式
      ["link", "image", "video"], // 链接、图片、视频
    ],
  },
  placeholder: "请输入内容",
  readOnly: props.readOnly
  readOnly: props.readOnly,
});
const styles = computed(() => {
@@ -107,15 +107,19 @@
});
const content = ref("");
watch(() => props.modelValue, (v) => {
  if (v !== content.value) {
    content.value = v == undefined ? "<p></p>" : v;
  }
}, { immediate: true });
watch(
  () => props.modelValue,
  (v) => {
    if (v !== content.value) {
      content.value = v == undefined ? "<p></p>" : v;
    }
  },
  { immediate: true }
);
// 如果设置了上传地址则自定义图片上传事件
onMounted(() => {
  if (props.type == 'url') {
  if (props.type == "url") {
    let quill = quillEditorRef.value.getQuill();
    let toolbar = quill.getModule("toolbar");
    toolbar.addHandler("image", (value) => {
@@ -125,7 +129,7 @@
        quill.format("image", false);
      }
    });
    quill.root.addEventListener('paste', handlePasteCapture, true);
    quill.root.addEventListener("paste", handlePasteCapture, true);
  }
});
@@ -158,7 +162,11 @@
    // 获取光标位置
    let length = quill.selection.savedRange.index;
    // 插入图片,res.url为服务器返回的图片链接地址
    quill.insertEmbed(length, "image", import.meta.env.VITE_APP_BASE_API + res.fileName);
    quill.insertEmbed(
      length,
      "image",
      import.meta.env.VITE_APP_BASE_API + res.fileName
    );
    // 调整光标到最后
    quill.setSelection(length + 1);
  } else {
@@ -177,7 +185,7 @@
  if (clipboard && clipboard.items) {
    for (let i = 0; i < clipboard.items.length; i++) {
      const item = clipboard.items[i];
      if (item.type.indexOf('image') !== -1) {
      if (item.type.indexOf("image") !== -1) {
        e.preventDefault();
        const file = item.getAsFile();
        insertImage(file);
@@ -189,9 +197,16 @@
function insertImage(file) {
  const formData = new FormData();
  formData.append("file", file);
  axios.post(uploadUrl.value, formData, { headers: { "Content-Type": "multipart/form-data", Authorization: headers.value.Authorization } }).then(res => {
    handleUploadSuccess(res.data);
  })
  axios
    .post(uploadUrl.value, formData, {
      headers: {
        "Content-Type": "multipart/form-data",
        Authorization: headers.value.Authorization,
      },
    })
    .then((res) => {
      handleUploadSuccess(res.data);
    });
}
</script>
@@ -199,7 +214,8 @@
.editor-img-uploader {
  display: none;
}
.editor, .ql-toolbar {
.editor,
.ql-toolbar {
  white-space: pre-wrap !important;
  line-height: normal !important;
}