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>

内容概要:本文围绕“面向高精度电流控制的PMSM多参数PSO辨识模型研究”,系统阐述了基于Simulink的永磁同步电机(PMSM)多参数辨识方法,重点采用粒子群优化算法(PSO)实现对电机关键内部参数的高精度辨识。研究构建了完整的PMSM控制系统仿真模型,结合智能优化算法,解决了传统参数辨识中精度低、收敛慢的问题,有效提升了电流环控制的动态性能与稳态精度。该方法特别适用于对控制精度和响应速度要求较高的工业应用场景,如高性能伺服系统、电动汽车驱动系统等,具有较强的工程实用价值和科研参考意义。文中提供了完整的Simulink仿真模型与配套代码,确保了研究内容的可复现性和实践操作性。; 适合人群:具备电机控制、自动化或电气工程等相关专业背景,熟悉MATLAB/Simulink仿真环境,从事电机驱动系统研发、控制算法设计或相关领域科研工作的工程师及研究生,尤其适合工作1-5年、希望深入理解先进参数辨识技术的研发人员。; 使用场景及目标:①开展高精度PMSM控制系统的设计与参数辨识研究;②学习并掌握PSO等智能优化算法在电机系统参数辨识中的具体实现与调优技巧;③完成学术论文复现、科研项目验证、毕业设计或工程原型开发,提升对现代电机控制核心技术的理解与应用能力。; 阅读建议:建议读者结合提供的Simulink模型与源代码进行动手实践,按照文档逻辑逐步搭建与调试仿真系统,重点关注PSO算法与电机模型的交互机制、目标函数设计及参数收敛过程,通过对比不同工况下的辨识结果,深入理解算法性能与控制精度之间的内在联系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值