This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
/
headingcommand.js
96 lines (84 loc) · 2.94 KB
/
headingcommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module heading/headingcommand
*/
import Command from '@ckeditor/ckeditor5-core/src/command';
import first from '@ckeditor/ckeditor5-utils/src/first';
/**
* The heading command. It is used by the {@link module:heading/heading~Heading heading feature} to apply headings.
*
* @extends module:core/command~Command
*/
export default class HeadingCommand extends Command {
/**
* Creates an instance of the command.
*
* @param {module:core/editor/editor~Editor} editor Editor instance.
* @param {Array.<String>} modelElements Names of the element which this command can apply in the model.
*/
constructor( editor, modelElements ) {
super( editor );
/**
* If the selection starts in a heading (which {@link #modelElements is supported by this command})
* the value is set to the name of that heading model element.
* It is set to `false` otherwise.
*
* @observable
* @readonly
* @member {Boolean|String} #value
*/
/**
* Set of defined model's elements names that this command support.
* See {@link module:heading/heading~HeadingOption}.
*
* @readonly
* @member {Array.<String>}
*/
this.modelElements = modelElements;
}
/**
* @inheritDoc
*/
refresh() {
const block = first( this.editor.model.document.selection.getSelectedBlocks() );
this.value = !!block && this.modelElements.includes( block.name ) && block.name;
this.isEnabled = !!block && this.modelElements.some( heading => checkCanBecomeHeading( block, heading, this.editor.model.schema ) );
}
/**
* Executes the command. Applies the heading to the selected blocks or, if the first selected
* block is a heading already, turns selected headings (of this level only) to paragraphs.
*
* @param {Object} options
* @param {String} options.value Name of the element which this command will apply in the model.
* @fires execute
*/
execute( options ) {
const model = this.editor.model;
const document = model.document;
const modelElement = options.value;
model.change( writer => {
const blocks = Array.from( document.selection.getSelectedBlocks() )
.filter( block => {
return checkCanBecomeHeading( block, modelElement, model.schema );
} );
for ( const block of blocks ) {
if ( !block.is( modelElement ) ) {
writer.rename( block, modelElement );
}
}
} );
}
}
// Checks whether the given block can be replaced by a specific heading.
//
// @private
// @param {module:engine/model/element~Element} block A block to be tested.
// @param {module:heading/headingcommand~HeadingCommand#modelElement} heading Command element name in the model.
// @param {module:engine/model/schema~Schema} schema The schema of the document.
// @returns {Boolean}
function checkCanBecomeHeading( block, heading, schema ) {
return schema.checkChild( block.parent, heading ) && !schema.isObject( block );
}