Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function getContentEditorKeymap(editor: AppContentEditor, schema: ContentEditorSchema) {
const isMac = typeof navigator != 'undefined' ? /Mac/.test(navigator.platform) : false;
const keymap = {
'Mod-z': undo,
'Shift-Mod-z': redo,
'Mod-b': toggleMark(schema.marks.strong),
'Mod-i': toggleMark(schema.marks.em),
'Mod-`': toggleMark(schema.marks.code),
'Shift-Enter': chainCommands(exitCodeStart(editor.capabilities), exitCode, insertHardBreak),
// open emoji panel
'Mod-e': () => {
if (editor.capabilities.emoji) {
editor.showEmojiPanel();
}
return true;
},
// Add/remove link
'Mod-k': showLinkModal(editor.capabilities, schema),
} as { [k: string]: any };
const enterCommands = [] as PMKeymapCommand[];
run: function (state, dispatch, view, attrs) {
// Toggle link off
if (markActive(state, markType)) {
toggleMark(markType)(state, dispatch)
return true
}
// Toggle link on
if (attrs) {
toggleMark(markType, attrs)(state, dispatch)
return true
}
// Prompt for link
const {from, to} = state.selection
const selectedText = state.doc.textBetween(from, to)
const urlLike = isUrlLike(selectedText)
const value = (urlLike ? selectedText : '')
const {ed} = key.get(state).options.edStuff
private dispatchMark(mark: MarkType, attrs?: { [key: string]: any }) {
toggleMark(mark, attrs)(this.view.state, tr => {
this.view.dispatch(tr);
});
}
'Mod-i': (em) => toggleMark(em),
'Mod-u': (underline) => toggleMark(underline),
function buildKeymap(schema, mapKeys) {
let keys = {}, type
function bind(key, cmd) {
if (mapKeys) {
let mapped = mapKeys[key]
if (mapped === false) return
if (mapped) key = mapped
}
keys[key] = cmd
}
bind("Mod-Z", undo)
bind("Mod-Y", redo)
if (type = schema.marks.strong)
bind("Mod-KeyB", toggleMark(type))
if (type = schema.marks.em)
bind("Mod-KeyI", toggleMark(type))
if (type = schema.marks.code)
bind("Mod-Backquote", toggleMark(type))
if (type = schema.nodes.bullet_list)
bind("Shift-Ctrl-Digit8", wrapInList(type))
if (type = schema.nodes.ordered_list)
bind("Shift-Ctrl-Digit9", wrapInList(type))
if (type = schema.nodes.blockquote)
bind("Shift-Ctrl-Period", wrapIn(type))
if (type = schema.nodes.hard_break) {
let br = type, cmd = chainCommands(newlineInCode, (state, onAction) => {
onAction(state.tr.replaceSelection(br.create()).scrollAction())
return true
})
* @param {EditorState} state the editor's current state
* @param {MarkType} type type of the mark based on the schema (e.g. schema.marks.strong )
* @return {boolean} True if the mark is currently active
*/
function markActive(state, type) {
const {
from, $from, to, empty,
} = state.selection;
if (empty) {
return type.isInSet(state.storedMarks || $from.marks());
}
return state.doc.rangeHasMark(from, to, type);
}
return new MenuItem({
command: toggleMark(markType),
icon: svgIcon(iconName),
label: title,
isActive: editorState => markActive(editorState, markType),
});
}
callback(attrs) {
toggleMark(markType, attrs)(view.state, view.props.onAction)
}
})
run(state, dispatch, view, openPrompt) {
if (markActive(state, markType)) {
toggleMark(markType)(state, view.dispatch)
return true
}
openPrompt({
callback(attrs) {
toggleMark(markType, attrs)(view.state, view.dispatch)
}
});
}
})
bind('Backspace', undoInputRule);
if (!mac) bind('Mod-y', redo);
bind('Alt-ArrowUp', joinUp);
bind('Alt-ArrowDown', joinDown);
bind('Mod-BracketLeft', lift);
bind('Escape', selectParentNode);
if (schema.marks.strong) {
bind('Mod-b', toggleMark(schema.marks.strong));
}
if (schema.marks.em) {
bind('Mod-i', toggleMark(schema.marks.em));
}
if (schema.marks.sup) {
bind('Mod-.', toggleMark(schema.marks.sup));
}
if (schema.marks.sub) {
bind('Mod-,', toggleMark(schema.marks.sub));
}
if (schema.marks.strike) {
bind('Mod-Shift-x', toggleMark(schema.marks.strike));
}
if (schema.marks.code) {
bind('Mod-<', toggleMark(schema.marks.code));
}
if (schema.marks.link) {
bind('Mod-k', toggleMark(schema.marks.link));
}
if (schema.nodes.bullet_list) {
bind('Shift-Ctrl-8', wrapInList(schema.nodes.bullet_list));