@ckeditor/ckeditor5-list 33.0.0 → 34.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 (52) hide show
  1. package/LICENSE.md +2 -2
  2. package/README.md +2 -1
  3. package/build/list.js +1 -1
  4. package/build/translations/de.js +1 -1
  5. package/build/translations/en-au.js +1 -1
  6. package/build/translations/gl.js +1 -1
  7. package/build/translations/jv.js +1 -0
  8. package/build/translations/lv.js +1 -1
  9. package/build/translations/pt-br.js +1 -1
  10. package/build/translations/sk.js +1 -1
  11. package/build/translations/ur.js +1 -0
  12. package/ckeditor5-metadata.json +52 -0
  13. package/lang/translations/de.po +4 -4
  14. package/lang/translations/en-au.po +4 -4
  15. package/lang/translations/gl.po +4 -4
  16. package/lang/translations/jv.po +125 -0
  17. package/lang/translations/lv.po +7 -7
  18. package/lang/translations/pt-br.po +4 -4
  19. package/lang/translations/sk.po +4 -4
  20. package/lang/translations/ur.po +125 -0
  21. package/package.json +37 -24
  22. package/src/documentlist/converters.js +470 -0
  23. package/src/documentlist/documentlistcommand.js +216 -0
  24. package/src/documentlist/documentlistediting.js +724 -0
  25. package/src/documentlist/documentlistindentcommand.js +182 -0
  26. package/src/documentlist/documentlistmergecommand.js +235 -0
  27. package/src/documentlist/documentlistsplitcommand.js +114 -0
  28. package/src/documentlist/utils/listwalker.js +260 -0
  29. package/src/documentlist/utils/model.js +534 -0
  30. package/src/documentlist/utils/postfixers.js +138 -0
  31. package/src/documentlist/utils/view.js +148 -0
  32. package/src/documentlist.js +36 -0
  33. package/src/documentlistproperties/converters.js +57 -0
  34. package/src/documentlistproperties/documentlistpropertiesediting.js +377 -0
  35. package/src/documentlistproperties/documentlistreversedcommand.js +76 -0
  36. package/src/documentlistproperties/documentliststartcommand.js +76 -0
  37. package/src/documentlistproperties/documentliststylecommand.js +163 -0
  38. package/src/documentlistproperties/utils/style.js +74 -0
  39. package/src/documentlistproperties.js +37 -0
  40. package/src/index.js +4 -0
  41. package/src/list/converters.js +4 -0
  42. package/src/list/listediting.js +12 -13
  43. package/src/list.js +3 -2
  44. package/src/listproperties/listpropertiesediting.js +1 -1
  45. package/src/listproperties/listpropertiesui.js +6 -1
  46. package/src/listproperties/listreversedcommand.js +1 -1
  47. package/src/listproperties/liststartcommand.js +1 -1
  48. package/src/listproperties/liststylecommand.js +1 -1
  49. package/src/listproperties.js +47 -8
  50. package/theme/documentlist.css +8 -0
  51. package/build/list.js.map +0 -1
  52. package/src/listproperties/ui/inputnumberview.js +0 -0
@@ -0,0 +1,182 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module list/documentlist/documentlistindentcommand
8
+ */
9
+
10
+ import { Command } from 'ckeditor5/src/core';
11
+ import {
12
+ expandListBlocksToCompleteItems,
13
+ indentBlocks,
14
+ isFirstBlockOfListItem,
15
+ isListItemBlock,
16
+ isSingleListItem,
17
+ outdentBlocksWithMerge,
18
+ sortBlocks,
19
+ splitListItemBefore
20
+ } from './utils/model';
21
+ import ListWalker from './utils/listwalker';
22
+
23
+ /**
24
+ * The document list indent command. It is used by the {@link module:list/documentlist~DocumentList list feature}.
25
+ *
26
+ * @extends module:core/command~Command
27
+ */
28
+ export default class DocumentListIndentCommand extends Command {
29
+ /**
30
+ * Creates an instance of the command.
31
+ *
32
+ * @param {module:core/editor/editor~Editor} editor The editor instance.
33
+ * @param {'forward'|'backward'} indentDirection The direction of indent. If it is equal to `backward`, the command
34
+ * will outdent a list item.
35
+ */
36
+ constructor( editor, indentDirection ) {
37
+ super( editor );
38
+
39
+ /**
40
+ * Determines by how much the command will change the list item's indent attribute.
41
+ *
42
+ * @readonly
43
+ * @private
44
+ * @member {'forward'|'backward'}
45
+ */
46
+ this._direction = indentDirection;
47
+ }
48
+
49
+ /**
50
+ * @inheritDoc
51
+ */
52
+ refresh() {
53
+ this.isEnabled = this._checkEnabled();
54
+ }
55
+
56
+ /**
57
+ * Indents or outdents (depending on the {@link #constructor}'s `indentDirection` parameter) selected list items.
58
+ *
59
+ * @fires execute
60
+ * @fires afterExecute
61
+ */
62
+ execute() {
63
+ const model = this.editor.model;
64
+ const blocks = getSelectedListBlocks( model.document.selection );
65
+
66
+ model.change( writer => {
67
+ const changedBlocks = [];
68
+
69
+ // Handle selection contained in the single list item and starting in the following blocks.
70
+ if ( isSingleListItem( blocks ) && !isFirstBlockOfListItem( blocks[ 0 ] ) ) {
71
+ // Allow increasing indent of following list item blocks.
72
+ if ( this._direction == 'forward' ) {
73
+ changedBlocks.push( ...indentBlocks( blocks, writer ) );
74
+ }
75
+
76
+ // For indent make sure that indented blocks have a new ID.
77
+ // For outdent just split blocks from the list item (give them a new IDs).
78
+ changedBlocks.push( ...splitListItemBefore( blocks[ 0 ], writer ) );
79
+ }
80
+ // More than a single list item is selected, or the first block of list item is selected.
81
+ else {
82
+ // Now just update the attributes of blocks.
83
+ if ( this._direction == 'forward' ) {
84
+ changedBlocks.push( ...indentBlocks( blocks, writer, { expand: true } ) );
85
+ } else {
86
+ changedBlocks.push( ...outdentBlocksWithMerge( blocks, writer ) );
87
+ }
88
+ }
89
+
90
+ // Align the list item type to match the previous list item (from the same list).
91
+ for ( const block of changedBlocks ) {
92
+ // This block become a plain block (for example a paragraph).
93
+ if ( !block.hasAttribute( 'listType' ) ) {
94
+ continue;
95
+ }
96
+
97
+ const previousItemBlock = ListWalker.first( block, { sameIndent: true } );
98
+
99
+ if ( previousItemBlock ) {
100
+ writer.setAttribute( 'listType', previousItemBlock.getAttribute( 'listType' ), block );
101
+ }
102
+ }
103
+
104
+ this._fireAfterExecute( changedBlocks );
105
+ } );
106
+ }
107
+
108
+ /**
109
+ * Fires the `afterExecute` event.
110
+ *
111
+ * @private
112
+ * @param {Array.<module:engine/model/element~Element>} changedBlocks The changed list elements.
113
+ */
114
+ _fireAfterExecute( changedBlocks ) {
115
+ /**
116
+ * Event fired by the {@link #execute} method.
117
+ *
118
+ * It allows to execute an action after executing the {@link ~DocumentListIndentCommand#execute} method,
119
+ * for example adjusting attributes of changed list items.
120
+ *
121
+ * @protected
122
+ * @event afterExecute
123
+ */
124
+ this.fire( 'afterExecute', sortBlocks( new Set( changedBlocks ) ) );
125
+ }
126
+
127
+ /**
128
+ * Checks whether the command can be enabled in the current context.
129
+ *
130
+ * @private
131
+ * @returns {Boolean} Whether the command should be enabled.
132
+ */
133
+ _checkEnabled() {
134
+ // Check whether any of position's ancestor is a list item.
135
+ let blocks = getSelectedListBlocks( this.editor.model.document.selection );
136
+ let firstBlock = blocks[ 0 ];
137
+
138
+ // If selection is not in a list item, the command is disabled.
139
+ if ( !firstBlock ) {
140
+ return false;
141
+ }
142
+
143
+ // If we are outdenting it is enough to be in list item. Every list item can always be outdented.
144
+ if ( this._direction == 'backward' ) {
145
+ return true;
146
+ }
147
+
148
+ // A single block of a list item is selected, so it could be indented as a sublist.
149
+ if ( isSingleListItem( blocks ) && !isFirstBlockOfListItem( blocks[ 0 ] ) ) {
150
+ return true;
151
+ }
152
+
153
+ blocks = expandListBlocksToCompleteItems( blocks );
154
+ firstBlock = blocks[ 0 ];
155
+
156
+ // Check if there is any list item before selected items that could become a parent of selected items.
157
+ const siblingItem = ListWalker.first( firstBlock, { sameIndent: true } );
158
+
159
+ if ( !siblingItem ) {
160
+ return false;
161
+ }
162
+
163
+ if ( siblingItem.getAttribute( 'listType' ) == firstBlock.getAttribute( 'listType' ) ) {
164
+ return true;
165
+ }
166
+
167
+ return false;
168
+ }
169
+ }
170
+
171
+ // Returns an array of selected blocks truncated to the first non list block element.
172
+ function getSelectedListBlocks( selection ) {
173
+ const blocks = Array.from( selection.getSelectedBlocks() );
174
+ const firstNonListBlockIndex = blocks.findIndex( block => !isListItemBlock( block ) );
175
+
176
+ if ( firstNonListBlockIndex != -1 ) {
177
+ blocks.length = firstNonListBlockIndex;
178
+ }
179
+
180
+ return blocks;
181
+ }
182
+
@@ -0,0 +1,235 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module list/documentlist/documentlistmergecommand
8
+ */
9
+
10
+ import { Command } from 'ckeditor5/src/core';
11
+ import {
12
+ getNestedListBlocks,
13
+ indentBlocks,
14
+ sortBlocks,
15
+ isFirstBlockOfListItem,
16
+ mergeListItemBefore,
17
+ isSingleListItem,
18
+ getSelectedBlockObject,
19
+ isListItemBlock
20
+ } from './utils/model';
21
+ import ListWalker from './utils/listwalker';
22
+
23
+ /**
24
+ * The document list merge command. It is used by the {@link module:list/documentlist~DocumentList list feature}.
25
+ *
26
+ * @extends module:core/command~Command
27
+ */
28
+ export default class DocumentListMergeCommand extends Command {
29
+ /**
30
+ * Creates an instance of the command.
31
+ *
32
+ * @param {module:core/editor/editor~Editor} editor The editor instance.
33
+ * @param {'backward'|'forward'} direction Whether list item should be merged before or after the selected block.
34
+ */
35
+ constructor( editor, direction ) {
36
+ super( editor );
37
+
38
+ /**
39
+ * Whether list item should be merged before or after the selected block.
40
+ *
41
+ * @readonly
42
+ * @private
43
+ * @member {'backward'|'forward'}
44
+ */
45
+ this._direction = direction;
46
+ }
47
+
48
+ /**
49
+ * @inheritDoc
50
+ */
51
+ refresh() {
52
+ this.isEnabled = this._checkEnabled();
53
+ }
54
+
55
+ /**
56
+ * Merges list blocks together (depending on the {@link #constructor}'s `direction` parameter).
57
+ *
58
+ * @fires execute
59
+ * @fires afterExecute
60
+ * @param {Object} [options] Command options.
61
+ * @param {String|Boolean} [options.shouldMergeOnBlocksContentLevel=false] When set `true`, merging will be performed together
62
+ * with {@link module:engine/model/model~Model#deleteContent} to get rid of the inline content in the selection or take advantage
63
+ * of the heuristics in `deleteContent()` that helps convert lists into paragraphs in certain cases.
64
+ */
65
+ execute( { shouldMergeOnBlocksContentLevel = false } = {} ) {
66
+ const model = this.editor.model;
67
+ const selection = model.document.selection;
68
+ const changedBlocks = [];
69
+
70
+ model.change( writer => {
71
+ const { firstElement, lastElement } = this._getMergeSubjectElements( selection, shouldMergeOnBlocksContentLevel );
72
+
73
+ const firstIndent = firstElement.getAttribute( 'listIndent' ) || 0;
74
+ const lastIndent = lastElement.getAttribute( 'listIndent' );
75
+ const lastElementId = lastElement.getAttribute( 'listItemId' );
76
+
77
+ if ( firstIndent != lastIndent ) {
78
+ const nestedLastElementBlocks = getNestedListBlocks( lastElement );
79
+
80
+ changedBlocks.push( ...indentBlocks( [ lastElement, ...nestedLastElementBlocks ], writer, {
81
+ indentBy: firstIndent - lastIndent,
82
+
83
+ // If outdenting, the entire sub-tree that follows must be included.
84
+ expand: firstIndent < lastIndent
85
+ } ) );
86
+ }
87
+
88
+ if ( shouldMergeOnBlocksContentLevel ) {
89
+ let sel = selection;
90
+
91
+ if ( selection.isCollapsed ) {
92
+ sel = writer.createSelection( writer.createRange(
93
+ writer.createPositionAt( firstElement, 'end' ),
94
+ writer.createPositionAt( lastElement, 0 )
95
+ ) );
96
+ }
97
+
98
+ // Delete selected content. Replace entire content only for non-collapsed selection.
99
+ model.deleteContent( sel, { doNotResetEntireContent: selection.isCollapsed } );
100
+
101
+ // Get the last "touched" element after deleteContent call (can't use the lastElement because
102
+ // it could get merged into the firstElement while deleting content).
103
+ const lastElementAfterDelete = sel.getLastPosition().parent;
104
+
105
+ // Check if the element after it was in the same list item and adjust it if needed.
106
+ const nextSibling = lastElementAfterDelete.nextSibling;
107
+
108
+ changedBlocks.push( lastElementAfterDelete );
109
+
110
+ if ( nextSibling && nextSibling !== lastElement && nextSibling.getAttribute( 'listItemId' ) == lastElementId ) {
111
+ changedBlocks.push( ...mergeListItemBefore( nextSibling, lastElementAfterDelete, writer ) );
112
+ }
113
+ } else {
114
+ changedBlocks.push( ...mergeListItemBefore( lastElement, firstElement, writer ) );
115
+ }
116
+
117
+ this._fireAfterExecute( changedBlocks );
118
+ } );
119
+ }
120
+
121
+ /**
122
+ * Fires the `afterExecute` event.
123
+ *
124
+ * @private
125
+ * @param {Array.<module:engine/model/element~Element>} changedBlocks The changed list elements.
126
+ */
127
+ _fireAfterExecute( changedBlocks ) {
128
+ /**
129
+ * Event fired by the {@link #execute} method.
130
+ *
131
+ * It allows to execute an action after executing the {@link ~DocumentListMergeCommand#execute} method,
132
+ * for example adjusting attributes of changed list items.
133
+ *
134
+ * @protected
135
+ * @event afterExecute
136
+ */
137
+ this.fire( 'afterExecute', sortBlocks( new Set( changedBlocks ) ) );
138
+ }
139
+
140
+ /**
141
+ * Checks whether the command can be enabled in the current context.
142
+ *
143
+ * @private
144
+ * @returns {Boolean} Whether the command should be enabled.
145
+ */
146
+ _checkEnabled() {
147
+ const model = this.editor.model;
148
+ const selection = model.document.selection;
149
+ const selectedBlockObject = getSelectedBlockObject( model );
150
+
151
+ if ( selection.isCollapsed || selectedBlockObject ) {
152
+ const positionParent = selectedBlockObject || selection.getFirstPosition().parent;
153
+
154
+ if ( !isListItemBlock( positionParent ) ) {
155
+ return false;
156
+ }
157
+
158
+ const siblingNode = this._direction == 'backward' ?
159
+ positionParent.previousSibling :
160
+ positionParent.nextSibling;
161
+
162
+ if ( !siblingNode ) {
163
+ return false;
164
+ }
165
+
166
+ if ( isSingleListItem( [ positionParent, siblingNode ] ) ) {
167
+ return false;
168
+ }
169
+ } else {
170
+ const lastPosition = selection.getLastPosition();
171
+ const firstPosition = selection.getFirstPosition();
172
+
173
+ // If deleting within a single block of a list item, there's no need to merge anything.
174
+ // The default delete should be executed instead.
175
+ if ( lastPosition.parent === firstPosition.parent ) {
176
+ return false;
177
+ }
178
+
179
+ if ( !isListItemBlock( lastPosition.parent ) ) {
180
+ return false;
181
+ }
182
+ }
183
+
184
+ return true;
185
+ }
186
+
187
+ /**
188
+ * Returns the boundary elements the merge should be executed for. These are not necessarily selection's first
189
+ * and last position parents but sometimes sibling or even further blocks depending on the context.
190
+ *
191
+ * @param {module:engine/model/selection~Selection} selection The selection the merge is executed for.
192
+ * @param {Boolean} shouldMergeOnBlocksContentLevel When `true`, merge is performed together with
193
+ * {@link module:engine/model/model~Model#deleteContent} to remove the inline content within the selection.
194
+ * @returns {Object} elements
195
+ * @returns {module:engine/model/element~Element} elements.firstElement
196
+ * @returns {module:engine/model/element~Element} elements.lastElement
197
+ */
198
+ _getMergeSubjectElements( selection, shouldMergeOnBlocksContentLevel ) {
199
+ const model = this.editor.model;
200
+ const selectedBlockObject = getSelectedBlockObject( model );
201
+ let firstElement, lastElement;
202
+
203
+ if ( selection.isCollapsed || selectedBlockObject ) {
204
+ const positionParent = selectedBlockObject || selection.getFirstPosition().parent;
205
+ const isFirstBlock = isFirstBlockOfListItem( positionParent );
206
+
207
+ if ( this._direction == 'backward' ) {
208
+ lastElement = positionParent;
209
+
210
+ if ( isFirstBlock && !shouldMergeOnBlocksContentLevel ) {
211
+ // For the "c" as an anchorElement:
212
+ // * a
213
+ // * b
214
+ // * [c] <-- this block should be merged with "a"
215
+ // It should find "a" element to merge with:
216
+ // * a
217
+ // * b
218
+ // c
219
+ firstElement = ListWalker.first( positionParent, { sameIndent: true, lowerIndent: true } );
220
+ } else {
221
+ firstElement = positionParent.previousSibling;
222
+ }
223
+ } else {
224
+ // In case of the forward merge there is no case as above, just merge with next sibling.
225
+ firstElement = positionParent;
226
+ lastElement = positionParent.nextSibling;
227
+ }
228
+ } else {
229
+ firstElement = selection.getFirstPosition().parent;
230
+ lastElement = selection.getLastPosition().parent;
231
+ }
232
+
233
+ return { firstElement, lastElement };
234
+ }
235
+ }
@@ -0,0 +1,114 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module list/documentlist/documentlistsplitcommand
8
+ */
9
+
10
+ import { Command } from 'ckeditor5/src/core';
11
+ import {
12
+ isFirstBlockOfListItem,
13
+ isListItemBlock,
14
+ sortBlocks,
15
+ splitListItemBefore
16
+ } from './utils/model';
17
+
18
+ /**
19
+ * The document list split command that splits the list item at the selection.
20
+ *
21
+ * It is used by the {@link module:list/documentlist~DocumentList document list feature}.
22
+ *
23
+ * @extends module:core/command~Command
24
+ */
25
+ export default class DocumentListSplitCommand extends Command {
26
+ /**
27
+ * Creates an instance of the command.
28
+ *
29
+ * @param {module:core/editor/editor~Editor} editor The editor instance.
30
+ * @param {'before'|'after'} direction Whether list item should be split before or after the selected block.
31
+ */
32
+ constructor( editor, direction ) {
33
+ super( editor );
34
+
35
+ /**
36
+ * Whether list item should be split before or after the selected block.
37
+ *
38
+ * @readonly
39
+ * @private
40
+ * @member {'before'|'after'}
41
+ */
42
+ this._direction = direction;
43
+ }
44
+
45
+ /**
46
+ * @inheritDoc
47
+ */
48
+ refresh() {
49
+ this.isEnabled = this._checkEnabled();
50
+ }
51
+
52
+ /**
53
+ * Splits the list item at the selection.
54
+ *
55
+ * @fires execute
56
+ * @fires afterExecute
57
+ */
58
+ execute() {
59
+ const editor = this.editor;
60
+
61
+ editor.model.change( writer => {
62
+ const changedBlocks = splitListItemBefore( this._getStartBlock(), writer );
63
+
64
+ this._fireAfterExecute( changedBlocks );
65
+ } );
66
+ }
67
+
68
+ /**
69
+ * Fires the `afterExecute` event.
70
+ *
71
+ * @private
72
+ * @param {Array.<module:engine/model/element~Element>} changedBlocks The changed list elements.
73
+ */
74
+ _fireAfterExecute( changedBlocks ) {
75
+ /**
76
+ * Event fired by the {@link #execute} method.
77
+ *
78
+ * It allows to execute an action after executing the {@link ~DocumentListSplitCommand#execute} method,
79
+ * for example adjusting attributes of changed list items.
80
+ *
81
+ * @protected
82
+ * @event afterExecute
83
+ */
84
+ this.fire( 'afterExecute', sortBlocks( new Set( changedBlocks ) ) );
85
+ }
86
+
87
+ /**
88
+ * Checks whether the command can be enabled in the current context.
89
+ *
90
+ * @private
91
+ * @returns {Boolean} Whether the command should be enabled.
92
+ */
93
+ _checkEnabled() {
94
+ const selection = this.editor.model.document.selection;
95
+ const block = this._getStartBlock();
96
+
97
+ return selection.isCollapsed &&
98
+ isListItemBlock( block ) &&
99
+ !isFirstBlockOfListItem( block );
100
+ }
101
+
102
+ /**
103
+ * Returns the model element that is the main focus of the command (according to the current selection and command direction).
104
+ *
105
+ * @private
106
+ * @returns {module:engine/model/element~Element}
107
+ */
108
+ _getStartBlock() {
109
+ const doc = this.editor.model.document;
110
+ const positionParent = doc.selection.getFirstPosition().parent;
111
+
112
+ return this._direction == 'before' ? positionParent : positionParent.nextSibling;
113
+ }
114
+ }