API 參考
本頁介紹主入口 @powerduck/md-editor 與 ./react 子路徑的所有匯出,簽章直接依據原始碼整理。
MarkdownEditor
頂層編輯器類別,建構時傳入容器與選項。
class MarkdownEditor {
constructor(container: HTMLElement | string, options?: MarkdownEditorOptions);
getValue(): string;
setValue(value: string): void;
getHtml(): string;
renderNow(): void;
setAutoPreview(auto: boolean): void;
setMode(mode: EditorMode): void;
setTheme(theme: EditorTheme): void;
focus(): void;
destroy(): void;
}
constructor(container, options?)
container: HTMLElement | string— DOM 元素或 CSS 選擇器。找不到符合的元素時擲出MarkdownEditor: mount container not found。options?: MarkdownEditorOptions— 請參閱設定。
建立版面配置、建立 CodeEditor、綁定鍵盤快捷鍵,並在 complex 模式下建立工具列與狀態列。建構時執行初始渲染及狀態列更新。
getValue(): string
回傳目前的 Markdown 原始碼。
setValue(value: string): void
取代整份文件,取消待執行的防抖渲染,同步重新渲染預覽並更新狀態列。
getHtml(): string
透過內部的 Renderer 將目前原始碼渲染為 HTML 並回傳字串。
renderNow(): void
取消待執行的防抖渲染,立即強制重新渲染預覽,適用於 autoPreview 為 false 時。
setAutoPreview(auto: boolean): void
啟用或停用編輯時自動重新渲染預覽。啟用後立即渲染目前內容。
setMode(mode: EditorMode): void
在 'simple' 與 'complex' 之間切換,視需要銷毀並重建工具列及狀態列,同時切換行號。模式不變時不執行任何動作。
setTheme(theme: EditorTheme): void
設定根元素的 data-theme 並重新設定 CodeMirror 的語法突顯設定區,同時將工具列的主題切換圖示在太陽與月亮之間切換。
focus(): void
將焦點移至 CodeMirror 編輯器。
destroy(): void
取消待執行的渲染,銷毀程式碼編輯器、工具列、狀態列、目前彈出視窗與增量渲染器,清空容器並移除類別名稱及屬性。卸載編輯器時應呼叫此方法。
CodeEditor
對 CodeMirror 6 EditorView 的輕量封裝,供進階嵌入情境使用。大多數使用者應直接使用 MarkdownEditor。
class CodeEditor {
readonly view: EditorView;
constructor(container: HTMLElement, options: CodeEditorOptions);
setKeymap(bindings: Array<{ key: string; run: (view: EditorView) => boolean }>): void;
getValue(): string;
setValue(value: string): void;
setLineNumbers(enabled: boolean): void;
setTheme(theme: EditorTheme): void;
insertAtCursor(text: string): void;
wrapSelection(prefix: string, suffix: string, placeholder: string): void;
getSelection(): string;
wrapLines(prefix: string, placeholder: string): void;
focus(): void;
insertMention(): void;
insertDocLink(): void;
toggleTaskList(): void;
destroy(): void;
}
CodeEditorOptions(未從主入口重新匯出)接受 value、onChange、lineNumbers、placeholder、onImageFile、mention、docLink 與 theme。
主要方法:
setKeymap(bindings)— 以指定的 CodeMirror 按鍵綁定取代自訂鍵位設定區。insertAtCursor(text)— 在主要選取範圍插入文字,並將游標移至插入內容之後。wrapSelection(prefix, suffix, placeholder)— 包住目前選取範圍;沒有選取文字時插入prefix + placeholder + suffix,並選取預留文字。wrapLines(prefix, placeholder)— 為每個選取行(或目前行)加上prefix前綴。toggleTaskList()— 切換目前行或選取行的- [ ]/- [x]標記。
Renderer 與 renderMarkdown
class Renderer
class Renderer {
readonly md: MarkdownIt;
constructor(options?: RendererOptions);
render(source: string): string;
hydrate(container: HTMLElement): Promise<void>;
}
constructor(options?)— 建立啟用linkify: true與typographer: true的 markdown-it 實例,依條件啟用數學公式、心智圖、程式碼突顯及提示區塊,並一律註冊工作清單、文件連結、提及及影片外掛。render(source)— 同步將原始碼渲染為 HTML 字串。hydrate(container)— HTML 插入 DOM 後,將心智圖預留位置啟用為 SVG 圖形。停用心智圖時不執行動作;錯誤會被忽略。
function renderMarkdown
function renderMarkdown(source: string, options?: RendererOptions): string;
建立暫時的 Renderer 並渲染原始碼。設定 theme 時會為結果加上具有主題的容器;Renderer.render 本身不 會加上這層容器。適合在不掛載編輯器的情況下渲染 Markdown。
interface RendererOptions
interface RendererOptions {
math?: boolean; // default true
mindmap?: boolean; // default true
codeHighlight?: boolean; // default true
tips?: boolean; // default true
html?: boolean; // default false
breaks?: boolean; // default false
theme?: 'light' | 'dark'; // wrapper added by renderMarkdown only
}
Toolbar
class Toolbar {
readonly el: HTMLDivElement;
constructor(actions: readonly ToolbarAction[]);
setActive(name: string, active: boolean): void;
setIcon(name: string, icon: string): void;
destroy(): void;
}
interface ToolbarAction
interface ToolbarAction {
name: string;
title: string;
icon: string;
handler: (button: HTMLButtonElement) => void;
groupEnd?: boolean;
toggle?: boolean;
active?: boolean;
}
按鈕使用 mousedown + preventDefault,避免處理函式執行前編輯器失去焦點並收合選取範圍。