Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return false;
}
// 1. Recursively clear format of all its child nodes
let allChildrenAreBlock = ([].slice.call(node.childNodes) as Node[])
.map(n => clearNodeFormat(n, tagsToUnwrap, tagsToStopUnwrap, attributesToPreserve))
.reduce((previousValue, value) => previousValue && value, true);
if (!canCollapse(tagsToStopUnwrap, node)) {
return false;
}
let returnBlockElement = isBlockElement(node);
// 2. If we should unwrap this tag, put it into an array and unwrap it later
if (tagsToUnwrap.indexOf(getTagOfNode(node)) >= 0 || allChildrenAreBlock) {
if (returnBlockElement && !allChildrenAreBlock) {
wrap(node);
}
unwrap(node);
} else {
// 3. Otherwise, remove all attributes
clearAttribute(node as HTMLElement, attributesToPreserve);
}
return returnBlockElement;
}
editor.addUndoSnapshot(() => {
element.parentNode.insertBefore(div, element);
// Select the new line when we are in table. This is the same behavior with Word
if (getTagOfNode(element) == 'TABLE') {
editor.select(new Position(div, PositionType.Begin).normalize());
}
});
event.rawEvent.preventDefault();
return cacheGetEventData(event, 'FIRST_STRUCTURE', () => {
// Provide a chance to keep browser default behavior by pressing SHIFT
let element = event.rawEvent.shiftKey ? null : editor.getElementAtCursor(CHILD_SELECTOR);
if (element) {
let range = editor.getSelectionRange();
if (
range &&
range.collapsed &&
isPositionAtBeginningOf(Position.getStart(range), element) &&
!editor.getBodyTraverser(element).getPreviousBlockElement()
) {
return editor.getElementAtCursor(CHILD_PARENT_TAG_MAP[getTagOfNode(element)]);
}
}
return null;
});
}
function getContainerListType(listItemContainer: Element): 'OL' | 'UL' | null {
const tag = getTagOfNode(listItemContainer.firstChild);
return tag == UNORDERED_LIST_TAG_NAME || tag == ORDERED_LIST_TAG_NAME ? tag : null;
}
function cacheGetListElement(event: PluginKeyboardEvent, editor: Editor) {
let li = cacheGetElementAtCursor(editor, event, 'LI,TABLE');
let listElement = li && getTagOfNode(li) == 'LI' && editor.getElementAtCursor('UL,OL', li);
return listElement ? [listElement, li] : null;
}
function canCollapse(tagsToStopUnwrap: string[], node: Node) {
return tagsToStopUnwrap.indexOf(getTagOfNode(node)) < 0;
}
collapsedListItemSections.forEach((section) => {
if (getTagOfNode(section.firstChild) == 'DIV') {
unwrap(section)
}
})
}
function toggleListAndPreventDefault(event: PluginKeyboardEvent, editor: Editor) {
let listInfo = cacheGetListElement(event, editor);
if (listInfo) {
let listElement = listInfo[0];
let tag = getTagOfNode(listElement);
if (tag == 'UL') {
toggleBullet(editor);
} else if (tag == 'OL') {
toggleNumbering(editor);
}
editor.focus();
event.rawEvent.preventDefault();
}
}
function unwrapFunction(node: HTMLElement): Node {
if (!node) {
return null;
}
let firstChild = node.childNodes[0];
if (node.childNodes.length == 1 && getTagOfNode(firstChild) == CODE_NODE_TAG) {
unwrap(firstChild);
}
return unwrap(node);
}
export function getElementBasedFormatState(
editor: Editor,
event?: PluginEvent
): ElementBasedFormatState {
let listTag = getTagOfNode(cacheGetElementAtCursor(editor, event, 'OL,UL'));
let headerTag = getTagOfNode(cacheGetElementAtCursor(editor, event, 'H1,H2,H3,H4,H5,H6'));
return {
isBullet: listTag == 'UL',
isNumbering: listTag == 'OL',
headerLevel: (headerTag && parseInt(headerTag[1])) || 0,
canUnlink: !!editor.queryElements('a[href]', QueryScope.OnSelection)[0],
canAddImageAltText: !!editor.queryElements('img', QueryScope.OnSelection)[0],
isBlockQuote: !!editor.queryElements('blockquote', QueryScope.OnSelection)[0],
};
}