/
listediting.js
219 lines (176 loc) · 6.57 KB
/
listediting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module list/list/listediting
*/
import ListCommand from './listcommand';
import IndentCommand from './indentcommand';
import { Plugin } from 'ckeditor5/src/core';
import { Enter } from 'ckeditor5/src/enter';
import { Delete } from 'ckeditor5/src/typing';
import {
cleanList,
cleanListItem,
modelViewInsertion,
modelViewChangeType,
modelViewMergeAfterChangeType,
modelViewMergeAfter,
modelViewRemove,
modelViewSplitOnInsert,
modelViewChangeIndent,
modelChangePostFixer,
modelIndentPasteFixer,
viewModelConverter,
modelToViewPosition,
viewToModelPosition
} from './converters';
/**
* The engine of the list feature. It handles creating, editing and removing lists and list items.
*
* It registers the `'numberedList'`, `'bulletedList'`, `'indentList'` and `'outdentList'` commands.
*
* @extends module:core/plugin~Plugin
*/
export default class ListEditing extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'ListEditing';
}
/**
* @inheritDoc
*/
static get requires() {
return [ Enter, Delete ];
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
// Schema.
// Note: in case `$block` will ever be allowed in `listItem`, keep in mind that this feature
// uses `Selection#getSelectedBlocks()` without any additional processing to obtain all selected list items.
// If there are blocks allowed inside list item, algorithms using `getSelectedBlocks()` will have to be modified.
editor.model.schema.register( 'listItem', {
inheritAllFrom: '$block',
allowAttributes: [ 'listType', 'listIndent' ]
} );
// Converters.
const data = editor.data;
const editing = editor.editing;
editor.model.document.registerPostFixer( writer => modelChangePostFixer( editor.model, writer ) );
editing.mapper.registerViewToModelLength( 'li', getViewListItemLength );
data.mapper.registerViewToModelLength( 'li', getViewListItemLength );
editing.mapper.on( 'modelToViewPosition', modelToViewPosition( editing.view ) );
editing.mapper.on( 'viewToModelPosition', viewToModelPosition( editor.model ) );
data.mapper.on( 'modelToViewPosition', modelToViewPosition( editing.view ) );
editor.conversion.for( 'editingDowncast' )
.add( dispatcher => {
dispatcher.on( 'insert', modelViewSplitOnInsert, { priority: 'high' } );
dispatcher.on( 'insert:listItem', modelViewInsertion( editor.model ) );
dispatcher.on( 'attribute:listType:listItem', modelViewChangeType, { priority: 'high' } );
dispatcher.on( 'attribute:listType:listItem', modelViewMergeAfterChangeType, { priority: 'low' } );
dispatcher.on( 'attribute:listIndent:listItem', modelViewChangeIndent( editor.model ) );
dispatcher.on( 'remove:listItem', modelViewRemove( editor.model ) );
dispatcher.on( 'remove', modelViewMergeAfter, { priority: 'low' } );
} );
editor.conversion.for( 'dataDowncast' )
.add( dispatcher => {
dispatcher.on( 'insert', modelViewSplitOnInsert, { priority: 'high' } );
dispatcher.on( 'insert:listItem', modelViewInsertion( editor.model ) );
} );
editor.conversion.for( 'upcast' )
.add( dispatcher => {
dispatcher.on( 'element:ul', cleanList, { priority: 'high' } );
dispatcher.on( 'element:ol', cleanList, { priority: 'high' } );
dispatcher.on( 'element:li', cleanListItem, { priority: 'high' } );
dispatcher.on( 'element:li', viewModelConverter );
} );
// Fix indentation of pasted items.
editor.model.on( 'insertContent', modelIndentPasteFixer, { priority: 'high' } );
// Register commands for numbered and bulleted list.
editor.commands.add( 'numberedList', new ListCommand( editor, 'numbered' ) );
editor.commands.add( 'bulletedList', new ListCommand( editor, 'bulleted' ) );
// Register commands for indenting.
editor.commands.add( 'indentList', new IndentCommand( editor, 'forward' ) );
editor.commands.add( 'outdentList', new IndentCommand( editor, 'backward' ) );
const viewDocument = editing.view.document;
// Overwrite default Enter key behavior.
// If Enter key is pressed with selection collapsed in empty list item, outdent it instead of breaking it.
this.listenTo( viewDocument, 'enter', ( evt, data ) => {
const doc = this.editor.model.document;
const positionParent = doc.selection.getLastPosition().parent;
if ( doc.selection.isCollapsed && positionParent.name == 'listItem' && positionParent.isEmpty ) {
this.editor.execute( 'outdentList' );
data.preventDefault();
evt.stop();
}
}, { context: 'li' } );
// Overwrite default Backspace key behavior.
// If Backspace key is pressed with selection collapsed on first position in first list item, outdent it. #83
this.listenTo( viewDocument, 'delete', ( evt, data ) => {
// Check conditions from those that require less computations like those immediately available.
if ( data.direction !== 'backward' ) {
return;
}
const selection = this.editor.model.document.selection;
if ( !selection.isCollapsed ) {
return;
}
const firstPosition = selection.getFirstPosition();
if ( !firstPosition.isAtStart ) {
return;
}
const positionParent = firstPosition.parent;
if ( positionParent.name !== 'listItem' ) {
return;
}
const previousIsAListItem = positionParent.previousSibling && positionParent.previousSibling.name === 'listItem';
if ( previousIsAListItem ) {
return;
}
this.editor.execute( 'outdentList' );
data.preventDefault();
evt.stop();
}, { context: 'li' } );
this.listenTo( editor.editing.view.document, 'tab', ( evt, data ) => {
const commandName = data.shiftKey ? 'outdentList' : 'indentList';
const command = this.editor.commands.get( commandName );
if ( command.isEnabled ) {
editor.execute( commandName );
data.stopPropagation();
data.preventDefault();
evt.stop();
}
}, { context: 'li' } );
}
/**
* @inheritDoc
*/
afterInit() {
const commands = this.editor.commands;
const indent = commands.get( 'indent' );
const outdent = commands.get( 'outdent' );
if ( indent ) {
indent.registerChildCommand( commands.get( 'indentList' ) );
}
if ( outdent ) {
outdent.registerChildCommand( commands.get( 'outdentList' ) );
}
}
}
function getViewListItemLength( element ) {
let length = 1;
for ( const child of element.getChildren() ) {
if ( child.name == 'ul' || child.name == 'ol' ) {
for ( const item of child.getChildren() ) {
length += getViewListItemLength( item );
}
}
}
return length;
}