/
checktododocumentlistcommand.ts
104 lines (89 loc) · 3.24 KB
/
checktododocumentlistcommand.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
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module list/tododocumentlist/checktododocumentlistcommand
*/
import { Command, type Editor } from 'ckeditor5/src/core';
import type { Element } from 'ckeditor5/src/engine';
import { getAllListItemBlocks } from '../documentlist/utils/model';
/**
* The check to-do command.
*
* The command is registered by the {@link module:list/tododocumentlist/tododocumentlistediting~TodoDocumentListEditing} as
* the `checkTodoList` editor command.
*/
export default class CheckTodoDocumentListCommand extends Command {
/**
* A list of to-do list items selected by the {@link module:engine/model/selection~Selection}.
*
* @observable
* @readonly
*/
declare public value: boolean;
/**
* @inheritDoc
*/
constructor( editor: Editor ) {
super( editor );
// Refresh command before executing to be sure all values are up to date.
// It is needed when selection has changed before command execution, in the same change block.
this.on( 'execute', () => {
this.refresh();
}, { priority: 'highest' } );
}
/**
* Updates the command's {@link #value} and {@link #isEnabled} properties based on the current selection.
*/
public override refresh(): void {
const selectedElements = this._getSelectedItems();
this.value = this._getValue( selectedElements );
this.isEnabled = !!selectedElements.length;
}
/**
* Executes the command.
*
* @param options.forceValue If set, it will force the command behavior. If `true`, the command will apply
* the attribute. Otherwise, the command will remove the attribute. If not set, the command will look for its current
* value to decide what it should do.
*/
public override execute( options: { forceValue?: boolean } = {} ): void {
this.editor.model.change( writer => {
const selectedElements = this._getSelectedItems();
const value = ( options.forceValue === undefined ) ? !this._getValue( selectedElements ) : options.forceValue;
for ( const element of selectedElements ) {
if ( value ) {
writer.setAttribute( 'todoListChecked', true, element );
} else {
writer.removeAttribute( 'todoListChecked', element );
}
}
} );
}
/**
* Returns a value for the command.
*/
private _getValue( selectedElements: Array<Element> ): boolean {
return selectedElements.every( element => element.getAttribute( 'todoListChecked' ) );
}
/**
* Gets all to-do list items selected by the {@link module:engine/model/selection~Selection}.
*/
private _getSelectedItems() {
const model = this.editor.model;
const schema = model.schema;
const selectionRange = model.document.selection.getFirstRange()!;
const startElement = selectionRange.start.parent as Element;
const elements: Array<Element> = [];
if ( schema.checkAttribute( startElement, 'todoListChecked' ) ) {
elements.push( ...getAllListItemBlocks( startElement ) );
}
for ( const item of selectionRange.getItems( { shallow: true } ) as Iterable<Element> ) {
if ( schema.checkAttribute( item, 'todoListChecked' ) && !elements.includes( item ) ) {
elements.push( ...getAllListItemBlocks( item ) );
}
}
return elements;
}
}