UNPKG

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