From 748023ade44e9b3d291dccc2459332f87f8c551c Mon Sep 17 00:00:00 2001 From: solarkraft <1337470+solarkraft@users.noreply.github.com> Date: Sat, 23 Oct 2021 00:35:23 +0200 Subject: [PATCH 1/2] Add save command --- script/customCommands.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/script/customCommands.js b/script/customCommands.js index 9602a8a7..6e65c3f0 100644 --- a/script/customCommands.js +++ b/script/customCommands.js @@ -1,3 +1,5 @@ +import { undoDepth } from 'prosemirror-history'; + /** * Returns a command that tries to set the selected textblocks to the given node type with the given attributes. * @@ -20,3 +22,21 @@ export function setBlockTypeNoAttrCheck(nodeType, attrs) { // eslint-disable-lin return true; }; } + + +/** + * Save the document, exiting the editor, if changes have been made + */ +export function save(state, dispatch) { + // The document should only be save-able if changes have been made + if (undoDepth(state) <= 0) return false; + + if (dispatch) { + // We don't use the dispatch function because no document state modification will happen + // (And using it anyway creates an error) + + const saveButton = document.querySelector('button[name="do[save]"]'); + saveButton.click(); + } + return true; +} From c650ec292e38d6de13d95b965a8e291171308b94 Mon Sep 17 00:00:00 2001 From: solarkraft <1337470+solarkraft@users.noreply.github.com> Date: Sat, 23 Oct 2021 00:35:34 +0200 Subject: [PATCH 2/2] Bind save command to Mod-Enter --- script/plugins/Keymap/keymap.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/script/plugins/Keymap/keymap.js b/script/plugins/Keymap/keymap.js index 11ce4da6..dc0c45ba 100644 --- a/script/plugins/Keymap/keymap.js +++ b/script/plugins/Keymap/keymap.js @@ -2,6 +2,7 @@ import { keymap } from 'prosemirror-keymap'; import { splitListItem } from 'prosemirror-schema-list'; import { baseKeymap, chainCommands } from 'prosemirror-commands'; import { redo, undo } from 'prosemirror-history'; +import { save } from '../../customCommands'; function getKeymapPlugin(schema) { // Mac OS has its own typical shortcuts @@ -10,6 +11,7 @@ function getKeymapPlugin(schema) { const customKeymap = {}; customKeymap.Enter = splitListItem(schema.nodes.list_item); // eslint-disable-line import/no-named-as-default-member + customKeymap['Mod-Enter'] = save; const combinedKeymapUnion = Object.keys(customKeymap).reduce((acc, key) => { if (baseKeymap[key]) {