editor.js/src/components/inline-tools/inline-tool-italic.ts
Peter Savchenko b1b582b150
feat(icons): codex icons package is used instead of svg sprite (#2173)
* chore(icons): migrating to the coded icon pack

* conversion toolbar

* inline toolbar, part 1

* inline-link tool has the new icons

* added a test for creating a link by Enter keydown in link input

* rm last icons, svg sprite, loaders

* rollback .ce-settings styles

* Update CHANGELOG.md

* Update settings.json
2022-11-25 22:26:23 +04:00

94 lines
1.8 KiB
TypeScript

import { InlineTool, SanitizerConfig } from '../../../types';
import { IconItalic } from '@codexteam/icons';
/**
* Italic Tool
*
* Inline Toolbar Tool
*
* Style selected text with italic
*/
export default class ItalicInlineTool implements InlineTool {
/**
* Specifies Tool as Inline Toolbar Tool
*
* @returns {boolean}
*/
public static isInline = true;
/**
* Title for hover-tooltip
*/
public static title = 'Italic';
/**
* Sanitizer Rule
* Leave <i> tags
*
* @returns {object}
*/
public static get sanitize(): SanitizerConfig {
return {
i: {},
} as SanitizerConfig;
}
/**
* 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,
};
/**
* 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.innerHTML = IconItalic;
return this.nodes.button;
}
/**
* Wrap range with <i> tag
*/
public surround(): void {
document.execCommand(this.commandName);
}
/**
* Check selection and set activated state to button if there are <i> tag
*/
public checkState(): 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';
}
}