Editor.js Drag and Drop Plugin

repository·main·Indexed 42 days ago

https://github.com/kommitters/editorjs-drag-drop

A plugin for Editor.js version 1.1.16 that provides drag-and-drop reordering capabilities for blocks. Users can move blocks by dragging the toolbar settings button. It supports custom drop indicator border styles and integration with React, react-editor-js, and the editorjs-undo plugin.

Tokens
1.5K
Snippets
7
Records
8
Agent score
3%

What's inside editorjs-drag-drop

  1. Install the EditorJS Drag/Drop plugin

    main

    You can install the plugin via NPM or load it directly from a CDN.

    Via NPM

    Install the package as a dev dependency:

    $ npm i --save-dev editorjs-drag-drop

    Then import it in your application:

    import DragDrop from "editorjs-drag-drop";

    Via CDN

    Include the script tag in your HTML page containing Editor.js:

    <script src="https://cdn.jsdelivr.net/npm/editorjs-drag-drop"></script>
  2. Initialize Drag/Drop in Editor.js

    main

    To enable drag and drop functionality, initialize the DragDrop plugin within the onReady callback of your Editor.js configuration.

    To use the plugin, select a block and drag the toolbar settings button to the desired position.

    Customizing the indicator border

    You can pass a second string parameter to the DragDrop constructor to define the CSS border style of the drop indicator. If no parameter is provided, it defaults to 1px dashed #aaa.

    // Default usage
    const editor = new EditorJS({
      onReady: () => {
        new DragDrop(editor);
      },
    });
    
    // Custom border style
    const editor = new EditorJS({
      onReady: () => {
        new DragDrop(editor, "2px solid #fff");
      },
    });
  3. Integrate Drag/Drop with editorjs-undo

    main

    If you are using the editorjs-undo plugin, ensure both plugins are initialized within the onReady callback. The order of initialization typically follows the order of plugin requirements.

    const editor = new EditorJS({
      onReady: () => {
        new Undo({ editor });
        new DragDrop(editor);
      },
    });
  4. Use Drag/Drop with react-editor-js

    main

    When using the react-editor-js library, use the onInitialize prop to capture the editor instance via a ref, and then use the onReady prop to initialize the DragDrop plugin using the internal _editorJS instance.

    export const ReactEditor = () => {
      const editorCore = React.useRef(null)
    
      const handleInitialize = React.useCallback((instance) => {
        editorCore.current = instance
      }, [])
    
      const handleReady = () => {
        const editor = editorCore.current._editorJS;
        new Undo({ editor })
        new DragDrop(editor);
      };
    
      const ReactEditorJS = createReactEditorJS()
      return(
      <ReactEditorJS
        onInitialize={handleInitialize}
        onReady = {handleReady}
        tools={....}
        defaultValue={....}
      />
      )
    }
  5. Use Drag/Drop with React

    main

    In a React environment, create a handler function to store the DragDrop instance. This function should be passed to the onReady prop of your Editor.js component.

    Note: If using editorjs-undo, your handleReady function must initialize both plugins.

    const handleReady = (editor) => {
      new DragDrop(editor);
    };
    
    class ReactEditor extends Component {
      render() {
        return (
          <EditorJs
            onReady = { handleReady }
            tools = { ... }
          />
        )
      }
    }
  6. Configure the DragDrop border style

    main

    When instantiating the DragDrop class, you can pass a second argument to define the CSS border style used to indicate where a block will be dropped. This style is applied to the .ce-block__content element of the target block.

    Default value: '1px dashed #aaa'

    // Example with a custom border style
    new DragDrop({
      configuration: { holder: 'editor' },
      blocks: editor.blocks,
      toolbar: editor.toolbar,
      save: editor.save
    }, '2px solid #000');
  7. Initialize the DragDrop plugin

    main

    The DragDrop class is the main entrypoint for enabling drag-and-drop functionality in Editor.js. It must be instantiated with an object containing the Editor.js API methods and a configuration object.

    To use it, pass an object with the following keys:

    • configuration: An object containing holder (the ID of the editor container or the DOM element itself) and readOnly (boolean).
    • blocks: The Editor.js blocks API object (used to access getCurrentBlockIndex, getBlocksCount, and move).
    • toolbar: The Editor.js toolbar API object (used to close the toolbar during drag).
    • save: The Editor.js save method.

    You can optionally provide a borderStyle string to customize the visual indicator used during dragging (defaults to '1px dashed #aaa').

    import DragDrop from 'editorjs-drag-drop';
    
    const dragDrop = new DragDrop({
      configuration: {
        holder: 'editorjs-container',
        readOnly: false
      },
      blocks: editorInstance.blocks,
      toolbar: editorInstance.toolbar,
      save: editorInstance.save
    }, '2px solid blue');