// 简单版本的论坛编辑器,确保图片插入功能正常 const { createApp, ref, computed, onMounted, nextTick, onUnmounted } = Vue; import { headTop } from "../component/head-top/head-top.js"; const { createEditor, createToolbar } = window.wangEditor; console.log("createEditor", createEditor); const editApp = createApp({ setup() { let titleLength = ref(200); let uniqid = ref(""); onMounted(() => { const params = getUrlParams(); uniqid.value = params.uniqid || ""; getUserInfoWin(); checkWConfig(); cUpload(); // 添加selectionchange事件监听,当鼠标选中区域内容时更新lastSelection document.addEventListener("selectionchange", handleSelectionChange); }); // 组件卸载时移除事件监听 onUnmounted(() => { document.removeEventListener("selectionchange", handleSelectionChange); }); let imageLength = 10; let videoLength = 5; const checkWConfig = () => { const wConfig = JSON.parse(localStorage.getItem("wConfig")) || {}; console.log("wConfig", wConfig); if (wConfig.time) { const time = new Date(wConfig.time); const now = new Date(); if (now - time > 24 * 60 * 60 * 1000) getWConfig(); else { const config = wConfig.config || {}; titleLength.value = config.max_topic_title_length; imageLength = config.topic_image_count || 0; videoLength = config.topic_video_count || 0; } } else { getWConfig(); } }; const getWConfig = () => { ajaxGet("/v2/api/config/website").then((res) => { if (res.code == 200) { let data = res["data"] || {}; const config = data.config || {}; titleLength.value = config.max_topic_title_length; imageLength = config.topic_image_count || 0; videoLength = config.topic_video_count || 0; data.time = new Date().toISOString(); localStorage.setItem("wConfig", JSON.stringify(data)); } }); }; let isLogin = ref(false); let realname = ref(0); // 是否已经实名 let userInfoWin = ref({}); let permissions = ref([]); const getUserInfoWin = () => { const checkUser = () => { const user = window.userInfoWin; if (!user) return; document.removeEventListener("getUser", checkUser); realname.value = user.realname; userInfoWin.value = user; if (user?.uin > 0 || user?.uid > 0) isLogin.value = true; permissions.value = user?.authority || []; }; document.addEventListener("getUser", checkUser); }; const openAttest = () => { const handleAttestClose = () => { document.removeEventListener("closeAttest", handleAttestClose); realname.value = window.userInfoWin?.realname || 0; }; // 启动认证流程时添加监听 document.addEventListener("closeAttest", handleAttestClose); loadAttest(2); }; // 跳转登录 const goLogin = () => { if (typeof window === "undefined") return; if (window["userInfoWin"] && Object.keys(window["userInfoWin"]).length !== 0) { if (window["userInfoWin"]["uid"]) isLogin.value = true; else ajax_login(); } else ajax_login(); }; let uConfigData = {}; const cUpload = () => { ajaxGet(`/v2/api/config/upload?type=topic`).then((res) => { const data = res.data; uConfigData = data; console.log("uConfigData", uConfigData); init(); }); }; let info = ref({}); let tagList = ref([]); let token = ref(""); let infoImages = []; const init = () => { ajax("/v2/api/forum/postPublishInit", { uniqid: uniqid.value, }) .then((res) => { const data = res.data; if (res.code != 200) { creationAlertBox("error", res.message || "操作失败"); return; } const infoTarget = data.info || {}; if (infoTarget.content) infoTarget.content = restoreHtml(infoTarget.content, infoTarget.attachments); info.value = infoTarget; token.value = data.token; // nextTick(() => { // judgeIsEmpty(); // }); initEditor(); }) .catch((err) => { console.log("err", err); }); }; let editor = null; const initEditor = () => { let infoTarget = info.value || {}; console.log("infoTarget", infoTarget); const editorConfig = { placeholder: "Type here...", enabledMenus: [], MENU_CONF: { ["emotion"]: { emotions: optionEmoji.value, }, ["uploadImage"]: { server: uConfigData.url, // form-data fieldName ,默认值 'wangeditor-uploaded-image' fieldName: uConfigData.requestName, // 单个文件的最大体积限制,默认为 2M maxFileSize: maxSize, // 1M // 最多可上传几个文件,默认为 100 maxNumberOfFiles: imageLength, // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 [] allowedFileTypes: ["image/*", ".png", ".jpg", ".jpeg"], // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。 meta: { ...uConfigData.params }, // 将 meta 拼接到 url 参数中,默认 false metaWithUrl: false, // 自定义增加 http header headers: { accept: "application/json, text/plain, */*", ...uConfigData.headers }, // 跨域是否传递 cookie ,默认为 false withCredentials: true, // 超时时间,默认为 10 秒 async customUpload(file, insertFn) { try { let config = uConfigData; // 1. 构造 FormData(包含你的接口所需字段) const formData = new FormData(); formData.append(config.requestName, file); // 文件数据 formData.append("name", file.name); // 文件名 formData.append("type", "image"); // 文件名 formData.append("data", config.params.data); // 文件名 ajax(config.url, formData).then((res) => { const data = res.data; insertFn(data.url); // 传入图片的可访问 URL }); } catch (err) { console.error("上传出错:", err); } }, }, }, // 4. 链接菜单:显式启用(默认启用,补充配置防止被过滤) link: { disabled: false, // 确保不禁用 showTarget: true, // 显示「是否新窗口打开」选项 showRel: true, // 显示「rel 属性」选项 }, // 5. 对齐菜单:显式启用(默认启用,兜底配置) justify: { disabled: false, }, onChange(editor) { const html = editor.getHtml(); console.log("editor content", html); // 也可以同步到