Detalhes do pacote

editorjs-undo

kommitters101.3kMIT2.0.28

Undo tool for Editor.js

undo, redo, editor.js, editorjs

readme (leia-me)

EditorJS Undo Plugin

Stability Badge Coverage Status OpenSSF Best Practices OpenSSF Scorecard

Undo/Redo feature for Editor.js.

Installation

Install via NPM

2.x.x (beta)

This new version introduces breaking changes. Remember that it is still in beta version.

Please, report bugs or behavior issues :).

What's new?

  • Overall plugin performance is improved, especially with large documents.
  • Undo/Redo lifecycle is managed through block updates instead of a full document render.
  • Solves issues in documents with several images, for example the blink with undo/redo.
  • Sets the caret in the respective position when the user is typing inside an existing text.
  • Adds support to ReadOnly toggle.
  • Optimizes the observer, saving with the EditorJS API only when the content changes.

Get the package

$ npm i --save-dev editorjs-undo

1.x.x (stable version)

To install the latest v1 stable version

Get the package

$ npm i --save-dev editorjs-undo@1.0.1

Include module in your application

import Undo from 'editorjs-undo';

Load from CDN

You can load a specific version of the package from jsDelivr CDN.

Require this script on a page with Editor.js.

<script src="https://cdn.jsdelivr.net/npm/editorjs-undo"></script>

Usage

const editor = new EditorJS({
  onReady: () => {
    new Undo({ editor });
  },
});

On the editor, use <kbd>Ctrl</kbd> + <kbd>Z</kbd> or <kbd>⌘</kbd> + <kbd>Z</kbd> to undo, or use <kbd>Ctrl</kbd> + <kbd>Y</kbd> or <kbd>⌘</kbd> + <kbd>Y</kbd> to redo.

Usage with React

If you are using React, you could create a function to handle the onReady property, the function will store the Undo instance. Then, you must call the function in onReady in the editorJS instance.

const handleReady = (editor) => {
  new Undo({ editor });
};

class ReactEditor extends Component {
  render() {
    return (
      <EditorJs
        onReady={ handleReady }
        tools={ ... }
      />
    )
  }
}

Usage with react-editor-js.

If you are using react-editor-js, you could create a function to handle the onReady property, the function will store the undo instance and the respective configuration or initialize method if you want to use them (they will be explained below). Then, you must call the function in onReady in the editorJS instance.

React class components:


class ReactEditor extends Component {
  constructor(props) {
    super(props);
    this.editorCore = React.createRef();
  }

  handleInitialize = (instance) => {
    this.editorCore.current = instance;
  };

  handleReady = () => {
    const editor = this.editorCore.current._editorJS;
    new Undo({ editor });
  };

  render() {
    return (
      <ReactEditorJS
        onInitialize={ this.handleInitialize }
        onReady={ this.handleReady }
        tools={ ... }
      />
    )
  }
}

Note: If you are already using editorjs-drag-drop your handleReady function must have the editorjs-drag-drop instance.

handleReady = () => {
  const editor = this.editorCore.current._editorJS;
  new Undo({ editor });
  new DragDrop(editor);
};

React functional components:

........
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={ ... }
  />
  )
}

Initialize the plugin with data

Note: If you have loaded EditorJS with any initial data (such as some saved content), you must pass in an initialData object. If you don't, the default initial undo state lead to an empty editor.

You may use the initialize method inside the editor's onReady callback.

const editor = new EditorJS({
  onReady: () => {
    const undo = new Undo({ editor });
    undo.initialize(initialData);
  },
});

Add a custom shortcut to undo and redo

Note: If you do not add any shortcut, the default shortcuts will be set up.

If you want to add custom shortcuts, pass a config object with a shortcut key in the undo instance, the shortcuts must be called undo and redo.

const config = {
  shortcuts: {
    undo: 'CMD+X',
    redo: 'CMD+ALT+C'
  }
}
const editor = new EditorJS({
  onReady: () => {
    const undo = new Undo({ editor, config });
  },
});

You can set each shortcut with two or three keys, the available special keys are: CMD, ALT, SHIFT. CMD will be set up automatically as <kbd>Ctrl</kbd> or <kbd>⌘</kbd> depending on your OS.

Provide a custom debounce time for the Observer

Internally, editorjs-undo uses an Observer instance that watches for changes in the text being entered. By default, it waits for a pause of 200ms in text entry before saving a change. If you want to have editorjs-undo save changes more or less often, pass a config object with a debounceTimer key in the undo instance and provide a different value in milliseconds.

const config = {
  debounceTimer: 100
};
const editor = new EditorJS({
  onReady: () => {
    const undo = new Undo({ editor, config });
  },
});

Lowering the debounceTimer value below 200ms will make editorjs-undo save changes more often. Raising the debounceTimer value above 200ms will make editorjs-undo save changes less often.

Setting the debounceTimer value to 0 makes editorjs-undo save every character entered as an individual change, which makes undo and redo operations only remove/replace one character at a time.

Available Options

Field Type Description
editor EditorJS Required. The EditorJS instance.
maxLength Number Max amount of changes recorded by the history stack.
onUpdate() function Callback called when the user performs an undo or redo action.
config object Set up the configuration to editorjs-undo like the shortcuts

Development

Development mode

$ yarn build:dev

Production release

  1. Create a production bundle
$ yarn build
  1. Commit dist/bundle.js

Run tests

$ yarn test

Code of conduct

We welcome everyone to contribute. Make sure you have read the CODE_OF_CONDUCT before.

Contributing

For information on how to contribute, please refer to our CONTRIBUTING guide.

Changelog

Features and bug fixes are listed in the CHANGELOG file.

License

This library is licensed under an MIT license. See LICENSE for details.

Acknowledgements

Made with 💙 by kommitters Open Source

changelog (log de mudanças)

Changelog

2.0.28 (25.04.2024)

  • Add stale issues policy. See PR #250

2.0.27 (01.04.2024)

  • Fix bug #243 - Fix unexpected behavior when there are initial data loaded.
  • Fix bug #243 - Fix unexpected behavior when there are several copied paragraphs.
  • Update #206 - Update Github workflows dependencies and allowed-endpoints.
  • Fix bug #224 - Fix unexpected behavior when trying to split and create a new paragraph.

2.0.26 (25.10.2023)

  • Fix bug #238 - Fix bug in v2.0.25 related to runtime handle.

2.0.25 (23.10.2023)

  • Fix bug #235 - Fix bug in caret in new Editor.js versions.

2.0.24 (17.10.2023)

  • Fix bug #227 - Fix unexpected behavior when there are line breaks.
  • Fix bug #202 - Solve undo with custom blocks.
  • Update #225 - Update codeql.yml and scorecards.yml allowed-endpoints.

2.0.23 (15.09.2023)

  • Fix bug #213 - Add support to different keyboard distributions.
  • Fix bug #209 - Solve bug in undo with deleted blocks.

2.0.22 (16.05.2023)

  • Fix bug #207 - Limit search scope to Editor (holder) only.

2.0.21 (18.04.2023)

  • Add default shortcut for CMD+SHIFT+Z command to redo.
  • Update all dependencies.

2.0.20 (10.03.2023)

  • Add the dist/bundle.js file of previous release - Fix bug when there is the last element to undo: Uncaught TypeError: Cannot read properties of undefined (reading 'holder').

2.0.19 (07.03.2023)

  • Fix bug when there is the last element to undo: Uncaught TypeError: Cannot read properties of undefined (reading 'holder').
  • Update all dependencies.

2.0.18 (13.01.2023)

  • Block egress traffic in GitHub Actions.
  • Add stability badge in README.

2.0.17 (28.12.2022)

  • Add Renovate as dependency update tool.
  • Keep read-only permissions in CI workflow.

2.0.16 (23.12.2022)

  • Harden GitHub Actions.

2.0.15 (06.12.2022)

  • Fix bug TypeError: can't access property "type", t[i] is undefined (#156)

2.0.14 (17.11.2022)

  • Update README usage instructions for React
  • Bump loader-utils from 2.0.3 to 2.0.4

2.0.13 (16.11.2022)

  • Fix validation of shortcuts on other keyboard layouts

2.0.12 (09.11.2022)

  • Bump loader-utils from 2.0.2 to 2.0.3

2.0.11 (28.10.2022)

  • Fix "master" branch by "main" in the CI

2.0.10 (28.10.2022)

  • Add Coverage Report with Coveralls

2.0.9 (13.09.2022)

  • Add OpenSSF BestPractices & Scorecard badges
  • Add CDN version documentation

2.0.8 (08.08.2022)

  • Add scorecards actions

2.0.7 (02.08.2022)

  • Fix bug, undo is ignored when pasting more than 1 paragraph

2.0.6 (25.07.2022)

  • Add security policy to repository
  • Update packages with known security breaches

2.0.5 (15.07.2022)

  • Add workflow for automatic publishing in npm

2.0.4 (29.06.2022)

  • Fix bug, hover over @editorjs/table trigger mutationDebouncer

2.0.3 (08.06.2022)

  • Fix bug in the undo fuction in the block was dropped validation which prevents blocks that were defined with an id to being drop consistently

2.0.2 (07.06.2022)

  • Fix bug in the block was dropped fuction, remove unnecesary index comparision

2.0.1 (10.05.2022)

  • Bug fix in the save function, use indexInState instead of index.

2.0.0 (29.04.2022)

  • Updated packages with known security breaches.
  • Add a new corner case to consider the case when the content has changed in the not current block.
  • solve a bug in shortcuts related to the common Mac commands.
  • Add a new corner case to handle empty blocks in the editor.

2.0.0-rc.3 (14.03.2022)

  • Add a custom debounceTimer in the config object to choose the save time.

2.0.0-rc.2 (28.02.2022)

  • Optimizes the observer, saving with the EditorJS API only when the content changes.

2.0.0-rc.1 (24.02.2022)

  • Set the caret feature to the undo and redo actions.
  • Add support to readOnly toggle.

2.0.0-rc.0 (28.12.2022)

  • In the undo/redo actions the plugin now updates the involved block.
  • Update eslint-plugin-import to support eslint.
  • Solve linter errors.

1.0.1 (07.12.2021)

  • Updated packages with known security breaches.

1.0.0 (22.11.2021)

  • Set the initial stack data to avoid deleting all the blocks in the last undo.

0.3.0 (21.09.2021)

  • Custom undo/redo keyboard shortcuts allowed.
  • Updated packages with known security breaches.

0.2.0 (09.09.2021)

  • Accept strings in the holder key.
  • Updated packages with known security breaches.
  • Docs updated.

0.1.7 (16.06.2021)

  • Set the undo/redo event listeners in the holder element instead of the whole document.

0.1.6 (04.05.2021)

  • Read-only mode support added.
  • Dependencies updated. Security breaches covered.
  • CI with github actions added.
  • Minor bug fixes.

0.1.5 (05.04.2021)

  • Bug fix: Cannot read property 'holder' of undefined.
  • Dependencies updated. Security breaches covered.

0.1.4 (14.08.2020)

  • Bug fix: The editor is not registering any change if its instance is destroyed.
  • New feature: The caret state is saved and recovered on each undo/redo operation.

0.1.3 (13.07.2020)

  • Modify custom MutationObserver to avoid saving changes related to the editor destroy process.

0.1.2 (29.06.2020)

  • Added custom MutationObserver to save changes in a shorter period of time.

0.1.1 (24.06.2020)

  • Added support for Mac users using <kbd>⌘</kbd> + <kbd>Z</kbd> and <kbd>⌘</kbd> + <kbd>Y</kbd>.
  • Bug fixes and other minor improvements.

0.1.0 (23.06.2020)

Initial release