feat: 🚀 文章分类添加图片
This commit is contained in:
@@ -108,7 +108,7 @@ const options = reactive({
|
||||
["blockquote", "code-block"], // 引用 代码块
|
||||
[{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
|
||||
[{ indent: "-1" }, { indent: "+1" }], // 缩进
|
||||
[{ size: ["small", false, "large", "huge"] }], // 字体大小
|
||||
[{ size: fontSizeStyle.whitelist }], // 字体大小
|
||||
[{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
|
||||
[{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
|
||||
[{ align: [] }], // 对齐方式
|
||||
|
||||
@@ -1,260 +0,0 @@
|
||||
<template>
|
||||
<el-upload
|
||||
:id="uuid"
|
||||
action="#"
|
||||
:multiple="true"
|
||||
:show-file-list="false"
|
||||
:http-request="() => {}"
|
||||
:before-upload="() => false"
|
||||
@change="handleFileChange"
|
||||
class="editor-img-uploader"
|
||||
accept=".jpeg,.jpg,.png,.gif"
|
||||
>
|
||||
<i ref="uploadRef" class="Plus editor-img-uploader"></i>
|
||||
</el-upload>
|
||||
<input
|
||||
type="file"
|
||||
accept="video/*"
|
||||
name="file"
|
||||
ref="uploadFileVideo"
|
||||
id="uploadFileVideo"
|
||||
@change="handleVideoUpload"
|
||||
style="width: 0; height: 0; cursor: pointer; opacity: 0"
|
||||
/>
|
||||
<div class="editor">
|
||||
<QuillEditor
|
||||
id="editorId"
|
||||
ref="myQuillEditor"
|
||||
v-model:content="editorContent"
|
||||
contentType="html"
|
||||
@update:content="onContentChange"
|
||||
:options="options"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup name="Editor">
|
||||
import { QuillEditor, Quill } from "@vueup/vue-quill";
|
||||
import "@vueup/vue-quill/dist/vue-quill.snow.css";
|
||||
import { getCurrentInstance, reactive, ref, toRaw, computed, onMounted } from "vue";
|
||||
import { generateUUID } from "@/utils";
|
||||
import { h } from "@/utils/url";
|
||||
import { routerObj } from "./utils.js";
|
||||
import { titleConfig } from "./titleConfig.js";
|
||||
import { uploadVideo, uploadImg } from "@/api/modules/upload";
|
||||
|
||||
// 字体配置
|
||||
let fontSizeStyle = Quill.import("attributors/style/size");
|
||||
fontSizeStyle.whitelist = ["12px", "14px", "16px", "18px", "20px", "22px", "24px", "26px", "28px", "30px", "32px"];
|
||||
Quill.register(fontSizeStyle, true);
|
||||
|
||||
// 自定义模块
|
||||
import ImageBlot from "./quill-image";
|
||||
import Video from "./quill-video";
|
||||
Quill.register(Video);
|
||||
Quill.register(ImageBlot);
|
||||
|
||||
// 路由相关
|
||||
const $router = useRouter();
|
||||
const routerValueName = $router.currentRoute.value.name;
|
||||
const routerName = ref(routerObj[routerValueName]);
|
||||
|
||||
// 核心:上传队列 + 临时去重集合
|
||||
const uploadQueue = ref([]);
|
||||
const isProcessing = ref(false);
|
||||
const uuid = ref(generateUUID());
|
||||
const tempProcessedFiles = ref(new Set());
|
||||
|
||||
// 编辑器基础
|
||||
const { proxy } = getCurrentInstance();
|
||||
const emit = defineEmits(["update:content", "handleRichTextContentChange"]);
|
||||
const uploadFileVideo = ref(null);
|
||||
const myQuillEditor = ref(null);
|
||||
const oldContent = ref("");
|
||||
|
||||
// Props
|
||||
const props = defineProps({
|
||||
content: { type: String, default: "" },
|
||||
readOnly: { type: Boolean, default: false },
|
||||
fileSizeLimit: { type: Number, default: 10 }
|
||||
});
|
||||
|
||||
// 内容双向绑定
|
||||
const editorContent = computed({
|
||||
get: () => props.content,
|
||||
set: val => emit("update:content", val)
|
||||
});
|
||||
|
||||
// 编辑器配置
|
||||
const options = reactive({
|
||||
theme: "snow",
|
||||
modules: {
|
||||
toolbar: {
|
||||
container: [
|
||||
["bold", "italic", "underline", "strike"],
|
||||
["blockquote", "code-block"],
|
||||
[{ list: "ordered" }, { list: "bullet" }],
|
||||
[{ indent: "-1" }, { indent: "+1" }],
|
||||
[{ size: fontSizeStyle.whitelist }],
|
||||
[{ header: [1, 2, 3, 4, 5, 6, false] }],
|
||||
[{ color: [] }, { background: [] }],
|
||||
[{ align: [] }],
|
||||
["clean"],
|
||||
["link", "image", "video"]
|
||||
],
|
||||
handlers: {
|
||||
image: () => proxy.$refs.uploadRef.click(),
|
||||
video: () => document.querySelector("#uploadFileVideo")?.click()
|
||||
}
|
||||
}
|
||||
},
|
||||
placeholder: "请输入内容...",
|
||||
readOnly: props.readOnly
|
||||
});
|
||||
|
||||
// 处理文件选择(关键修改:记录原始顺序)
|
||||
const handleFileChange = (file, fileList) => {
|
||||
if (!fileList.length) return;
|
||||
|
||||
const rawEditor = toRaw(myQuillEditor.value);
|
||||
const quill = rawEditor?.getQuill();
|
||||
if (!quill) return;
|
||||
|
||||
// 生成文件唯一标识
|
||||
const getFileKey = file => `${file.name}-${file.size}-${file.lastModified}`;
|
||||
|
||||
// 过滤重复文件
|
||||
const newFiles = fileList.filter(item => {
|
||||
const file = item.raw;
|
||||
const key = getFileKey(file);
|
||||
if (tempProcessedFiles.value.has(key)) {
|
||||
console.log(`跳过重复文件: ${file.name}`);
|
||||
return false;
|
||||
}
|
||||
if (!validateFile(file)) return false;
|
||||
tempProcessedFiles.value.add(key);
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!newFiles.length) return;
|
||||
|
||||
// 获取初始光标位置(所有图片将按顺序插入此位置之后)
|
||||
const baseCursorIndex = quill.selection.savedRange?.index || quill.getLength();
|
||||
|
||||
// 按用户选择顺序添加到队列(关键修改:使用原始索引)
|
||||
newFiles.forEach((item, index) => {
|
||||
uploadQueue.value.push({
|
||||
file: item.raw,
|
||||
originalIndex: index, // 记录用户选择的原始顺序
|
||||
baseCursorIndex // 所有图片共享同一个基础位置
|
||||
});
|
||||
});
|
||||
|
||||
if (!isProcessing.value) {
|
||||
processQueue();
|
||||
}
|
||||
};
|
||||
|
||||
// 文件校验
|
||||
const validateFile = file => {
|
||||
const isImage = file.type.startsWith("image/");
|
||||
if (!isImage) {
|
||||
alert("请上传图片文件");
|
||||
return false;
|
||||
}
|
||||
|
||||
const fileSizeMB = file.size / 1024 / 1024;
|
||||
if (fileSizeMB > props.fileSizeLimit) {
|
||||
alert(`文件 ${file.name} 大小超过 ${props.fileSizeLimit} MB`);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
// 处理上传队列(关键修改:按原始顺序插入)
|
||||
const processQueue = async () => {
|
||||
if (isProcessing.value || uploadQueue.value.length === 0) return;
|
||||
|
||||
isProcessing.value = true;
|
||||
const { file, originalIndex, baseCursorIndex } = uploadQueue.value[0];
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append("image", file);
|
||||
const result = await uploadImg(formData, routerName.value);
|
||||
|
||||
if (result?.code === 0) {
|
||||
const rawEditor = toRaw(myQuillEditor.value);
|
||||
const quill = rawEditor.getQuill();
|
||||
|
||||
// 关键修改:计算正确的插入位置(基于原始顺序和基础位置)
|
||||
const insertPosition = baseCursorIndex + originalIndex;
|
||||
|
||||
quill.insertEmbed(insertPosition, "image", {
|
||||
url: h + result.data.path,
|
||||
id: generateUUID()
|
||||
});
|
||||
|
||||
// 不更新光标位置,确保后续图片按原始顺序插入
|
||||
// quill.setSelection(insertPosition + 1);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`文件 ${file.name} 上传失败:`, error);
|
||||
} finally {
|
||||
uploadQueue.value.shift();
|
||||
if (uploadQueue.value.length === 0) {
|
||||
tempProcessedFiles.value.clear();
|
||||
}
|
||||
isProcessing.value = false;
|
||||
processQueue();
|
||||
}
|
||||
};
|
||||
|
||||
// 视频上传
|
||||
const handleVideoUpload = async evt => {
|
||||
if (evt.target.files.length === 0) return;
|
||||
|
||||
const file = evt.target.files[0];
|
||||
const formData = new FormData();
|
||||
formData.append("video", file);
|
||||
|
||||
try {
|
||||
const rawEditor = toRaw(myQuillEditor.value);
|
||||
const quill = rawEditor.getQuill();
|
||||
const cursorIndex = quill.selection.savedRange?.index || 0;
|
||||
|
||||
const { data } = await uploadVideo(formData);
|
||||
quill.insertEmbed(cursorIndex, "video", {
|
||||
url: h + data.path,
|
||||
id: generateUUID()
|
||||
});
|
||||
quill.setSelection(cursorIndex + 1);
|
||||
} catch (error) {
|
||||
console.error("视频上传失败:", error);
|
||||
} finally {
|
||||
evt.target.value = "";
|
||||
}
|
||||
};
|
||||
|
||||
// 内容变化监听
|
||||
const onContentChange = content => {
|
||||
emit("handleRichTextContentChange", content);
|
||||
};
|
||||
|
||||
// 初始化标题提示
|
||||
const initTitle = () => {
|
||||
for (const item of titleConfig.value) {
|
||||
const tip = document.querySelector(`.ql-toolbar ${item.Choice}`);
|
||||
if (tip) tip.setAttribute("title", item.title);
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
initTitle();
|
||||
oldContent.value = props.content;
|
||||
});
|
||||
</script>
|
||||
<style lang="scss">
|
||||
@import "./index.scss";
|
||||
</style>
|
||||
@@ -1,10 +0,0 @@
|
||||
//lineHeight.js
|
||||
import Quill from "quill";
|
||||
const Parchment = Quill.import("parchment");
|
||||
class lineHeightAttributor extends Parchment.Attributor.Style {}
|
||||
const lineHeightStyle = new lineHeightAttributor("lineHeight", "line-height", {
|
||||
scope: Parchment.Scope.INLINE,
|
||||
whitelist: ["1", "2", "3", "4", "5"]
|
||||
});
|
||||
|
||||
export { lineHeightStyle };
|
||||
@@ -1,124 +0,0 @@
|
||||
// 自定义字体大小模块,支持预设选项和输入框
|
||||
export class CustomFontSize {
|
||||
constructor(quill, options) {
|
||||
this.quill = quill;
|
||||
this.options = options;
|
||||
|
||||
// 创建工具栏容器
|
||||
const container = quill.getModule("toolbar").container;
|
||||
|
||||
// 创建字体大小选择器
|
||||
this.selector = document.createElement("select");
|
||||
this.selector.classList.add("ql-size");
|
||||
|
||||
// 添加预设选项
|
||||
options.presets.forEach(size => {
|
||||
if (size === false) {
|
||||
// 添加分隔线
|
||||
const option = document.createElement("option");
|
||||
option.disabled = true;
|
||||
option.innerHTML = "──────────";
|
||||
this.selector.appendChild(option);
|
||||
} else {
|
||||
// 添加预设大小选项
|
||||
const option = document.createElement("option");
|
||||
option.value = size;
|
||||
option.innerHTML = this.getSizeLabel(size);
|
||||
this.selector.appendChild(option);
|
||||
}
|
||||
});
|
||||
|
||||
// 添加自定义输入选项
|
||||
const inputOption = document.createElement("option");
|
||||
inputOption.value = "custom";
|
||||
inputOption.innerHTML = "自定义";
|
||||
this.selector.appendChild(inputOption);
|
||||
|
||||
// 创建自定义输入框
|
||||
this.input = document.createElement("input");
|
||||
this.input.type = "number";
|
||||
this.input.placeholder = "输入字号";
|
||||
this.input.min = 1;
|
||||
this.input.style.width = "50px";
|
||||
this.input.style.display = "none"; // 默认隐藏
|
||||
|
||||
// 将选择器和输入框添加到工具栏
|
||||
container.appendChild(this.selector);
|
||||
container.appendChild(this.input);
|
||||
|
||||
// 绑定事件
|
||||
this.selector.addEventListener("change", this.onSelectChange.bind(this));
|
||||
this.input.addEventListener("change", this.onInputChange.bind(this));
|
||||
this.quill.on("text-change", this.update.bind(this));
|
||||
|
||||
// 初始化
|
||||
this.update();
|
||||
}
|
||||
|
||||
// 获取大小标签
|
||||
getSizeLabel(size) {
|
||||
const labels = {
|
||||
small: "小",
|
||||
normal: "正常",
|
||||
large: "大",
|
||||
huge: "超大"
|
||||
};
|
||||
return labels[size] || size;
|
||||
}
|
||||
|
||||
// 选择器变更事件
|
||||
onSelectChange() {
|
||||
const value = this.selector.value;
|
||||
|
||||
if (value === "custom") {
|
||||
// 显示输入框
|
||||
this.input.style.display = "inline-block";
|
||||
this.input.focus();
|
||||
} else {
|
||||
// 隐藏输入框并应用选择的大小
|
||||
this.input.style.display = "none";
|
||||
this.quill.format("size", value);
|
||||
}
|
||||
}
|
||||
|
||||
// 输入框变更事件
|
||||
onInputChange() {
|
||||
const value = this.input.value;
|
||||
if (value) {
|
||||
this.quill.format("size", `${value}px`);
|
||||
}
|
||||
// 选择"自定义"选项,保持输入框显示
|
||||
this.selector.value = "custom";
|
||||
}
|
||||
|
||||
// 更新当前选中状态
|
||||
update() {
|
||||
const selection = this.quill.getSelection();
|
||||
if (selection) {
|
||||
const [format] = this.quill.getFormat(selection.index, 1);
|
||||
const size = format.size;
|
||||
|
||||
if (size && size.endsWith("px")) {
|
||||
const sizeValue = size.replace("px", "");
|
||||
this.input.value = sizeValue;
|
||||
|
||||
// 如果是预设值,选中对应的选项
|
||||
if (this.options.presets.includes(size)) {
|
||||
this.selector.value = size;
|
||||
this.input.style.display = "none";
|
||||
} else {
|
||||
// 否则选中"自定义"并显示输入框
|
||||
this.selector.value = "custom";
|
||||
this.input.style.display = "inline-block";
|
||||
}
|
||||
} else {
|
||||
// 没有选中的大小,重置为默认
|
||||
this.selector.value = "normal";
|
||||
this.input.style.display = "none";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// // 注册自定义模块
|
||||
// Quill.register("modules/customFontSize", CustomFontSize);
|
||||
Reference in New Issue
Block a user