-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
headingui.js
110 lines (90 loc) · 3.05 KB
/
headingui.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module heading/headingui
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import Model from '@ckeditor/ckeditor5-ui/src/model';
import { createDropdown, addListToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
import { getLocalizedOptions } from './utils';
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
import '../theme/heading.css';
/**
* The headings UI feature. It introduces the `headings` dropdown.
*
* @extends module:core/plugin~Plugin
*/
export default class HeadingUI extends Plugin {
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const t = editor.t;
const options = getLocalizedOptions( editor );
const defaultTitle = t( 'Choose heading' );
const dropdownTooltip = t( 'Heading' );
// Register UI component.
editor.ui.componentFactory.add( 'heading', locale => {
const titles = {};
const itemDefinitions = new Collection();
const headingCommand = editor.commands.get( 'heading' );
const paragraphCommand = editor.commands.get( 'paragraph' );
const commands = [ headingCommand ];
for ( const option of options ) {
const def = {
type: 'button',
model: new Model( {
label: option.title,
class: option.class,
withText: true
} )
};
if ( option.model === 'paragraph' ) {
def.model.bind( 'isOn' ).to( paragraphCommand, 'value' );
def.model.set( 'commandName', 'paragraph' );
commands.push( paragraphCommand );
} else {
def.model.bind( 'isOn' ).to( headingCommand, 'value', value => value === option.model );
def.model.set( {
commandName: 'heading',
commandValue: option.model
} );
}
// Add the option to the collection.
itemDefinitions.add( def );
titles[ option.model ] = option.title;
}
const dropdownView = createDropdown( locale );
addListToDropdown( dropdownView, itemDefinitions );
dropdownView.buttonView.set( {
isOn: false,
withText: true,
tooltip: dropdownTooltip
} );
dropdownView.extendTemplate( {
attributes: {
class: [
'ck-heading-dropdown'
]
}
} );
dropdownView.bind( 'isEnabled' ).toMany( commands, 'isEnabled', ( ...areEnabled ) => {
return areEnabled.some( isEnabled => isEnabled );
} );
dropdownView.buttonView.bind( 'label' ).to( headingCommand, 'value', paragraphCommand, 'value', ( value, para ) => {
const whichModel = value || para && 'paragraph';
// If none of the commands is active, display default title.
return titles[ whichModel ] ? titles[ whichModel ] : defaultTitle;
} );
// Execute command when an item from the dropdown is selected.
this.listenTo( dropdownView, 'execute', evt => {
editor.execute( evt.source.commandName, evt.source.commandValue ? { value: evt.source.commandValue } : undefined );
editor.editing.view.focus();
} );
return dropdownView;
} );
}
}