1.0.13 • Published 6 months ago

@satek-vn/react-editor v1.0.13

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

React Editor Component

npm license

A rich text editor component built with React and TypeScript, powered by Tiptap.

Installation

npm install @satek-vn/react-editor

Usage

import React, { useState } from "react";
import { Editor, EditorConfig } from "@satek-vn/react-editor";

const App = () => {
    const [content, setContent] = useState('');
    const config: EditorConfig = {
        placeholder: 'Enter content...',
        menubar: ['bold', 'italic', 'underline', 'strike', '', 'bullet-list', 'ordered-list', '', 'text-align', '', 'image', 'link', '', 'h1', 'h2', 'h3', '', 'mention'],
        uploadImage: async (files) => {
            // Example: Upload files and return URLs
            return Array.from(files).map(file => URL.createObjectURL(file));
        },
        popupLink: (previousUrl, submit) => {
            // Example: Open popup and submit url
            const url = window.prompt('URL', previousUrl);
            if (url === null) {
                return;
            }

            submit(url);
        },
        getSuggestion: (query) => {
            // Example: Return mention suggestions
            const list = [
                {id: 1, label: 'Lea Thompson'},
                {id: 2, label: 'Cyndi Lauper'},
                {id: 3, label: 'Tom Cruise'},
                {id: 4, label: 'Madonna'},
                {id: 5, label: 'Jerry Hall'},
                {id: 6, label: 'Joan Collins'},
                {id: 7, label: 'Winona Ryder'},
                {id: 8, label: 'Christina Applegate'},
                {id: 9, label: 'Alyssa Milano'},
                {id: 10, label: 'Molly Ringwald'},
                {id: 11, label: 'Ally Sheedy'},
                {id: 12, label: 'Debbie Harry'},
                {id: 13, label: 'Olivia Newton-John'},
                {id: 14, label: 'Elton John'},
                {id: 15, label: 'Michael J. Fox'},
                {id: 16, label: 'Axl Rose'},
                {id: 17, label: 'Emilio Estevez'},
                {id: 18, label: 'Ralph Macchio'},
                {id: 19, label: 'Rob Lowe'},
                {id: 20, label: 'Jennifer Grey'},
                {id: 21, label: 'Mickey Rourke'},
                {id: 22, label: 'John Cusack'},
                {id: 23, label: 'Matthew Broderick'},
                {id: 24, label: 'Justine Bateman'},
                {id: 25, label: 'Lisa Bonet'},
            ];

            return list.filter((item) => item.label.toLowerCase().startsWith(query.toLowerCase())).slice(0, 5);
        },
    };

    return (
        <div>
            <Editor config={config} value={content} onChange={setContent} />
        </div>
    );
};

export default App;

Screenshots

Editor screenshots

Features

The editor supports the following features:

  • Text Formatting: Bold, Italic, Underline, Strike-through.
  • Headings: H1, H2, H3, H4, H5, H6.
  • Lists: Bullet List, Ordered List.
  • Block Elements: Blockquote, Code Block.
  • Links: Add and edit hyperlinks.
  • Images: Upload and insert images.
  • Files: Upload and insert files.
  • Mentions: Mention users with suggestions.
  • Text Alignment: Align text (Left, Center, Right, Justify).
  • Placeholder: Customizable placeholder text.

Props

PropTypeRequiredDescription
configEditorConfigYesConfiguration object for the editor.
valuestringNoInitial content of the editor.
onChange(value) => voidNoCallback function triggered when the content changes.
onFocus(event) => voidNoCallback function triggered when the editor gains focus.
onBlur(event) => voidNoCallback function triggered when the editor loses focus.
classNamestringNoAdditional CSS class for the editor container.

EditorConfig

PropertyTypeRequiredDescription
menubarArray<MenuItem>YesList of menu items to display in the toolbar.
placeholderstringNoPlaceholder text displayed when the editor is empty.
classNamestringNoAdditional CSS class for the editor.
readOnlybooleanNoIf set to true, the editor switches to read-only mode, disabling content editing.
popupLink(previousUrl, submit) => voidNoCustom popup for adding/editing links.
uploadImage(files) => Promise<string[]>NoFunction to handle image uploads and return URLs.
uploadFile(files) => Promise<FileItem[]>NoFunction to handle file uploads and return URLs.
getSuggestion(query) => Array<SuggestionItem>NoFunction to provide mention suggestions based on the query.

MenuItem

NameDescription
boldApply bold formatting to the selected text. `
italicApply italic formatting to the selected text.
underlineApply underline formatting to the selected text.
strikeApply strikethrough formatting to the selected text.
linkAdd or edit a hyperlink.
imageUpload and insert an image.
fileUpload and insert an file.
blockquoteConvert the selected text into a blockquote.
code-blockInsert a block of code.
bullet-listCreate a bullet list.
ordered-listCreate a numbered list.
text-alignAlign text (left, center, right, justify).
mentionMention a user with suggestions.
h1Apply Heading 1 style.
h2Apply Heading 2 style.
h3Apply Heading 3 style.
h4Apply Heading 4 style.
h5Apply Heading 5 style.
h6Apply Heading 6 style.

Development

To run the component locally:

npm install
npm run dev

License

This project is licensed under the MIT License.