Skip to content

Commit

Permalink
Merge pull request #237 from kommitters/v2.0
Browse files Browse the repository at this point in the history
Release V2.0.25
  • Loading branch information
miguelnietoa committed Oct 24, 2023
2 parents 3536d4c + 8d9131e commit 6136ce8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.0.25 (23.10.2023)

- Fix bug [#235](https://github.com/kommitters/editorjs-undo/pull/235) - Fix bug in caret in new Editor.js versions.

## 2.0.24 (17.10.2023)

- Fix bug [#227](https://github.com/kommitters/editorjs-undo/issues/227) - Fix unexpected behavior when there are line breaks.
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "editorjs-undo",
"version": "2.0.24",
"version": "2.0.25",
"keywords": [
"undo",
"redo",
Expand Down Expand Up @@ -38,6 +38,7 @@
"eslint-plugin-jest": "^27.0.0",
"jest": "^29.0.0",
"jest-environment-jsdom": "^29.3.1",
"regenerator-runtime": "^0.14.0",
"webpack": "^5.52.0",
"webpack-cli": "^5.0.0"
},
Expand Down
40 changes: 17 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export default class Undo {
/**
* Decreases the current position and update the respective block in the editor.
*/
undo() {
async undo() {
if (this.canUndo()) {
const { index: nextIndex, state: nextState } = this.stack[this.position];

Expand All @@ -272,29 +272,25 @@ export default class Undo {
if (this.blockWasDeleted(state, nextState)) {
this.insertDeletedBlock(state, nextState, index);
} else if (this.blockWasSkipped(index, nextIndex, state, nextState)) {
this.blocks.delete(nextIndex);
await this.blocks.delete(nextIndex);
this.caret.setToBlock(index, "end");
} else if (blockCount > state.length) {
this.blocks
.render({ blocks: state })
.then(() => {
this.editor.blocks.insert(this.defaultBlock, {});
this.setCaretIndex(index, caretIndex);
});
await this.blocks.render({ blocks: state });
await this.editor.blocks.insert(this.defaultBlock, {});
this.setCaretIndex(index, caretIndex);
} else if (this.blockWasDropped(state, nextState)) {
this.blocks
.render({ blocks: state })
.then(() => this.caret.setToBlock(index, "end"));
await this.blocks.render({ blocks: state });
this.caret.setToBlock(index, 'end');
} else if (this.contentChangedInNoFocusBlock(index, nextIndex)) {
const { id } = this.blocks.getBlockByIndex(nextIndex);

this.blocks.update(id, state[nextIndex].data);
await this.blocks.update(id, state[nextIndex].data);
this.setCaretIndex(index, caretIndex);
}

const block = this.blocks.getBlockByIndex(index);
if (block) {
this.blocks.update(block.id, state[index].data);
await this.blocks.update(block.id, state[index].data);
this.setCaretIndex(index, caretIndex);
}
}
Expand All @@ -310,8 +306,7 @@ export default class Undo {
if (caretIndex && caretIndex !== -1) {
const blocks = this.holder.getElementsByClassName("ce-block__content");
const caretBlock = new VanillaCaret(blocks[index].firstChild);

caretBlock.setPos(caretIndex);
setTimeout(() => caretBlock.setPos(caretIndex), 50);
} else {
this.caret.setToBlock(index, "end");
}
Expand All @@ -322,8 +317,8 @@ export default class Undo {
* @param {Array} state is the current state according to this.position.
* @param {Number} index is the block index
*/
insertBlock(state, index) {
this.blocks.insert(
async insertBlock(state, index) {
await this.blocks.insert(
state[index].type,
state[index].data,
{},
Expand All @@ -346,7 +341,7 @@ export default class Undo {
/**
* Increases the current position and update the respective block in the editor.
*/
redo() {
async redo() {
if (this.canRedo()) {
this.position += 1;
this.shouldSaveHistory = false;
Expand All @@ -355,21 +350,20 @@ export default class Undo {
this.stack[this.position - 1];

if (this.blockWasDeleted(prevState, state)) {
this.blocks.delete();
await this.blocks.delete();
this.caret.setToBlock(index, "end");
} else if (this.blockWasSkipped(prevIndex, index, state, prevState)) {
this.insertSkippedBlocks(prevState.length, state);
this.caret.setToBlock(index, 'end');
} else if (this.blockWasDropped(state, prevState) && this.position !== 1) {
this.blocks
.render({ blocks: state })
.then(() => this.caret.setToBlock(index, "end"));
await this.blocks.render({ blocks: state });
this.caret.setToBlock(index, "end");
}

this.onUpdate();
const block = this.blocks.getBlockByIndex(index);
if (block) {
this.blocks.update(block.id, state[index].data);
await this.blocks.update(block.id, state[index].data);
this.setCaretIndex(index, caretIndex);
}
}
Expand Down
1 change: 1 addition & 0 deletions test/undo.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @jest-environment jsdom
*/
import 'regenerator-runtime/runtime';
import Undo from '../src/index';
import {
initialData,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4439,6 +4439,11 @@ regenerator-runtime@^0.13.4:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52"
integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==

regenerator-runtime@^0.14.0:
version "0.14.0"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz#5e19d68eb12d486f797e15a3c6a918f7cec5eb45"
integrity sha512-srw17NI0TUWHuGa5CFGGmhfNIeja30WMBfbslPNhf6JrqQlLN5gcrvig1oqPxiVaXb0oW0XRKtH6Nngs5lKCIA==

regenerator-transform@^0.14.2:
version "0.14.5"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.5.tgz#c98da154683671c9c4dcb16ece736517e1b7feb4"
Expand Down

0 comments on commit 6136ce8

Please sign in to comment.