1
|
|
|
import { undoDepth } from 'prosemirror-history'; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Returns a command that tries to set the selected textblocks to the given node type with the given attributes. |
5
|
|
|
* |
6
|
|
|
* Copied and adjusted from prosemirror-commands::setBlockType to not check for the node attributes |
7
|
|
|
*/ |
8
|
|
|
export function setBlockTypeNoAttrCheck(nodeType, attrs) { // eslint-disable-line import/prefer-default-export |
9
|
|
|
return function setBlockTypeNoAttrCheckDispatch(state, dispatch) { |
10
|
|
|
const { from, to } = state.selection; |
11
|
|
|
let applicable = false; |
12
|
|
|
state.doc.nodesBetween(from, to, (node, pos) => { |
13
|
|
|
if (applicable) return false; |
|
|
|
|
14
|
|
|
if (!node.isTextblock || node.type === nodeType) return true; |
|
|
|
|
15
|
|
|
const $pos = state.doc.resolve(pos); |
16
|
|
|
const index = $pos.index(); |
17
|
|
|
applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType); |
18
|
|
|
return true; |
19
|
|
|
}); |
20
|
|
|
if (!applicable) return false; |
|
|
|
|
21
|
|
|
if (dispatch) dispatch(state.tr.setBlockType(from, to, nodeType, attrs).scrollIntoView()); |
|
|
|
|
22
|
|
|
return true; |
23
|
|
|
}; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Save the document, exiting the editor, if changes have been made |
29
|
|
|
*/ |
30
|
|
|
export function save(state, dispatch) { |
31
|
|
|
// The document should only be save-able if changes have been made |
32
|
|
|
if (undoDepth(state) <= 0) return false; |
|
|
|
|
33
|
|
|
|
34
|
|
|
if (dispatch) { |
35
|
|
|
// We don't use the dispatch function because no document state modification will happen |
36
|
|
|
// (And using it anyway creates an error) |
37
|
|
|
|
38
|
|
|
const saveButton = document.querySelector('button[name="do[save]"]'); |
39
|
|
|
saveButton.click(); |
40
|
|
|
} |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.