Vdit 实现所见即所得(WYSIWYG) 模式的 Markdown 编辑器

一个完整的 HTML 案例,使用 **Vditor** 实现**所见即所得(WYSIWYG)** 模式的 Markdown 编辑器。

复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vditor 所见即所得编辑器示例</title>
    
    <!-- 1. 引入 Vditor 的 CSS 样式 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vditor@3.10.5/dist/index.css" />
    
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f7f9;
        }
        h2 { color: #333; }
        
        /* 按钮样式 */
        .btn-group {
            margin-top: 20px;
            display: flex;
            gap: 10px;
        }
        button {
            padding: 8px 16px;
            border: 1px solid #d1d5db;
            border-radius: 4px;
            background-color: #fff;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.2s;
        }
        button:hover {
            background-color: #f3f4f6;
            border-color: #9ca3af;
        }
        button.primary {
            background-color: #3b82f6;
            color: white;
            border-color: #3b82f6;
        }
        button.primary:hover {
            background-color: #2563eb;
        }

        /* 输出结果展示区 */
        #output-container {
            margin-top: 20px;
        }
        #output {
            margin-top: 10px;
            padding: 15px;
            border: 1px dashed #cbd5e1;
            border-radius: 6px;
            background-color: #ffffff;
            min-height: 100px;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: monospace;
            font-size: 13px;
            color: #475569;
        }
    </style>
</head>
<body>

    <h2>📝 Vditor 所见即所得 (WYSIWYG) 编辑器</h2>
    
    <!-- 2. 编辑器挂载的 DOM 容器 -->
    <div id="vditor"></div>

    <!-- 3. 操作按钮 -->
    <div class="btn-group">
        <button class="primary" onclick="getMarkdown()">获取 Markdown 源码</button>
        <button onclick="getHTML()">获取渲染后 HTML</button>
        <button onclick="setContent()">动态设置内容</button>
        <button onclick="clearContent()">清空内容</button>
    </div>

    <!-- 4. 结果展示区 -->
    <div id="output-container">
        <h3>📤 输出结果:</h3>
        <div id="output">点击上方按钮查看结果...</div>
    </div>

    <!-- 5. 引入 Vditor 的 JS 核心文件 -->
    <script src="https://cdn.jsdelivr.net/npm/vditor@3.10.5/dist/index.min.js"></script>
    
    <script>
        // 6. 初始化 Vditor 实例
        const vditor = new Vditor('vditor', {
            // 核心配置:指定为所见即所得模式
            mode: 'wysiwyg', 
            
            // 编辑器高度
            height: 450,
            
            // 自定义工具栏 (可根据需要增删)
            toolbar: [
                'headings', 'bold', 'italic', 'strike', '|',
                'line', 'quote', 'list', 'ordered-list', 'check', '|',
                'code', 'inline-code', 'table', 'link', 'upload', '|',
                'undo', 'redo', '|',
                'edit-mode', 'both', 'preview', 'outline', 'export', 'devtools'
            ],
            
            // 开启本地缓存,防止意外刷新丢失内容
            cache: {
                enable: true,
                id: 'vditor-wysiwyg-demo-cache'
            },
            
            // 占位符
            placeholder: '请输入 Markdown 内容,支持所见即所得编辑...',
            
            // 编辑器初始化完成后的回调函数
            after: () => {
                console.log('Vditor 初始化完成!');
                // 可以在这里设置初始默认内容
                // vditor.setValue('## 欢迎使用 Vditor\n\n这是一个**所见即所得**的 Markdown 编辑器。');
            },
            
            // 图片上传配置 (示例:本地预览,实际项目中需配置服务器接口)
            upload: {
                url: '/api/upload', // 替换为你的实际上传接口
                max: 10 * 1024 * 1024, // 最大 10MB
                accept: 'image/*',
                // 如果不想配置后端,可以使用本地 base64 预览
                // linkToImgUrl: '/api/fetch-image' 
            }
        });

        // ================= 交互逻辑 =================

        // 获取 Markdown 源码
        function getMarkdown() {
            const mdContent = vditor.getValue();
            document.getElementById('output').innerText = mdContent || '(编辑器为空)';
        }

        // 获取渲染后的 HTML
        function getHTML() {
            const htmlContent = vditor.getHTML();
            document.getElementById('output').innerText = htmlContent || '(编辑器为空)';
        }

        // 动态设置内容
        function setContent() {
            const newContent = `## 动态注入的内容\n\n这是通过 JavaScript 动态设置的内容。\n\n- 支持列表\n- **支持加粗**\n- \`支持代码\`\n\n> 这是一个引用块\n\n\`\`\`javascript\nconsole.log("Hello Vditor!");\n\`\`\``;
            vditor.setValue(newContent);
            document.getElementById('output').innerText = '✅ 内容已成功设置到编辑器中!';
        }

        // 清空内容
        function clearContent() {
            vditor.setValue('');
            document.getElementById('output').innerText = '🗑️ 内容已清空。';
        }
    </script>
</body>
</html>
相关推荐
LabVIEW开发19 小时前
LabVIEW图标编辑器文字模糊现象
ui·编辑器·labview·labview知识·labview功能·labview程序
on_pluto_2 天前
【debug】vscode 和 codex 不兼容问题
ide·人工智能·vscode·编辑器
Web - Anonymous2 天前
Vue3 + Element Plus 集成 JSON Editor 实现实时校验 JSON 编辑器(高亮/暗亮主题/分栏回显/组件复用) - 附完整示例
vue.js·编辑器·json
迷路爸爸1803 天前
cursor资源管理器修改为与vscode一样的纵向布局
ide·vscode·编辑器
TO_ZRG3 天前
Unity 编辑器之 EditorTool、EditorToolBar、Overlay属性
编辑器
星羽XingYu3 天前
esp32 vscode clangd 无法索引库函数解决方案
ide·vscode·编辑器
北漂燕郊杨哥3 天前
DotMD:一款基于 Go + Wails 的轻量开源 Markdown 桌面编辑器
golang·开源·编辑器·markdown·md
末代iOS程序员华仔3 天前
Cursor 编辑器 + Figma‑MCP 服务器(真正让 Codex 直接读取 Figma 链接,高级方案)
服务器·编辑器·figma
平头某3 天前
DSH:Agent 的 VSCode 时刻?
ide·vscode·编辑器
Dontla3 天前
VSCode打开关闭控制台面板快捷键ctrl+`(terminal快捷键、vscode控制台快捷键)
ide·vscode·编辑器