editor.js/src/components/inline-tools/inline-tool-bold.ts
Peter Savchenko 3272efc3f7
chore(linting): eslint updated, code linted (#2174)
* update eslint + autofix

* a bunch of eslint fixes

* some spelling & eslint fixes

* fix some eslint errors and spells

* Update __module.ts

* a bunch of eslint fixes in tests

* Update cypress.yml

* Update cypress.yml

* fix cypress docker image name

* fixes for tests

* more tests fixed

* rm rule ignore

* rm another ignored rule

* Update .eslintrc
2022-11-25 21:56:50 +04:00

99 lines
1.9 KiB
TypeScript

import $ from '../dom';
import { InlineTool, SanitizerConfig } from '../../../types';
/**
* Bold Tool
*
* Inline Toolbar Tool
*
* Makes selected text bolder
*/
export default class BoldInlineTool implements InlineTool {
/**
* Specifies Tool as Inline Toolbar Tool
*
* @returns {boolean}
*/
public static isInline = true;
/**
* Title for hover-tooltip
*/
public static title = 'Bold';
/**
* Sanitizer Rule
* Leave <b> tags
*
* @returns {object}
*/
public static get sanitize(): SanitizerConfig {
return {
b: {},
} as SanitizerConfig;
}
/**
* Native Document's command that uses for Bold
*/
private readonly commandName: string = 'bold';
/**
* Styles
*/
private readonly CSS = {
button: 'ce-inline-tool',
buttonActive: 'ce-inline-tool--active',
buttonModifier: 'ce-inline-tool--bold',
};
/**
* Elements
*/
private nodes: {button: HTMLButtonElement} = {
button: undefined,
};
/**
* Create button for Inline Toolbar
*/
public render(): HTMLElement {
this.nodes.button = document.createElement('button') as HTMLButtonElement;
this.nodes.button.type = 'button';
this.nodes.button.classList.add(this.CSS.button, this.CSS.buttonModifier);
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
this.nodes.button.appendChild($.svg('bold', 12, 14));
return this.nodes.button;
}
/**
* Wrap range with <b> tag
*/
public surround(): void {
document.execCommand(this.commandName);
}
/**
* Check selection and set activated state to button if there are <b> tag
*
* @returns {boolean}
*/
public checkState(): boolean {
const isActive = document.queryCommandState(this.commandName);
this.nodes.button.classList.toggle(this.CSS.buttonActive, isActive);
return isActive;
}
/**
* Set a shortcut
*
* @returns {boolean}
*/
public get shortcut(): string {
return 'CMD+B';
}
}