editor.js/src/components/inline-tools/inline-tool-bold.ts
George Berezhnoy bcdfcdadbc
Move to typescript (#474)
* Move all modules to ts

* It works

* Update README.md

Co-Authored-By: gohabereg <gohabereg@users.noreply.github.com>

* Interfaces

* Interfaces

* Move depending interfaces to external types

* Update README.md

* update tools

* add some docs

* Add some fixes

* Add desctiprion for Block declaration and Core properties

* Fixes due comments
:

* Remove Block from external types

* Bump version

* Update src/components/modules/tools.ts

Co-Authored-By: gohabereg <gohabereg@users.noreply.github.com>

* Update src/components/core.ts

Co-Authored-By: gohabereg <gohabereg@users.noreply.github.com>

* Rename gteBlockHTMLByIndex to getBlockByIndex

* Remove unnecessary constructors

* Clean up bindEvents method

* Add InlineToolConstructable interface

* Delete legacy notifications class

* Fix zero-configuration bugs

* Update inline tools and block tunes constructors
2018-11-23 19:11:50 +03:00

89 lines
1.8 KiB
TypeScript

import $ from '../dom';
import {API, 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
*
* @return {boolean}
*/
public static isInline = true;
/**
* Sanitizer Rule
* Leave <b> tags
* @return {object}
*/
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);
this.nodes.button.appendChild($.svg('bold', 13, 15));
return this.nodes.button;
}
/**
* Wrap range with <b> tag
* @param {Range} range
*/
public surround(range: Range): void {
document.execCommand(this.commandName);
}
/**
* Check selection and set activated state to button if there are <b> tag
* @param {Selection} selection
*/
public checkState(selection: Selection): boolean {
const isActive = document.queryCommandState(this.commandName);
this.nodes.button.classList.toggle(this.CSS.buttonActive, isActive);
return isActive;
}
/**
* Set a shortcut
*/
public get shortcut(): string {
return 'CMD+B';
}
}