editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

Undo operations by Ctrl+Z

Open gohabereg opened this issue 8 years ago • 32 comments

gohabereg avatar Nov 23 '18 17:11 gohabereg

I'd like to see this too, currently if you delete something, it's gone for good.

eduardomart avatar Apr 11 '19 01:04 eduardomart

On the undo-redo note - I've been using immer for a project recently and adding in my own undo / redo functionality was trivial. It is a great library because it offers the ability to use "patches" (https://github.com/mweststrate/immer#patches) which are records of the individual pieces of your JSON tree that you have changed on your object.

The easy of use it brings to editing big JSON objects directly might be useful as a whole to this project, and the patches would be a great addition to allow complete control over undo / redo.

Behind the scenes they look something like this (you get both regular and inverse patches so you can go both ways):

// inverse
[
    {
        "op": "replace",
        "path": ["profile"],
        "value": {"name": "Veria", "age": 5}
    },
    {"op": "remove", "path": ["tags", 3]}
]

// regular
[
    {"op": "replace", "path": ["profile"], "value": {"name": "Noa", "age": 6}},
    {"op": "add", "path": ["tags", 3], "value": "kiddo"}
]

I actually think its the perfect data tool for something like a block editor. I understand not wanting to add dependencies, but this one could be worth looking at and its only 4.2kB gzipped.

lostpebble avatar Apr 11 '19 10:04 lostpebble

Hi @lostpebble !

I was working on these features using "patches" too, thanks for pointing out the immer!

This also would be really good for the future collaborative mode

gohabereg avatar Apr 11 '19 10:04 gohabereg

This also would be really good for the future collaborative mode

Yep exactly! That's part of the magic - only sending exactly the changes that are being made instead of the entire tree.

This could definitely kill two birds with one stone, undo / redo and collaborative modes.

I would definitely take more of a look at immer as whole too, it really has made my life 10x easier when working on my state library (https://github.com/lostpebble/pullstate) - in fact I made it a core mechanic - as it allows you to mutate your state objects directly and it can figure out when nothing has changed very easily (good for performance checks). Overall a brilliant library!

lostpebble avatar Apr 11 '19 10:04 lostpebble

The problem I faced is we need to work with DOM state directly if we don't want to plugin developers care about that. It is a little inconsistent with the idea of working with the data as JS objects. Anyway will look at the immer, thanks!

gohabereg avatar Apr 11 '19 10:04 gohabereg

Ah, okay. That is a little disappointing requirement, I was thinking that this was more of a "virtual" DOM as a whole - based completely off of the JSON object.

lostpebble avatar Apr 11 '19 11:04 lostpebble

We will think of options how to implement this. I hope we will include it to the next major release

gohabereg avatar Apr 11 '19 11:04 gohabereg

Great library guys! Without undo operations, it is hard to envision anyone using this for production apps.

If the DOM could somehow be reactive to the underlying JSON model, then one could fairly easily implement undo/redo and collaborative editing using a CRDT library such as AutoMerge.

Using CDRT, in my opinion, would give this library a huge leg up on the competition, which primarily use Operational Transformation (which from my understanding requires a server to ensure that operations are transformed in the correct order, as opposed to CDRT, which does not care about order of transactions).

bbbford avatar Apr 18 '19 17:04 bbbford

Hi! Any info about this bug? Can't work without undo-redo =(

just99 avatar Sep 26 '19 12:09 just99

what is the current progress on the undo/redo feature? how about the simple idea to keep change history and call Block.render(previousDATA)?

Render
Method removes all Blocks and fills with new passed JSON data

kenyxy avatar Nov 13 '19 04:11 kenyxy

Hey Guys, any news about this feature?

hiitsgabe avatar Nov 26 '19 00:11 hiitsgabe

On the undo-redo note - I've been using immer for a project recently and adding in my own undo / redo functionality was trivial. It is a great library because it offers the ability to use "patches" (https://github.com/mweststrate/immer#patches) which are records of the individual pieces of your JSON tree that you have changed on your object.

The easy of use it brings to editing big JSON objects directly might be useful as a whole to this project, and the patches would be a great addition to allow complete control over undo / redo.

Behind the scenes they look something like this (you get both regular and inverse patches so you can go both ways):

// inverse
[
    {
        "op": "replace",
        "path": ["profile"],
        "value": {"name": "Veria", "age": 5}
    },
    {"op": "remove", "path": ["tags", 3]}
]

// regular
[
    {"op": "replace", "path": ["profile"], "value": {"name": "Noa", "age": 6}},
    {"op": "add", "path": ["tags", 3], "value": "kiddo"}
]

I actually think its the perfect data tool for something like a block editor. I understand not wanting to add dependencies, but this one could be worth looking at and its only 4.2kB gzipped.

@lostpebble Could you provide an example of your implementation?

hiitsgabe avatar Nov 26 '19 00:11 hiitsgabe

another possible approach is let editor.js-core maintains an undo/redo stack, once ctrl-Z action triggered, each plugin's renderIfChanged method will be called, just like React's shouldComponentUpdate, then the renderIfChanged method will re-execute the render method.

what you guys think? @gohabereg @eduardomart @lostpebble @bbbford @just99

mydearxym avatar Dec 09 '19 06:12 mydearxym

@gohabereg @neSpecc Is there any progress regarding this issue?

kfirba avatar Apr 08 '20 07:04 kfirba

Everyone waiting for this feature ^^

rathpanha avatar Apr 10 '20 03:04 rathpanha

This lib is awesome. It would be super awesome if it has this feature :D

crokobit avatar May 11 '20 02:05 crokobit

What is happening about this matter?

kaaaaaaaaaaai avatar Jun 28 '20 11:06 kaaaaaaaaaaai

it seems https://editorjs.io/blocks#render can be used to do this ?

mydearxym avatar Jun 29 '20 05:06 mydearxym

👋👋👋 We have developed a plugin that can address this feature. We hope this may help :).

https://github.com/kommitters/editorjs-undo

The code is already open and published to NPM ... everyone is welcome to contribute 🎩.

juanhurtado10 avatar Jul 06 '20 20:07 juanhurtado10

I forgot to mention ... Redo is also supported ;)

juanhurtado10 avatar Jul 06 '20 20:07 juanhurtado10