If this helps you, please give us a star so you can be notified when new versions are released.
npm install @catmasks/free-editoror
pnpm add @catmasks/free-editorEditorPluginKey:
| Plugin Key | Name | Description |
|---|---|---|
heading |
Heading | Supports H1–H6 headings |
fontBold |
Bold | Toggle bold |
fontItalic |
Italic | Toggle italic |
fontColor |
Font Color | Set text color |
fontHighlight |
Highlight | Set text background highlight |
fontFamily |
Font Family | Set font family |
fontSize |
Font Size | Set font size |
link |
Link | Insert/edit/remove link |
codeBlock |
Code Block | Insert code block |
| image | Image | Insert image, supports drag‑and‑drop / paste upload |
| video | Video | Insert video, supports drag‑and‑drop / paste upload |
See Chapter 3 – i18n for details.
import { Editor } from "@catmasks/free-editor";
import "@catmasks/free-editor/style.css";
// Create the editor
const editor = new Editor(document.getElementById("editor"), {
content: "<p>Hello World</p>",
placeholder: "Please enter content...",
});Signature:
constructor(el: HTMLElement, options?: EditorOptions)Parameters:
| Parameter | Type | Description |
|---|---|---|
el |
HTMLElement |
DOM element to mount the editor on |
options |
EditorOptions |
Editor configuration (optional) |
content?: stringInitial HTML content of the editor.
locale?: LocaleInitial locale for the editor.
Default: "zh-CN"
Allowed values: "zh-CN" | "en" | "ja-JP"
theme?: EditorThemeEditor theme.
Default: "light"
Allowed values: "light" | "dark"
placeholder?: stringPlaceholder text shown when the editor is empty.
Default: "Please enter content..."
include?: EditorPluginKey[]Only include the specified plugins. If empty, all plugins are included.
Default: [] (all plugins)
exclude?: EditorPluginKey[]Exclude the specified plugins.
Default: [] (none excluded)
uploader?: MediaUploaderOptionsMedia upload configuration, supports separate settings for images, videos, and files.
See Chapter 2 for details.
Returns whether the editor is mounted.
console.log(editor.isMounted); // trueReturns whether the editor has been destroyed.
console.log(editor.isDestroyed); // falseReturns the current theme ("light" or "dark").
console.log(editor.theme); // "light"Returns true if dark mode is active.
console.log(editor.isDark); // falsesetTheme(theme: EditorTheme): voidSets the theme.
Parameters:
| Parameter | Type | Description |
|---|---|---|
theme |
EditorTheme |
Theme name: "light" or "dark" |
toggleTheme(): voidToggles between light and dark themes.
getHtml(): stringReturns the editor’s HTML content.
Returns: string – HTML string
Throws: Error: Editor has been destroyed if the editor is destroyed.
destroy(): voidDestroys the editor, cleaning up all resources and event listeners.
A collection of upload configurations, grouped by media type.
interface MediaUploaderOptions {
image?: MediaUploaderConfig;
video?: MediaUploaderConfig;
file?: MediaUploaderConfig;
}Properties:
| Property | Type | Description |
|---|---|---|
image |
MediaUploaderConfig |
Image upload config |
video |
MediaUploaderConfig |
Video upload config |
file |
MediaUploaderConfig |
File upload config |
Configuration for a single media type.
action?: stringUpload endpoint URL.
Default: undefined
method?: stringHTTP method.
Default: "POST"
headers?: HeadersInitRequest headers.
withCredentials?: booleanWhether to send credentials (cookies, etc.).
Default: false
fieldName?: stringForm field name.
Default: "file"
maxSize?: numberMaximum file size in bytes.
Default: Infinity
accept?: string[]
`
Accepted file MIME types.
#### `data`
```typescript
data?: Record<string, any> | (() => Record<string, any>)Additional form data.
format?: (result: any) => UploadResult | Promise<UploadResult>Format the response result.
Parameters:
result– The server response
Returns: UploadResult – Formatted upload result
Example:
{
format: (response) => ({
url: response.data.url,
name: response.data.filename,
}),
}upload?: (file: File, context: UploadContext) => Promise<UploadResult>Custom upload function. If provided, the default upload logic is replaced.
Parameters:
file– The file objectcontext– Upload context containingsignal,config, andonProgress
Returns: Promise<UploadResult> – Upload result
Example:
{
upload: async (file, context) => {
const formData = new FormData();
formData.append("file", file);
const res = await fetch("/api/upload", {
method: "POST",
body: formData,
signal: context.signal,
});
const data = await res.json();
context.onProgress?.({ percent: 100 });
return {
url: data.url,
name: data.name,
};
},
}beforeUpload?: (file: File) => File | false | Promise<File | false>Pre‑upload hook. Return false to cancel upload, or return a new File to replace the original.
Example:
{
beforeUpload: (file) => {
// Compress image and return
return compressImage(file);
},
}validate?: (file: File) => string | voidValidate the file. Return an error message string to indicate failure.
Example:
{
validate: (file) => {
if (file.size > 10 * 1024 * 1024) {
return "File cannot exceed 10MB";
}
},
}onProgress?: (progress: UploadProgress, file: File) => voidProgress callback.
onSuccess?: (result: UploadResult, file: File) => voidSuccess callback.
onUploadError?: (error: Error, file: File) => voidUpload error callback.
onTypeError?: (error: Error, file: File) => voidFile type error callback.
onSizeError?: (error: Error, file: File) => voidFile size error callback.
onValidateError?: (error: Error, file: File) => voidValidation error callback.
The current locale of the instance.
console.log(i18n.locale); // zh-CNt(key: string, ...args: any[]): stringReturns the translated text for key in the current locale.
Returns: string – Translated text, or the key itself if not found.
Parameters:
| Parameter | Type | Description |
|---|---|---|
key |
string |
Message key, supports dot‑path notation, e.g., "toolbar.bold" |
args |
any[] |
Optional arguments to replace placeholders {0}, {1}, … in the message |
Example:
i18n.t("toolbar.bold");setLocale(locale: Locale): voidSets the locale for the editor instance.
Parameters:
| Parameter | Type | Description |
|---|---|---|
locale |
Locale |
Target locale: "zh-CN", "en", or "ja-JP" |
extend(messages: DeepPartial<LocaleMessages>): voidExtends the current locale’s message object.
Parameters:
| Parameter | Type | Description |
|---|---|---|
messages |
DeepPartial<LocaleMessages> |
Messages to extend |
Note: This method must be called before the editor is initialized; otherwise it has no effect on the editor.
Example:
i18n.extend({
toolbar: { bold: "Custom Bold", italic: "Custom Italic", test: "Test Text" },
});subscribe(callback: (locale: Locale) => void): () => voidSubscribes to locale change events. Useful for using i18n outside the editor.
Parameters:
| Parameter | Type | Description |
|---|---|---|
callback |
(locale: Locale) => void |
Callback invoked when the locale changes |
Returns: An unsubscribe function.
Note: Call the returned unsubscribe function when destroying to prevent memory leaks.
Example:
let unsubscribeLocale: (() => void) | null = null;
unsubscribeLocale = i18n.subscribe(() => {
// Handle locale change...
});
// On destroy:
unsubscribeLocale?.();
unsubscribeLocale = null;