1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Command } from 'ckeditor5/src/core';
|
9 | import { first } from 'ckeditor5/src/utils';
|
10 | import { expandListBlocksToCompleteList, isListItemBlock } from '../documentlist/utils/model';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export default class DocumentListStartCommand extends Command {
|
17 | |
18 |
|
19 |
|
20 | refresh() {
|
21 | const value = this._getValue();
|
22 | this.value = value;
|
23 | this.isEnabled = value != null;
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | execute({ startIndex = 1 } = {}) {
|
32 | const model = this.editor.model;
|
33 | const document = model.document;
|
34 | let blocks = Array.from(document.selection.getSelectedBlocks())
|
35 | .filter(block => isListItemBlock(block) && block.getAttribute('listType') == 'numbered');
|
36 | blocks = expandListBlocksToCompleteList(blocks);
|
37 | model.change(writer => {
|
38 | for (const block of blocks) {
|
39 | writer.setAttribute('listStart', startIndex >= 0 ? startIndex : 1, block);
|
40 | }
|
41 | });
|
42 | }
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | _getValue() {
|
49 | const model = this.editor.model;
|
50 | const document = model.document;
|
51 | const block = first(document.selection.getSelectedBlocks());
|
52 | if (block && isListItemBlock(block) && block.getAttribute('listType') == 'numbered') {
|
53 | return block.getAttribute('listStart');
|
54 | }
|
55 | return null;
|
56 | }
|
57 | }
|