Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
break;
case ContentPosition.Range:
case ContentPosition.SelectionStart:
let { range, rangeToRestore } = getInitialRange(core, option);
if (!range) {
return;
}
// if to replace the selection and the selection is not collapsed, remove the the content at selection first
if (option.replaceSelection && !range.collapsed) {
range.deleteContents();
}
let pos = Position.getStart(range);
let blockElement: BlockElement;
if (
option.insertOnNewLine &&
(blockElement = getBlockElementAtNode(contentDiv, pos.normalize().node))
) {
pos = new Position(blockElement.getEndNode(), PositionType.After);
} else {
pos = adjustNodeInsertPosition(contentDiv, node, pos);
}
let nodeForCursor = node.nodeType == NodeType.DocumentFragment ? node.lastChild : node;
range = createRange(pos);
range.insertNode(node);
if (option.updateCursor && nodeForCursor) {
rangeToRestore = createRange(
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;
});
}
break;
case ContentPosition.Range:
case ContentPosition.SelectionStart:
let { range, rangeToRestore } = getInitialRange(core, option);
if (!range) {
return;
}
// if to replace the selection and the selection is not collapsed, remove the the content at selection first
if (option.replaceSelection && !range.collapsed) {
range.deleteContents();
}
let pos = Position.getStart(range);
let blockElement: BlockElement;
if (
option.insertOnNewLine &&
(blockElement = getBlockElementAtNode(contentDiv, pos.normalize().node))
) {
pos = new Position(blockElement.getEndNode(), PositionType.After);
} else {
pos = adjustNodeInsertPosition(contentDiv, node, pos);
}
let nodeForCursor = node.nodeType == NodeType.DocumentFragment ? node.lastChild : node;
range = createRange(pos);
range.insertNode(node);
if (option.updateCursor && nodeForCursor) {
rangeToRestore = createRange(
export function getStyleBasedFormatState(editor: Editor): StyleBasedFormatState {
let range = editor.getSelectionRange();
let node = range && Position.getStart(range).normalize().node;
let styles = node ? getComputedStyles(node) : [];
return {
fontName: styles[0],
fontSize: styles[1],
textColor: styles[2],
backgroundColor: styles[3],
};
}
core: EditorCore,
callback: (start: NodePosition, end: NodePosition, snapshotBeforeCallback: string) => any,
changeSource: ChangeSource | string
) => {
let isNested = core.currentUndoSnapshot !== null;
let data: any;
if (!isNested) {
core.currentUndoSnapshot = core.corePlugins.undo.addUndoSnapshot();
}
try {
if (callback) {
let range = core.api.getSelectionRange(core, true /*tryGetFromCache*/);
data = callback(
range && Position.getStart(range).normalize(),
range && Position.getEnd(range).normalize(),
core.currentUndoSnapshot
);
if (!isNested) {
core.corePlugins.undo.addUndoSnapshot();
}
}
} finally {
if (!isNested) {
core.currentUndoSnapshot = null;
}
}
if (callback && changeSource) {
let event: ContentChangedEvent = {
private tryNormalizeTyping(event: PluginKeyboardEvent, range?: Range) {
let position = this.ensureTypeInElement(
Position.getStart(range || this.editor.getSelectionRange()),
event
);
this.editor.select(position);
}