UNPKG

2.03 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/documentlistreversedcommand
7 */
8import { Command } from 'ckeditor5/src/core';
9import { first } from 'ckeditor5/src/utils';
10import { expandListBlocksToCompleteList, isListItemBlock } from '../documentlist/utils/model';
11/**
12 * The list reversed command. It changes the `listReversed` attribute of the selected list items,
13 * letting the user to choose the order of an ordered list.
14 * It is used by the {@link module:list/documentlistproperties~DocumentListProperties list properties feature}.
15 */
16export default class DocumentListReversedCommand 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.reversed Whether the list should be reversed.
30 */
31 execute(options = {}) {
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('listReversed', !!options.reversed, block);
40 }
41 });
42 }
43 /**
44 * Checks the command's {@link #value}.
45 */
46 _getValue() {
47 const model = this.editor.model;
48 const document = model.document;
49 const block = first(document.selection.getSelectedBlocks());
50 if (isListItemBlock(block) && block.getAttribute('listType') == 'numbered') {
51 return block.getAttribute('listReversed');
52 }
53 return null;
54 }
55}