UNPKG

@atlaskit/editor-plugin-media

Version:

Media plugin for @atlaskit/editor-core

78 lines 3.44 kB
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) { var 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); } var ignoreLinksInSteps = function ignoreLinksInSteps(state) { var mediaPluginState = stateKey.getState(state); mediaPluginState.ignoreLinks = true; return false; }; var splitMediaGroup = function splitMediaGroup(state) { var mediaPluginState = stateKey.getState(state); return mediaPluginState.splitMediaGroup(); }; var insertAndSelectCaption = function insertAndSelectCaption(editorAnalyticsAPI) { return function (state, dispatch) { var selection = state.selection, schema = state.schema; if (selection instanceof NodeSelection && selection.node.type === schema.nodes.mediaSingle && schema.nodes.caption) { if (dispatch) { var from = selection.from, node = selection.node; if (!insertAndSelectCaptionFromMediaSinglePos(editorAnalyticsAPI)(from, node)(state, dispatch)) { selectCaptionFromMediaSinglePos(from, node)(state, dispatch); } } return true; } return false; }; }; var arrowLeftFromMediaSingle = function arrowLeftFromMediaSingle(editorSelectionAPI) { return function (state, dispatch) { var selection = state.selection; if (editorSelectionAPI && selection instanceof NodeSelection && selection.node.type.name === 'mediaSingle') { var tr = editorSelectionAPI.selectNearNode({ selectionRelativeToNode: undefined, selection: new GapCursorSelection(state.doc.resolve(selection.from), Side.LEFT) })(state); if (dispatch) { dispatch(tr); } return true; } return false; }; }; var arrowRightFromMediaSingle = function arrowRightFromMediaSingle(editorSelectionAPI) { return function (state, dispatch) { var selection = state.selection; if (editorSelectionAPI && selection instanceof NodeSelection && selection.node.type.name === 'mediaSingle') { var 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;