schlechtenburg/packages/italic/lib/util.ts

26 lines
533 B
TypeScript
Raw Normal View History

2022-12-29 23:33:04 +00:00
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;
}