Skip to content

Commit

Permalink
Merge pull request #44 from kommitters/v0.1.7
Browse files Browse the repository at this point in the history
* Set event listeners in holder element instead of the document (#42)

* Test undo and redo events fired inside and outside EditorJS holder element

* Bump library version

Co-authored-by: Diego Lao Tebar <howarto@ymail.com>
  • Loading branch information
juanhurtado10 and Howarto authored Jun 16, 2021
2 parents 0891a5b + f0784cf commit 82e092f
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 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.
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "editorjs-undo",
"version": "0.1.6",
"version": "0.1.7",
"keywords": [
"undo",
"redo",
Expand Down
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ export default class Undo {
* Sets events listeners to allow keyboard actions support.
*/
setEventListeners() {
const { holder } = this.editor.configuration;
const buttonKey = /(Mac)/i.test(navigator.platform) ? 'metaKey' : 'ctrlKey';

const handleUndo = (e) => {
if (e[buttonKey] && e.key === 'z') {
e.preventDefault();
Expand All @@ -206,12 +208,13 @@ export default class Undo {
};

const handleDestroy = () => {
document.removeEventListener('keydown', handleUndo);
document.removeEventListener('keydown', handleRedo);
holder.removeEventListener('keydown', handleUndo);
holder.removeEventListener('keydown', handleRedo);
};

document.addEventListener('keydown', handleUndo);
document.addEventListener('keydown', handleRedo);
document.addEventListener('destroy', handleDestroy);
holder.addEventListener('keydown', handleUndo);
holder.addEventListener('keydown', handleRedo);
holder.addEventListener('destroy', handleDestroy);
}

}
5 changes: 1 addition & 4 deletions test/fixtures/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const editor = {
caret: {
setToBlock() {},
},
configuration: {
holder: 'editorjs',
},
configuration: {},
};

const readOnlyEditor = {
Expand All @@ -27,7 +25,6 @@ const readOnlyEditor = {
setToBlock() {},
},
configuration: {
holder: 'editorjs',
readOnly: true,
},
};
Expand Down
80 changes: 80 additions & 0 deletions test/undo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { editor, readOnlyEditor } from './fixtures/editor';
describe('Undo', () => {
beforeEach(() => {
document.body.innerHTML = '<div id="editorjs"></div>';
// EditorJS uses as a holder an HTMLElement instead of a query selector.
// This has to be assigned each time that DOM is reset.
editor.configuration.holder = document.querySelector('#editorjs');
readOnlyEditor.configuration.holder = document.querySelector('#editorjs');
});

describe('Read-only mode active', () => {
Expand Down Expand Up @@ -119,4 +123,80 @@ describe('Undo', () => {
expect(state).toEqual(newChange.blocks);
});
});

describe('Undo/redo events fired inside and outside Editor\'s holder', () => {
let undo;

beforeEach(() => {
undo = new Undo({ editor });
undo.initialize(initialData.blocks);
undo.save(firstChange.blocks);
undo.save(secondChange.blocks);
});

it('undo event outside Editor\'s holder has not to cause changes in Undo Plugin stack', () => {
// Set metaKey and ctrlKey to true in order to work in Mac and other OSs.
const keyboardEvent = new KeyboardEvent('keydown', {
key: 'z',
metaKey: true,
ctrlKey: true,
});

document.dispatchEvent(keyboardEvent);

expect(undo.count()).toEqual(2);
expect(undo.position).toEqual(2);
const { state } = undo.stack[2];
expect(state).toEqual(secondChange.blocks);
});

it('undo event inside Editor\'s holder has to cause changes in Undo Plugin stack', () => {
const keyboardEvent = new KeyboardEvent('keydown', {
key: 'z',
metaKey: true,
ctrlKey: true,
});

editor.configuration.holder.dispatchEvent(keyboardEvent);

expect(undo.count()).toEqual(2);
expect(undo.position).toEqual(1);
const { state } = undo.stack[1];
expect(state).toEqual(firstChange.blocks);
});

it('redo event outside Editor\'s holder has not to cause changes in Undo Plugin stack', () => {
undo.undo();

const keyboardEvent = new KeyboardEvent('keydown', {
key: 'y',
metaKey: true,
ctrlKey: true,
});

document.dispatchEvent(keyboardEvent);

expect(undo.count()).toEqual(2);
expect(undo.position).toEqual(1);
const { state } = undo.stack[1];
expect(state).toEqual(firstChange.blocks);
});

it('redo event inside Editor\'s holder has to cause changes in Undo Plugin stack', () => {
undo.undo();

const keyboardEvent = new KeyboardEvent('keydown', {
key: 'y',
metaKey: true,
ctrlKey: true,
});

editor.configuration.holder.dispatchEvent(keyboardEvent);

expect(undo.count()).toEqual(2);
expect(undo.position).toEqual(2);
const { state } = undo.stack[2];
expect(state).toEqual(secondChange.blocks);
});
});
});

0 comments on commit 82e092f

Please sign in to comment.