Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save command #158

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions script/customCommands.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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;
}
2 changes: 2 additions & 0 deletions script/plugins/Keymap/keymap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]) {
Expand Down