1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>TinyMCE Demo</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #f0f2f5; min-height: 100vh; padding: 40px 20px; } .container { max-width: 960px; margin: 0 auto; background: #fff; border-radius: 12px; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08); overflow: hidden; } .header { padding: 24px 32px; border-bottom: 1px solid #eee; } .header h1 { font-size: 22px; color: #1a1a1a; margin-bottom: 6px; } .header p { font-size: 14px; color: #999; } .actions { display: flex; gap: 12px; padding: 12px 32px; border-bottom: 1px solid #eee; flex-wrap: wrap; } .btn { padding: 8px 20px; border: none; border-radius: 6px; font-size: 14px; cursor: pointer; transition: all 0.2s; font-weight: 500; } .btn-primary { background: #1677ff; color: #fff; } .btn-primary:hover { background: #4096ff; } .btn-outline { background: #fff; color: #333; border: 1px solid #d9d9d9; } .btn-outline:hover { border-color: #1677ff; color: #1677ff; }
.main-content { display: flex; min-height: 500px; } .editor-panel { flex: 1; min-width: 0; } .output-panel { width: 380px; border-left: 1px solid #eee; display: flex; flex-direction: column; max-height: 600px; } .output-tabs { display: flex; border-bottom: 1px solid #eee; } .output-tab { flex: 1; padding: 10px 16px; text-align: center; font-size: 13px; cursor: pointer; background: #fafafa; border: none; color: #666; transition: all 0.2s; } .output-tab.active { background: #fff; color: #1677ff; border-bottom: 2px solid #1677ff; font-weight: 500; } .output-body { flex: 1; overflow-y: auto; padding: 20px; } .output-body .preview-content { line-height: 1.8; color: #333; } .output-body .preview-content img, .output-body .preview-content video { max-width: 100%; height: auto; border-radius: 6px; } .output-body .preview-content blockquote { border-left: 4px solid #1677ff; padding: 8px 16px; margin: 12px 0; background: #f6f8fa; color: #666; } .output-body .preview-content table { border-collapse: collapse; width: 100%; margin: 12px 0; } .output-body .preview-content th, .output-body .preview-content td { border: 1px solid #d9d9d9; padding: 8px 12px; text-align: left; } .output-body .preview-content th { background: #fafafa; font-weight: 600; } .output-body .preview-content a { color: #1677ff; } .source-code { background: #1e1e1e; color: #d4d4d4; padding: 16px; border-radius: 8px; font-family: "SF Mono", "Fira Code", monospace; font-size: 13px; line-height: 1.6; white-space: pre-wrap; word-break: break-all; }
@media (max-width: 800px) { .main-content { flex-direction: column; } .output-panel { width: 100%; border-left: none; border-top: 1px solid #eee; } } </style> </head> <body> <div class="container"> <div class="header"> <h1>TinyMCE Demo</h1> <p>企业级富文本编辑器 — 插件体系 + 自定义格式 + 实时预览</p> </div> <div class="actions"> <button class="btn btn-primary" id="btn-refresh">刷新预览</button> <button class="btn btn-outline" id="btn-load-sample">加载示例内容</button> <button class="btn btn-outline" id="btn-get-text">获取纯文本</button> <button class="btn btn-outline" id="btn-clear">清空</button> </div> <div class="main-content"> <div class="editor-panel"> <textarea id="tinymce-editor"></textarea> </div> <div class="output-panel"> <div class="output-tabs"> <button class="output-tab active" data-tab="preview">预览</button> <button class="output-tab" data-tab="source">源码</button> </div> <div class="output-body"> <div class="preview-content" id="output-preview"></div> <pre class="source-code" id="output-source" style="display:none;"></pre> </div> </div> </div> </div>
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
<script> const sampleContent = ` <h2 style="text-align: center;">欢迎使用 TinyMCE</h2> <p>TinyMCE 是全球最流行的<strong>富文本编辑器</strong>之一,提供丰富的插件生态和灵活的 API。</p>
<h3>📊 表格功能</h3> <table style="border-collapse: collapse; width: 100%;"> <thead> <tr><th>插件</th><th>功能</th><th>说明</th></tr> </thead> <tbody> <tr><td>advlist</td><td>高级列表</td><td>支持有序/无序列表样式切换</td></tr> <tr><td>table</td><td>表格编辑</td><td>支持增删行列、单元格合并</td></tr> <tr><td>codesample</td><td>代码高亮</td><td>基于 Prism.js 的语法高亮</td></tr> <tr><td>media</td><td>媒体嵌入</td><td>支持视频、音频嵌入</td></tr> </tbody> </table>
<h3>🎨 格式与样式</h3> <p>支持<strong>加粗</strong>、<em>斜体</em>、<span style="text-decoration: underline;">下划线</span>、<span style="text-decoration: line-through;">删除线</span>、 <span style="color: #ff4d4f; font-weight: bold;">红色强调</span>、 <span style="background-color: #fff7e6; padding: 2px 6px; border-radius: 3px;">高亮标记</span>等格式。</p>
<h3>💬 引用块</h3> <blockquote><p>TinyMCE 的 setup 钩子提供了强大的扩展能力,可以在编辑器初始化、内容变化等生命周期节点执行自定义逻辑。</p></blockquote>
<h3>🔗 超链接</h3> <p>插入链接:<a href="https://www.tiny.cloud" target="_blank" rel="noopener">TinyMCE 官方网站</a></p>
<h3>📹 视频嵌入</h3> <div style="max-width: 640px;"> <video width="640" height="360" controls="controls" style="width: 640px; height: 360px;"> <source src="https://www.w3schools.com/html/mov_bbb.mp4"> </video> </div>
<h3>💻 代码高亮</h3> <pre class="language-javascript" style="background: #1e1e1e; color: #d4d4d4; padding: 16px; border-radius: 8px;"> function useRichEditor() { const editor = tinymce.get('editor') const html = editor.getContent() console.log('编辑器内容:', html) }</pre>
<p style="text-align: center; color: #999; margin-top: 32px;"> —— 你可以自由编辑以上内容 —— </p> `;
tinymce.init({ selector: "#tinymce-editor", height: 500, language: "zh_CN", menubar: false, branding: false, statusbar: true, resize: true,
content_style: ` body { font-family: -apple-system, sans-serif; font-size: 15px; line-height: 1.8; color: #333; padding: 16px; } img { max-width: 100%; height: auto; border-radius: 6px; } video { max-width: 100%; height: auto; border-radius: 6px; } blockquote { border-left: 4px solid #1677ff; padding: 8px 16px; margin: 12px 0; background: #f6f8fa; color: #666; } table { border-collapse: collapse; width: 100%; margin: 12px 0; } th, td { border: 1px solid #d9d9d9; padding: 8px 12px; text-align: left; } th { background: #fafafa; font-weight: 600; } a { color: #1677ff; } pre { background: #1e1e1e; color: #d4d4d4; padding: 16px; border-radius: 8px; overflow-x: auto; } `,
plugins: [ "advlist autolink lists link image charmap", "searchreplace visualblocks code fullscreen", "media table codesample preview wordcount", ].join(" "),
toolbar: "undo redo | styles | " + "bold italic underline strikethrough forecolor backcolor | " + "alignleft aligncenter alignright alignjustify | " + "bullist numlist outdent indent | " + "link image media table codesample | " + "code preview fullscreen",
style_formats: [ { title: "正文", block: "p" }, { title: "一级标题", block: "h1" }, { title: "二级标题", block: "h2" }, { title: "三级标题", block: "h3" }, { title: "引用块", block: "blockquote" }, { title: "红色强调", inline: "span", styles: { color: "#ff4d4f", "font-weight": "bold" }, }, { title: "高亮标记", inline: "span", styles: { background: "#fff7e6", padding: "2px 6px", "border-radius": "3px" }, }, ],
table_default_attributes: { border: "1" }, table_default_styles: { "border-collapse": "collapse", width: "100%" },
image_dimensions: false, image_class_list: [ { title: "自适应", value: "img-responsive" }, { title: "圆角", value: "img-rounded" }, ],
media_live_embeds: true, media_dimensions: false,
paste_data_images: false, paste_retain_style_properties: "color font-size font-weight text-decoration",
codesample_languages: [ { text: "JavaScript", value: "javascript" }, { text: "HTML/XML", value: "markup" }, { text: "CSS", value: "css" }, { text: "Python", value: "python" }, { text: "SQL", value: "sql" }, { text: "Bash", value: "bash" }, ],
setup: editor => { editor.on("init", () => { editor.setContent(sampleContent); updatePreview(editor.getContent()); updateSource(editor.getContent()); });
editor.on("change keyup", () => { const html = editor.getContent(); updatePreview(html); updateSource(html); }); }, });
function updatePreview(html) { document.getElementById("output-preview").innerHTML = html; } function updateSource(html) { document.getElementById("output-source").textContent = html; }
document.getElementById("btn-refresh").addEventListener("click", () => { const html = tinymce.activeEditor.getContent(); updatePreview(html); updateSource(html); });
document.getElementById("btn-load-sample").addEventListener("click", () => { tinymce.activeEditor.setContent(sampleContent); });
document.getElementById("btn-get-text").addEventListener("click", () => { const text = tinymce.activeEditor.getContent({ format: "text" }); alert("纯文本内容:\n\n" + text); });
document.getElementById("btn-clear").addEventListener("click", () => { if (confirm("确定要清空编辑器内容吗?")) { tinymce.activeEditor.setContent(""); updatePreview(""); updateSource(""); } });
document.querySelectorAll(".output-tab").forEach(tab => { tab.addEventListener("click", () => { document.querySelectorAll(".output-tab").forEach(t => t.classList.remove("active")); tab.classList.add("active"); const target = tab.dataset.tab; document.getElementById("output-preview").style.display = target === "preview" ? "" : "none"; document.getElementById("output-source").style.display = target === "source" ? "" : "none"; }); }); </script> </body> </html>
|