@atlaskit/editor-plugin-list 0.2.0 → 1.1.0

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 (181) hide show
  1. package/.eslintrc.js +14 -0
  2. package/CHANGELOG.md +16 -0
  3. package/README.md +1 -1
  4. package/dist/cjs/actions/conversions.js +153 -0
  5. package/dist/cjs/actions/indent-list-items-selected.js +125 -0
  6. package/dist/cjs/actions/indent-list.js +49 -0
  7. package/dist/cjs/actions/join-list-items-forward.js +59 -0
  8. package/dist/cjs/actions/join-list-items-scenarios/index.js +40 -0
  9. package/dist/cjs/actions/join-list-items-scenarios/join-list-item-with-paragraph.js +88 -0
  10. package/dist/cjs/actions/join-list-items-scenarios/join-list-item-with-parent-nested-list.js +85 -0
  11. package/dist/cjs/actions/join-list-items-scenarios/join-nested-list-with-parent-list-item.js +79 -0
  12. package/dist/cjs/actions/join-list-items-scenarios/join-paragraph-with-list.js +45 -0
  13. package/dist/cjs/actions/join-list-items-scenarios/join-sibling-list-items.js +56 -0
  14. package/dist/cjs/actions/merge-lists.js +27 -0
  15. package/dist/cjs/actions/outdent-list-items-selected.js +289 -0
  16. package/dist/cjs/actions/wrap-and-join-lists.js +100 -0
  17. package/dist/cjs/commands/indent-list.js +69 -0
  18. package/dist/cjs/commands/index.js +349 -0
  19. package/dist/cjs/commands/isFirstChildOfParent.js +12 -0
  20. package/dist/cjs/commands/join-list-item-forward.js +61 -0
  21. package/dist/cjs/commands/listBackspace.js +284 -0
  22. package/dist/cjs/commands/outdent-list.js +69 -0
  23. package/dist/cjs/index.js +8 -1
  24. package/dist/cjs/messages.js +37 -0
  25. package/dist/cjs/plugin.js +137 -0
  26. package/dist/cjs/pm-plugins/input-rules/create-list-input-rule.js +63 -0
  27. package/dist/cjs/pm-plugins/input-rules/index.js +38 -0
  28. package/dist/cjs/pm-plugins/input-rules/wrapping-join-rule.js +60 -0
  29. package/dist/cjs/pm-plugins/keymap.js +27 -0
  30. package/dist/cjs/pm-plugins/main.js +166 -0
  31. package/dist/cjs/transforms.js +99 -0
  32. package/dist/cjs/types.js +4 -1
  33. package/dist/cjs/utils/analytics.js +22 -0
  34. package/dist/cjs/utils/find.js +68 -0
  35. package/dist/cjs/utils/indentation.js +22 -0
  36. package/dist/cjs/utils/mark.js +40 -0
  37. package/dist/cjs/utils/node.js +16 -0
  38. package/dist/cjs/utils/selection.js +95 -0
  39. package/dist/es2019/actions/conversions.js +160 -0
  40. package/dist/es2019/actions/indent-list-items-selected.js +124 -0
  41. package/dist/es2019/actions/indent-list.js +44 -0
  42. package/dist/es2019/actions/join-list-items-forward.js +54 -0
  43. package/dist/es2019/actions/join-list-items-scenarios/index.js +5 -0
  44. package/dist/es2019/actions/join-list-items-scenarios/join-list-item-with-paragraph.js +74 -0
  45. package/dist/es2019/actions/join-list-items-scenarios/join-list-item-with-parent-nested-list.js +77 -0
  46. package/dist/es2019/actions/join-list-items-scenarios/join-nested-list-with-parent-list-item.js +71 -0
  47. package/dist/es2019/actions/join-list-items-scenarios/join-paragraph-with-list.js +37 -0
  48. package/dist/es2019/actions/join-list-items-scenarios/join-sibling-list-items.js +48 -0
  49. package/dist/es2019/actions/merge-lists.js +24 -0
  50. package/dist/es2019/actions/outdent-list-items-selected.js +293 -0
  51. package/dist/es2019/actions/wrap-and-join-lists.js +93 -0
  52. package/dist/es2019/commands/indent-list.js +61 -0
  53. package/dist/es2019/commands/index.js +322 -0
  54. package/dist/es2019/commands/isFirstChildOfParent.js +7 -0
  55. package/dist/es2019/commands/join-list-item-forward.js +53 -0
  56. package/dist/es2019/commands/listBackspace.js +276 -0
  57. package/dist/es2019/commands/outdent-list.js +60 -0
  58. package/dist/es2019/index.js +1 -1
  59. package/dist/es2019/messages.js +29 -0
  60. package/dist/es2019/plugin.js +123 -0
  61. package/dist/es2019/pm-plugins/input-rules/create-list-input-rule.js +56 -0
  62. package/dist/es2019/pm-plugins/input-rules/index.js +35 -0
  63. package/dist/es2019/pm-plugins/input-rules/wrapping-join-rule.js +55 -0
  64. package/dist/es2019/pm-plugins/keymap.js +19 -0
  65. package/dist/es2019/pm-plugins/main.js +156 -0
  66. package/dist/es2019/transforms.js +101 -0
  67. package/dist/es2019/types.js +1 -1
  68. package/dist/es2019/utils/analytics.js +12 -0
  69. package/dist/es2019/utils/find.js +61 -0
  70. package/dist/es2019/utils/indentation.js +15 -0
  71. package/dist/es2019/utils/mark.js +30 -0
  72. package/dist/es2019/utils/node.js +12 -0
  73. package/dist/es2019/utils/selection.js +96 -0
  74. package/dist/esm/actions/conversions.js +147 -0
  75. package/dist/esm/actions/indent-list-items-selected.js +117 -0
  76. package/dist/esm/actions/indent-list.js +43 -0
  77. package/dist/esm/actions/join-list-items-forward.js +52 -0
  78. package/dist/esm/actions/join-list-items-scenarios/index.js +5 -0
  79. package/dist/esm/actions/join-list-items-scenarios/join-list-item-with-paragraph.js +81 -0
  80. package/dist/esm/actions/join-list-items-scenarios/join-list-item-with-parent-nested-list.js +78 -0
  81. package/dist/esm/actions/join-list-items-scenarios/join-nested-list-with-parent-list-item.js +72 -0
  82. package/dist/esm/actions/join-list-items-scenarios/join-paragraph-with-list.js +38 -0
  83. package/dist/esm/actions/join-list-items-scenarios/join-sibling-list-items.js +49 -0
  84. package/dist/esm/actions/merge-lists.js +21 -0
  85. package/dist/esm/actions/outdent-list-items-selected.js +281 -0
  86. package/dist/esm/actions/wrap-and-join-lists.js +94 -0
  87. package/dist/esm/commands/indent-list.js +61 -0
  88. package/dist/esm/commands/index.js +323 -0
  89. package/dist/esm/commands/isFirstChildOfParent.js +5 -0
  90. package/dist/esm/commands/join-list-item-forward.js +53 -0
  91. package/dist/esm/commands/listBackspace.js +275 -0
  92. package/dist/esm/commands/outdent-list.js +61 -0
  93. package/dist/esm/index.js +1 -1
  94. package/dist/esm/messages.js +29 -0
  95. package/dist/esm/plugin.js +130 -0
  96. package/dist/esm/pm-plugins/input-rules/create-list-input-rule.js +57 -0
  97. package/dist/esm/pm-plugins/input-rules/index.js +32 -0
  98. package/dist/esm/pm-plugins/input-rules/wrapping-join-rule.js +54 -0
  99. package/dist/esm/pm-plugins/keymap.js +19 -0
  100. package/dist/esm/pm-plugins/main.js +156 -0
  101. package/dist/esm/transforms.js +91 -0
  102. package/dist/esm/types.js +1 -1
  103. package/dist/esm/utils/analytics.js +12 -0
  104. package/dist/esm/utils/find.js +59 -0
  105. package/dist/esm/utils/indentation.js +15 -0
  106. package/dist/esm/utils/mark.js +33 -0
  107. package/dist/esm/utils/node.js +10 -0
  108. package/dist/esm/utils/selection.js +81 -0
  109. package/dist/types/actions/conversions.d.ts +6 -0
  110. package/dist/types/actions/indent-list-items-selected.d.ts +2 -0
  111. package/dist/types/actions/indent-list.d.ts +2 -0
  112. package/dist/types/actions/join-list-items-forward.d.ts +13 -0
  113. package/dist/types/actions/join-list-items-scenarios/index.d.ts +5 -0
  114. package/dist/types/actions/join-list-items-scenarios/join-list-item-with-paragraph.d.ts +9 -0
  115. package/dist/types/actions/join-list-items-scenarios/join-list-item-with-parent-nested-list.d.ts +9 -0
  116. package/dist/types/actions/join-list-items-scenarios/join-nested-list-with-parent-list-item.d.ts +9 -0
  117. package/dist/types/actions/join-list-items-scenarios/join-paragraph-with-list.d.ts +9 -0
  118. package/dist/types/actions/join-list-items-scenarios/join-sibling-list-items.d.ts +9 -0
  119. package/dist/types/actions/merge-lists.d.ts +7 -0
  120. package/dist/types/actions/outdent-list-items-selected.d.ts +3 -0
  121. package/dist/types/actions/wrap-and-join-lists.d.ts +17 -0
  122. package/dist/types/commands/indent-list.d.ts +6 -0
  123. package/dist/types/commands/index.d.ts +15 -0
  124. package/dist/types/commands/isFirstChildOfParent.d.ts +2 -0
  125. package/dist/types/commands/join-list-item-forward.d.ts +3 -0
  126. package/dist/types/commands/listBackspace.d.ts +10 -0
  127. package/dist/types/commands/outdent-list.d.ts +6 -0
  128. package/dist/types/index.d.ts +2 -1
  129. package/dist/types/messages.d.ts +27 -0
  130. package/dist/types/plugin.d.ts +2 -0
  131. package/dist/types/pm-plugins/input-rules/create-list-input-rule.d.ts +11 -0
  132. package/dist/types/pm-plugins/input-rules/index.d.ts +5 -0
  133. package/dist/types/pm-plugins/input-rules/wrapping-join-rule.d.ts +13 -0
  134. package/dist/types/pm-plugins/keymap.d.ts +5 -0
  135. package/dist/types/pm-plugins/main.d.ts +11 -0
  136. package/dist/types/transforms.d.ts +4 -0
  137. package/dist/types/types.d.ts +13 -13
  138. package/dist/types/utils/analytics.d.ts +5 -0
  139. package/dist/types/utils/find.d.ts +10 -0
  140. package/dist/types/utils/indentation.d.ts +2 -0
  141. package/dist/types/utils/mark.d.ts +8 -0
  142. package/dist/types/utils/node.d.ts +2 -0
  143. package/dist/types/utils/selection.d.ts +14 -0
  144. package/dist/types-ts4.5/actions/conversions.d.ts +6 -0
  145. package/dist/types-ts4.5/actions/indent-list-items-selected.d.ts +2 -0
  146. package/dist/types-ts4.5/actions/indent-list.d.ts +2 -0
  147. package/dist/types-ts4.5/actions/join-list-items-forward.d.ts +16 -0
  148. package/dist/types-ts4.5/actions/join-list-items-scenarios/index.d.ts +5 -0
  149. package/dist/types-ts4.5/actions/join-list-items-scenarios/join-list-item-with-paragraph.d.ts +9 -0
  150. package/dist/types-ts4.5/actions/join-list-items-scenarios/join-list-item-with-parent-nested-list.d.ts +9 -0
  151. package/dist/types-ts4.5/actions/join-list-items-scenarios/join-nested-list-with-parent-list-item.d.ts +9 -0
  152. package/dist/types-ts4.5/actions/join-list-items-scenarios/join-paragraph-with-list.d.ts +9 -0
  153. package/dist/types-ts4.5/actions/join-list-items-scenarios/join-sibling-list-items.d.ts +9 -0
  154. package/dist/types-ts4.5/actions/merge-lists.d.ts +7 -0
  155. package/dist/types-ts4.5/actions/outdent-list-items-selected.d.ts +3 -0
  156. package/dist/types-ts4.5/actions/wrap-and-join-lists.d.ts +17 -0
  157. package/dist/types-ts4.5/commands/indent-list.d.ts +6 -0
  158. package/dist/types-ts4.5/commands/index.d.ts +15 -0
  159. package/dist/types-ts4.5/commands/isFirstChildOfParent.d.ts +2 -0
  160. package/dist/types-ts4.5/commands/join-list-item-forward.d.ts +3 -0
  161. package/dist/types-ts4.5/commands/listBackspace.d.ts +13 -0
  162. package/dist/types-ts4.5/commands/outdent-list.d.ts +6 -0
  163. package/dist/types-ts4.5/index.d.ts +2 -1
  164. package/dist/types-ts4.5/messages.d.ts +27 -0
  165. package/dist/types-ts4.5/plugin.d.ts +2 -0
  166. package/dist/types-ts4.5/pm-plugins/input-rules/create-list-input-rule.d.ts +11 -0
  167. package/dist/types-ts4.5/pm-plugins/input-rules/index.d.ts +5 -0
  168. package/dist/types-ts4.5/pm-plugins/input-rules/wrapping-join-rule.d.ts +13 -0
  169. package/dist/types-ts4.5/pm-plugins/keymap.d.ts +5 -0
  170. package/dist/types-ts4.5/pm-plugins/main.d.ts +11 -0
  171. package/dist/types-ts4.5/transforms.d.ts +4 -0
  172. package/dist/types-ts4.5/types.d.ts +13 -13
  173. package/dist/types-ts4.5/utils/analytics.d.ts +5 -0
  174. package/dist/types-ts4.5/utils/find.d.ts +10 -0
  175. package/dist/types-ts4.5/utils/indentation.d.ts +2 -0
  176. package/dist/types-ts4.5/utils/mark.d.ts +8 -0
  177. package/dist/types-ts4.5/utils/node.d.ts +2 -0
  178. package/dist/types-ts4.5/utils/selection.d.ts +14 -0
  179. package/package.json +8 -5
  180. package/report.api.md +17 -18
  181. package/tmp/api-report-tmp.d.ts +15 -11
@@ -0,0 +1,322 @@
1
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
2
+ import { findCutBefore } from '@atlaskit/editor-common/commands';
3
+ import { getCommonListAnalyticsAttributes, moveTargetIntoList } from '@atlaskit/editor-common/lists';
4
+ import { editorCommandToPMCommand } from '@atlaskit/editor-common/preset';
5
+ import { GapCursorSelection } from '@atlaskit/editor-common/selection';
6
+ import { filterCommand as filter, hasVisibleContent, isEmptySelectionAtStart } from '@atlaskit/editor-common/utils';
7
+ import { chainCommands } from '@atlaskit/editor-prosemirror/commands';
8
+ import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
9
+ import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
10
+ import { findPositionOfNodeBefore, hasParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
11
+ import { convertListType } from '../actions/conversions';
12
+ import { wrapInListAndJoin } from '../actions/wrap-and-join-lists';
13
+ import { liftFollowingList, liftNodeSelectionList, liftTextSelectionList } from '../transforms';
14
+ import { sanitiseMarksInSelection } from '../utils/mark';
15
+ import { canJoinToPreviousListItem, isInsideListItem, selectionContainsList } from '../utils/selection';
16
+ import { indentList } from './indent-list';
17
+ import { isFirstChildOfParent } from './isFirstChildOfParent';
18
+ import { joinListItemForward } from './join-list-item-forward';
19
+ import { listBackspace } from './listBackspace';
20
+ import { outdentList } from './outdent-list';
21
+ export { outdentList, indentList };
22
+ export const enterKeyCommand = editorAnalyticsAPI => featureFlags => (state, dispatch) => {
23
+ const {
24
+ selection
25
+ } = state;
26
+ if (selection.empty) {
27
+ const {
28
+ $from
29
+ } = selection;
30
+ const {
31
+ listItem,
32
+ codeBlock
33
+ } = state.schema.nodes;
34
+ const wrapper = $from.node($from.depth - 1);
35
+ if (wrapper && wrapper.type === listItem) {
36
+ /** Check if the wrapper has any visible content */
37
+ const wrapperHasContent = hasVisibleContent(wrapper);
38
+ if (!wrapperHasContent) {
39
+ return editorCommandToPMCommand(outdentList(editorAnalyticsAPI)(INPUT_METHOD.KEYBOARD, featureFlags))(state, dispatch);
40
+ } else if (!hasParentNodeOfType(codeBlock)(selection)) {
41
+ return splitListItem(listItem)(state, dispatch);
42
+ }
43
+ }
44
+ }
45
+ return false;
46
+ };
47
+ export const backspaceKeyCommand = editorAnalyticsAPI => featureFlags => (state, dispatch) => {
48
+ return chainCommands(listBackspace(editorAnalyticsAPI),
49
+ // if we're at the start of a list item, we need to either backspace
50
+ // directly to an empty list item above, or outdent this node
51
+ filter([isEmptySelectionAtStart,
52
+ // list items might have multiple paragraphs; only do this at the first one
53
+ isFirstChildOfParent, state => isInsideListItem(state.tr)], chainCommands(deletePreviousEmptyListItem, editorCommandToPMCommand(outdentList(editorAnalyticsAPI)(INPUT_METHOD.KEYBOARD, featureFlags)))),
54
+ // if we're just inside a paragraph node (or gapcursor is shown) and backspace, then try to join
55
+ // the text to the previous list item, if one exists
56
+ filter([isEmptySelectionAtStart, state => canJoinToPreviousListItem(state.tr)], joinToPreviousListItem))(state, dispatch);
57
+ };
58
+ export const deleteKeyCommand = editorAnalyticsAPI => joinListItemForward(editorAnalyticsAPI);
59
+
60
+ // Get the depth of the nearest ancestor list
61
+ export const rootListDepth = (pos, nodes) => {
62
+ const {
63
+ bulletList,
64
+ orderedList,
65
+ listItem
66
+ } = nodes;
67
+ let depth;
68
+ for (let i = pos.depth - 1; i > 0; i--) {
69
+ const node = pos.node(i);
70
+ if (node.type === bulletList || node.type === orderedList) {
71
+ depth = i;
72
+ }
73
+ if (node.type !== bulletList && node.type !== orderedList && node.type !== listItem) {
74
+ break;
75
+ }
76
+ }
77
+ return depth;
78
+ };
79
+ function untoggleSelectedList(tr) {
80
+ const {
81
+ selection
82
+ } = tr;
83
+ const depth = rootListDepth(selection.$to, tr.doc.type.schema.nodes);
84
+ tr = liftFollowingList(selection.$to.pos, selection.$to.end(depth), depth || 0, tr);
85
+ if (selection instanceof NodeSelection || selection instanceof GapCursorSelection) {
86
+ return liftNodeSelectionList(selection, tr);
87
+ }
88
+ return liftTextSelectionList(selection, tr);
89
+ }
90
+ export const toggleList = editorAnalyticsAPI => (inputMethod, listType) => {
91
+ return function ({
92
+ tr
93
+ }) {
94
+ const listInsideSelection = selectionContainsList(tr);
95
+ const listNodeType = tr.doc.type.schema.nodes[listType];
96
+ const actionSubjectId = listType === 'bulletList' ? ACTION_SUBJECT_ID.FORMAT_LIST_BULLET : ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
97
+ if (listInsideSelection) {
98
+ const {
99
+ selection
100
+ } = tr;
101
+
102
+ // for gap cursor or node selection - list is expected 1 level up (listItem -> list)
103
+ // for text selection - list is expected 2 levels up (paragraph -> listItem -> list)
104
+ const positionDiff = selection instanceof GapCursorSelection || selection instanceof NodeSelection ? 1 : 2;
105
+ const fromNode = selection.$from.node(selection.$from.depth - positionDiff);
106
+ const toNode = selection.$to.node(selection.$to.depth - positionDiff);
107
+ const transformedFrom = listInsideSelection.type.name === 'bulletList' ? ACTION_SUBJECT_ID.FORMAT_LIST_BULLET : ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
108
+ if ((fromNode === null || fromNode === void 0 ? void 0 : fromNode.type.name) === listType && (toNode === null || toNode === void 0 ? void 0 : toNode.type.name) === listType) {
109
+ const commonAttributes = getCommonListAnalyticsAttributes(tr);
110
+ untoggleSelectedList(tr);
111
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
112
+ action: ACTION.CONVERTED,
113
+ actionSubject: ACTION_SUBJECT.LIST,
114
+ actionSubjectId: ACTION_SUBJECT_ID.TEXT,
115
+ eventType: EVENT_TYPE.TRACK,
116
+ attributes: {
117
+ ...commonAttributes,
118
+ transformedFrom,
119
+ inputMethod
120
+ }
121
+ })(tr);
122
+ return tr;
123
+ }
124
+ convertListType({
125
+ tr,
126
+ nextListNodeType: listNodeType
127
+ });
128
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
129
+ action: ACTION.CONVERTED,
130
+ actionSubject: ACTION_SUBJECT.LIST,
131
+ actionSubjectId,
132
+ eventType: EVENT_TYPE.TRACK,
133
+ attributes: {
134
+ ...getCommonListAnalyticsAttributes(tr),
135
+ transformedFrom,
136
+ inputMethod
137
+ }
138
+ })(tr);
139
+ } else {
140
+ // Need to have this before wrapInList so the wrapping is done with valid content
141
+ // For example, if trying to convert centre or right aligned paragraphs to lists
142
+ sanitiseMarksInSelection(tr, listNodeType);
143
+ wrapInListAndJoin(listNodeType, tr);
144
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
145
+ action: ACTION.INSERTED,
146
+ actionSubject: ACTION_SUBJECT.LIST,
147
+ actionSubjectId,
148
+ eventType: EVENT_TYPE.TRACK,
149
+ attributes: {
150
+ inputMethod
151
+ }
152
+ })(tr);
153
+ }
154
+
155
+ // If document wasn't changed, return false from the command to indicate that the
156
+ // editing action failed
157
+ if (!tr.docChanged) {
158
+ return null;
159
+ }
160
+ return tr;
161
+ };
162
+ };
163
+ export const toggleBulletList = editorAnalyticsAPI => (inputMethod = INPUT_METHOD.TOOLBAR) => {
164
+ return toggleList(editorAnalyticsAPI)(inputMethod, 'bulletList');
165
+ };
166
+ export const toggleOrderedList = editorAnalyticsAPI => (inputMethod = INPUT_METHOD.TOOLBAR) => {
167
+ return toggleList(editorAnalyticsAPI)(inputMethod, 'orderedList');
168
+ };
169
+
170
+ /**
171
+ * Implementation taken and modified for our needs from PM
172
+ * @param itemType Node
173
+ * Splits the list items, specific implementation take from PM
174
+ */
175
+ function splitListItem(itemType) {
176
+ return function (state, dispatch) {
177
+ const ref = state.selection;
178
+ const $from = ref.$from;
179
+ const $to = ref.$to;
180
+ const node = ref.node;
181
+ if (node && node.isBlock || $from.depth < 2 || !$from.sameParent($to)) {
182
+ return false;
183
+ }
184
+ const grandParent = $from.node(-1);
185
+ if (grandParent.type !== itemType) {
186
+ return false;
187
+ }
188
+ /** --> The following line changed from the original PM implementation to allow list additions with multiple paragraphs */
189
+ if (
190
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
191
+ grandParent.content.content.length <= 1 && $from.parent.content.size === 0 && !(grandParent.content.size === 0)) {
192
+ // In an empty block. If this is a nested list, the wrapping
193
+ // list item should be split. Otherwise, bail out and let next
194
+ // command handle lifting.
195
+ if ($from.depth === 2 || $from.node(-3).type !== itemType || $from.index(-2) !== $from.node(-2).childCount - 1) {
196
+ return false;
197
+ }
198
+ if (dispatch) {
199
+ let wrap = Fragment.empty;
200
+ const keepItem = $from.index(-1) > 0;
201
+ // Build a fragment containing empty versions of the structure
202
+ // from the outer list item to the parent node of the cursor
203
+ for (let d = $from.depth - (keepItem ? 1 : 2); d >= $from.depth - 3; d--) {
204
+ wrap = Fragment.from($from.node(d).copy(wrap));
205
+ }
206
+ // Add a second list item with an empty default start node
207
+ wrap = wrap.append(Fragment.from(itemType.createAndFill()));
208
+ const tr$1 = state.tr.replace($from.before(keepItem ? undefined : -1), $from.after(-3), new Slice(wrap, keepItem ? 3 : 2, 2));
209
+ tr$1.setSelection(
210
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
211
+ state.selection.constructor.near(tr$1.doc.resolve($from.pos + (keepItem ? 3 : 2))));
212
+ dispatch(tr$1.scrollIntoView());
213
+ }
214
+ return true;
215
+ }
216
+ const nextType = $to.pos === $from.end() ? grandParent.contentMatchAt(0).defaultType : null;
217
+ const tr = state.tr.delete($from.pos, $to.pos);
218
+ const types = nextType && [null, {
219
+ type: nextType
220
+ }];
221
+ if (dispatch) {
222
+ dispatch(tr.split($from.pos, 2, types !== null && types !== void 0 ? types : undefined).scrollIntoView());
223
+ }
224
+ return true;
225
+ };
226
+ }
227
+ const deletePreviousEmptyListItem = (state, dispatch) => {
228
+ const {
229
+ $from
230
+ } = state.selection;
231
+ const {
232
+ listItem
233
+ } = state.schema.nodes;
234
+ const $cut = findCutBefore($from);
235
+ if (!$cut || !$cut.nodeBefore || !($cut.nodeBefore.type === listItem)) {
236
+ return false;
237
+ }
238
+ const previousListItemEmpty = $cut.nodeBefore.childCount === 1 && $cut.nodeBefore.firstChild.nodeSize <= 2;
239
+ if (previousListItemEmpty) {
240
+ const {
241
+ tr
242
+ } = state;
243
+ if (dispatch) {
244
+ dispatch(tr.delete($cut.pos - $cut.nodeBefore.nodeSize, $from.pos).scrollIntoView());
245
+ }
246
+ return true;
247
+ }
248
+ return false;
249
+ };
250
+ const joinToPreviousListItem = (state, dispatch) => {
251
+ const {
252
+ $from
253
+ } = state.selection;
254
+ const {
255
+ paragraph,
256
+ listItem,
257
+ codeBlock,
258
+ bulletList,
259
+ orderedList
260
+ } = state.schema.nodes;
261
+ const isGapCursorShown = state.selection instanceof GapCursorSelection;
262
+ const $cutPos = isGapCursorShown ? state.doc.resolve($from.pos + 1) : $from;
263
+ let $cut = findCutBefore($cutPos);
264
+ if (!$cut) {
265
+ return false;
266
+ }
267
+
268
+ // see if the containing node is a list
269
+ if ($cut.nodeBefore && [bulletList, orderedList].indexOf($cut.nodeBefore.type) > -1) {
270
+ // and the node after this is a paragraph or a codeBlock
271
+ if ($cut.nodeAfter && ($cut.nodeAfter.type === paragraph || $cut.nodeAfter.type === codeBlock)) {
272
+ // find the nearest paragraph that precedes this node
273
+ let $lastNode = $cut.doc.resolve($cut.pos - 1);
274
+ while ($lastNode.parent.type !== paragraph) {
275
+ $lastNode = state.doc.resolve($lastNode.pos - 1);
276
+ }
277
+ let {
278
+ tr
279
+ } = state;
280
+ if (isGapCursorShown) {
281
+ const nodeBeforePos = findPositionOfNodeBefore(tr.selection);
282
+ if (typeof nodeBeforePos !== 'number') {
283
+ return false;
284
+ }
285
+ // append the codeblock to the list node
286
+ const list = $cut.nodeBefore.copy($cut.nodeBefore.content.append(Fragment.from(listItem.createChecked({}, $cut.nodeAfter))));
287
+ tr.replaceWith(nodeBeforePos, $from.pos + $cut.nodeAfter.nodeSize, list);
288
+ } else {
289
+ const step = moveTargetIntoList({
290
+ insertPosition: $lastNode.pos,
291
+ $target: $cut
292
+ });
293
+
294
+ // ED-13966: check if the step will cause an ProseMirror error
295
+ // if there's an error don't apply the step as it will might lead into a data loss.
296
+ // It doesn't play well with media being a leaf node.
297
+ const stepResult = state.tr.maybeStep(step);
298
+ if (stepResult.failed) {
299
+ return false;
300
+ } else {
301
+ tr = state.tr.step(step);
302
+ }
303
+ }
304
+
305
+ // find out if there's now another list following and join them
306
+ // as in, [list, p, list] => [list with p, list], and we want [joined list]
307
+ let $postCut = tr.doc.resolve(tr.mapping.map($cut.pos + $cut.nodeAfter.nodeSize));
308
+ if ($postCut.nodeBefore && $postCut.nodeAfter && $postCut.nodeBefore.type === $postCut.nodeAfter.type && [bulletList, orderedList].indexOf($postCut.nodeBefore.type) > -1) {
309
+ tr = tr.join($postCut.pos);
310
+ }
311
+ if (dispatch) {
312
+ var _tr$doc$resolve$nodeB;
313
+ if (!((_tr$doc$resolve$nodeB = tr.doc.resolve($lastNode.pos).nodeBefore) !== null && _tr$doc$resolve$nodeB !== void 0 && _tr$doc$resolve$nodeB.isBlock) || tr.doc.resolve($lastNode.pos).nodeBefore === null) {
314
+ tr = tr.setSelection(TextSelection.near(tr.doc.resolve(tr.mapping.map($cut.pos)), -1));
315
+ }
316
+ dispatch(tr.scrollIntoView());
317
+ }
318
+ return true;
319
+ }
320
+ }
321
+ return false;
322
+ };
@@ -0,0 +1,7 @@
1
+ import { GapCursorSelection } from '@atlaskit/editor-common/selection';
2
+ export const isFirstChildOfParent = state => {
3
+ const {
4
+ $from
5
+ } = state.selection;
6
+ return $from.depth > 1 ? state.selection instanceof GapCursorSelection && $from.parentOffset === 0 || $from.index($from.depth - 1) === 0 : true;
7
+ };
@@ -0,0 +1,53 @@
1
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, DELETE_DIRECTION, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
2
+ import { isEmptySelectionAtEnd, walkNextNode } from '@atlaskit/editor-common/utils';
3
+ import { findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
4
+ import { calcJoinListScenario } from '../actions/join-list-items-forward';
5
+ export const joinListItemForward = editorAnalyticsAPI => (state, dispatch) => {
6
+ const {
7
+ tr,
8
+ selection: {
9
+ $head
10
+ }
11
+ } = state;
12
+ const walkNode = walkNextNode($head);
13
+ if (!isEmptySelectionAtEnd(state)) {
14
+ return false;
15
+ }
16
+ const scenarios = calcJoinListScenario(walkNode, $head);
17
+ if (!scenarios) {
18
+ return false;
19
+ }
20
+ const [scenario, action] = scenarios;
21
+ const result = action({
22
+ tr,
23
+ $next: walkNode.$pos,
24
+ $head: $head
25
+ });
26
+ if (!result) {
27
+ return false;
28
+ }
29
+ const {
30
+ bulletList,
31
+ orderedList
32
+ } = state.schema.nodes;
33
+ const listParent = findParentNodeOfType([bulletList, orderedList])(tr.selection);
34
+ let actionSubjectId = ACTION_SUBJECT_ID.FORMAT_LIST_BULLET;
35
+ if (listParent && listParent.node.type === orderedList) {
36
+ actionSubjectId = ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
37
+ }
38
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
39
+ action: ACTION.LIST_ITEM_JOINED,
40
+ actionSubject: ACTION_SUBJECT.LIST,
41
+ actionSubjectId,
42
+ eventType: EVENT_TYPE.TRACK,
43
+ attributes: {
44
+ inputMethod: INPUT_METHOD.KEYBOARD,
45
+ direction: DELETE_DIRECTION.FORWARD,
46
+ scenario
47
+ }
48
+ })(tr);
49
+ if (dispatch) {
50
+ dispatch(tr);
51
+ }
52
+ return true;
53
+ };
@@ -0,0 +1,276 @@
1
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, DELETE_DIRECTION, EVENT_TYPE, INPUT_METHOD, LIST_TEXT_SCENARIOS } from '@atlaskit/editor-common/analytics';
2
+ import { insertContentDeleteRange, isEmptySelectionAtStart, isListNode, isParagraphNode, walkPrevNode } from '@atlaskit/editor-common/utils';
3
+ import { findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
4
+ import { isPosInsideList, isPosInsideParagraph } from '../utils/selection';
5
+ //Cases below refer to the cases found in this document: https://product-fabric.atlassian.net/wiki/spaces/E/pages/1146954996/List+Backspace+and+Delete+Behaviour
6
+ //Case for two adjacent list items of the same indentation
7
+ const listBackspaceCase2 = (tr, dispatch, $prev, $head) => {
8
+ /* CASE 2
9
+ * Initial Structure:
10
+ *
11
+ * List A {
12
+ * ListItem B {
13
+ * ...Children C
14
+ * Paragraph D { text1 |textInsertPos| } //Cant have children since that would be Case 4
15
+ * |$prev||childrenGInsertPos| }
16
+ * ListItem E {
17
+ * Paragraph F { |$head| text2 }
18
+ * ...Children G
19
+ * }
20
+ * }
21
+ *
22
+ * Converts to:
23
+ *
24
+ * List A {
25
+ * ListItem B {
26
+ * ...Children C
27
+ * Paragraph C { text1text2 }
28
+ * ...Children G
29
+ * }
30
+ * }
31
+ *
32
+ */
33
+
34
+ const listItemE = $head.node(-1); //Head is inside listItem E so it must have a first and last child
35
+ if (!listItemE.firstChild) {
36
+ return false;
37
+ }
38
+ const beforeListItemE = $head.before(-1);
39
+ const afterListItemE = $head.after(-1);
40
+ const textInsertPos = $prev.pos - 1; //Paragraph D must be directly behind $prev otherwise it would be case 4
41
+ const childrenGInsertPos = $prev.pos;
42
+ const textContent = $head.parent.content;
43
+ const childrenGContent = listItemE.content.cut(listItemE.firstChild.nodeSize);
44
+ insertContentDeleteRange(tr, tr => tr.doc.resolve(textInsertPos), [[textContent, textInsertPos], [childrenGContent, childrenGInsertPos]], [[beforeListItemE, afterListItemE]]);
45
+ if (dispatch) {
46
+ dispatch(tr);
47
+ }
48
+ return true;
49
+ };
50
+
51
+ //Case for two adjacent list items with the first being of lower indentation
52
+ const listBackspaceCase3 = (tr, dispatch, $prev, $head) => {
53
+ /* CASE 3
54
+ * Initial Structure:
55
+ *
56
+ * List A {
57
+ * ListItem B {
58
+ * ...Children C
59
+ * Paragraph D { text1 |$prev||textInsertPos| } |childrenHInsertPos|
60
+ * List E { |childrenJInsertPos|
61
+ * ListItem F {
62
+ * Paragraph G { |$head| text2 }
63
+ * ...Children H
64
+ * List? I {
65
+ * ...Children J
66
+ * }
67
+ * }
68
+ * ...Children K
69
+ * }
70
+ * }
71
+ * }
72
+ *
73
+ * Converts to:
74
+ *
75
+ * List A {
76
+ * ListItem B {
77
+ * ...Children C
78
+ * Paragraph D { text1text2 }
79
+ * ...Children H
80
+ * List E {
81
+ * ...Children J
82
+ * ...Children K
83
+ * }
84
+ * }
85
+ * }
86
+ *
87
+ */
88
+
89
+ const listE = $head.node(-2);
90
+ const listItemF = $head.node(-1); //Head is inside listItem F so it must have a first and last child
91
+ if (!listItemF.firstChild || !listItemF.lastChild) {
92
+ return false;
93
+ }
94
+ const beforeListE = $head.before(-2);
95
+ const beforeListItemF = $head.before(-1);
96
+ const afterParagraphD = $prev.after();
97
+ const afterListE = $head.after(-2);
98
+ const afterListItemF = $head.after(-1);
99
+ const startListE = $head.start(-2);
100
+ const containsChildrenJ = isListNode(listItemF.lastChild);
101
+ const shouldRemoveListE = listE.childCount === 1 && !containsChildrenJ; //Assures no Children J and K
102
+ const textInsertPos = $prev.pos;
103
+ const childrenHInsertPos = afterParagraphD;
104
+ const childrenJInsertPos = startListE;
105
+ const textContent = $head.parent.content;
106
+ const childrenHContent = containsChildrenJ ? listItemF.content.cut(listItemF.firstChild.nodeSize, listItemF.nodeSize - listItemF.lastChild.nodeSize - 2) : listItemF.content.cut(listItemF.firstChild.nodeSize); //If Children J doesn't exist then Children H will include the last node
107
+ const childrenJContent = listItemF.lastChild.content; //Will be invalid if there are no Children J but it will be unused
108
+
109
+ insertContentDeleteRange(tr, tr => tr.doc.resolve(textInsertPos), containsChildrenJ ? [[textContent, textInsertPos], [childrenHContent, childrenHInsertPos], [childrenJContent, childrenJInsertPos]] : [[textContent, textInsertPos], [childrenHContent, childrenHInsertPos]], [shouldRemoveListE ? [beforeListE, afterListE] : [beforeListItemF, afterListItemF]]);
110
+ if (dispatch) {
111
+ dispatch(tr);
112
+ }
113
+ return true;
114
+ };
115
+
116
+ //Case for two adjacent list items with the first being of greater indentation
117
+ const listBackspaceCase4 = (tr, dispatch, $prev, $head, $last) => {
118
+ /* CASE 4
119
+ * Initial Structure:
120
+ *
121
+ * List A {
122
+ * ListItem B {
123
+ * Paragraph C { text1 }
124
+ * ...Children D
125
+ * List E {
126
+ * ...
127
+ * List F { //May be multiple levels of lists
128
+ * ...Children G
129
+ * ListItem H { //Last node of the block
130
+ * ...Children I
131
+ * Paragraph J { text2 |$last||textInsertPos| } |childrenMInsertPos| //Cant have children since this ListItem is the last of the block
132
+ * }
133
+ * }
134
+ * ...
135
+ * |childrenOInsertPosition| }
136
+ * |$prev| }
137
+ * ListItem K {
138
+ * Paragraph L { |$head| text3 }
139
+ * ...Children M
140
+ * List? N {
141
+ * ...Children O
142
+ * }
143
+ * }
144
+ * }
145
+ *
146
+ * Converts to:
147
+ *
148
+ * List A {
149
+ * ListItem B {
150
+ * Paragraph C { text1 }
151
+ * ...Children D
152
+ * List E {
153
+ * ...
154
+ * List F {
155
+ * ...Children G
156
+ * ListItem H {
157
+ * ...Children I
158
+ * Paragraph J { text2text3 }
159
+ * ...Children M
160
+ * }
161
+ * }
162
+ * ...
163
+ * ...Children O
164
+ * }
165
+ * }
166
+ * }
167
+ *
168
+ */
169
+
170
+ if (!$last) {
171
+ //Exit if an invalid last was given as a parameter
172
+ return false;
173
+ }
174
+ const listItemK = $head.node(-1); //Head is inside listItem K so it must have a first and last child
175
+ if (!listItemK.firstChild || !listItemK.lastChild) {
176
+ return false;
177
+ }
178
+ const paragraphL = $head.parent;
179
+ const beforeListItemK = $head.before(-1);
180
+ const afterParagraphJ = $last.after();
181
+ const afterListItemK = $head.after(-1);
182
+ const containsChildrenO = isListNode(listItemK.lastChild);
183
+ const textInsertPos = $last.pos;
184
+ const childrenMInsertPos = afterParagraphJ;
185
+ const childrenOInsertPos = $prev.pos - 1; //Last item of listItem B must be a list therefore we can simply decrement $prev to get there
186
+
187
+ const textContent = paragraphL.content;
188
+ const childrenMContent = containsChildrenO ? listItemK.content.cut(listItemK.firstChild.nodeSize, listItemK.nodeSize - listItemK.lastChild.nodeSize - 2) : listItemK.content.cut(listItemK.firstChild.nodeSize);
189
+ const childrenOContent = listItemK.lastChild.content;
190
+ insertContentDeleteRange(tr, tr => tr.doc.resolve(textInsertPos), containsChildrenO ? [[textContent, textInsertPos], [childrenMContent, childrenMInsertPos], [childrenOContent, childrenOInsertPos]] : [[textContent, textInsertPos], [childrenMContent, childrenMInsertPos]], [[beforeListItemK, afterListItemK]]);
191
+ if (dispatch) {
192
+ dispatch(tr);
193
+ }
194
+ return true;
195
+ };
196
+ const BACKSPACE_COMMANDS = {
197
+ [LIST_TEXT_SCENARIOS.JOIN_SIBLINGS]: listBackspaceCase2,
198
+ [LIST_TEXT_SCENARIOS.JOIN_DESCENDANT_TO_PARENT]: listBackspaceCase3,
199
+ [LIST_TEXT_SCENARIOS.JOIN_TO_SIBLING_DESCENDANT]: listBackspaceCase4
200
+ };
201
+ export const calcJoinListScenario = (walkNode, $head, tr) => {
202
+ const {
203
+ $pos: $prev,
204
+ foundNode: prevFoundNode
205
+ } = walkNode;
206
+ const prevInList = isPosInsideList($prev);
207
+ const headInParagraph = isPosInsideParagraph($head);
208
+ const headInFirstChild = $head.index(-1) === 0;
209
+ const headInList = isPosInsideList($head);
210
+
211
+ //Must be at the start of the selection of the first child in the listItem
212
+
213
+ if (!prevFoundNode || !prevInList || !headInParagraph || !headInFirstChild || !headInList) {
214
+ return false;
215
+ }
216
+ const prevInParagraph = isPosInsideParagraph($prev);
217
+ if (prevInParagraph) {
218
+ return [LIST_TEXT_SCENARIOS.JOIN_DESCENDANT_TO_PARENT, null];
219
+ }
220
+ const prevParentLastChildIsList = $prev.parent.lastChild && isListNode($prev.parent.lastChild);
221
+ const prevParentLastChildIsParagraph = isParagraphNode($prev.parent.lastChild);
222
+
223
+ // Will search for the possible last node for case 4 (where the list could be indented multiple times)
224
+ // $last is required to determine whether we are in case 2 or 4
225
+ let $last = tr.doc.resolve($prev.pos);
226
+ let lastFoundNode;
227
+ do {
228
+ let walkNode = walkPrevNode($last);
229
+ $last = walkNode.$pos;
230
+ lastFoundNode = walkNode.foundNode;
231
+ } while (lastFoundNode && !$last.parent.isTextblock);
232
+ const lastInParagraph = isPosInsideParagraph($last);
233
+ if (lastFoundNode && prevParentLastChildIsList && lastInParagraph) {
234
+ return [LIST_TEXT_SCENARIOS.JOIN_TO_SIBLING_DESCENDANT, $last];
235
+ } else if (prevParentLastChildIsParagraph) {
236
+ return [LIST_TEXT_SCENARIOS.JOIN_SIBLINGS, null];
237
+ }
238
+ return false;
239
+ };
240
+ export const listBackspace = editorAnalyticsAPI => (state, dispatch) => {
241
+ const {
242
+ tr,
243
+ selection: {
244
+ $head
245
+ }
246
+ } = state;
247
+ const walkNode = walkPrevNode($head);
248
+ if (!isEmptySelectionAtStart(state)) {
249
+ return false;
250
+ }
251
+ const scenario = calcJoinListScenario(walkNode, $head, tr);
252
+ if (!scenario) {
253
+ return false;
254
+ }
255
+ const {
256
+ bulletList,
257
+ orderedList
258
+ } = state.schema.nodes;
259
+ const listParent = findParentNodeOfType([bulletList, orderedList])(tr.selection);
260
+ let actionSubjectId = ACTION_SUBJECT_ID.FORMAT_LIST_BULLET;
261
+ if (listParent && listParent.node.type === orderedList) {
262
+ actionSubjectId = ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
263
+ }
264
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
265
+ action: ACTION.LIST_ITEM_JOINED,
266
+ actionSubject: ACTION_SUBJECT.LIST,
267
+ actionSubjectId,
268
+ eventType: EVENT_TYPE.TRACK,
269
+ attributes: {
270
+ inputMethod: INPUT_METHOD.KEYBOARD,
271
+ direction: DELETE_DIRECTION.BACKWARD,
272
+ scenario: scenario[0]
273
+ }
274
+ })(tr);
275
+ return BACKSPACE_COMMANDS[scenario[0]](tr, dispatch, walkNode.$pos, $head, scenario[1]);
276
+ };