Skip to content

Commit

Permalink
feat(toolbar-api): method for toggling toolbox (codex-team#2332)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
GuillaumeOnepilot and neSpecc authored Apr 13, 2023
1 parent 707ff72 commit 0491155
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 2.27.0

- `New`*Toolbar API* — Toolbox toggling method added.
- `Refactoring` — Popover class refactored.
- `Improvement`*Toolbox* — Number of `close()` method calls optimized.
- `Improvement` — The `onChange` callback won't be triggered only if all mutations contain nodes with the `data-mutation-free` attributes.
Expand Down
24 changes: 24 additions & 0 deletions src/components/modules/api/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class ToolbarAPI extends Module {
close: (): void => this.close(),
open: (): void => this.open(),
toggleBlockSettings: (openingState?: boolean): void => this.toggleBlockSettings(openingState),
toggleToolbox: (openingState?: boolean): void => this.toggleToolbox(openingState),
};
}

Expand Down Expand Up @@ -55,4 +56,27 @@ export default class ToolbarAPI extends Module {
this.Editor.BlockSettings.close();
}
}


/**
* Open toolbox
*
* @param {boolean} openingState - Opening state of toolbox
*/
public toggleToolbox(openingState: boolean): void {
if (this.Editor.BlockManager.currentBlockIndex === -1) {
_.logLabeled('Could\'t toggle the Toolbox because there is no block selected ', 'warn');

return;
}

const canOpenToolbox = openingState ?? !this.Editor.Toolbar.toolbox.opened;

if (canOpenToolbox) {
this.Editor.Toolbar.moveAndOpen();
this.Editor.Toolbar.toolbox.open();
} else {
this.Editor.Toolbar.toolbox.close();
}
}
}
2 changes: 1 addition & 1 deletion src/types-internal/editor-modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import NotifierAPI from '../components/modules/api/notifier';
import SaverAPI from '../components/modules/api/saver';
import Saver from '../components/modules/saver';
import BlockSelection from '../components/modules/blockSelection';
import RectangleSelection from '../components/modules/RectangleSelection';
import RectangleSelection from '../components/modules/rectangleSelection';
import InlineToolbarAPI from '../components/modules/api/inlineToolbar';
import CrossBlockSelection from '../components/modules/crossBlockSelection';
import ConversionToolbar from '../components/modules/toolbar/conversion';
Expand Down
86 changes: 86 additions & 0 deletions test/cypress/tests/api/toolbar.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* There will be described test cases of 'api.toolbar.*' API
*/
import EditorJS from '../../../../types';

describe('api.toolbar', () => {
/**
* api.toolbar.openToolbox(openingState?: boolean)
*/
const firstBlock = {
id: 'bwnFX5LoX7',
type: 'paragraph',
data: {
text: 'The first block content mock.',
},
};
const editorDataMock = {
blocks: [
firstBlock,
],
};

beforeEach(function () {
cy.createEditor({
data: editorDataMock,
readOnly: false,
}).as('editorInstance');
});

afterEach(function () {
if (this.editorInstance) {
this.editorInstance.destroy();
}
});

describe('*.toggleToolbox()', () => {
const isToolboxVisible = (): void => {
cy.get('[data-cy=editorjs]').find('div.ce-toolbox')
.then((toolbox) => {
if (toolbox.is(':visible')) {
assert.isOk(true, 'Toolbox visible');
} else {
assert.isNotOk(false, 'Toolbox should be visible');
}
});
};

const isToolboxNotVisible = (): void => {
cy.get('[data-cy=editorjs]').find('div.ce-toolbox')
.then((toolbox) => {
if (!toolbox.is(':visible')) {
assert.isOk(true, 'Toolbox not visible');
} else {
assert.isNotOk(false, 'Toolbox should not be visible');
}
});
};

it('should open the toolbox', function () {
cy.get<EditorJS>('@editorInstance').then(async function (editor) {
editor.toolbar.toggleToolbox(true);
isToolboxVisible();
});
});

it('should close the toolbox', function () {
cy.get<EditorJS>('@editorInstance').then(async function (editor) {
editor.toolbar.toggleToolbox(true);

isToolboxVisible();

editor.toolbar.toggleToolbox(false);
isToolboxNotVisible();
});
});
it('should toggle the toolbox', function () {
cy.get<EditorJS>('@editorInstance').then(async function (editor) {
editor.toolbar.toggleToolbox();
isToolboxVisible();

editor.toolbar.toggleToolbox();
isToolboxNotVisible();
});
});
});
});
6 changes: 6 additions & 0 deletions types/api/toolbar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ export interface Toolbar {
* @param {boolean} openingState — opening state of Block Setting
*/
toggleBlockSettings(openingState?: boolean): void;

/**
* Toggle toolbox
* @param {boolean} openingState — opening state of the toolbox
*/
toggleToolbox(openingState?: boolean): void;
}

0 comments on commit 0491155

Please sign in to comment.