@kerebron/extension-lsp 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +82 -0
  3. package/esm/_dnt.polyfills.d.ts +101 -0
  4. package/esm/_dnt.polyfills.d.ts.map +1 -0
  5. package/esm/_dnt.polyfills.js +127 -0
  6. package/esm/_dnt.shims.d.ts +6 -0
  7. package/esm/_dnt.shims.d.ts.map +1 -0
  8. package/esm/_dnt.shims.js +61 -0
  9. package/esm/editor/src/CoreEditor.d.ts +40 -0
  10. package/esm/editor/src/CoreEditor.d.ts.map +1 -0
  11. package/esm/editor/src/CoreEditor.js +252 -0
  12. package/esm/editor/src/DummyEditorView.d.ts +60 -0
  13. package/esm/editor/src/DummyEditorView.d.ts.map +1 -0
  14. package/esm/editor/src/DummyEditorView.js +277 -0
  15. package/esm/editor/src/Extension.d.ts +31 -0
  16. package/esm/editor/src/Extension.d.ts.map +1 -0
  17. package/esm/editor/src/Extension.js +53 -0
  18. package/esm/editor/src/ExtensionManager.d.ts +32 -0
  19. package/esm/editor/src/ExtensionManager.d.ts.map +1 -0
  20. package/esm/editor/src/ExtensionManager.js +260 -0
  21. package/esm/editor/src/Mark.d.ts +23 -0
  22. package/esm/editor/src/Mark.d.ts.map +1 -0
  23. package/esm/editor/src/Mark.js +51 -0
  24. package/esm/editor/src/Node.d.ts +32 -0
  25. package/esm/editor/src/Node.d.ts.map +1 -0
  26. package/esm/editor/src/Node.js +60 -0
  27. package/esm/editor/src/commands/CommandManager.d.ts +23 -0
  28. package/esm/editor/src/commands/CommandManager.d.ts.map +1 -0
  29. package/esm/editor/src/commands/CommandManager.js +118 -0
  30. package/esm/editor/src/commands/baseCommandFactories.d.ts +3 -0
  31. package/esm/editor/src/commands/baseCommandFactories.d.ts.map +1 -0
  32. package/esm/editor/src/commands/baseCommandFactories.js +836 -0
  33. package/esm/editor/src/commands/createChainableState.d.ts +3 -0
  34. package/esm/editor/src/commands/createChainableState.d.ts.map +1 -0
  35. package/esm/editor/src/commands/createChainableState.js +29 -0
  36. package/esm/editor/src/commands/keyCommandFactories.d.ts +3 -0
  37. package/esm/editor/src/commands/keyCommandFactories.d.ts.map +1 -0
  38. package/esm/editor/src/commands/keyCommandFactories.js +10 -0
  39. package/esm/editor/src/commands/mod.d.ts +7 -0
  40. package/esm/editor/src/commands/mod.d.ts.map +1 -0
  41. package/esm/editor/src/commands/mod.js +76 -0
  42. package/esm/editor/src/commands/replaceCommandFactories.d.ts +3 -0
  43. package/esm/editor/src/commands/replaceCommandFactories.d.ts.map +1 -0
  44. package/esm/editor/src/commands/replaceCommandFactories.js +94 -0
  45. package/esm/editor/src/commands/types.d.ts +18 -0
  46. package/esm/editor/src/commands/types.d.ts.map +1 -0
  47. package/esm/editor/src/commands/types.js +1 -0
  48. package/esm/editor/src/mod.d.ts +9 -0
  49. package/esm/editor/src/mod.d.ts.map +1 -0
  50. package/esm/editor/src/mod.js +8 -0
  51. package/esm/editor/src/nodeToTreeString.d.ts +10 -0
  52. package/esm/editor/src/nodeToTreeString.d.ts.map +1 -0
  53. package/esm/editor/src/nodeToTreeString.js +74 -0
  54. package/esm/editor/src/plugins/TrackSelecionPlugin.d.ts +6 -0
  55. package/esm/editor/src/plugins/TrackSelecionPlugin.d.ts.map +1 -0
  56. package/esm/editor/src/plugins/TrackSelecionPlugin.js +24 -0
  57. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts +23 -0
  58. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts.map +1 -0
  59. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.js +163 -0
  60. package/esm/editor/src/plugins/keymap/keymap.d.ts +11 -0
  61. package/esm/editor/src/plugins/keymap/keymap.d.ts.map +1 -0
  62. package/esm/editor/src/plugins/keymap/keymap.js +125 -0
  63. package/esm/editor/src/plugins/keymap/w3c-keyname.d.ts +4 -0
  64. package/esm/editor/src/plugins/keymap/w3c-keyname.d.ts.map +1 -0
  65. package/esm/editor/src/plugins/keymap/w3c-keyname.js +124 -0
  66. package/esm/editor/src/types.d.ts +52 -0
  67. package/esm/editor/src/types.d.ts.map +1 -0
  68. package/esm/editor/src/types.js +1 -0
  69. package/esm/editor/src/ui.d.ts +15 -0
  70. package/esm/editor/src/ui.d.ts.map +1 -0
  71. package/esm/editor/src/ui.js +16 -0
  72. package/esm/editor/src/utilities/SmartOutput.d.ts +41 -0
  73. package/esm/editor/src/utilities/SmartOutput.d.ts.map +1 -0
  74. package/esm/editor/src/utilities/SmartOutput.js +228 -0
  75. package/esm/editor/src/utilities/createNodeFromContent.d.ts +9 -0
  76. package/esm/editor/src/utilities/createNodeFromContent.d.ts.map +1 -0
  77. package/esm/editor/src/utilities/createNodeFromContent.js +32 -0
  78. package/esm/editor/src/utilities/getHtmlAttributes.d.ts +9 -0
  79. package/esm/editor/src/utilities/getHtmlAttributes.d.ts.map +1 -0
  80. package/esm/editor/src/utilities/getHtmlAttributes.js +47 -0
  81. package/esm/editor/src/utilities/getShadowRoot.d.ts +2 -0
  82. package/esm/editor/src/utilities/getShadowRoot.d.ts.map +1 -0
  83. package/esm/editor/src/utilities/getShadowRoot.js +16 -0
  84. package/esm/editor/src/utilities/mod.d.ts +6 -0
  85. package/esm/editor/src/utilities/mod.d.ts.map +1 -0
  86. package/esm/editor/src/utilities/mod.js +5 -0
  87. package/esm/editor/src/utilities/toRawTextResult.d.ts +3 -0
  88. package/esm/editor/src/utilities/toRawTextResult.d.ts.map +1 -0
  89. package/esm/editor/src/utilities/toRawTextResult.js +21 -0
  90. package/esm/extension-autocomplete/src/AutocompletePlugin.d.ts +8 -0
  91. package/esm/extension-autocomplete/src/AutocompletePlugin.d.ts.map +1 -0
  92. package/esm/extension-autocomplete/src/AutocompletePlugin.js +232 -0
  93. package/esm/extension-autocomplete/src/DefaultRenderer.d.ts +17 -0
  94. package/esm/extension-autocomplete/src/DefaultRenderer.d.ts.map +1 -0
  95. package/esm/extension-autocomplete/src/DefaultRenderer.js +137 -0
  96. package/esm/extension-autocomplete/src/ExtensionAutocomplete.d.ts +26 -0
  97. package/esm/extension-autocomplete/src/ExtensionAutocomplete.d.ts.map +1 -0
  98. package/esm/extension-autocomplete/src/ExtensionAutocomplete.js +30 -0
  99. package/esm/extension-autocomplete/src/createDefaultMatcher.d.ts +11 -0
  100. package/esm/extension-autocomplete/src/createDefaultMatcher.d.ts.map +1 -0
  101. package/esm/extension-autocomplete/src/createDefaultMatcher.js +58 -0
  102. package/esm/extension-autocomplete/src/createRegexMatcher.d.ts +4 -0
  103. package/esm/extension-autocomplete/src/createRegexMatcher.d.ts.map +1 -0
  104. package/esm/extension-autocomplete/src/createRegexMatcher.js +50 -0
  105. package/esm/extension-autocomplete/src/mod.d.ts +3 -0
  106. package/esm/extension-autocomplete/src/mod.d.ts.map +1 -0
  107. package/esm/extension-autocomplete/src/mod.js +2 -0
  108. package/esm/extension-autocomplete/src/types.d.ts +60 -0
  109. package/esm/extension-autocomplete/src/types.d.ts.map +1 -0
  110. package/esm/extension-autocomplete/src/types.js +1 -0
  111. package/esm/extension-basic-editor/src/ExtensionHtml.d.ts +15 -0
  112. package/esm/extension-basic-editor/src/ExtensionHtml.d.ts.map +1 -0
  113. package/esm/extension-basic-editor/src/ExtensionHtml.js +108 -0
  114. package/esm/extension-lsp/src/DiagnosticPlugin.d.ts +32 -0
  115. package/esm/extension-lsp/src/DiagnosticPlugin.d.ts.map +1 -0
  116. package/esm/extension-lsp/src/DiagnosticPlugin.js +131 -0
  117. package/esm/extension-lsp/src/ExtensionLsp.d.ts +25 -0
  118. package/esm/extension-lsp/src/ExtensionLsp.d.ts.map +1 -0
  119. package/esm/extension-lsp/src/ExtensionLsp.js +126 -0
  120. package/esm/extension-lsp/src/LSPClient.d.ts +56 -0
  121. package/esm/extension-lsp/src/LSPClient.d.ts.map +1 -0
  122. package/esm/extension-lsp/src/LSPClient.js +449 -0
  123. package/esm/extension-lsp/src/LspWebSocketTransport.d.ts +20 -0
  124. package/esm/extension-lsp/src/LspWebSocketTransport.d.ts.map +1 -0
  125. package/esm/extension-lsp/src/LspWebSocketTransport.js +137 -0
  126. package/esm/extension-lsp/src/computeIncrementalChanges.d.ts +8 -0
  127. package/esm/extension-lsp/src/computeIncrementalChanges.d.ts.map +1 -0
  128. package/esm/extension-lsp/src/computeIncrementalChanges.js +82 -0
  129. package/esm/extension-lsp/src/createLspAutocomplete.d.ts +23 -0
  130. package/esm/extension-lsp/src/createLspAutocomplete.d.ts.map +1 -0
  131. package/esm/extension-lsp/src/createLspAutocomplete.js +68 -0
  132. package/esm/extension-lsp/src/mod.d.ts +5 -0
  133. package/esm/extension-lsp/src/mod.d.ts.map +1 -0
  134. package/esm/extension-lsp/src/mod.js +4 -0
  135. package/esm/extension-lsp/src/types.d.ts +20 -0
  136. package/esm/extension-lsp/src/types.d.ts.map +1 -0
  137. package/esm/extension-lsp/src/types.js +1 -0
  138. package/esm/extension-lsp/src/workspace.d.ts +62 -0
  139. package/esm/extension-lsp/src/workspace.d.ts.map +1 -0
  140. package/esm/extension-lsp/src/workspace.js +168 -0
  141. package/esm/extension-markdown/src/DocumentMarkdownInlineTokenizer.d.ts +36 -0
  142. package/esm/extension-markdown/src/DocumentMarkdownInlineTokenizer.d.ts.map +1 -0
  143. package/esm/extension-markdown/src/DocumentMarkdownInlineTokenizer.js +240 -0
  144. package/esm/extension-markdown/src/DocumentMarkdownTokenizer.d.ts +26 -0
  145. package/esm/extension-markdown/src/DocumentMarkdownTokenizer.d.ts.map +1 -0
  146. package/esm/extension-markdown/src/DocumentMarkdownTokenizer.js +115 -0
  147. package/esm/extension-markdown/src/ExtensionMarkdown.d.ts +22 -0
  148. package/esm/extension-markdown/src/ExtensionMarkdown.d.ts.map +1 -0
  149. package/esm/extension-markdown/src/ExtensionMarkdown.js +48 -0
  150. package/esm/extension-markdown/src/MarkdownParser.d.ts +61 -0
  151. package/esm/extension-markdown/src/MarkdownParser.d.ts.map +1 -0
  152. package/esm/extension-markdown/src/MarkdownParser.js +249 -0
  153. package/esm/extension-markdown/src/MarkdownSerializer.d.ts +42 -0
  154. package/esm/extension-markdown/src/MarkdownSerializer.d.ts.map +1 -0
  155. package/esm/extension-markdown/src/MarkdownSerializer.js +325 -0
  156. package/esm/extension-markdown/src/PositionMapper.d.ts +15 -0
  157. package/esm/extension-markdown/src/PositionMapper.d.ts.map +1 -0
  158. package/esm/extension-markdown/src/PositionMapper.js +100 -0
  159. package/esm/extension-markdown/src/TokenSource.d.ts +11 -0
  160. package/esm/extension-markdown/src/TokenSource.d.ts.map +1 -0
  161. package/esm/extension-markdown/src/TokenSource.js +39 -0
  162. package/esm/extension-markdown/src/mdToPmConverter.d.ts +5 -0
  163. package/esm/extension-markdown/src/mdToPmConverter.d.ts.map +1 -0
  164. package/esm/extension-markdown/src/mdToPmConverter.js +111 -0
  165. package/esm/extension-markdown/src/pmToMdConverter.d.ts +16 -0
  166. package/esm/extension-markdown/src/pmToMdConverter.d.ts.map +1 -0
  167. package/esm/extension-markdown/src/pmToMdConverter.js +433 -0
  168. package/esm/extension-markdown/src/token_handlers/basic_token_handlers.d.ts +4 -0
  169. package/esm/extension-markdown/src/token_handlers/basic_token_handlers.d.ts.map +1 -0
  170. package/esm/extension-markdown/src/token_handlers/basic_token_handlers.js +151 -0
  171. package/esm/extension-markdown/src/token_handlers/footnote_token_handlers.d.ts +3 -0
  172. package/esm/extension-markdown/src/token_handlers/footnote_token_handlers.d.ts.map +1 -0
  173. package/esm/extension-markdown/src/token_handlers/footnote_token_handlers.js +34 -0
  174. package/esm/extension-markdown/src/token_handlers/inline_token_handlers.d.ts +6 -0
  175. package/esm/extension-markdown/src/token_handlers/inline_token_handlers.d.ts.map +1 -0
  176. package/esm/extension-markdown/src/token_handlers/inline_token_handlers.js +380 -0
  177. package/esm/extension-markdown/src/token_handlers/lists_token_handlers.d.ts +3 -0
  178. package/esm/extension-markdown/src/token_handlers/lists_token_handlers.d.ts.map +1 -0
  179. package/esm/extension-markdown/src/token_handlers/lists_token_handlers.js +323 -0
  180. package/esm/extension-markdown/src/token_handlers/table_token_handlers.d.ts +9 -0
  181. package/esm/extension-markdown/src/token_handlers/table_token_handlers.d.ts.map +1 -0
  182. package/esm/extension-markdown/src/token_handlers/table_token_handlers.js +308 -0
  183. package/esm/extension-markdown/src/treeSitterTokenizer.d.ts +5 -0
  184. package/esm/extension-markdown/src/treeSitterTokenizer.d.ts.map +1 -0
  185. package/esm/extension-markdown/src/treeSitterTokenizer.js +769 -0
  186. package/esm/extension-markdown/src/types.d.ts +28 -0
  187. package/esm/extension-markdown/src/types.d.ts.map +1 -0
  188. package/esm/extension-markdown/src/types.js +131 -0
  189. package/esm/extension-markdown/src/utils.d.ts +8 -0
  190. package/esm/extension-markdown/src/utils.d.ts.map +1 -0
  191. package/esm/extension-markdown/src/utils.js +86 -0
  192. package/esm/package.json +3 -0
  193. package/esm/tree-sitter-shim/src/main.d.ts +15 -0
  194. package/esm/tree-sitter-shim/src/main.d.ts.map +1 -0
  195. package/esm/tree-sitter-shim/src/main.js +25 -0
  196. package/esm/tree-sitter-shim/src/tree_sitter/node.d.ts +6 -0
  197. package/esm/tree-sitter-shim/src/tree_sitter/node.d.ts.map +1 -0
  198. package/esm/tree-sitter-shim/src/tree_sitter/node.js +1 -0
  199. package/esm/tree-sitter-shim/src/tree_sitter/parser.d.ts +7 -0
  200. package/esm/tree-sitter-shim/src/tree_sitter/parser.d.ts.map +1 -0
  201. package/esm/tree-sitter-shim/src/tree_sitter/parser.js +5 -0
  202. package/esm/tree-sitter-shim/src/tree_sitter/tree.d.ts +6 -0
  203. package/esm/tree-sitter-shim/src/tree_sitter/tree.d.ts.map +1 -0
  204. package/esm/tree-sitter-shim/src/tree_sitter/tree.js +1 -0
  205. package/esm/wasm/src/mod.d.ts +12 -0
  206. package/esm/wasm/src/mod.d.ts.map +1 -0
  207. package/esm/wasm/src/mod.js +48 -0
  208. package/esm/wasm/wasm.d.ts +138 -0
  209. package/esm/wasm/wasm.d.ts.map +1 -0
  210. package/esm/wasm/wasm.js +120 -0
  211. package/package.json +31 -0
@@ -0,0 +1,836 @@
1
+ import { canJoin, canSplit, findWrapping, joinPoint, liftTarget, ReplaceAroundStep, ReplaceStep, replaceStep, } from 'prosemirror-transform';
2
+ import { Fragment, NodeRange, Slice, } from 'prosemirror-model';
3
+ import { AllSelection, NodeSelection, Selection, SelectionRange, TextSelection, } from 'prosemirror-state';
4
+ /// Returns a command function that wraps the selection in a list with
5
+ /// the given type an attributes. If `dispatch` is null, only return a
6
+ /// value to indicate whether this is possible, but don't actually
7
+ /// perform the change.
8
+ const wrapInList = (listType, attrs = null) => {
9
+ return function (state, dispatch, view) {
10
+ let { $from, $to } = state.selection;
11
+ let range = $from.blockRange($to);
12
+ if (!range)
13
+ return false;
14
+ // let tr = dispatch ? state.tr : null;
15
+ const tr = state.tr;
16
+ if (!wrapRangeInList(tr, range, listType, attrs))
17
+ return false;
18
+ if (dispatch)
19
+ dispatch(tr.scrollIntoView());
20
+ return true;
21
+ };
22
+ };
23
+ /// Try to wrap the given node range in a list of the given type.
24
+ /// Return `true` when this is possible, `false` otherwise. When `tr`
25
+ /// is non-null, the wrapping is added to that transaction. When it is
26
+ /// `null`, the function only queries whether the wrapping is
27
+ /// possible.
28
+ const wrapRangeInList = (tr, range, listType, attrs = null) => {
29
+ let doJoin = false, outerRange = range, doc = range.$from.doc;
30
+ // This is at the top of an existing list item
31
+ if (range.depth >= 2 &&
32
+ range.$from.node(range.depth - 1).type.compatibleContent(listType) &&
33
+ range.startIndex == 0) {
34
+ // Don't do anything if this is the top of the list
35
+ if (range.$from.index(range.depth - 1) == 0)
36
+ return false;
37
+ let $insert = doc.resolve(range.start - 2);
38
+ outerRange = new NodeRange($insert, $insert, range.depth);
39
+ if (range.endIndex < range.parent.childCount) {
40
+ range = new NodeRange(range.$from, doc.resolve(range.$to.end(range.depth)), range.depth);
41
+ }
42
+ doJoin = true;
43
+ }
44
+ let wrap = findWrapping(outerRange, listType, attrs, range);
45
+ if (!wrap)
46
+ return false;
47
+ if (tr)
48
+ doWrapInList(tr, range, wrap, doJoin, listType);
49
+ return true;
50
+ };
51
+ function doWrapInList(tr, range, wrappers, joinBefore, listType) {
52
+ let content = Fragment.empty;
53
+ for (let i = wrappers.length - 1; i >= 0; i--) {
54
+ content = Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content));
55
+ }
56
+ tr.step(new ReplaceAroundStep(range.start - (joinBefore ? 2 : 0), range.end, range.start, range.end, new Slice(content, 0, 0), wrappers.length, true));
57
+ let found = 0;
58
+ for (let i = 0; i < wrappers.length; i++) {
59
+ if (wrappers[i].type == listType)
60
+ found = i + 1;
61
+ }
62
+ let splitDepth = wrappers.length - found;
63
+ let splitPos = range.start + wrappers.length - (joinBefore ? 2 : 0), parent = range.parent;
64
+ for (let i = range.startIndex, e = range.endIndex, first = true; i < e; i++, first = false) {
65
+ if (!first && canSplit(tr.doc, splitPos, splitDepth)) {
66
+ tr.split(splitPos, splitDepth);
67
+ splitPos += 2 * splitDepth;
68
+ }
69
+ splitPos += parent.child(i).nodeSize;
70
+ }
71
+ return tr;
72
+ }
73
+ /// Delete the selection, if there is one.
74
+ const deleteSelection = () => (state, dispatch) => {
75
+ if (state.selection.empty)
76
+ return false;
77
+ if (dispatch)
78
+ dispatch(state.tr.deleteSelection().scrollIntoView());
79
+ return true;
80
+ };
81
+ function atBlockStart(state, view) {
82
+ let { $cursor } = state.selection;
83
+ if (!$cursor ||
84
+ (view ? !view.endOfTextblock('backward', state) : $cursor.parentOffset > 0)) {
85
+ return null;
86
+ }
87
+ return $cursor;
88
+ }
89
+ /// If the selection is empty and at the start of a textblock, try to
90
+ /// reduce the distance between that block and the one before it—if
91
+ /// there's a block directly before it that can be joined, join them.
92
+ /// If not, try to move the selected block closer to the next one in
93
+ /// the document structure by lifting it out of its parent or moving it
94
+ /// into a parent of the previous block. Will use the view for accurate
95
+ /// (bidi-aware) start-of-textblock detection if given.
96
+ const joinBackward = () => (state, dispatch, view) => {
97
+ let $cursor = atBlockStart(state, view);
98
+ if (!$cursor)
99
+ return false;
100
+ let $cut = findCutBefore($cursor);
101
+ // If there is no node before this, try to lift
102
+ if (!$cut) {
103
+ let range = $cursor.blockRange(), target = range && liftTarget(range);
104
+ if (target == null)
105
+ return false;
106
+ if (dispatch)
107
+ dispatch(state.tr.lift(range, target).scrollIntoView());
108
+ return true;
109
+ }
110
+ let before = $cut.nodeBefore;
111
+ // Apply the joining algorithm
112
+ if (deleteBarrier(state, $cut, dispatch, -1))
113
+ return true;
114
+ // If the node below has no content and the node above is
115
+ // selectable, delete the node below and select the one above.
116
+ if ($cursor.parent.content.size == 0 &&
117
+ (textblockAt(before, 'end') || NodeSelection.isSelectable(before))) {
118
+ for (let depth = $cursor.depth;; depth--) {
119
+ let delStep = replaceStep(state.doc, $cursor.before(depth), $cursor.after(depth), Slice.empty);
120
+ if (delStep &&
121
+ delStep.slice.size <
122
+ delStep.to - delStep.from) {
123
+ if (dispatch) {
124
+ let tr = state.tr.step(delStep);
125
+ tr.setSelection(textblockAt(before, 'end')
126
+ ? Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos, -1)), -1)
127
+ : NodeSelection.create(tr.doc, $cut.pos - before.nodeSize));
128
+ dispatch(tr.scrollIntoView());
129
+ }
130
+ return true;
131
+ }
132
+ if (depth == 1 || $cursor.node(depth - 1).childCount > 1)
133
+ break;
134
+ }
135
+ }
136
+ // If the node before is an atom, delete it
137
+ if (before.isAtom && $cut.depth == $cursor.depth - 1) {
138
+ if (dispatch) {
139
+ dispatch(state.tr.delete($cut.pos - before.nodeSize, $cut.pos).scrollIntoView());
140
+ }
141
+ return true;
142
+ }
143
+ return false;
144
+ };
145
+ /// A more limited form of [`joinBackward`](#commands.joinBackward)
146
+ /// that only tries to join the current textblock to the one before
147
+ /// it, if the cursor is at the start of a textblock.
148
+ const joinTextblockBackward = () => (state, dispatch, view) => {
149
+ let $cursor = atBlockStart(state, view);
150
+ if (!$cursor)
151
+ return false;
152
+ let $cut = findCutBefore($cursor);
153
+ return $cut ? joinTextblocksAround(state, $cut, dispatch) : false;
154
+ };
155
+ /// A more limited form of [`joinForward`](#commands.joinForward)
156
+ /// that only tries to join the current textblock to the one after
157
+ /// it, if the cursor is at the end of a textblock.
158
+ const joinTextblockForward = () => (state, dispatch, view) => {
159
+ let $cursor = atBlockEnd(state, view);
160
+ if (!$cursor)
161
+ return false;
162
+ let $cut = findCutAfter($cursor);
163
+ return $cut ? joinTextblocksAround(state, $cut, dispatch) : false;
164
+ };
165
+ function joinTextblocksAround(state, $cut, dispatch) {
166
+ let before = $cut.nodeBefore, beforeText = before, beforePos = $cut.pos - 1;
167
+ for (; !beforeText.isTextblock; beforePos--) {
168
+ if (beforeText.type.spec.isolating)
169
+ return false;
170
+ let child = beforeText.lastChild;
171
+ if (!child)
172
+ return false;
173
+ beforeText = child;
174
+ }
175
+ let after = $cut.nodeAfter, afterText = after, afterPos = $cut.pos + 1;
176
+ for (; !afterText.isTextblock; afterPos++) {
177
+ if (afterText.type.spec.isolating)
178
+ return false;
179
+ let child = afterText.firstChild;
180
+ if (!child)
181
+ return false;
182
+ afterText = child;
183
+ }
184
+ let step = replaceStep(state.doc, beforePos, afterPos, Slice.empty);
185
+ if (!step || step.from != beforePos ||
186
+ step instanceof ReplaceStep && step.slice.size >= afterPos - beforePos)
187
+ return false;
188
+ if (dispatch) {
189
+ let tr = state.tr.step(step);
190
+ tr.setSelection(TextSelection.create(tr.doc, beforePos));
191
+ dispatch(tr.scrollIntoView());
192
+ }
193
+ return true;
194
+ }
195
+ function textblockAt(node, side, only = false) {
196
+ for (let scan = node; scan; scan = side == 'start' ? scan.firstChild : scan.lastChild) {
197
+ if (scan.isTextblock)
198
+ return true;
199
+ if (only && scan.childCount != 1)
200
+ return false;
201
+ }
202
+ return false;
203
+ }
204
+ /// When the selection is empty and at the start of a textblock, select
205
+ /// the node before that textblock, if possible. This is intended to be
206
+ /// bound to keys like backspace, after
207
+ /// [`joinBackward`](#commands.joinBackward) or other deleting
208
+ /// commands, as a fall-back behavior when the schema doesn't allow
209
+ /// deletion at the selected point.
210
+ const selectNodeBackward = () => (state, dispatch, view) => {
211
+ let { $head, empty } = state.selection, $cut = $head;
212
+ if (!empty)
213
+ return false;
214
+ if ($head.parent.isTextblock) {
215
+ if (view ? !view.endOfTextblock('backward', state) : $head.parentOffset > 0)
216
+ return false;
217
+ $cut = findCutBefore($head);
218
+ }
219
+ let node = $cut && $cut.nodeBefore;
220
+ if (!node || !NodeSelection.isSelectable(node))
221
+ return false;
222
+ if (dispatch) {
223
+ dispatch(state.tr.setSelection(NodeSelection.create(state.doc, $cut.pos - node.nodeSize)).scrollIntoView());
224
+ }
225
+ return true;
226
+ };
227
+ function findCutBefore($pos) {
228
+ if (!$pos.parent.type.spec.isolating) {
229
+ for (let i = $pos.depth - 1; i >= 0; i--) {
230
+ if ($pos.index(i) > 0) {
231
+ return $pos.doc.resolve($pos.before(i + 1));
232
+ }
233
+ if ($pos.node(i).type.spec.isolating)
234
+ break;
235
+ }
236
+ }
237
+ return null;
238
+ }
239
+ function atBlockEnd(state, view) {
240
+ let { $cursor } = state.selection;
241
+ if (!$cursor ||
242
+ (view
243
+ ? !view.endOfTextblock('forward', state)
244
+ : $cursor.parentOffset < $cursor.parent.content.size)) {
245
+ return null;
246
+ }
247
+ return $cursor;
248
+ }
249
+ /// If the selection is empty and the cursor is at the end of a
250
+ /// textblock, try to reduce or remove the boundary between that block
251
+ /// and the one after it, either by joining them or by moving the other
252
+ /// block closer to this one in the tree structure. Will use the view
253
+ /// for accurate start-of-textblock detection if given.
254
+ const joinForward = () => (state, dispatch, view) => {
255
+ let $cursor = atBlockEnd(state, view);
256
+ if (!$cursor)
257
+ return false;
258
+ let $cut = findCutAfter($cursor);
259
+ // If there is no node after this, there's nothing to do
260
+ if (!$cut)
261
+ return false;
262
+ let after = $cut.nodeAfter;
263
+ // Try the joining algorithm
264
+ if (deleteBarrier(state, $cut, dispatch, 1))
265
+ return true;
266
+ // If the node above has no content and the node below is
267
+ // selectable, delete the node above and select the one below.
268
+ if ($cursor.parent.content.size == 0 &&
269
+ (textblockAt(after, 'start') || NodeSelection.isSelectable(after))) {
270
+ let delStep = replaceStep(state.doc, $cursor.before(), $cursor.after(), Slice.empty);
271
+ if (delStep &&
272
+ delStep.slice.size <
273
+ delStep.to - delStep.from) {
274
+ if (dispatch) {
275
+ let tr = state.tr.step(delStep);
276
+ tr.setSelection(textblockAt(after, 'start')
277
+ ? Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos)), 1)
278
+ : NodeSelection.create(tr.doc, tr.mapping.map($cut.pos)));
279
+ dispatch(tr.scrollIntoView());
280
+ }
281
+ return true;
282
+ }
283
+ }
284
+ // If the next node is an atom, delete it
285
+ if (after.isAtom && $cut.depth == $cursor.depth - 1) {
286
+ if (dispatch) {
287
+ dispatch(state.tr.delete($cut.pos, $cut.pos + after.nodeSize).scrollIntoView());
288
+ }
289
+ return true;
290
+ }
291
+ return false;
292
+ };
293
+ /// When the selection is empty and at the end of a textblock, select
294
+ /// the node coming after that textblock, if possible. This is intended
295
+ /// to be bound to keys like delete, after
296
+ /// [`joinForward`](#commands.joinForward) and similar deleting
297
+ /// commands, to provide a fall-back behavior when the schema doesn't
298
+ /// allow deletion at the selected point.
299
+ const selectNodeForward = () => (state, dispatch, view) => {
300
+ let { $head, empty } = state.selection, $cut = $head;
301
+ if (!empty)
302
+ return false;
303
+ if ($head.parent.isTextblock) {
304
+ if (view
305
+ ? !view.endOfTextblock('forward', state)
306
+ : $head.parentOffset < $head.parent.content.size) {
307
+ return false;
308
+ }
309
+ $cut = findCutAfter($head);
310
+ }
311
+ let node = $cut && $cut.nodeAfter;
312
+ if (!node || !NodeSelection.isSelectable(node))
313
+ return false;
314
+ if (dispatch) {
315
+ dispatch(state.tr.setSelection(NodeSelection.create(state.doc, $cut.pos))
316
+ .scrollIntoView());
317
+ }
318
+ return true;
319
+ };
320
+ function findCutAfter($pos) {
321
+ if (!$pos.parent.type.spec.isolating) {
322
+ for (let i = $pos.depth - 1; i >= 0; i--) {
323
+ let parent = $pos.node(i);
324
+ if ($pos.index(i) + 1 < parent.childCount) {
325
+ return $pos.doc.resolve($pos.after(i + 1));
326
+ }
327
+ if (parent.type.spec.isolating) {
328
+ break;
329
+ }
330
+ }
331
+ }
332
+ return null;
333
+ }
334
+ /// Join the selected block or, if there is a text selection, the
335
+ /// closest ancestor block of the selection that can be joined, with
336
+ /// the sibling above it.
337
+ const joinUp = () => (state, dispatch) => {
338
+ let sel = state.selection, nodeSel = sel instanceof NodeSelection, point;
339
+ if (nodeSel) {
340
+ if (sel.node.isTextblock || !canJoin(state.doc, sel.from))
341
+ return false;
342
+ point = sel.from;
343
+ }
344
+ else {
345
+ point = joinPoint(state.doc, sel.from, -1);
346
+ if (point == null)
347
+ return false;
348
+ }
349
+ if (dispatch) {
350
+ let tr = state.tr.join(point);
351
+ if (nodeSel) {
352
+ tr.setSelection(NodeSelection.create(tr.doc, point - state.doc.resolve(point).nodeBefore.nodeSize));
353
+ }
354
+ dispatch(tr.scrollIntoView());
355
+ }
356
+ return true;
357
+ };
358
+ /// Join the selected block, or the closest ancestor of the selection
359
+ /// that can be joined, with the sibling after it.
360
+ const joinDown = () => (state, dispatch) => {
361
+ let sel = state.selection, point;
362
+ if (sel instanceof NodeSelection) {
363
+ if (sel.node.isTextblock || !canJoin(state.doc, sel.to))
364
+ return false;
365
+ point = sel.to;
366
+ }
367
+ else {
368
+ point = joinPoint(state.doc, sel.to, 1);
369
+ if (point == null)
370
+ return false;
371
+ }
372
+ if (dispatch) {
373
+ dispatch(state.tr.join(point).scrollIntoView());
374
+ }
375
+ return true;
376
+ };
377
+ /// Lift the selected block, or the closest ancestor block of the
378
+ /// selection that can be lifted, out of its parent node.
379
+ const lift = () => (state, dispatch) => {
380
+ let { $from, $to } = state.selection;
381
+ let range = $from.blockRange($to), target = range && liftTarget(range);
382
+ if (target == null)
383
+ return false;
384
+ if (dispatch)
385
+ dispatch(state.tr.lift(range, target).scrollIntoView());
386
+ return true;
387
+ };
388
+ /// If the selection is in a node whose type has a truthy
389
+ /// [`code`](#model.NodeSpec.code) property in its spec, replace the
390
+ /// selection with a newline character.
391
+ const newlineInCode = () => (state, dispatch) => {
392
+ let { $head, $anchor } = state.selection;
393
+ if (!$head.parent.type.spec.code || !$head.sameParent($anchor))
394
+ return false;
395
+ if (dispatch)
396
+ dispatch(state.tr.insertText('\n').scrollIntoView());
397
+ return true;
398
+ };
399
+ function defaultBlockAt(match) {
400
+ for (let i = 0; i < match.edgeCount; i++) {
401
+ let { type } = match.edge(i);
402
+ if (type.isTextblock && !type.hasRequiredAttrs())
403
+ return type;
404
+ }
405
+ return null;
406
+ }
407
+ /// When the selection is in a node with a truthy
408
+ /// [`code`](#model.NodeSpec.code) property in its spec, create a
409
+ /// default block after the code block, and move the cursor there.
410
+ const exitCode = () => (state, dispatch) => {
411
+ let { $head, $anchor } = state.selection;
412
+ if (!$head.parent.type.spec.code || !$head.sameParent($anchor))
413
+ return false;
414
+ let above = $head.node(-1), after = $head.indexAfter(-1), type = defaultBlockAt(above.contentMatchAt(after));
415
+ if (!type || !above.canReplaceWith(after, after, type))
416
+ return false;
417
+ if (dispatch) {
418
+ let pos = $head.after(), tr = state.tr.replaceWith(pos, pos, type.createAndFill());
419
+ tr.setSelection(Selection.near(tr.doc.resolve(pos), 1));
420
+ dispatch(tr.scrollIntoView());
421
+ }
422
+ return true;
423
+ };
424
+ /// If a block node is selected, create an empty paragraph before (if
425
+ /// it is its parent's first child) or after it.
426
+ const createParagraphNear = () => (state, dispatch) => {
427
+ let sel = state.selection, { $from, $to } = sel;
428
+ if (sel instanceof AllSelection || $from.parent.inlineContent ||
429
+ $to.parent.inlineContent)
430
+ return false;
431
+ let type = defaultBlockAt($to.parent.contentMatchAt($to.indexAfter()));
432
+ if (!type || !type.isTextblock)
433
+ return false;
434
+ if (dispatch) {
435
+ let side = (!$from.parentOffset && $to.index() < $to.parent.childCount ? $from : $to)
436
+ .pos;
437
+ let tr = state.tr.insert(side, type.createAndFill());
438
+ tr.setSelection(TextSelection.create(tr.doc, side + 1));
439
+ dispatch(tr.scrollIntoView());
440
+ }
441
+ return true;
442
+ };
443
+ /// If the cursor is in an empty textblock that can be lifted, lift the
444
+ /// block.
445
+ const liftEmptyBlock = () => (state, dispatch) => {
446
+ let { $cursor } = state.selection;
447
+ if (!$cursor || $cursor.parent.content.size)
448
+ return false;
449
+ if ($cursor.depth > 1 && $cursor.after() != $cursor.end(-1)) {
450
+ let before = $cursor.before();
451
+ if (canSplit(state.doc, before)) {
452
+ if (dispatch)
453
+ dispatch(state.tr.split(before).scrollIntoView());
454
+ return true;
455
+ }
456
+ }
457
+ let range = $cursor.blockRange(), target = range && liftTarget(range);
458
+ if (target == null)
459
+ return false;
460
+ if (dispatch)
461
+ dispatch(state.tr.lift(range, target).scrollIntoView());
462
+ return true;
463
+ };
464
+ /// Create a variant of [`splitBlock`](#commands.splitBlock) that uses
465
+ /// a custom function to determine the type of the newly split off block.
466
+ const splitBlockAs = (splitNode) => {
467
+ return (state, dispatch) => {
468
+ let { $from, $to } = state.selection;
469
+ if (state.selection instanceof NodeSelection && state.selection.node.isBlock) {
470
+ if (!$from.parentOffset || !canSplit(state.doc, $from.pos))
471
+ return false;
472
+ if (dispatch)
473
+ dispatch(state.tr.split($from.pos).scrollIntoView());
474
+ return true;
475
+ }
476
+ if (!$from.depth)
477
+ return false;
478
+ let types = [];
479
+ let splitDepth, deflt, atEnd = false, atStart = false;
480
+ for (let d = $from.depth;; d--) {
481
+ let node = $from.node(d);
482
+ if (node.isBlock) {
483
+ atEnd = $from.end(d) == $from.pos + ($from.depth - d);
484
+ atStart = $from.start(d) == $from.pos - ($from.depth - d);
485
+ deflt = defaultBlockAt($from.node(d - 1).contentMatchAt($from.indexAfter(d - 1)));
486
+ let splitType = splitNode && splitNode($to.parent, atEnd, $from);
487
+ types.unshift(splitType || (atEnd && deflt ? { type: deflt } : null));
488
+ splitDepth = d;
489
+ break;
490
+ }
491
+ else {
492
+ if (d == 1)
493
+ return false;
494
+ types.unshift(null);
495
+ }
496
+ }
497
+ let tr = state.tr;
498
+ if (state.selection instanceof TextSelection ||
499
+ state.selection instanceof AllSelection)
500
+ tr.deleteSelection();
501
+ let splitPos = tr.mapping.map($from.pos);
502
+ let can = canSplit(tr.doc, splitPos, types.length, types);
503
+ if (!can) {
504
+ types[0] = deflt ? { type: deflt } : null;
505
+ can = canSplit(tr.doc, splitPos, types.length, types);
506
+ }
507
+ tr.split(splitPos, types.length, types);
508
+ if (!atEnd && atStart && $from.node(splitDepth).type != deflt) {
509
+ let first = tr.mapping.map($from.before(splitDepth)), $first = tr.doc.resolve(first);
510
+ if (deflt &&
511
+ $from.node(splitDepth - 1).canReplaceWith($first.index(), $first.index() + 1, deflt)) {
512
+ tr.setNodeMarkup(tr.mapping.map($from.before(splitDepth)), deflt);
513
+ }
514
+ }
515
+ if (dispatch)
516
+ dispatch(tr.scrollIntoView());
517
+ return true;
518
+ };
519
+ };
520
+ /// Split the parent block of the selection. If the selection is a text
521
+ /// selection, also delete its content.
522
+ const splitBlock = () => splitBlockAs();
523
+ /// Acts like [`splitBlock`](#commands.splitBlock), but without
524
+ /// resetting the set of active marks at the cursor.
525
+ const splitBlockKeepMarks = () => (state, dispatch) => {
526
+ return splitBlockAs()(state, dispatch && ((tr) => {
527
+ let marks = state.storedMarks ||
528
+ (state.selection.$to.parentOffset && state.selection.$from.marks());
529
+ if (marks)
530
+ tr.ensureMarks(marks);
531
+ dispatch(tr);
532
+ }));
533
+ };
534
+ /// Move the selection to the node wrapping the current selection, if
535
+ /// any. (Will not select the document node.)
536
+ const selectParentNode = () => (state, dispatch) => {
537
+ let { $from, to } = state.selection, pos;
538
+ let same = $from.sharedDepth(to);
539
+ if (same == 0)
540
+ return false;
541
+ pos = $from.before(same);
542
+ if (dispatch) {
543
+ dispatch(state.tr.setSelection(NodeSelection.create(state.doc, pos)));
544
+ }
545
+ return true;
546
+ };
547
+ /// Select the whole document.
548
+ const selectAll = () => (state, dispatch) => {
549
+ if (dispatch)
550
+ dispatch(state.tr.setSelection(new AllSelection(state.doc)));
551
+ return true;
552
+ };
553
+ function joinMaybeClear(state, $pos, dispatch) {
554
+ let before = $pos.nodeBefore, after = $pos.nodeAfter, index = $pos.index();
555
+ if (!before || !after || !before.type.compatibleContent(after.type)) {
556
+ return false;
557
+ }
558
+ if (!before.content.size && $pos.parent.canReplace(index - 1, index)) {
559
+ if (dispatch) {
560
+ dispatch(state.tr.delete($pos.pos - before.nodeSize, $pos.pos).scrollIntoView());
561
+ }
562
+ return true;
563
+ }
564
+ if (!$pos.parent.canReplace(index, index + 1) ||
565
+ !(after.isTextblock || canJoin(state.doc, $pos.pos))) {
566
+ return false;
567
+ }
568
+ if (dispatch) {
569
+ dispatch(state.tr.join($pos.pos).scrollIntoView());
570
+ }
571
+ return true;
572
+ }
573
+ function deleteBarrier(state, $cut, dispatch, dir) {
574
+ let before = $cut.nodeBefore, after = $cut.nodeAfter, conn, match;
575
+ let isolated = before.type.spec.isolating || after.type.spec.isolating;
576
+ if (!isolated && joinMaybeClear(state, $cut, dispatch))
577
+ return true;
578
+ let canDelAfter = !isolated &&
579
+ $cut.parent.canReplace($cut.index(), $cut.index() + 1);
580
+ if (canDelAfter &&
581
+ (conn = (match = before.contentMatchAt(before.childCount)).findWrapping(after.type)) &&
582
+ match.matchType(conn[0] || after.type).validEnd) {
583
+ if (dispatch) {
584
+ let end = $cut.pos + after.nodeSize, wrap = Fragment.empty;
585
+ for (let i = conn.length - 1; i >= 0; i--) {
586
+ wrap = Fragment.from(conn[i].create(null, wrap));
587
+ }
588
+ wrap = Fragment.from(before.copy(wrap));
589
+ let tr = state.tr.step(new ReplaceAroundStep($cut.pos - 1, end, $cut.pos, end, new Slice(wrap, 1, 0), conn.length, true));
590
+ let $joinAt = tr.doc.resolve(end + 2 * conn.length);
591
+ if ($joinAt.nodeAfter && $joinAt.nodeAfter.type == before.type &&
592
+ canJoin(tr.doc, $joinAt.pos))
593
+ tr.join($joinAt.pos);
594
+ dispatch(tr.scrollIntoView());
595
+ }
596
+ return true;
597
+ }
598
+ let selAfter = after.type.spec.isolating || (dir > 0 && isolated)
599
+ ? null
600
+ : Selection.findFrom($cut, 1);
601
+ let range = selAfter && selAfter.$from.blockRange(selAfter.$to), target = range && liftTarget(range);
602
+ if (target != null && target >= $cut.depth) {
603
+ if (dispatch)
604
+ dispatch(state.tr.lift(range, target).scrollIntoView());
605
+ return true;
606
+ }
607
+ if (canDelAfter && textblockAt(after, 'start', true) &&
608
+ textblockAt(before, 'end')) {
609
+ let at = before, wrap = [];
610
+ for (;;) {
611
+ wrap.push(at);
612
+ if (at.isTextblock)
613
+ break;
614
+ at = at.lastChild;
615
+ }
616
+ let afterText = after, afterDepth = 1;
617
+ for (; !afterText.isTextblock; afterText = afterText.firstChild) {
618
+ afterDepth++;
619
+ }
620
+ if (at.canReplace(at.childCount, at.childCount, afterText.content)) {
621
+ if (dispatch) {
622
+ let end = Fragment.empty;
623
+ for (let i = wrap.length - 1; i >= 0; i--) {
624
+ end = Fragment.from(wrap[i].copy(end));
625
+ }
626
+ let tr = state.tr.step(new ReplaceAroundStep($cut.pos - wrap.length, $cut.pos + after.nodeSize, $cut.pos + afterDepth, $cut.pos + after.nodeSize - afterDepth, new Slice(end, wrap.length, 0), 0, true));
627
+ dispatch(tr.scrollIntoView());
628
+ }
629
+ return true;
630
+ }
631
+ }
632
+ return false;
633
+ }
634
+ function selectTextblockSide(side) {
635
+ return function (state, dispatch) {
636
+ let sel = state.selection, $pos = side < 0 ? sel.$from : sel.$to;
637
+ let depth = $pos.depth;
638
+ while ($pos.node(depth).isInline) {
639
+ if (!depth)
640
+ return false;
641
+ depth--;
642
+ }
643
+ if (!$pos.node(depth).isTextblock)
644
+ return false;
645
+ if (dispatch) {
646
+ dispatch(state.tr.setSelection(TextSelection.create(state.doc, side < 0 ? $pos.start(depth) : $pos.end(depth))));
647
+ }
648
+ return true;
649
+ };
650
+ }
651
+ // Parameterized commands
652
+ /// Wrap the selection in a node of the given type with the given
653
+ /// attributes.
654
+ const wrapIn = (nodeType, attrs = null) => {
655
+ return function (state, dispatch) {
656
+ let { $from, $to } = state.selection;
657
+ let range = $from.blockRange($to), wrapping = range && findWrapping(range, nodeType, attrs);
658
+ if (!wrapping)
659
+ return false;
660
+ if (dispatch)
661
+ dispatch(state.tr.wrap(range, wrapping).scrollIntoView());
662
+ return true;
663
+ };
664
+ };
665
+ /// Returns a command that tries to set the selected textblocks to the
666
+ /// given node type with the given attributes.
667
+ const setBlockType = (nodeType, attrs = null) => {
668
+ return function (state, dispatch) {
669
+ let applicable = false;
670
+ for (let i = 0; i < state.selection.ranges.length && !applicable; i++) {
671
+ let { $from: { pos: from }, $to: { pos: to } } = state.selection.ranges[i];
672
+ state.doc.nodesBetween(from, to, (node, pos) => {
673
+ if (applicable)
674
+ return false;
675
+ if (!node.isTextblock || node.hasMarkup(nodeType, attrs))
676
+ return;
677
+ if (node.type == nodeType) {
678
+ applicable = true;
679
+ }
680
+ else {
681
+ let $pos = state.doc.resolve(pos), index = $pos.index();
682
+ applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType);
683
+ }
684
+ });
685
+ }
686
+ if (!applicable)
687
+ return false;
688
+ if (dispatch) {
689
+ let tr = state.tr;
690
+ for (let i = 0; i < state.selection.ranges.length; i++) {
691
+ let { $from: { pos: from }, $to: { pos: to } } = state.selection.ranges[i];
692
+ tr.setBlockType(from, to, nodeType, attrs);
693
+ }
694
+ dispatch(tr.scrollIntoView());
695
+ }
696
+ return true;
697
+ };
698
+ };
699
+ function markApplies(doc, ranges, type, enterAtoms) {
700
+ for (let i = 0; i < ranges.length; i++) {
701
+ let { $from, $to } = ranges[i];
702
+ let can = $from.depth == 0
703
+ ? doc.inlineContent && doc.type.allowsMarkType(type)
704
+ : false;
705
+ doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
706
+ if (can ||
707
+ !enterAtoms && node.isAtom && node.isInline && pos >= $from.pos &&
708
+ pos + node.nodeSize <= $to.pos) {
709
+ return false;
710
+ }
711
+ can = node.inlineContent && node.type.allowsMarkType(type);
712
+ });
713
+ if (can)
714
+ return true;
715
+ }
716
+ return false;
717
+ }
718
+ function removeInlineAtoms(ranges) {
719
+ let result = [];
720
+ for (let i = 0; i < ranges.length; i++) {
721
+ let { $from, $to } = ranges[i];
722
+ $from.doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
723
+ if (node.isAtom && node.content.size && node.isInline && pos >= $from.pos &&
724
+ pos + node.nodeSize <= $to.pos) {
725
+ if (pos + 1 > $from.pos) {
726
+ result.push(new SelectionRange($from, $from.doc.resolve(pos + 1)));
727
+ }
728
+ $from = $from.doc.resolve(pos + 1 + node.content.size);
729
+ return false;
730
+ }
731
+ });
732
+ if ($from.pos < $to.pos)
733
+ result.push(new SelectionRange($from, $to));
734
+ }
735
+ return result;
736
+ }
737
+ /// Create a command function that toggles the given mark with the
738
+ /// given attributes. Will return `false` when the current selection
739
+ /// doesn't support that mark. This will remove the mark if any marks
740
+ /// of that type exist in the selection, or add it otherwise. If the
741
+ /// selection is empty, this applies to the [stored
742
+ /// marks](#state.EditorState.storedMarks) instead of a range of the
743
+ /// document.
744
+ function toggleMark(markType, attrs = null, options) {
745
+ let removeWhenPresent = (options && options.removeWhenPresent) !== false;
746
+ let enterAtoms = (options && options.enterInlineAtoms) !== false;
747
+ let dropSpace = !(options && options.includeWhitespace);
748
+ return function (state, dispatch) {
749
+ let { empty, $cursor, ranges } = state.selection;
750
+ if ((empty && !$cursor) ||
751
+ !markApplies(state.doc, ranges, markType, enterAtoms))
752
+ return false;
753
+ if (dispatch) {
754
+ if ($cursor) {
755
+ if (markType.isInSet(state.storedMarks || $cursor.marks())) {
756
+ dispatch(state.tr.removeStoredMark(markType));
757
+ }
758
+ else {
759
+ dispatch(state.tr.addStoredMark(markType.create(attrs)));
760
+ }
761
+ }
762
+ else {
763
+ let add, tr = state.tr;
764
+ if (!enterAtoms)
765
+ ranges = removeInlineAtoms(ranges);
766
+ if (removeWhenPresent) {
767
+ add = !ranges.some((r) => state.doc.rangeHasMark(r.$from.pos, r.$to.pos, markType));
768
+ }
769
+ else {
770
+ add = !ranges.every((r) => {
771
+ let missing = false;
772
+ tr.doc.nodesBetween(r.$from.pos, r.$to.pos, (node, pos, parent) => {
773
+ if (missing)
774
+ return false;
775
+ missing = !markType.isInSet(node.marks) && !!parent &&
776
+ parent.type.allowsMarkType(markType) &&
777
+ !(node.isText &&
778
+ /^\s*$/.test(node.textBetween(Math.max(0, r.$from.pos - pos), Math.min(node.nodeSize, r.$to.pos - pos))));
779
+ });
780
+ return !missing;
781
+ });
782
+ }
783
+ for (let i = 0; i < ranges.length; i++) {
784
+ let { $from, $to } = ranges[i];
785
+ if (!add) {
786
+ tr.removeMark($from.pos, $to.pos, markType);
787
+ }
788
+ else {
789
+ let from = $from.pos, to = $to.pos, start = $from.nodeAfter, end = $to.nodeBefore;
790
+ let spaceStart = dropSpace && start && start.isText
791
+ ? /^\s*/.exec(start.text)[0].length
792
+ : 0;
793
+ let spaceEnd = dropSpace && end && end.isText
794
+ ? /\s*$/.exec(end.text)[0].length
795
+ : 0;
796
+ if (from + spaceStart < to) {
797
+ from += spaceStart;
798
+ to -= spaceEnd;
799
+ }
800
+ tr.addMark(from, to, markType.create(attrs));
801
+ }
802
+ }
803
+ dispatch(tr.scrollIntoView());
804
+ }
805
+ }
806
+ return true;
807
+ };
808
+ }
809
+ export const baseCommandFactories = {
810
+ wrapInList,
811
+ wrapRangeInList,
812
+ deleteSelection,
813
+ joinBackward,
814
+ joinTextblockBackward,
815
+ joinTextblockForward,
816
+ joinForward,
817
+ joinUp,
818
+ joinDown,
819
+ lift,
820
+ newlineInCode,
821
+ exitCode,
822
+ createParagraphNear,
823
+ liftEmptyBlock,
824
+ splitBlockAs,
825
+ splitBlock,
826
+ splitBlockKeepMarks,
827
+ selectAll,
828
+ selectParentNode,
829
+ selectNodeBackward,
830
+ selectNodeForward,
831
+ selectTextblockStart: () => selectTextblockSide(-1),
832
+ selectTextblockEnd: () => selectTextblockSide(1),
833
+ wrapIn,
834
+ setBlockType,
835
+ toggleMark,
836
+ };