@atlaskit/editor-plugin-caption
Version:
Caption plugin for @atlaskit/editor-core
89 lines (82 loc) • 3.78 kB
JavaScript
import { bindKeymapWithCommand, enter, moveDown, moveLeft, moveUp, shiftTab, tab } from '@atlaskit/editor-common/keymaps';
import { GapCursorSelection } from '@atlaskit/editor-common/selection';
import { createNewParagraphBelow } from '@atlaskit/editor-common/utils';
import { keymap } from '@atlaskit/editor-prosemirror/keymap';
import { Selection } from '@atlaskit/editor-prosemirror/state';
import { findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
export function captionKeymap() {
var list = {};
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(moveDown.common, createNewParagraphBelowCaption, list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(enter.common, createNewParagraphBelowCaption, list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(moveDown.common, getOutOfCaption, list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(enter.common, getOutOfCaption, list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(moveUp.common, selectParentMediaSingle, list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(shiftTab.common, selectParentMediaSingle, list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(tab.common, getOutOfCaption, list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(moveLeft.common, gapCursorSelectLeftParentMediaSingle, list);
return keymap(list);
}
var createNewParagraphBelowCaption = function createNewParagraphBelowCaption(state, dispatch) {
var caption = findParentNodeOfType(state.schema.nodes.caption)(state.selection);
if (caption) {
return createNewParagraphBelow(state, dispatch);
}
return false;
};
var getOutOfCaption = function getOutOfCaption(state, dispatch) {
var caption = findParentNodeOfType(state.schema.nodes.caption)(state.selection);
if (caption) {
if (dispatch) {
var tr = state.tr.setSelection(Selection.near(state.tr.doc.resolve(caption.pos + caption.node.nodeSize)));
dispatch(tr);
}
return true;
}
return false;
};
var selectParentMediaSingle = function selectParentMediaSingle(state, dispatch) {
if (findParentNodeOfType(state.schema.nodes.caption)(state.selection)) {
var mediaSingleParent = findParentNodeOfType(state.schema.nodes.mediaSingle)(state.selection);
if (mediaSingleParent) {
if (dispatch) {
var tr = state.tr.setSelection(Selection.near(state.tr.doc.resolve(mediaSingleParent.pos)));
dispatch(tr);
}
return true;
}
}
return false;
};
var gapCursorSelectLeftParentMediaSingle = function gapCursorSelectLeftParentMediaSingle(state, dispatch) {
var caption = findParentNodeOfType(state.schema.nodes.caption)(state.selection);
if (caption) {
var mediaSingleParent = findParentNodeOfType(state.schema.nodes.mediaSingle)(state.selection);
if (mediaSingleParent && state.selection.empty && state.tr.doc.resolve(state.selection.from).parentOffset === 0) {
var gapCursorSelection = GapCursorSelection.findFrom(state.tr.doc.resolve(mediaSingleParent.pos), 0, false);
if (gapCursorSelection) {
if (dispatch) {
var tr = state.tr.setSelection(gapCursorSelection);
dispatch(tr);
}
return true;
}
}
}
return false;
};