npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

nam-rich-text-editor

v9.1.7

Published

A custom rich text editor similar to Word, with HTML content

Readme

Rich Text Editor

A custom rich text editor similar to Word, built with JavaScript and TypeScript. The content is stored as HTML.

Installation

npm install rich-text-editor

Usage

As a Class

import { RichTextEditor } from "rich-text-editor";

const editor = new RichTextEditor();
const editorElement = editor.getElement();
document.body.appendChild(editorElement);

// Get HTML content
const html = editor.getHTML();

// Set HTML content
editor.setHTML("<p>Hello <strong>world</strong>!</p>");

As a React Component

import React from "react";
import { RichTextEditorComponent } from "rich-text-editor";

const MyEditor = () => {
  const handleChange = (html) => {
    console.log("HTML content:", html);
  };

  return (
    <RichTextEditorComponent
      onChange={handleChange}
      initialValue="<p>Start editing...</p>"
    />
  );
};

Features

  • Text Formatting: Bold, Italic, Underline, Strikethrough
  • Lists: Bullet and numbered lists
  • Alignment: Left, Center, Right, Justify
  • Headings: H1, H2, H3, H4
  • Media: Insert images and videos (including YouTube embeds)
  • Links: Create hyperlinks
  • Quotes: Insert formatted blockquotes
  • Colors: Text and background colors
  • Fonts: Multiple font families and sizes
  • Undo/Redo: Full history support
  • Fullscreen Mode: Focus writing mode
  • HTML Content: Content is stored as HTML

Multilingual Support

The editor supports managing content for multiple languages. Here's how to preserve content when switching between language tabs:

Vanilla JavaScript Example

import { RichTextEditor } from "rich-text-editor";

// Create storage for different languages
const contentStorage = {
  vi: "",
  en: "",
  zh: "",
};
let currentLanguage = "vi";

const editor = new RichTextEditor();
document.body.appendChild(editor.getElement());

// Function to switch language
function switchLanguage(newLang) {
  // Save current content before switching
  contentStorage[currentLanguage] = editor.getHTML();

  // Update current language
  currentLanguage = newLang;

  // Load content for new language
  editor.setHTML(contentStorage[newLang] || "");
}

// Auto-save on input (optional)
const editorDiv = editor.getElement().querySelector(".editor");
editorDiv.addEventListener("input", () => {
  contentStorage[currentLanguage] = editor.getHTML();
});

// Save to localStorage
function saveAll() {
  contentStorage[currentLanguage] = editor.getHTML();
  localStorage.setItem("content-vi", contentStorage.vi);
  localStorage.setItem("content-en", contentStorage.en);
  localStorage.setItem("content-zh", contentStorage.zh);
}

// Load from localStorage
function loadAll() {
  contentStorage.vi = localStorage.getItem("content-vi") || "";
  contentStorage.en = localStorage.getItem("content-en") || "";
  contentStorage.zh = localStorage.getItem("content-zh") || "";
  editor.setHTML(contentStorage[currentLanguage]);
}

React Example

import React, { useState, useEffect, useRef } from "react";
import { RichTextEditor } from "rich-text-editor";

const MultilingualEditor = () => {
  const [currentLang, setCurrentLang] = useState("vi");
  const [content, setContent] = useState({
    vi: "<p>Nội dung tiếng Việt...</p>",
    en: "<p>English content...</p>",
    zh: "<p>中文内容...</p>",
  });

  const editorRef = useRef(null);
  const containerRef = useRef(null);

  useEffect(() => {
    if (!editorRef.current) {
      editorRef.current = new RichTextEditor();
      containerRef.current.appendChild(editorRef.current.getElement());
      editorRef.current.setHTML(content[currentLang]);
    }
  }, []);

  const switchLanguage = (newLang) => {
    // Save current content
    const currentContent = editorRef.current.getHTML();
    setContent((prev) => ({ ...prev, [currentLang]: currentContent }));

    // Switch language
    setCurrentLang(newLang);

    // Load new content
    setTimeout(() => {
      editorRef.current.setHTML(content[newLang] || "");
    }, 0);
  };

  return (
    <div>
      <div>
        <button onClick={() => switchLanguage("vi")}>🇻🇳 Tiếng Việt</button>
        <button onClick={() => switchLanguage("en")}>🇬🇧 English</button>
        <button onClick={() => switchLanguage("zh")}>🇨🇳 中文</button>
      </div>
      <div ref={containerRef} />
    </div>
  );
};

Important Notes for Multilingual Content

  1. Always save before switching: Call editor.getHTML() to save the current content before switching languages
  2. Images and Videos: All media (images, videos) are preserved in the HTML content
  3. Storage Options:
    • Memory: Store in JavaScript object (lost on page refresh)
    • localStorage: Persist data in browser (survives page refresh)
    • Backend: Send to server for permanent storage
  4. Auto-save: Use input event listener for automatic saving
  5. State Management: In React, use useState or state management libraries (Redux, Zustand, etc.)

For complete examples, see:

  • /examples/multilingual-editor.html - Vanilla JavaScript example with full UI
  • /examples/react-multilingual-example.tsx - React component with custom hook

API Reference

RichTextEditor Class

Constructor

new RichTextEditor();

Creates a new rich text editor instance.

Methods

getElement(): HTMLElement

Returns the container element of the editor.

const editor = new RichTextEditor();
const element = editor.getElement();
document.body.appendChild(element);
getHTML(): string

Returns the current HTML content of the editor.

const htmlContent = editor.getHTML();
console.log(htmlContent);
setHTML(html: string): void

Sets the HTML content of the editor.

editor.setHTML("<h1>Hello World</h1><p>This is a paragraph.</p>");

RichTextEditorComponent (React)

Props

| Prop | Type | Default | Description | | -------------- | ------------------------ | ----------- | ----------------------------------- | | initialValue | string | '' | Initial HTML content | | onChange | (html: string) => void | undefined | Callback fired when content changes | | className | string | undefined | CSS class for the container | | style | React.CSSProperties | undefined | Inline styles for the container |

Usage

import { RichTextEditorComponent } from "rich-text-editor";

function MyComponent() {
  const [content, setContent] = useState("");

  return (
    <RichTextEditorComponent
      initialValue="<p>Start typing...</p>"
      onChange={(html) => setContent(html)}
      className="my-editor"
    />
  );
}

Development

npm run dev  # Start development server
npm run build  # Build for production