<textarea> + 處理器 (早期 BBS、論壇)
contentEditable / designMode (2003-2015 黃金期)
<iframe id="editor"></iframe>
<script>
const iframe = document.getElementById('editor');
const win = iframe.contentWindow;
const doc = win.document;
doc.open();
doc.write('<!DOCTYPE html><html><body></body></html>');
doc.close();
doc.designMode = 'on'; // 整個文件可編輯
// 取得內容
function getContent() { return doc.body.innerHTML; }
// 設定內容
function setContent(html) { doc.body.innerHTML = html; }
</script>
缺點:需處理跨框架通訊、樣式隔離、焦點管理、行動裝置虛擬鍵盤彈出問題。
<div id="editor" contenteditable="true"></div>
<script>
const editor = document.getElementById('editor');
// 監聽輸入
editor.addEventListener('input', e => {
// 這裡需自行處理:清理標籤、正規化、光標恢複
});
// 處理貼上
editor.addEventListener('paste', e => {
e.preventDefault();
const text = e.clipboardData.getData('text/plain');
document.execCommand('insertText', false, text); // 已棄用但仍通用
});
</script>
| 編輯器 | 模型類型 | 渲染層 | 協同支援 | 生態 |
|---|---|---|---|---|
| ProseMirror | 嚴格 Schema (Node/Mark) | DOM (可換) | Yjs / 自帶 | 成熟、文件完善 |
| Slate | Immutable.js / 純 JS 物件 | React | Yjs / Socket.io | React 生態最強 |
| TipTap | 基於 ProseMirror | Vue/React/Vanilla | Hocuspocus (Yjs) | 元件化、無頭模式 |
| Lexical | Lexical Node (可序列化) | React/Vanilla | Yjs / 自帶 | Meta 出品、效能導向 |
| BlockNote | Block-based (Notion 風) | React/Vanilla | Yjs | 區塊編輯器快速上手 |
script, iframe, style, on* 等高風險向量。
4. 上傳圖片/檔案:驗證 MIME、掃毒、重命名、存非執行目錄、CDN 域分離。
需要協同編輯? → Lexical + Yjs / TipTap + Hocuspocus 需要 Notion 風區塊? → BlockNote / Slate + 自定義區塊 React 專案、高度客製? → Slate / TipTap (React) Vue 專案? → TipTap (Vue) / Vue-Editor (Quill 封裝) 極簡、零依賴? → Pell (1kb) / 簡易 contentEditable 封裝 Markdown 為主? → Milkdown / Toast UI Editor 企業級、成熟穩定? → TinyMCE 6 / CKEditor 5 (商業授權)