1.1.9 • Published 2 years ago

letterpad-editor v1.1.9

Weekly downloads
44
License
ISC
Repository
-
Last release
2 years ago

Letterpad Editor

This editor is an extract from the Letterpad CMS.

This editor gives a high level api of the draft editor along with a flexible plugin architecture.

How to use

# using yarn
yarn add letterpad-editor

# using npm
npm install letterpad-editor

Now you can use this in your react project

import React from "react";
import LetterpadEditor from "letterpad-editor";

const MyEditor = () => {
  return (
    <LetterpadEditor
      theme="dark"
      html="Hello World"
      onChange={(html: string) => {
        console.log(html);
      }}
    />
  );
};

export default MyEditor;

Parameters

  • theme: string

    default: dark

    Set the theme. Options - light | dark

  • setHelpers: (props: Helpers) => void

    You may want to store these props in some state, so that you can access them anytime. See below:

    import { Helpers } from "letterpad-editor/dist/types";
    import LetterpadEditor from "letterpad-editor";
    
    const MyEditor = () => {
      const [helpers, setHelpers] = useState<Helpers>();
    
      const insertImage = () => {
        helpers.pluginHelpers.imagePlugin.insertImage({
          src: "https://example.com/image.jpg",
          caption: "caption",
          width: 300,
          height: 200,
          placeholderSrc: "https://example.com/1x1.jpg"
        });
      };
    
      return (
        <div>
          <LetterpadEditor
            theme="dark"
            html="Hello World"
            setHelpers={setHelpers}
            onChange={(html: string) => {
              console.log(html);
            }}
          />
          <button onClick={insertImage}>Insert Image</button>
        </div>
      );
    };
    // Helpers
    {
      pluginHelpers, // instance of each plugin by name
      getPlugins, // a function returning a list of all the plugins
      getProps, // a function returning a list of all the props pass into the Editor
      setEditorState, // a function to update the EditorState
      getEditorState, // a function to get the current EditorState
      getReadOnly, // a function returning of the Editor is set to readOnly
      setReadOnly, // a function which allows to set the Editor to readOnly
      getEditorRef, // a function to get the editor reference
    }
  • onImageClick: ((insert: TypeInsertImageFn) => void)

    insert is a callback function to insert the image in the editor.

      ...
      onImageClick={insert => {
        // display file explorer
        // on select image, get the urls and some additional info
        insert({
          src,
          caption,
          width,
          height,
          placeholderSrc
        })
      }}
      ...

    Scenario: If a user is uploading an image, you might want to show some sort of a loading placeholder in the editor. And then when the image has uploaded, you will have to replace the placeholder. You can do so in this way.

    import { Helpers } from "letterpad-editor/dist/types";
    import LetterpadEditor from "letterpad-editor";
    
    const MyEditor = () => {
    const [helpers, setHelpers] = useState<Helpers>();
    
    const insertImage = () => {
      const image = getImageFromUser(); // implement this method.
      const {insertImage, updateImageBlock} = helpers.pluginHelpers.imagePlugin;
    
      const key = insertImage({
        src: "https://example.com/placeholder.svg"
      });
    
      const uploadedSrc = await uploadImageToServer(image);
    
      updateImageBlock(key, {
        src: uploadedSrc
      });
    };
    
    return (
      <div>
        <LetterpadEditor
          theme="dark"
          onChange={(html: string) => {
            console.log(html);
          }}
          html="Hello World"
          setHelpers={setHelpers}
        />
        <button onClick={insertImage}>Insert Image</button>
      </div>
    );
    };
  • onVideoClick: ((insert: (url:string) => void) => void)

  • onChange(html: string): (html:string) => void

    Receive html whenever there is a change in the editor.

  • html - string

    Load the initial html. If you want empty page, the enter empty string. If its null, it will load sample data.

Development

If you would like to contribute then setup your dev environment this way.

comment: <> (You will find some documentation over here - https://app.gitbook.com/@letterpad/s/editor/)

git clone git@github.com:letterpad/editor.git
cd editor
yarn install
yarn dev
1.1.1

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.0

3 years ago

1.0.22

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.27

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.16

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

0.0.30

3 years ago

0.0.31

3 years ago

0.0.32

3 years ago

0.0.33

3 years ago

0.0.28

4 years ago

0.0.29

4 years ago

0.0.27

4 years ago

0.0.26

4 years ago

0.0.25

4 years ago

0.0.24

4 years ago

0.0.23

4 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago