UNPKG

2.09 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5/**
6 * @module list/documentlistproperties/documentliststartcommand
7 */
8import { Command } from 'ckeditor5/src/core';
9import { first } from 'ckeditor5/src/utils';
10import { expandListBlocksToCompleteList, isListItemBlock } from '../documentlist/utils/model';
11/**
12 * The list start index command. It changes the `listStart` attribute of the selected list items,
13 * letting the user to choose the starting point of an ordered list.
14 * It is used by the {@link module:list/documentlistproperties~DocumentListProperties list properties feature}.
15 */
16export default class DocumentListStartCommand extends Command {
17 /**
18 * @inheritDoc
19 */
20 refresh() {
21 const value = this._getValue();
22 this.value = value;
23 this.isEnabled = value != null;
24 }
25 /**
26 * Executes the command.
27 *
28 * @fires execute
29 * @param options.startIndex The list start index.
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 * Checks the command's {@link #value}.
45 *
46 * @returns The current value.
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}