Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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];
});
}
private createEventHandlers() {
this.eventDisposers = [
attachDomEvent(this.core, 'input', null, this.stopPropagation),
attachDomEvent(this.core, 'keypress', PluginEventType.KeyPress, this.onKeyPress),
attachDomEvent(this.core, 'keydown', PluginEventType.KeyDown, this.stopPropagation),
attachDomEvent(this.core, 'keyup', PluginEventType.KeyUp, this.stopPropagation),
attachDomEvent(this.core, 'mousedown', PluginEventType.MouseDown),
attachDomEvent(this.core, 'mouseup', PluginEventType.MouseUp),
attachDomEvent(this.core, 'compositionstart', null, () => (this.inIME = true)),
attachDomEvent(
this.core,
'compositionend',
PluginEventType.CompositionEnd,
() => (this.inIME = false)
),
attachDomEvent(this.core, 'focus', null, () => {
// Restore the last saved selection first
if (this.core.cachedSelectionRange && !this.disableRestoreSelectionOnFocus) {
restoreSelection(this.core);
}
this.core.cachedSelectionRange = null;
public onPluginEvent(event: PluginEvent): void {
// if editor is in IME, don't do anything
if (this.editor.isInIME()) {
return;
}
switch (event.eventType) {
case PluginEventType.EditorReady:
if (!this.preserveSnapshots || (!this.canUndo() && !this.canRedo())) {
// Only add initial snapshot when we don't need to preserve snapshots or there is no existing snapshot
// Otherwise preserved undo/redo state may be ruined
this.addUndoSnapshot();
}
break;
case PluginEventType.KeyDown:
this.onKeyDown(event.rawEvent);
break;
case PluginEventType.KeyPress:
this.onKeyPress(event.rawEvent);
break;
case PluginEventType.CompositionEnd:
this.clearRedoForInput();
this.addUndoSnapshot();
break;
case PluginEventType.ContentChanged:
if (!this.isRestoring) {
this.clearRedoForInput();
}
break;
}
}
export default function tryHandleAutoBullet(
editor: Editor,
event: PluginEvent,
keyboardEvent: KeyboardEvent
): boolean {
if (event.eventType == PluginEventType.KeyDown) {
if (
keyboardEvent.which == KEY_SPACE &&
!keyboardEvent.ctrlKey &&
!keyboardEvent.altKey &&
!keyboardEvent.metaKey
) {
let cursorData = cacheGetCursorEventData(event, editor);
// We pick 3 characters before cursor so that if any characters exist before "1." or "*",
// we do not fire auto list.
let textBeforeCursor: string = cursorData.getXCharsBeforeCursor(3);
// Auto list is triggered if:
// 1. Text before cursor exactly mathces '*', '-' or '1.'
// 2. Cursor is not in html list
// 3. There's no non-text inline entities before cursor
public onPluginEvent(event: PluginEvent): void {
const domEvent = event as PluginDomEvent;
const keyboardEvent = domEvent.rawEvent as KeyboardEvent;
if (event.eventType === PluginEventType.KeyDown) {
this._eventHandledOnKeyDown = false;
if (this._isSuggesting) {
this._onKeyDownSuggestingDomEvent(domEvent);
} else if (keyboardEvent.which === KeyCodes.backspace && this._canUndoEmoji) {
// If KeyDown is backspace and canUndoEmoji, call editor undo
this._editor.undo();
this._handleEventOnKeyDown(domEvent);
this._canUndoEmoji = false;
}
} else if (event.eventType === PluginEventType.KeyUp && !this._isModifierKey(keyboardEvent.key)) {
if (this._isSuggesting) {
this._onKeyUpSuggestingDomEvent(domEvent);
} else {
this._onKeyUpDomEvent(domEvent);
}
} else if (event.eventType === PluginEventType.MouseUp) {
core,
'blur',
PluginEventType.Blur,
isIEorEdge
? null
: () => {
// For browsers that do not support beforedeactivate, still do the saving selection in onBlur
// Also check if there's already a selection range cache because in Chrome onBlur can be triggered multiple times when user clicks to other places,
// in that case the second time when fetching the selection range may result in a wrong selection.
if (!core.cachedSelectionRange) {
saveSelectionRange(core);
}
}
),
new DOMEventHandler(core, 'keypress', PluginEventType.KeyPress, event => onKeyPress(core, event as KeyboardEvent)),
new DOMEventHandler(core, 'keydown', PluginEventType.KeyDown),
new DOMEventHandler(core, 'keyup', PluginEventType.KeyUp),
new DOMEventHandler(core, 'compositionstart', null, () => (core.isInIME = true)),
new DOMEventHandler(
core,
'compositionend',
PluginEventType.CompositionEnd,
() => (core.isInIME = false)
),
new DOMEventHandler(core, 'mousedown', PluginEventType.MouseDown),
new DOMEventHandler(core, 'mouseup', PluginEventType.MouseUp),
new DOMEventHandler(core, 'mouseover', PluginEventType.MouseOver),
new DOMEventHandler(core, 'mouseout', PluginEventType.MouseOut),
new DOMEventHandler(core, 'paste', PluginEventType.Paste),
new DOMEventHandler(core, 'copy', PluginEventType.Copy, () => {}),
new DOMEventHandler(core, 'focus', PluginEventType.Focus, () => {
// Restore the last saved selection first
public onPluginEvent(event: PluginEvent): void {
if (this.commandBars && RoosterCommandBarPlugin.EventTypesToHandle[event.eventType]) {
this.commandBars.forEach(commandBar => commandBar.refreshFormatState());
return;
}
if (event.eventType === PluginEventType.KeyDown) {
this.handleShortcuts(event);
}
}
export function getCommandFromEvent(event: PluginEvent): RoosterShortcutCommands {
if (event.eventType !== PluginEventType.KeyDown) {
return RoosterShortcutCommands.None;
}
const commands = Browser.isMac ? macCommands : winCommands;
const keyboardEvent = (event as PluginDomEvent).rawEvent as KeyboardEvent;
for (const cmd of commands) {
if (
!keyboardEvent.altKey &&
cmd.ctrlKey === keyboardEvent.ctrlKey &&
cmd.metaKey === keyboardEvent.metaKey &&
cmd.shiftKey === keyboardEvent.shiftKey &&
cmd.which === keyboardEvent.which
) {
return cmd.command;
}
}
public willHandleEventExclusively(event: PluginEvent) {
return (
this.isSuggesting &&
(event.eventType == PluginEventType.KeyDown ||
event.eventType == PluginEventType.KeyUp ||
event.eventType == PluginEventType.Input)
);
}
private isBlockQuoteEvent(event: PluginEvent, interestedKeyCodes: number[], nodeAtCursor: Node): [boolean, Node] {
let isBlockQuoteEvent = false;
let blockQuoteElement: Node;
if (event.eventType == PluginEventType.KeyDown) {
let keybordEvent = (event as PluginDomEvent).rawEvent as KeyboardEvent;
blockQuoteElement = getBlockQuoteElement(this.editor, nodeAtCursor);
isBlockQuoteEvent = !!blockQuoteElement &&
interestedKeyCodes.indexOf(keybordEvent.which) >= 0 &&
!keybordEvent.ctrlKey &&
!keybordEvent.altKey &&
!keybordEvent.metaKey;
}
return [isBlockQuoteEvent, blockQuoteElement];
}
}