wangEditor基本使用

wangEditor上传图片基本使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
    /* 生产的html标签默认不带样式,需要自己自定义,如果想要默认样式可以参考官网,如下 */
    <link rel="stylesheet" href="https://www.wangeditor.com/demo/css/view.css"> 
</head>
    <style>
        #editor—wrapper {
        border: 1px solid #ccc;
        z-index: 100; /* 按需定义 */
        }
        #toolbar-container { border-bottom: 1px solid #ccc; }
        #editor-container { height: 500px; }
    </style>
<body>
	/* 回显html标签需要用此div包裹 */
	<div id="editor-content-view" class="editor-content-view">
	<blockquote>此标签在编辑器里为 “引用”样式,原生html不能解析,需自定义样式。</blockquote>
	</div>
    <div id="editor—wrapper">
        <div id="toolbar-container"><!-- 工具栏 --></div>
        <div id="editor-container"><!-- 编辑器 --></div>
    </div>

    <script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
    <script>

        const { createEditor, createToolbar } = window.wangEditor

        const editorConfig = {
            placeholder: '输入内容...',
            onChange(editor) {
            // 获取输入的文本转化html标签
            const html = editor.getHtml()
            },
            MENU_CONF: {
              uploadImage: {}
            }
        }  

        editorConfig.MENU_CONF['uploadImage'] = {
            fieldName: 'img',
            server: 'http://localhost/upload',
            base64LimitSize: 5 * 1024 * 1024, // 10M 以下插入 base64
            // 自定义插入图片
            async customInsert(res, insertFn) {
                // res 即服务端的返回结果
                console.log(res);
                const url = res.data.url
                // 从 res 中找到 url alt href ,然后插图图片
                insertFn(url)
          },
          // 自定义增加 http  header, 其他携带参数见官网
          headers: {
              token: 'eyJhbGciOiJIUzI1NiJ...'
          },
        }
		// 设置代码高亮
        editorConfig.MENU_CONF['codeSelectLang'] = {
        // 代码语言
        codeLangs: [
            { text: 'CSS', value: 'css' },
            { text: 'HTML', value: 'html' },
            { text: 'XML', value: 'xml' },
            { text: 'JAVA', value: 'java' },
            // 其他
        ]
    }

        const editor = createEditor({
            selector: '#editor-container',
            html: "",
            config: editorConfig,
            mode: 'default', // or 'simple'
        })

        const toolbarConfig = {}
        const toolbar = createToolbar({
            editor,
            selector: '#toolbar-container',
            config: toolbarConfig,
            mode: 'default', // or 'simple'
        })
    </script>
</body>
</html>

详情见官网文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值