Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function cacheGetStructuredElement(event: PluginKeyboardEvent, editor: Editor) {
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 cacheGetCommand(event: PluginKeyboardEvent) {
return cacheGetEventData(event, 'DEFAULT_SHORT_COMMAND', () => {
let e = event.rawEvent;
let key =
// Need to check ALT key to be false since in some language (e.g. Polski) uses AltGr to input some special charactors
// In that case, ctrlKey and altKey are both true in Edge, but we should not trigger any shortcut function here
event.eventType == PluginEventType.KeyDown && !e.altKey
? e.which |
(e.metaKey && Keys.Meta) |
(e.shiftKey && Keys.Shift) |
(e.ctrlKey && Keys.Ctrl)
: 0;
return key && commands.filter(cmd => (Browser.isMac ? cmd.macKey : cmd.winKey) == key)[0];
});
}
function cacheGetLinkData(event: PluginEvent, editor: Editor): LinkData {
return event.eventType == PluginEventType.KeyDown ||
(event.eventType == PluginEventType.ContentChanged && event.source == ChangeSource.Paste)
? cacheGetEventData(event, 'LINK_DATA', () => {
// First try to match link from the whole paste string from the plain text in clipboard.
// This helps when we paste a link next to some existing character, and the text we got
// from clipboard will only contain what we pasted, any existing characters will not
// be included.
let clipboardData =
event.eventType == PluginEventType.ContentChanged &&
event.source == ChangeSource.Paste &&
(event.data as ClipboardData);
let link = matchLink((clipboardData.text || '').trim());
let searcher = cacheGetContentSearcher(event, editor);
// In case the matched link is already inside a <a> tag, we do a range search.
// getRangeFromText will return null if the given text is already in a LinkInlineElement
if (link && searcher.getRangeFromText(link.originalUrl, false /*exactMatch*/)) {
return link;
}
</a>
export default function cacheGetHeaderLevel(editor: Editor, event?: PluginEvent): number {
return cacheGetEventData(event, EVENTDATACACHE_HEADER_LEVEL, () => {
for (let i = 1; i <= 6; i++) {
if (queryNodesWithSelection(editor, 'H' + i).length > 0) {
return i;
}
}
return 0;
});
}
function cacheGetTableCell(event: PluginEvent, editor: Editor): HTMLTableCellElement {
return cacheGetEventData(event, 'TABLECELL_FOR_TABLE_FEATURES', () => {
let pos = editor.getFocusedPosition();
let firstTd = editor.getElementAtCursor('TD,TH,LI', pos.node);
return getTagOfNode(firstTd) == 'LI' ? null : (firstTd as HTMLTableCellElement);
});
}
export function cacheGetNodeAtCursor(
editor: Editor,
event: PluginEvent,
expectedTags?: string | string[]
): HTMLElement {
let selector = expectedTags instanceof Array ? expectedTags.join(',') : expectedTags;
return cacheGetEventData(event, 'GET_NODE_AT_CURSOR_' + selector, () =>
getNodeAtCursor(editor, expectedTags)
);
}
function cacheGetQuoteChild(event: PluginKeyboardEvent, editor: Editor): Node {
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;
});
}
export function cacheGetCursorEventData(event: PluginEvent, editor: Editor): CursorData {
return cacheGetEventData(
event,
EVENTDATACACHE_CURSORDATA,
() => new CursorData(editor)
);
}