mirror of
https://github.com/codex-team/editor.js
synced 2024-06-01 13:32:43 +02:00
0491155e33
* 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>
83 lines
2.1 KiB
TypeScript
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();
|
|
}
|
|
}
|
|
}
|