@atlaskit/editor-plugin-save-on-enter
Version:
Save-on-enter plugin for @atlaskit/editor-core
61 lines • 1.84 kB
JavaScript
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { analyticsEventKey } from '@atlaskit/editor-common/utils';
import { keymap } from '@atlaskit/editor-prosemirror/keymap';
export const createPlugin = (eventDispatch, onSave) => {
if (!onSave) {
return;
}
return keymap({
Enter(state, _dispatch, editorView) {
if (editorView && canSaveOnEnter(editorView)) {
eventDispatch(analyticsEventKey, analyticsPayload(state));
onSave(editorView);
return true;
}
return false;
}
});
};
function canSaveOnEnter(editorView) {
const {
$cursor
} = editorView.state.selection;
const {
decisionItem,
paragraph,
taskItem
} = editorView.state.schema.nodes;
return !$cursor || $cursor.parent.type === paragraph && $cursor.depth === 1 || $cursor.parent.type === decisionItem && !isEmptyAtCursor($cursor) || $cursor.parent.type === taskItem && !isEmptyAtCursor($cursor);
}
function isEmptyAtCursor($cursor) {
const {
content
} = $cursor.parent;
return !(content && content.size);
}
const analyticsPayload = state => ({
payload: {
action: ACTION.STOPPED,
actionSubject: ACTION_SUBJECT.EDITOR,
actionSubjectId: ACTION_SUBJECT_ID.SAVE,
attributes: {
inputMethod: INPUT_METHOD.SHORTCUT,
documentSize: state.doc.nodeSize
// TODO: ED-26961 - add individual node counts - tables, headings, lists, mediaSingles, mediaGroups, mediaCards, panels, extensions, decisions, action, codeBlocks
},
eventType: EVENT_TYPE.UI
}
});
export const saveOnEnterPlugin = ({
config: onSave
}) => ({
name: 'saveOnEnter',
pmPlugins() {
return [{
name: 'saveOnEnter',
plugin: ({
dispatch
}) => createPlugin(dispatch, onSave)
}];
}
});