@atlaskit/editor-plugin-media
Version:
Media plugin for @atlaskit/editor-core
80 lines • 3.17 kB
JavaScript
import { bindKeymapWithCommand, enter, insertNewLine, moveDown, moveLeft, moveRight, tab, undo } from '@atlaskit/editor-common/keymaps';
import { GapCursorSelection, Side } from '@atlaskit/editor-common/selection';
import { keymap } from '@atlaskit/editor-prosemirror/keymap';
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { insertAndSelectCaptionFromMediaSinglePos, selectCaptionFromMediaSinglePos } from '../commands/captions';
import { stateKey } from '../pm-plugins/plugin-key';
export function keymapPlugin(options, editorAnalyticsAPI, editorSelectionAPI) {
const list = {};
bindKeymapWithCommand(undo.common, ignoreLinksInSteps, list);
bindKeymapWithCommand(enter.common, splitMediaGroup, list);
if (options !== null && options !== void 0 && options.allowCaptions) {
bindKeymapWithCommand(moveDown.common, insertAndSelectCaption(editorAnalyticsAPI), list);
bindKeymapWithCommand(tab.common, insertAndSelectCaption(editorAnalyticsAPI), list);
bindKeymapWithCommand(moveLeft.common, arrowLeftFromMediaSingle(editorSelectionAPI), list);
bindKeymapWithCommand(moveRight.common, arrowRightFromMediaSingle(editorSelectionAPI), list);
}
bindKeymapWithCommand(insertNewLine.common, splitMediaGroup, list);
return keymap(list);
}
const ignoreLinksInSteps = state => {
const mediaPluginState = stateKey.getState(state);
mediaPluginState.ignoreLinks = true;
return false;
};
const splitMediaGroup = state => {
const mediaPluginState = stateKey.getState(state);
return mediaPluginState.splitMediaGroup();
};
const insertAndSelectCaption = editorAnalyticsAPI => (state, dispatch) => {
const {
selection,
schema
} = state;
if (selection instanceof NodeSelection && selection.node.type === schema.nodes.mediaSingle && schema.nodes.caption) {
if (dispatch) {
const {
from,
node
} = selection;
if (!insertAndSelectCaptionFromMediaSinglePos(editorAnalyticsAPI)(from, node)(state, dispatch)) {
selectCaptionFromMediaSinglePos(from, node)(state, dispatch);
}
}
return true;
}
return false;
};
const arrowLeftFromMediaSingle = editorSelectionAPI => (state, dispatch) => {
const {
selection
} = state;
if (editorSelectionAPI && selection instanceof NodeSelection && selection.node.type.name === 'mediaSingle') {
const tr = editorSelectionAPI.selectNearNode({
selectionRelativeToNode: undefined,
selection: new GapCursorSelection(state.doc.resolve(selection.from), Side.LEFT)
})(state);
if (dispatch) {
dispatch(tr);
}
return true;
}
return false;
};
const arrowRightFromMediaSingle = editorSelectionAPI => (state, dispatch) => {
const {
selection
} = state;
if (editorSelectionAPI && selection instanceof NodeSelection && selection.node.type.name === 'mediaSingle') {
const tr = editorSelectionAPI.selectNearNode({
selectionRelativeToNode: undefined,
selection: new GapCursorSelection(state.doc.resolve(selection.to), Side.RIGHT)
})(state);
if (dispatch) {
dispatch(tr);
}
return true;
}
return false;
};
export default keymapPlugin;