schlechtenburg/packages/italic/lib/util.ts

26 lines
533 B
TypeScript

import {
getSelection,
getRangeFromSelection,
} from '@schlechtenburg/core';
/**
* Wrap range with <i> tag
*/
export const surround = (range: Range) => {
range.surroundContents(document.createElement('i'));
}
/**
* Check selection and set activated state to button if there are <i> tag
*/
export const checkState = (): boolean => {
const selection = getSelection();
const range = getRangeFromSelection(selection);
console.log(range);
const isActive = document.queryCommandState('italic');
return isActive;
}