Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ensureTypeInElement(position: NodePosition, event?: PluginKeyboardEvent): NodePosition {
let result = position.normalize();
let block = this.editor.getBlockElementAtNode(result.node);
let formatNode: HTMLElement;
if (block) {
formatNode = block.collapseToSingleElement();
// if the block is empty, apply default format
// Otherwise, leave it as it is as we don't want to change the style for existing data
// unless the block was just created by the keyboard event (e.g. ctrl+a & start typing)
const shouldSetNodeStyles =
isNodeEmpty(formatNode) ||
(event && this.wasNodeJustCreatedByKeyboardEvent(event, formatNode));
formatNode = formatNode && shouldSetNodeStyles ? formatNode : null;
} else {
// Only reason we don't get the selection block is that we have an empty content div
// which can happen when users removes everything (i.e. select all and DEL, or backspace from very end to begin)
// The fix is to add a DIV wrapping, apply default format and move cursor over
formatNode = fromHtml(
Browser.isEdge ? '<div><span><br></span></div>' : '<div><br></div>',
this.editor.getDocument()
)[0] as HTMLElement;
this.editor.insertNode(formatNode, {
position: ContentPosition.End,
updateCursor: false,
replaceSelection: false,
insertOnNewLine: false,
});
shouldHandleEvent: (event, editor) => {
let li = cacheGetElementAtCursor(editor, event, 'LI');
return li && isNodeEmpty(li) && !li.previousSibling;
},
handleEvent: toggleListAndPreventDefault,
return cacheGetEventData(event, 'QUOTE_CHILD', () => {
let quote = editor.getElementAtCursor(STRUCTURED_TAGS);
if (quote && getTagOfNode(quote) == QUOTE_TAG) {
let pos = editor.getFocusedPosition();
let block = pos && editor.getBlockElementAtNode(pos.normalize().node);
if (block) {
let node =
block.getStartNode() == quote
? block.getStartNode()
: block.collapseToSingleElement();
return isNodeEmpty(node) ? node : null;
}
}
return null;
});
}
shouldHandleEvent: (event, editor) => {
let childOfQuote = cacheGetQuoteChild(event, editor);
let shift = event.rawEvent.shiftKey;
return !shift && childOfQuote && isNodeEmpty(childOfQuote);
},
handleEvent: (event, editor) => editor.performAutoComplete(() => splitQuote(event, editor)),
shouldHandleEvent: (event, editor) => {
let childOfQuote = cacheGetQuoteChild(event, editor);
return childOfQuote && isNodeEmpty(childOfQuote) && !childOfQuote.previousSibling;
},
handleEvent: splitQuote,
public isEmpty(trim?: boolean): boolean {
return isNodeEmpty(this.core.contentDiv, trim);
}