Lightweight web-based rich text editor (WYSIWYG editor) built with JavaScript and CSS for modern browsers
npx @tessl/cli install tessl/npm-wangeditor@3.1.0wangEditor is a lightweight web-based rich text editor (WYSIWYG editor) built with JavaScript and CSS for modern browsers (IE10+). It provides comprehensive rich text editing capabilities with a clean and intuitive interface, supporting standard formatting operations, image insertion, link creation, and table management.
npm install wangeditor// ES6 / CommonJS
import wangEditor from 'wangeditor';
// or
const wangEditor = require('wangeditor');
// Browser (global)
const E = window.wangEditor;import wangEditor from 'wangeditor';
// Create editor instance
const editor = new wangEditor('#toolbar'); // toolbar container
// Optional: separate text container
// const editor = new wangEditor('#toolbar', '#text');
// Configure editor (optional)
editor.customConfig.onchange = function (html) {
console.log('Content changed:', html);
};
// Initialize editor
editor.create();
// Get/set content
const html = editor.txt.html();
editor.txt.html('<p>New content</p>');wangEditor is built around several key components:
customConfig objecteditor.txt)editor.cmd)editor.selection)editor.menus)editor.uploadImg)Core editor lifecycle management including instance creation, configuration, and initialization.
// Main constructor
function wangEditor(toolbarSelector, textSelector?): EditorInstance;
interface EditorInstance {
id: string;
customConfig: CustomConfig;
create(): void;
initSelection(newLine?: boolean): void;
}Text and HTML content manipulation API for getting, setting, and modifying editor content.
interface TextAPI {
html(val?: string): string | void;
text(val?: string): string | void;
append(html: string): void;
clear(): void;
getJSON(): Array<any>;
}Programmatic formatting and editing commands for bold, italic, colors, and other rich text operations.
interface CommandAPI {
do(name: string, value?: any): void;
queryCommandSupported(name: string): boolean;
}Text selection and cursor positioning APIs for programmatic text selection control.
interface SelectionAPI {
getRange(): Range | null;
saveRange(range?: Range): void;
restoreSelection(): void;
createRangeByElem(elem: Element, toStart: boolean, isCollapseToEnd: boolean): void;
getSelectionContainerElem(range?: Range): Element;
}Comprehensive configuration options for customizing editor behavior, appearance, and functionality.
interface CustomConfig {
menus?: string[];
fontNames?: string[];
colors?: string[];
emotions?: Array<any>;
zIndex?: number;
debug?: boolean;
onchange?: (html: string) => void;
onblur?: (html: string) => void;
onfocus?: () => void;
// ... extensive configuration options
}Programmatic access to toolbar menus for state management and custom interactions.
interface MenusAPI {
menus: {[menuName: string]: MenuInstance};
changeActive(): void;
}
interface MenuInstance {
type: 'click' | 'droplist' | 'panel';
$elem: Element;
tryChangeActive?(): void;
onClick?(event: Event): void;
}Image upload functionality with support for local uploads, network images, and various cloud storage services.
interface UploadImgAPI {
uploadImg(files: FileList | File[]): void;
}
interface UploadConfig {
uploadImgServer?: string;
uploadImgMaxSize?: number;
uploadImgMaxLength?: number;
uploadImgShowBase64?: boolean;
uploadImgHooks?: UploadHooks;
customUploadImg?: (files: FileList, insert: Function) => void;
// ... additional upload options
}// Get current content
const htmlContent = editor.txt.html();
const plainText = editor.txt.text();
const jsonContent = editor.txt.getJSON();
// Set new content
editor.txt.html('<p>Hello <strong>World</strong></p>');
editor.txt.append('<p>Additional content</p>');
editor.txt.clear(); // Clear all content// Apply formatting
editor.cmd.do('bold');
editor.cmd.do('fontSize', '18px');
editor.cmd.do('foreColor', '#ff0000');
editor.cmd.do('insertHTML', '<p>Custom HTML</p>');editor.customConfig.onchange = function(html) {
console.log('Content changed:', html);
// Save to backend, update preview, etc.
};
editor.customConfig.onblur = function(html) {
console.log('Editor lost focus');
};