UNPKG

1.71 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/listproperties/liststartcommand
7 */
8import { Command } from 'ckeditor5/src/core';
9import { getSelectedListItems } from '../list/utils';
10/**
11 * The list start index command. It changes the `listStart` attribute of the selected list items.
12 * It is used by the {@link module:list/listproperties~ListProperties list properties feature}.
13 */
14export default class ListStartCommand extends Command {
15 /**
16 * @inheritDoc
17 */
18 refresh() {
19 const value = this._getValue();
20 this.value = value;
21 this.isEnabled = value != null;
22 }
23 /**
24 * Executes the command.
25 *
26 * @fires execute
27 * @param options.startIndex The list start index.
28 */
29 execute({ startIndex = 1 } = {}) {
30 const model = this.editor.model;
31 const listItems = getSelectedListItems(model)
32 .filter(item => item.getAttribute('listType') == 'numbered');
33 model.change(writer => {
34 for (const item of listItems) {
35 writer.setAttribute('listStart', startIndex >= 0 ? startIndex : 1, item);
36 }
37 });
38 }
39 /**
40 * Checks the command's {@link #value}.
41 *
42 * @returns The current value.
43 */
44 _getValue() {
45 const listItem = this.editor.model.document.selection.getFirstPosition().parent;
46 if (listItem && listItem.is('element', 'listItem') && listItem.getAttribute('listType') == 'numbered') {
47 return listItem.getAttribute('listStart');
48 }
49 return null;
50 }
51}