Skip to content

Commit 0491155

Browse files
feat(toolbar-api): method for toggling toolbox (#2332)
* Expose toolbox control method * Add test for toolbox * rename method to toggleToolbox * add missing test case * Add changelog * remove eslint rule * Update changelog Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * fix linter problems --------- Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
1 parent 707ff72 commit 0491155

5 files changed

Lines changed: 118 additions & 1 deletion

File tree

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### 2.27.0
44

5+
- `New`*Toolbar API* — Toolbox toggling method added.
56
- `Refactoring` — Popover class refactored.
67
- `Improvement`*Toolbox* — Number of `close()` method calls optimized.
78
- `Improvement` — The `onChange` callback won't be triggered only if all mutations contain nodes with the `data-mutation-free` attributes.

src/components/modules/api/toolbar.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default class ToolbarAPI extends Module {
1616
close: (): void => this.close(),
1717
open: (): void => this.open(),
1818
toggleBlockSettings: (openingState?: boolean): void => this.toggleBlockSettings(openingState),
19+
toggleToolbox: (openingState?: boolean): void => this.toggleToolbox(openingState),
1920
};
2021
}
2122

@@ -55,4 +56,27 @@ export default class ToolbarAPI extends Module {
5556
this.Editor.BlockSettings.close();
5657
}
5758
}
59+
60+
61+
/**
62+
* Open toolbox
63+
*
64+
* @param {boolean} openingState - Opening state of toolbox
65+
*/
66+
public toggleToolbox(openingState: boolean): void {
67+
if (this.Editor.BlockManager.currentBlockIndex === -1) {
68+
_.logLabeled('Could\'t toggle the Toolbox because there is no block selected ', 'warn');
69+
70+
return;
71+
}
72+
73+
const canOpenToolbox = openingState ?? !this.Editor.Toolbar.toolbox.opened;
74+
75+
if (canOpenToolbox) {
76+
this.Editor.Toolbar.moveAndOpen();
77+
this.Editor.Toolbar.toolbox.open();
78+
} else {
79+
this.Editor.Toolbar.toolbox.close();
80+
}
81+
}
5882
}

src/types-internal/editor-modules.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import NotifierAPI from '../components/modules/api/notifier';
2222
import SaverAPI from '../components/modules/api/saver';
2323
import Saver from '../components/modules/saver';
2424
import BlockSelection from '../components/modules/blockSelection';
25-
import RectangleSelection from '../components/modules/RectangleSelection';
25+
import RectangleSelection from '../components/modules/rectangleSelection';
2626
import InlineToolbarAPI from '../components/modules/api/inlineToolbar';
2727
import CrossBlockSelection from '../components/modules/crossBlockSelection';
2828
import ConversionToolbar from '../components/modules/toolbar/conversion';
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* There will be described test cases of 'api.toolbar.*' API
3+
*/
4+
import EditorJS from '../../../../types';
5+
6+
describe('api.toolbar', () => {
7+
/**
8+
* api.toolbar.openToolbox(openingState?: boolean)
9+
*/
10+
const firstBlock = {
11+
id: 'bwnFX5LoX7',
12+
type: 'paragraph',
13+
data: {
14+
text: 'The first block content mock.',
15+
},
16+
};
17+
const editorDataMock = {
18+
blocks: [
19+
firstBlock,
20+
],
21+
};
22+
23+
beforeEach(function () {
24+
cy.createEditor({
25+
data: editorDataMock,
26+
readOnly: false,
27+
}).as('editorInstance');
28+
});
29+
30+
afterEach(function () {
31+
if (this.editorInstance) {
32+
this.editorInstance.destroy();
33+
}
34+
});
35+
36+
describe('*.toggleToolbox()', () => {
37+
const isToolboxVisible = (): void => {
38+
cy.get('[data-cy=editorjs]').find('div.ce-toolbox')
39+
.then((toolbox) => {
40+
if (toolbox.is(':visible')) {
41+
assert.isOk(true, 'Toolbox visible');
42+
} else {
43+
assert.isNotOk(false, 'Toolbox should be visible');
44+
}
45+
});
46+
};
47+
48+
const isToolboxNotVisible = (): void => {
49+
cy.get('[data-cy=editorjs]').find('div.ce-toolbox')
50+
.then((toolbox) => {
51+
if (!toolbox.is(':visible')) {
52+
assert.isOk(true, 'Toolbox not visible');
53+
} else {
54+
assert.isNotOk(false, 'Toolbox should not be visible');
55+
}
56+
});
57+
};
58+
59+
it('should open the toolbox', function () {
60+
cy.get<EditorJS>('@editorInstance').then(async function (editor) {
61+
editor.toolbar.toggleToolbox(true);
62+
isToolboxVisible();
63+
});
64+
});
65+
66+
it('should close the toolbox', function () {
67+
cy.get<EditorJS>('@editorInstance').then(async function (editor) {
68+
editor.toolbar.toggleToolbox(true);
69+
70+
isToolboxVisible();
71+
72+
editor.toolbar.toggleToolbox(false);
73+
isToolboxNotVisible();
74+
});
75+
});
76+
it('should toggle the toolbox', function () {
77+
cy.get<EditorJS>('@editorInstance').then(async function (editor) {
78+
editor.toolbar.toggleToolbox();
79+
isToolboxVisible();
80+
81+
editor.toolbar.toggleToolbox();
82+
isToolboxNotVisible();
83+
});
84+
});
85+
});
86+
});

types/api/toolbar.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ export interface Toolbar {
1717
* @param {boolean} openingState — opening state of Block Setting
1818
*/
1919
toggleBlockSettings(openingState?: boolean): void;
20+
21+
/**
22+
* Toggle toolbox
23+
* @param {boolean} openingState — opening state of the toolbox
24+
*/
25+
toggleToolbox(openingState?: boolean): void;
2026
}

0 commit comments

Comments
 (0)