/
indentblockcommand.ts
128 lines (104 loc) · 3.48 KB
/
indentblockcommand.ts
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
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module indent/indentblockcommand
*/
import { Command, type Editor } from 'ckeditor5/src/core.js';
import type { Element } from 'ckeditor5/src/engine.js';
import type { ListUtils } from '@ckeditor/ckeditor5-list';
import { first } from 'ckeditor5/src/utils.js';
import type { IndentBehavior } from './indentcommandbehavior/indentbehavior.js';
/**
* The indent block command.
*
* The command is registered by the {@link module:indent/indentblock~IndentBlock} as `'indentBlock'` for indenting blocks and
* `'outdentBlock'` for outdenting blocks.
*
* To increase block indentation at the current selection, execute the command:
*
* ```ts
* editor.execute( 'indentBlock' );
* ```
*
* To decrease block indentation at the current selection, execute the command:
*
* ```ts
* editor.execute( 'outdentBlock' );
* ```
*/
export default class IndentBlockCommand extends Command {
/**
* The command's indentation behavior.
*/
private readonly _indentBehavior: IndentBehavior;
/**
* Creates an instance of the command.
*/
constructor( editor: Editor, indentBehavior: IndentBehavior ) {
super( editor );
this._indentBehavior = indentBehavior;
}
/**
* @inheritDoc
*/
public override refresh(): void {
const editor = this.editor;
const model = editor.model;
const block = first( model.document.selection.getSelectedBlocks() );
if ( !block || !this._isIndentationChangeAllowed( block ) ) {
this.isEnabled = false;
return;
}
this.isEnabled = this._indentBehavior.checkEnabled( block.getAttribute( 'blockIndent' ) as string );
}
/**
* @inheritDoc
*/
public override execute(): void {
const model = this.editor.model;
const blocksToChange = this._getBlocksToChange();
model.change( writer => {
for ( const block of blocksToChange ) {
const currentIndent = block.getAttribute( 'blockIndent' ) as string;
const nextIndent = this._indentBehavior.getNextIndent( currentIndent );
if ( nextIndent ) {
writer.setAttribute( 'blockIndent', nextIndent, block );
} else {
writer.removeAttribute( 'blockIndent', block );
}
}
} );
}
/**
* Returns blocks from selection that should have blockIndent selection set.
*/
private _getBlocksToChange(): Array<Element> {
const model = this.editor.model;
const selection = model.document.selection;
const blocksInSelection = Array.from( selection.getSelectedBlocks() );
return blocksInSelection.filter( block => this._isIndentationChangeAllowed( block ) );
}
/**
* Returns false if indentation cannot be applied, i.e.:
* - for blocks disallowed by schema declaration
* - for blocks in Document Lists (disallowed forward indentation only). See https://github.com/ckeditor/ckeditor5/issues/14155.
* Otherwise returns true.
*/
private _isIndentationChangeAllowed( element: Element ): boolean {
const editor = this.editor;
if ( !editor.model.schema.checkAttribute( element, 'blockIndent' ) ) {
return false;
}
if ( !editor.plugins.has( 'ListUtils' ) ) {
return true;
}
// Only forward indentation is disallowed in list items. This allows the user to outdent blocks that are already indented.
if ( !this._indentBehavior.isForward ) {
return true;
}
const documentListUtils: ListUtils = editor.plugins.get( 'ListUtils' );
return !documentListUtils.isListItemBlock( element );
}
}