vue3使用UEditor并引入135编辑器

  百度了很久,看过很多篇文章,但是有很多都是用了没效果的,自己搞了半天才弄好。

  用来记录,防止自己忘记。以后用可以过来翻。
135的按钮,点击之后可以弹出一个135编辑器的页面.png

在弹窗里面修改了之后,点击完成编辑,可以将修改的内容同步到UEditor.png

话不多话,开始。


首先要先安装npm 安装 vue-ueditor-wrap 这个组件,官方文档

在这个文档里面有百度ueditor的下载链接,直接下载

随便下,我下的utf8的,只有后端语言部分的不同,前端代码是一样的.png

下载完之后,将文件解压,放到自己项目的public里面。

这里还得在下载135编辑器提供的官方文件,135的地址

这两个链接.png

将135editor.js文件和下载的html文件更名为135EditorDialogPage.html。将135editor.js放在UEditor的根目录,
在dialogs文件夹下新增135editor文件夹,然后将135EditorDialogPage.html放进去。


项目里的文件结构.png

然后在ueditor.config.js中toolbars项里增加一个135editor菜单项。


135editor.png

到这里准备工作就结束了,下面开始写组件。


<vue-ueditor-wrap
    v-model="msg"
    :config="myConfig"
    @ready="ready"
    @beforeInit="addCustomButtom"
    :editor-dependencies="editorDependencies"
></vue-ueditor-wrap>
import { VueUeditorWrap } from "vue-ueditor-wrap";

const myConfig: object = reactive({
  UEDITOR_HOME_URL: "/lib/UEditor/", // 这里的路径要和你UEditor在public文件夹里的一样,不然会报错
  enableAutoSave: false, // 禁用自动保存
  autoFloatEnabled: false, // 关闭菜单悬浮
  initialFrameHeight: 500, // 高度
  autoHeightEnabled: false  // 关闭自动撑高
})

// 引入的js文件,这里是一定要写的,不然135的按钮出不来,之前我就是卡在这里。
const editorDependencies: string[] = reactive(
["ueditor.config.js", "ueditor.all.min.js", "lang/zh-cn/zh-cn.js", "135editor.js"]
);

// 再把css样式放到这里来
<style lang="scss">
  .edui-button.edui-for-135editor .edui-button-wrap .edui-button-body .edui-icon {
    background-image: url("http://static.135editor.com/img/icons/editor-135-icon.png") !important;
    background-size: 85%;
    background-position: center;
    background-repeat: no-repeat;
  }
</style>

到这里就解决啦。我文件上传用的是自定义的按钮,所以就没配置serverUrl。如果不是自己写的话,还是要配置一下的。

// 文件全部内容  组件的配置我没有写成活得,因为我项目里用不到。
// 里面用了自定义上传视频图片,用不到的可以不写 addCustomButtom 和 el-dialog 里面的东西

// -------------子组件------------------
<template>
    <div>
        <vue-ueditor-wrap
            v-model="msg"
            :config="myConfig"
            @ready="ready"
            @beforeInit="addCustomButtom"
            :editor-dependencies="editorDependencies"
        ></vue-ueditor-wrap>
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-upload
                class="upload-demo"
                drag
                action=""
                :http-request="handleHttpUpload"
                :accept="accept"
                :before-upload="beforeUpload"
                :show-file-list="false"
            >
                <el-icon class="el-icon--upload">
                    <upload-filled />
                </el-icon>
                <div class="el-upload__text">拖拽文件到此处 或者 <em>点击上传</em></div>
            </el-upload>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" name="UEditor">
import { computed, reactive, ref, shallowRef } from "vue";
import { VueUeditorWrap } from "vue-ueditor-wrap";
import { ElMessage, UploadRequestOptions } from "element-plus";
import { uploadImg, uploadVideo } from "@/api/modules/upload";

let accept = ref("");
let title = ref("");
let myConfig: object = reactive({
    UEDITOR_HOME_URL: "/lib/UEditor/",
    toolbars: [
        [
            "fullscreen",
            "source",
            "|",
            "undo",
            "redo",
            "|",
            "bold",
            "italic",
            "underline",
            "fontborder",
            "strikethrough",
            "superscript",
            "subscript",
            "removeformat",
            "formatmatch",
            "autotypeset",
            "blockquote",
            "pasteplain",
            "|",
            "forecolor",
            "backcolor",
            "insertorderedlist",
            "insertunorderedlist",
            "selectall",
            "cleardoc",
            "|",
            "rowspacingtop",
            "rowspacingbottom",
            "lineheight",
            "|",
            "customisable",
            "paragraph",
            "fontfamily",
            "fontsize",
            "|",
            "directionalityltr",
            "directionalityrtl",
            "indent",
            "|",
            "justifyleft",
            "justifycenter",
            "justifyright",
            "justifyjustify",
            "|",
            "touppercase",
            "tolowercase",
            "|",
            "imagenone",
            "imageleft",
            "imageright",
            "imagecenter",
            "|",
            "uploadImg",
            "uploadVideo",
            "horizontal",
            "|",
            "inserttable",
            "deletetable",
            "insertparagraphbeforetable",
            "insertrow",
            "deleterow",
            "insertcol",
            "deletecol",
            "mergecells",
            "mergeright",
            "mergedown",
            "splittocells",
            "splittorows",
            "splittocols",
            "charts",
            "|",
            "135editor"
        ]
    ],
    enableAutoSave: false, // 禁用自动保存
    autoFloatEnabled: false, // 关闭菜单悬浮
    initialFrameHeight: 500,
    autoHeightEnabled: false
});
let dialogVisible = ref(false);
let props = defineProps(["html"]);
const myUEditor = shallowRef();
type EmitProps = {
    (e: "update:html", val: string): void;
    (e: "check-validate"): void;
};
const emit = defineEmits<EmitProps>();
const msg = computed({
    get() {
        return props.html;
    },
    set(val: string) {
        // 防止富文本内容为空时,校验失败
        if (!myUEditor.value.getContent()) val = "";
        emit("update:html", val);
    }
});
const ready = (editorInstance: any) => {
    myUEditor.value = editorInstance;
};
// 自定义上传按钮
const addCustomButtom = () => {
    window.UE.registerUI(
        "uploadVideo",
        function(editor: any, uiName: any) {
            // 注册按钮执行时的 command 命令,使用命令默认就会带有回退操作
            editor.registerCommand(uiName, {
                execCommand: function() {
                    editor.execCommand("inserthtml", ``);
                }
            });
            // 参考http://ueditor.baidu.com/doc/#COMMAND.LIST
            let btn = new window.UE.ui.Button({
                name: "uploadVideo",
                title: "上传视频",
                cssRules: `background-position: -320px -20px;`,
                onclick: function() {
                    // 渲染dialog
                    title.value = "上传视频";
                    accept.value = "video/*";
                    dialogVisible.value = true;
                    editor.execCommand(uiName);
                }
            });
            return btn;
        },
        51 /* 指定添加到工具栏上的那个位置,默认时追加到最后 */
    );
    window.UE.registerUI(
        "uploadImg",
        function(editor: any, uiName: any) {
            // 注册按钮执行时的 command 命令,使用命令默认就会带有回退操作
            editor.registerCommand(uiName, {
                execCommand: function() {
                    editor.execCommand("inserthtml", ``);
                }
            });
            // 参考http://ueditor.baidu.com/doc/#COMMAND.LIST
            let btn = new window.UE.ui.Button({
                name: "uploadImg",
                title: "上传图片",
                cssRules: `background-position: -380px 0px;`,
                onclick: function() {
                    // 渲染dialog
                    title.value = "上传图片";
                    accept.value = "image/*";
                    dialogVisible.value = true;
                    editor.execCommand(uiName);
                }
            });
            return btn;
        },
        51 /* 指定添加到工具栏上的那个位置,默认时追加到最后 */
    );
};
// 引入的js文件
let editorDependencies: string[] = reactive(["ueditor.config.js", "ueditor.all.min.js", "lang/zh-cn/zh-cn.js", "135editor.js"]);
// 文件上传
const handleHttpUpload = async (options: UploadRequestOptions) => {
    let { file } = options;
    try {
        let ossRes;
        if (title.value === "上传图片") {
            ossRes = await uploadImg(file.name, file, "image/jpeg");
            myUEditor.value.execCommand("insertHtml", `<img src="${ossRes.url}" style="max-width: 100%">`);
        } else {
            ossRes = await uploadImg(file.name, file, "video/mp4");
            console.log(ossRes, "ossRes");
            myUEditor.value.execCommand(
                "insertHtml",
                "<video src=\"" +
                ossRes.url +
                "\" class=\"playsinline\" controls=\"true\" preload=\"auto\"  style=\"object-fit:fill;width:100%;max-width:360px;\"></video>"
            );
        }
        dialogVisible.value = false;
    } catch (error) {
        console.log(error);
    }
};
// 上传之前
const beforeUpload = (file: any) => {
    if (title.value === "上传图片") {
        if (file.type.indexOf("image") === -1) {
            ElMessage({
                message: "请上传图片类型的文件",
                type: "warning"
            });
            return false;
        }
    } else {
        if (file.type.indexOf("video") === -1) {
            ElMessage({
                message: "请上传视频类型的文件",
                type: "warning"
            });
            return false;
        }
    }
};
</script>

<style lang="scss">
.edui-button.edui-for-135editor .edui-button-wrap .edui-button-body .edui-icon {
    background-image: url("http://static.135editor.com/img/icons/editor-135-icon.png") !important;
    background-size: 85%;
    background-position: center;
    background-repeat: no-repeat;
}

.edui-editor {
    width: 100% !important;
}

#edui1_iframeholder {
    width: 100% !important;
}

.edui-combox-body,
.edui-button-wrap {
    line-height: normal;
}
</style>


// ----------父组件使用-------------
<UEditor v-model:html="drawerData.content"></UEditor>


好兄弟,如果觉得有用的话点个赞呗😭

©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,828评论 6 526
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,669评论 3 412
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,467评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,238评论 1 306
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,039评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,561评论 1 319
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,658评论 3 433
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,806评论 0 285
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,316评论 1 329
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,293评论 3 353
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,443评论 1 364
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,998评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,698评论 3 342
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,097评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,333评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,012评论 3 385
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,434评论 2 370

推荐阅读更多精彩内容