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 pathalignmentui.js
169 lines (139 loc) · 4.29 KB
/
alignmentui.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
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module alignment/alignmentui
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
import bindOneToMany from '@ckeditor/ckeditor5-ui/src/bindings/bindonetomany';
import { createDropdown, addToolbarToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
import { commandNameFromOptionName } from './alignmentcommand';
import { isSupported } from './alignmentediting';
import alignLeftIcon from '../theme/icons/align-left.svg';
import alignRightIcon from '../theme/icons/align-right.svg';
import alignCenterIcon from '../theme/icons/align-center.svg';
import alignJustifyIcon from '../theme/icons/align-justify.svg';
const icons = new Map( [
[ 'left', alignLeftIcon ],
[ 'right', alignRightIcon ],
[ 'center', alignCenterIcon ],
[ 'justify', alignJustifyIcon ]
] );
/**
* The default Alignment UI plugin.
*
* It introduces the `'alignLeft'`, `'alignRight'`, `'alignCenter'` and `'alignJustify'` buttons
* and the `'alignmentDropdown'` dropdown.
*
* @extends module:core/plugin~Plugin
*/
export default class AlignmentUI extends Plugin {
/**
* Returns the localized option titles provided by the plugin.
*
* The following localized titles corresponding with
* {@link module:alignment/alignment~AlignmentConfig#options} are available:
*
* * `'left'`,
* * `'right'`,
* * `'center'`,
* * `'justify'`.
*
* @readonly
* @type {Object.<String,String>}
*/
get localizedOptionTitles() {
const t = this.editor.t;
return {
'left': t( 'Align left' ),
'right': t( 'Align right' ),
'center': t( 'Align center' ),
'justify': t( 'Justify' )
};
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'AlignmentUI';
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const componentFactory = editor.ui.componentFactory;
const t = editor.t;
const options = editor.config.get( 'alignment.options' );
options
.filter( isSupported )
.forEach( option => this._addButton( option ) );
componentFactory.add( 'alignmentDropdown', locale => {
const dropdownView = createDropdown( locale );
dropdownView.set( {
label: t( 'Text alignment' ),
defaultIcon: alignLeftIcon,
withText: false,
isVertical: true,
tooltip: true
} );
const buttons = options.map( option => {
return componentFactory.create( commandNameFromOptionName( option ) );
} );
// TODO: binding with callback as in headings
// Add specialised behavior
// Change icon upon selection
bindOneToMany( dropdownView, 'icon', buttons, 'isOn', ( ...areActive ) => {
const index = areActive.findIndex( value => value );
// If none of the commands is active, display either defaultIcon or first button icon.
if ( index < 0 && dropdownView.defaultIcon ) {
return dropdownView.defaultIcon;
}
return buttons[ index < 0 ? 0 : index ].icon;
} );
bindOneToMany( dropdownView, 'isEnabled', buttons, 'isEnabled',
( ...areEnabled ) => areEnabled.some( isEnabled => isEnabled )
);
addToolbarToDropdown( dropdownView, buttons );
return dropdownView;
} );
}
/**
* Helper method for initializing a button and linking it with an appropriate command.
*
* @private
* @param {String} option The name of the alignment option for which to add a button.
*/
_addButton( option ) {
const editor = this.editor;
const commandName = commandNameFromOptionName( option );
const command = editor.commands.get( commandName );
editor.ui.componentFactory.add( commandName, locale => {
const buttonView = new ButtonView( locale );
buttonView.set( {
label: this.localizedOptionTitles[ option ],
icon: icons.get( option ),
tooltip: true
} );
// Bind button model to command.
buttonView.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
// Execute command.
this.listenTo( buttonView, 'execute', () => {
editor.execute( commandName );
editor.editing.view.focus();
} );
return buttonView;
} );
}
}
/**
* TODO: move somewhere
* Defines default icon which is used when no button is active.
*
* Also see {@link #icon}.
*
* @observable
* @member {String} #defaultIcon
*/