Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (
!contains(core.contentDiv, range) ||
!(selection = core.document.defaultView.getSelection())
) {
return false;
}
if (selection.rangeCount > 0) {
// Workaround IE exception 800a025e
try {
let currentRange: Range;
// Do not remove/add range if current selection is the same with target range
// Without this check, execCommand() may fail in Edge since we changed the selection
if (
(skipSameRange || Browser.isEdge) &&
(currentRange = selection.rangeCount == 1 ? selection.getRangeAt(0) : null) &&
currentRange.startContainer == range.startContainer &&
currentRange.startOffset == range.startOffset &&
currentRange.endContainer == range.endContainer &&
currentRange.endOffset == range.endOffset
) {
needAddRange = false;
} else {
selection.removeAllRanges();
}
} catch (e) {}
}
if (needAddRange) {
selection.addRange(range);
}
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,
});
// element points to a wrapping node we added "<div><br></div>". We should move the selection left to <br>
result = new Position(formatNode.firstChild, PositionType.Begin);
}
if (formatNode) {
applyFormat(formatNode, this.editor.getDefaultFormat(), this.editor.isDarkMode());
}
import { cacheGetEventData, ContentEditFeature, Editor, Keys } from 'roosterjs-editor-core';
import { PluginKeyboardEvent, PositionType } from 'roosterjs-editor-types';
import {
Browser,
fromHtml,
isPositionAtBeginningOf,
Position,
getTagOfNode,
} from 'roosterjs-editor-dom';
// Edge can sometimes lose current format when Enter to new line.
// So here we add an extra SPAN for Edge to workaround this bug
const NEWLINE_HTML = Browser.isEdge ? '<div><span><br></span></div>' : '<div><br></div>';
const CHILD_PARENT_TAG_MAP: { [childTag: string]: string } = {
TD: 'TABLE',
TH: 'TABLE',
LI: 'OL,UL',
};
const CHILD_SELECTOR = Object.keys(CHILD_PARENT_TAG_MAP).join(',');
/**
* InsertLineBeforeStructuredNode edit feature, provides the ability to insert an empty line before
* a structured element (bullet/numbering list, blockquote, table) if the element is at beginning of
* document
*/
export const InsertLineBeforeStructuredNodeFeature: ContentEditFeature = {
keys: [Keys.ENTER],
shouldHandleEvent: cacheGetStructuredElement,
handleEvent: (event, editor) => {
format.headerLevel == 0 && styles.inactive
}>{`Header ${format.headerLevel}`}
Undo
{this.renderSpan(format.canUndo, 'Can Undo')}
{this.renderSpan(format.canRedo, 'Can Redo')}
Browser
{this.renderSpan(Browser.isChrome, 'Chrome')}
{this.renderSpan(Browser.isEdge, 'Edge')}
{this.renderSpan(Browser.isFirefox, 'Firefox')}
{this.renderSpan(Browser.isIE11OrGreater, 'IE10/11')}
{this.renderSpan(Browser.isIE, 'IE')}
{this.renderSpan(Browser.isIEOrEdge, 'IE/Edge')}
{this.renderSpan(Browser.isSafari, 'Safari')}
{this.renderSpan(Browser.isWebKit, 'Webkit')}
OS
{this.renderSpan(Browser.isMac, 'MacOS')}
{this.renderSpan(Browser.isWin, 'Windows')}