@ckeditor/ckeditor5-indent 47.5.0 → 47.6.0-alpha.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.
@@ -0,0 +1,328 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
+ */
5
+ import { Plugin } from 'ckeditor5/src/core.js';
6
+ import { addMarginStylesRules } from 'ckeditor5/src/engine.js';
7
+ import { IndentBlockListCommand } from './indentblocklistcommand.js';
8
+ import { IndentBlockListItemCommand } from './indentblocklistitemcommand.js';
9
+ import { IndentUsingOffset } from '../indentcommandbehavior/indentusingoffset.js';
10
+ import { IndentUsingClasses } from '../indentcommandbehavior/indentusingclasses.js';
11
+ /**
12
+ * This integration enables using block indentation feature with lists.
13
+ */
14
+ export class IndentBlockListIntegration extends Plugin {
15
+ /**
16
+ * @inheritDoc
17
+ */
18
+ static get pluginName() {
19
+ return 'IndentBlockListIntegration';
20
+ }
21
+ /**
22
+ * @inheritDoc
23
+ */
24
+ static get isOfficialPlugin() {
25
+ return true;
26
+ }
27
+ /**
28
+ * @inheritDoc
29
+ */
30
+ init() {
31
+ const editor = this.editor;
32
+ if (!this.editor.plugins.has('ListEditing')) {
33
+ return;
34
+ }
35
+ const config = editor.config.get('indentBlock');
36
+ if (config.classes && config.classes.length) {
37
+ this._setupConversionUsingClassesForListBlock(config.classes);
38
+ this._setupConversionUsingClassesForListItemBlock(config.classes);
39
+ editor.commands.add('indentBlockList', new IndentBlockListCommand(editor, new IndentUsingClasses({
40
+ direction: 'forward',
41
+ classes: config.classes
42
+ })));
43
+ editor.commands.add('outdentBlockList', new IndentBlockListCommand(editor, new IndentUsingClasses({
44
+ direction: 'backward',
45
+ classes: config.classes
46
+ })));
47
+ editor.commands.add('indentBlockListItem', new IndentBlockListItemCommand(editor, new IndentUsingClasses({
48
+ direction: 'forward',
49
+ classes: config.classes
50
+ })));
51
+ editor.commands.add('outdentBlockListItem', new IndentBlockListItemCommand(editor, new IndentUsingClasses({
52
+ direction: 'backward',
53
+ classes: config.classes
54
+ })));
55
+ }
56
+ else {
57
+ editor.data.addStyleProcessorRules(addMarginStylesRules);
58
+ this._setupConversionUsingOffsetForListBlock();
59
+ this._setupConversionUsingOffsetForListItemBlock();
60
+ editor.commands.add('indentBlockList', new IndentBlockListCommand(editor, new IndentUsingOffset({
61
+ direction: 'forward',
62
+ offset: config.offset,
63
+ unit: config.unit
64
+ })));
65
+ editor.commands.add('outdentBlockList', new IndentBlockListCommand(editor, new IndentUsingOffset({
66
+ direction: 'backward',
67
+ offset: config.offset,
68
+ unit: config.unit
69
+ })));
70
+ editor.commands.add('indentBlockListItem', new IndentBlockListItemCommand(editor, new IndentUsingOffset({
71
+ direction: 'forward',
72
+ offset: config.offset,
73
+ unit: config.unit
74
+ })));
75
+ editor.commands.add('outdentBlockListItem', new IndentBlockListItemCommand(editor, new IndentUsingOffset({
76
+ direction: 'backward',
77
+ offset: config.offset,
78
+ unit: config.unit
79
+ })));
80
+ }
81
+ const listEditing = editor.plugins.get('ListEditing');
82
+ // Make sure that all items in a single list (items at the same level & listType) have the same blockIndentList attribute value.
83
+ listEditing.on('postFixer', (evt, { listNodes, writer }) => {
84
+ for (const { node, previousNodeInList } of listNodes) {
85
+ // This is a first item of a nested list.
86
+ if (!previousNodeInList) {
87
+ continue;
88
+ }
89
+ if (previousNodeInList.getAttribute('listType') != node.getAttribute('listType')) {
90
+ continue;
91
+ }
92
+ if (ensureIndentValuesConsistency('blockIndentList', node, previousNodeInList, writer)) {
93
+ evt.return = true;
94
+ }
95
+ if (previousNodeInList.getAttribute('listItemId') != node.getAttribute('listItemId')) {
96
+ continue;
97
+ }
98
+ if (ensureIndentValuesConsistency('blockIndentListItem', node, previousNodeInList, writer)) {
99
+ evt.return = true;
100
+ }
101
+ }
102
+ });
103
+ this.listenTo(editor.editing.view.document, 'tab', (evt, data) => {
104
+ const commandName = data.shiftKey ? 'outdentBlockList' : 'indentBlockList';
105
+ const command = this.editor.commands.get(commandName);
106
+ if (command.isEnabled) {
107
+ editor.execute(commandName, { firstListOnly: true });
108
+ data.stopPropagation();
109
+ data.preventDefault();
110
+ evt.stop();
111
+ }
112
+ }, { context: 'li', priority: 'high' });
113
+ }
114
+ /**
115
+ * @inheritDoc
116
+ */
117
+ afterInit() {
118
+ const editor = this.editor;
119
+ const model = editor.model;
120
+ const schema = model.schema;
121
+ if (!editor.plugins.has('ListEditing')) {
122
+ return;
123
+ }
124
+ // Schema registration.
125
+ schema.extend('$listItem', { allowAttributes: ['blockIndentList', 'blockIndentListItem'] });
126
+ schema.setAttributeProperties('blockIndentList', { isFormatting: true });
127
+ schema.setAttributeProperties('blockIndentListItem', { isFormatting: true });
128
+ model.schema.addAttributeCheck(context => {
129
+ const item = context.last;
130
+ if (!item.getAttribute('listItemId')) {
131
+ return false;
132
+ }
133
+ }, 'blockIndentList');
134
+ model.schema.addAttributeCheck(context => {
135
+ const item = context.last;
136
+ if (!item.getAttribute('listItemId')) {
137
+ return false;
138
+ }
139
+ }, 'blockIndentListItem');
140
+ // Clear blockIndentList and blockIndentListItem when list indent changes.
141
+ const clearBlockIndentAttributesOnListIndentChange = (_evt, changedBlocks) => {
142
+ editor.model.change(writer => {
143
+ for (const node of changedBlocks) {
144
+ if (node.hasAttribute('listItemId')) {
145
+ if (node.hasAttribute('blockIndentList')) {
146
+ writer.removeAttribute('blockIndentList', node);
147
+ }
148
+ if (node.hasAttribute('blockIndentListItem')) {
149
+ writer.removeAttribute('blockIndentListItem', node);
150
+ }
151
+ }
152
+ }
153
+ });
154
+ };
155
+ const indentListCommand = editor.commands.get('indentList');
156
+ const outdentListCommand = editor.commands.get('outdentList');
157
+ if (indentListCommand) {
158
+ this.listenTo(indentListCommand, 'afterExecute', clearBlockIndentAttributesOnListIndentChange);
159
+ }
160
+ if (outdentListCommand) {
161
+ this.listenTo(outdentListCommand, 'afterExecute', clearBlockIndentAttributesOnListIndentChange);
162
+ }
163
+ // Register list block indent commands in multi command.
164
+ const indentCommand = editor.commands.get('indent');
165
+ const outdentCommand = editor.commands.get('outdent');
166
+ indentCommand.registerChildCommand(editor.commands.get('indentBlockList'));
167
+ outdentCommand.registerChildCommand(editor.commands.get('outdentBlockList'));
168
+ indentCommand.registerChildCommand(editor.commands.get('indentBlockListItem'));
169
+ outdentCommand.registerChildCommand(editor.commands.get('outdentBlockListItem'));
170
+ }
171
+ /**
172
+ * Setups conversion for list block indent using offset indents.
173
+ */
174
+ _setupConversionUsingOffsetForListBlock() {
175
+ const editor = this.editor;
176
+ const conversion = editor.conversion;
177
+ const locale = editor.locale;
178
+ const marginProperty = locale.contentLanguageDirection === 'rtl' ? 'margin-right' : 'margin-left';
179
+ const listEditing = editor.plugins.get('ListEditing');
180
+ conversion.for('upcast').add(dispatcher => {
181
+ dispatcher.on('element:ol', listBlockIndentUpcastConverter('blockIndentList', marginProperty));
182
+ dispatcher.on('element:ul', listBlockIndentUpcastConverter('blockIndentList', marginProperty));
183
+ });
184
+ listEditing.registerDowncastStrategy({
185
+ scope: 'list',
186
+ attributeName: 'blockIndentList',
187
+ setAttributeOnDowncast(writer, value, element) {
188
+ if (value) {
189
+ writer.setStyle(marginProperty, value, element);
190
+ }
191
+ }
192
+ });
193
+ }
194
+ /**
195
+ * Setups conversion for list item block indent using offset indents.
196
+ */
197
+ _setupConversionUsingOffsetForListItemBlock() {
198
+ const editor = this.editor;
199
+ const locale = editor.locale;
200
+ const conversion = editor.conversion;
201
+ const marginProperty = locale.contentLanguageDirection === 'rtl' ? 'margin-right' : 'margin-left';
202
+ const listEditing = editor.plugins.get('ListEditing');
203
+ conversion.for('upcast').add(dispatcher => {
204
+ dispatcher.on('element:li', listBlockIndentUpcastConverter('blockIndentListItem', marginProperty), { priority: 'low' });
205
+ });
206
+ listEditing.registerDowncastStrategy({
207
+ scope: 'item',
208
+ attributeName: 'blockIndentListItem',
209
+ setAttributeOnDowncast(writer, value, element) {
210
+ if (value) {
211
+ writer.setStyle(marginProperty, value, element);
212
+ }
213
+ }
214
+ });
215
+ }
216
+ /**
217
+ * Setups conversion for list block indent using classes.
218
+ */
219
+ _setupConversionUsingClassesForListBlock(classes) {
220
+ const editor = this.editor;
221
+ const conversion = editor.conversion;
222
+ const listEditing = editor.plugins.get('ListEditing');
223
+ conversion.for('upcast').add(dispatcher => {
224
+ dispatcher.on('element:ol', listBlockIndentUpcastConverterUsingClasses('blockIndentList', classes));
225
+ dispatcher.on('element:ul', listBlockIndentUpcastConverterUsingClasses('blockIndentList', classes));
226
+ });
227
+ listEditing.registerDowncastStrategy({
228
+ scope: 'list',
229
+ attributeName: 'blockIndentList',
230
+ setAttributeOnDowncast(writer, value, element) {
231
+ if (value) {
232
+ writer.addClass(value, element);
233
+ }
234
+ }
235
+ });
236
+ }
237
+ /**
238
+ * Setups conversion for list item block indent using classes.
239
+ */
240
+ _setupConversionUsingClassesForListItemBlock(classes) {
241
+ const editor = this.editor;
242
+ const conversion = editor.conversion;
243
+ const listEditing = editor.plugins.get('ListEditing');
244
+ conversion.for('upcast').add(dispatcher => {
245
+ dispatcher.on('element:li', listBlockIndentUpcastConverterUsingClasses('blockIndentListItem', classes), { priority: 'low' });
246
+ });
247
+ listEditing.registerDowncastStrategy({
248
+ scope: 'item',
249
+ attributeName: 'blockIndentListItem',
250
+ setAttributeOnDowncast(writer, value, element) {
251
+ if (value) {
252
+ writer.addClass(value, element);
253
+ }
254
+ }
255
+ });
256
+ }
257
+ }
258
+ function listBlockIndentUpcastConverterUsingClasses(attributeName, classes) {
259
+ return (evt, data, conversionApi) => {
260
+ const { writer, consumable } = conversionApi;
261
+ if (!data.modelRange) {
262
+ Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
263
+ }
264
+ const viewClasses = Array.from(data.viewItem.getClassNames());
265
+ const matchedClass = classes.find(cls => viewClasses.includes(cls));
266
+ if (matchedClass === undefined) {
267
+ return;
268
+ }
269
+ let applied = false;
270
+ let indentLevel;
271
+ for (const item of data.modelRange.getItems({ shallow: true })) {
272
+ if (indentLevel === undefined) {
273
+ indentLevel = item.getAttribute('listIndent');
274
+ }
275
+ if (item.hasAttribute(attributeName)) {
276
+ continue;
277
+ }
278
+ if (item.getAttribute('listIndent') !== indentLevel) {
279
+ continue;
280
+ }
281
+ writer.setAttribute(attributeName, matchedClass, item);
282
+ applied = true;
283
+ }
284
+ if (applied) {
285
+ consumable.consume(data.viewItem, { classes: matchedClass });
286
+ }
287
+ };
288
+ }
289
+ function listBlockIndentUpcastConverter(attributeName, marginProperty) {
290
+ return (evt, data, conversionApi) => {
291
+ const { writer, consumable } = conversionApi;
292
+ if (!data.modelRange) {
293
+ Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
294
+ }
295
+ const marginValue = data.viewItem.getStyle(marginProperty);
296
+ let applied = false;
297
+ let indentLevel;
298
+ for (const item of data.modelRange.getItems({ shallow: true })) {
299
+ if (indentLevel === undefined) {
300
+ indentLevel = item.getAttribute('listIndent');
301
+ }
302
+ if (item.hasAttribute(attributeName)) {
303
+ continue;
304
+ }
305
+ if (item.getAttribute('listIndent') !== indentLevel) {
306
+ continue;
307
+ }
308
+ writer.setAttribute(attributeName, marginValue, item);
309
+ applied = true;
310
+ }
311
+ if (applied) {
312
+ consumable.consume(data.viewItem, { styles: marginProperty });
313
+ }
314
+ };
315
+ }
316
+ function ensureIndentValuesConsistency(attributeName, node, previousNodeInList, writer) {
317
+ const prevNodeIndentListValue = previousNodeInList.getAttribute(attributeName);
318
+ if (node.getAttribute(attributeName) === prevNodeIndentListValue) {
319
+ return false;
320
+ }
321
+ if (prevNodeIndentListValue) {
322
+ writer.setAttribute(attributeName, prevNodeIndentListValue, node);
323
+ }
324
+ else {
325
+ writer.removeAttribute(attributeName, node);
326
+ }
327
+ return true;
328
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
+ */
5
+ /**
6
+ * @module indent/integrations/indentblocklistitemcommand
7
+ */
8
+ import { Command, type Editor } from 'ckeditor5/src/core.js';
9
+ import type { IndentBehavior } from '../indentcommandbehavior/indentbehavior.js';
10
+ /**
11
+ * The indent block list item command.
12
+ *
13
+ * The command is registered by the {@link module:indent/integrations/indentblocklistintegration~IndentBlockListIntegration} as
14
+ * `'indentBlockListItem'` for indenting list items and `'outdentBlockListItem'` for outdenting list items.
15
+ *
16
+ * It's only possible to reset the block indentation of a list item to `0`.
17
+ * This means that if a list item has negative block indentation, the command will only allow forward indentation
18
+ * to make it possible to reset it to `0` and if a list item has positive block indentation, the command
19
+ * will only allow backward indentation to make it possible to reset it to `0`.
20
+ *
21
+ * To increase block indentation of the list item, execute the command:
22
+ *
23
+ * ```ts
24
+ * editor.execute( 'indentBlockListItem' );
25
+ * ```
26
+ *
27
+ * To decrease block indentation of the list item, execute the command:
28
+ *
29
+ * ```ts
30
+ * editor.execute( 'outdentBlockListItem' );
31
+ * ```
32
+ */
33
+ export declare class IndentBlockListItemCommand extends Command {
34
+ /**
35
+ * The command's indentation behavior.
36
+ */
37
+ private readonly _indentBehavior;
38
+ /**
39
+ * Creates an instance of the command.
40
+ */
41
+ constructor(editor: Editor, indentBehavior: IndentBehavior);
42
+ /**
43
+ * @inheritDoc
44
+ */
45
+ refresh(): void;
46
+ /**
47
+ * @inheritDoc
48
+ */
49
+ execute(): void;
50
+ /**
51
+ * Returns an array of list items which block indentation should be changed.
52
+ */
53
+ private _getAffectedListItems;
54
+ /**
55
+ * Returns `true` if changing the block indentation is allowed for the given list item.
56
+ *
57
+ * Indentation of a list item is only allowed if it moves toward zero. This means that:
58
+ * - when currentIndent = 0, the command should be disabled
59
+ * - when currentIndent < 0, only forward indentation should be allowed
60
+ * - when currentIndent > 0, only backward indentation should be allowed
61
+ *
62
+ * For classes-based indentation, the command should be enabled if there is a class to be removed.
63
+ */
64
+ private _isIndentationChangeAllowed;
65
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
+ */
5
+ /**
6
+ * @module indent/integrations/indentblocklistitemcommand
7
+ */
8
+ import { Command } from 'ckeditor5/src/core.js';
9
+ /**
10
+ * The indent block list item command.
11
+ *
12
+ * The command is registered by the {@link module:indent/integrations/indentblocklistintegration~IndentBlockListIntegration} as
13
+ * `'indentBlockListItem'` for indenting list items and `'outdentBlockListItem'` for outdenting list items.
14
+ *
15
+ * It's only possible to reset the block indentation of a list item to `0`.
16
+ * This means that if a list item has negative block indentation, the command will only allow forward indentation
17
+ * to make it possible to reset it to `0` and if a list item has positive block indentation, the command
18
+ * will only allow backward indentation to make it possible to reset it to `0`.
19
+ *
20
+ * To increase block indentation of the list item, execute the command:
21
+ *
22
+ * ```ts
23
+ * editor.execute( 'indentBlockListItem' );
24
+ * ```
25
+ *
26
+ * To decrease block indentation of the list item, execute the command:
27
+ *
28
+ * ```ts
29
+ * editor.execute( 'outdentBlockListItem' );
30
+ * ```
31
+ */
32
+ export class IndentBlockListItemCommand extends Command {
33
+ /**
34
+ * The command's indentation behavior.
35
+ */
36
+ _indentBehavior;
37
+ /**
38
+ * Creates an instance of the command.
39
+ */
40
+ constructor(editor, indentBehavior) {
41
+ super(editor);
42
+ this._indentBehavior = indentBehavior;
43
+ }
44
+ /**
45
+ * @inheritDoc
46
+ */
47
+ refresh() {
48
+ this.isEnabled = this._getAffectedListItems().length > 0;
49
+ }
50
+ /**
51
+ * @inheritDoc
52
+ */
53
+ execute() {
54
+ const editor = this.editor;
55
+ const model = editor.model;
56
+ model.change(writer => {
57
+ for (const block of this._getAffectedListItems()) {
58
+ writer.removeAttribute('blockIndentListItem', block);
59
+ }
60
+ });
61
+ }
62
+ /**
63
+ * Returns an array of list items which block indentation should be changed.
64
+ */
65
+ _getAffectedListItems() {
66
+ const model = this.editor.model;
67
+ const selection = model.document.selection;
68
+ const listUtils = this.editor.plugins.get('ListUtils');
69
+ const blocksInSelection = Array.from(selection.getSelectedBlocks());
70
+ const expandedBlocks = listUtils.expandListBlocksToCompleteItems(blocksInSelection);
71
+ return expandedBlocks.filter(block => this._isIndentationChangeAllowed(block));
72
+ }
73
+ /**
74
+ * Returns `true` if changing the block indentation is allowed for the given list item.
75
+ *
76
+ * Indentation of a list item is only allowed if it moves toward zero. This means that:
77
+ * - when currentIndent = 0, the command should be disabled
78
+ * - when currentIndent < 0, only forward indentation should be allowed
79
+ * - when currentIndent > 0, only backward indentation should be allowed
80
+ *
81
+ * For classes-based indentation, the command should be enabled if there is a class to be removed.
82
+ */
83
+ _isIndentationChangeAllowed(element) {
84
+ if (!element.hasAttribute('blockIndentListItem')) {
85
+ return false;
86
+ }
87
+ const currentIndent = parseFloat(element.getAttribute('blockIndentListItem'));
88
+ // Class based indent, allow only outdent.
89
+ // TODO find a better way, probably use behavior to find out.
90
+ if (isNaN(currentIndent)) {
91
+ return !this._indentBehavior.isForward;
92
+ }
93
+ return this._indentBehavior.isForward && currentIndent < 0 ||
94
+ !this._indentBehavior.isForward && currentIndent > 0;
95
+ }
96
+ }