editor.js/src/components/inline-tools/inline-tool-italic.ts
Murod Khaydarov 3f8c7fbb7b
Sanitizer features (#467)
* Sanitizer features

* move to ts, use sanitizer module to clean taintstring

* Sanitizer is ready

* it works

* Code refactored (#476)

* Use new features for paste handling

* Fix asterix

* Add types and some comments

* Use any type for deepSanitize method

* Make sanitize property static and use apiSettings object

* Use sanitize for single-block pasting

* Fix comment

* little updates

* rename sanitize in inline-tools docs

* Update pattern handling

* Use public getter for available tools

* Fix typo
2018-10-23 10:34:00 +03:00

97 lines
1.9 KiB
TypeScript

import InlineTool from '../interfaces/tools/inline-tool';
import ISanitizerConfig from '../interfaces/sanitizer-config';
declare var $: any;
/**
* Italic Tool
*
* Inline Toolbar Tool
*
* Style selected text with italic
*/
export default class ItalicInlineTool implements InlineTool {
/**
* Specifies Tool as Inline Toolbar Tool
*
* @return {boolean}
*/
public static isInline = true;
/**
* Sanitizer Rule
* Leave <i> tags
* @return {object}
*/
static get sanitize(): ISanitizerConfig {
return {
i: {},
};
}
/**
* Native Document's command that uses for Italic
*/
private readonly commandName: string = 'italic';
/**
* Styles
*/
private readonly CSS = {
button: 'ce-inline-tool',
buttonActive: 'ce-inline-tool--active',
buttonModifier: 'ce-inline-tool--italic',
};
/**
* Elements
*/
private nodes: {button: HTMLButtonElement} = {
button: null,
};
/**
* @param {{api: IAPI}} - CodeX Editor API
*/
constructor({api}) {
}
/**
* Create button for Inline Toolbar
*/
public render(): HTMLElement {
this.nodes.button = document.createElement('button');
this.nodes.button.type = 'button';
this.nodes.button.classList.add(this.CSS.button, this.CSS.buttonModifier);
this.nodes.button.appendChild($.svg('italic', 6, 15));
return this.nodes.button;
}
/**
* Wrap range with <i> tag
* @param {Range} range
*/
public surround(range: Range): void {
document.execCommand(this.commandName);
}
/**
* Check selection and set activated state to button if there are <i> 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+I';
}
}