editor.js/src/components/modules/api/toolbar.ts
Guillaume Leon 0491155e33
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>
2023-04-13 20:02:46 +03:00

83 lines
2.1 KiB
TypeScript

import { Toolbar } from '../../../../types/api';
import Module from '../../__module';
import * as _ from './../../utils';
/**
* @class ToolbarAPI
* Provides methods for working with the Toolbar
*/
export default class ToolbarAPI extends Module {
/**
* Available methods
*
* @returns {Toolbar}
*/
public get methods(): Toolbar {
return {
close: (): void => this.close(),
open: (): void => this.open(),
toggleBlockSettings: (openingState?: boolean): void => this.toggleBlockSettings(openingState),
toggleToolbox: (openingState?: boolean): void => this.toggleToolbox(openingState),
};
}
/**
* Open toolbar
*/
public open(): void {
this.Editor.Toolbar.moveAndOpen();
}
/**
* Close toolbar and all included elements
*/
public close(): void {
this.Editor.Toolbar.close();
}
/**
* Toggles Block Setting of the current block
*
* @param {boolean} openingState — opening state of Block Setting
*/
public toggleBlockSettings(openingState?: boolean): void {
if (this.Editor.BlockManager.currentBlockIndex === -1) {
_.logLabeled('Could\'t toggle the Toolbar because there is no block selected ', 'warn');
return;
}
/** Check that opening state is set or not */
const canOpenBlockSettings = openingState ?? !this.Editor.BlockSettings.opened;
if (canOpenBlockSettings) {
this.Editor.Toolbar.moveAndOpen();
this.Editor.BlockSettings.open();
} else {
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();
}
}
}