This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathheading.js
222 lines (193 loc) · 7.24 KB
/
heading.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module heading/heading
*/
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import HeadingEngine from './headingengine';
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import Model from '@ckeditor/ckeditor5-ui/src/model';
import bindOneToMany from '@ckeditor/ckeditor5-ui/src/bindings/bindonetomany';
import { createDropdown, addListViewToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
import '../theme/heading.css';
/**
* The headings feature. It introduces the `headings` drop-down and the `heading1`-`headingN` commands which allow
* to convert paragraphs into headings.
*
* For a detailed overview, check the {@glink features/headings Headings feature documentation}.
*
* @extends module:core/plugin~Plugin
*/
export default class Heading extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ Paragraph, HeadingEngine ];
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'Heading';
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const dropdownItems = new Collection();
const options = this._getLocalizedOptions();
const commands = [];
const t = editor.t;
const defaultTitle = t( 'Choose heading' );
const dropdownTooltip = t( 'Heading' );
for ( const option of options ) {
const command = editor.commands.get( option.model );
const itemModel = new Model( {
commandName: option.model,
label: option.title,
class: option.class
} );
itemModel.bind( 'isActive' ).to( command, 'value' );
// Add the option to the collection.
dropdownItems.add( itemModel );
commands.push( command );
}
// Register UI component.
editor.ui.componentFactory.add( 'headings', locale => {
const dropdownView = createDropdown( locale );
dropdownView.set( {
isOn: false,
withText: true,
tooltip: dropdownTooltip
} );
addListViewToDropdown( dropdownView, dropdownItems );
bindOneToMany( dropdownView, 'isEnabled', commands, 'isEnabled', ( ...areEnabled ) => {
return areEnabled.some( isEnabled => isEnabled );
} );
bindOneToMany( dropdownView, 'label', commands, 'value', ( ...areActive ) => {
const index = areActive.findIndex( value => value );
// If none of the commands is active, display default title.
return options[ index ] ? options[ index ].title : defaultTitle;
} );
dropdownView.extendTemplate( {
attributes: {
class: [
'ck-heading-dropdown'
]
}
} );
// Execute command when an item from the dropdown is selected.
this.listenTo( dropdownView, 'execute', evt => {
editor.execute( evt.source.commandName );
editor.editing.view.focus();
} );
return dropdownView;
} );
}
/**
* Returns heading options as defined in `config.heading.options` but processed to consider
* editor localization, i.e. to display {@link module:heading/heading~HeadingOption}
* in the correct language.
*
* Note: The reason behind this method is that there's no way to use {@link module:utils/locale~Locale#t}
* when the user config is defined because the editor does not exist yet.
*
* @private
* @returns {Array.<module:heading/heading~HeadingOption>}.
*/
_getLocalizedOptions() {
const editor = this.editor;
const t = editor.t;
const localizedTitles = {
Paragraph: t( 'Paragraph' ),
'Heading 1': t( 'Heading 1' ),
'Heading 2': t( 'Heading 2' ),
'Heading 3': t( 'Heading 3' )
};
return editor.config.get( 'heading.options' ).map( option => {
const title = localizedTitles[ option.title ];
if ( title && title != option.title ) {
// Clone the option to avoid altering the original `config.heading.options`.
option = Object.assign( {}, option, { title } );
}
return option;
} );
}
}
/**
* The configuration of the heading feature. Introduced by the {@link module:heading/headingengine~HeadingEngine} feature.
*
* Read more in {@link module:heading/heading~HeadingConfig}.
*
* @member {module:heading/heading~HeadingConfig} module:core/editor/editorconfig~EditorConfig#heading
*/
/**
* The configuration of the heading feature.
* The option is used by the {@link module:heading/headingengine~HeadingEngine} feature.
*
* ClassicEditor
* .create( {
* heading: ... // Heading feature config.
* } )
* .then( ... )
* .catch( ... );
*
* See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
*
* @interface HeadingConfig
*/
/**
* The available heading options.
*
* The default value is:
*
* const headingConfig = {
* options: [
* { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
* { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
* { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
* { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
* ]
* };
*
* It defines 3 levels of headings. In the editor model they will use `heading1`, `heading2`, and `heading3` elements.
* Their respective view elements (so the elements output by the editor) will be: `h2`, `h3`, and `h4`. This means that
* if you choose "Heading 1" in the headings dropdown the editor will turn the current block to `<heading1>` in the model
* which will result in rendering (and outputting to data) the `<h2>` element.
*
* The `title` and `class` properties will be used by the `headings` dropdown to render available options.
* Usually, the first option in the headings dropdown is the "Paragraph" option, hence it's also defined on the list.
* However, you don't need to define its view representation because it's handled by
* the {@link module:paragraph/paragraph~Paragraph} feature (which is required by
* the {@link module:heading/headingengine~HeadingEngine} feature).
*
* You can **read more** about configuring heading levels and **see more examples** in
* the {@glink features/headings Headings} guide.
*
* Note: In the model you should always start from `heading1`, regardless of how the headings are represented in the view.
* That's assumption is used by features like {@link module:autoformat/autoformat~Autoformat} to know which element
* they should use when applying the first level heading.
*
* The defined headings are also available in {@link module:core/commandcollection~CommandCollection} under their model names.
* For example, the below code will apply `<heading1>` to the current selection:
*
* editor.execute( 'heading1' );
*
* @member {Array.<module:heading/heading~HeadingOption>} module:heading/heading~HeadingConfig#options
*/
/**
* Heading option descriptor.
*
* This format is compatible with {@link module:engine/conversion/definition-based-converters~ConverterDefinition}
* and adds to additional properties: `title` and `class`.
*
* @typedef {Object} module:heading/heading~HeadingOption
* @extends module:engine/conversion/definition-based-converters~ConverterDefinition
* @property {String} title The user-readable title of the option.
* @property {String} class The class which will be added to the dropdown item representing this option.
*/