@ckeditor/ckeditor5-alignment 35.3.2 → 36.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,188 +1,147 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module alignment/alignmentediting
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
-
12
9
  import AlignmentCommand from './alignmentcommand';
13
10
  import { isDefault, isSupported, normalizeAlignmentOptions, supportedOptions } from './utils';
14
-
15
11
  /**
16
12
  * The alignment editing feature. It introduces the {@link module:alignment/alignmentcommand~AlignmentCommand command} and adds
17
13
  * the `alignment` attribute for block elements in the {@link module:engine/model/model~Model model}.
18
- * @extends module:core/plugin~Plugin
19
14
  */
20
15
  export default class AlignmentEditing extends Plugin {
21
- /**
22
- * @inheritDoc
23
- */
24
- static get pluginName() {
25
- return 'AlignmentEditing';
26
- }
27
-
28
- /**
29
- * @inheritDoc
30
- */
31
- constructor( editor ) {
32
- super( editor );
33
-
34
- editor.config.define( 'alignment', {
35
- options: [ ...supportedOptions.map( option => ( { name: option } ) ) ]
36
- } );
37
- }
38
-
39
- /**
40
- * @inheritDoc
41
- */
42
- init() {
43
- const editor = this.editor;
44
- const locale = editor.locale;
45
- const schema = editor.model.schema;
46
-
47
- const options = normalizeAlignmentOptions( editor.config.get( 'alignment.options' ) );
48
-
49
- // Filter out unsupported options and those that are redundant, e.g. `left` in LTR / `right` in RTL mode.
50
- const optionsToConvert = options.filter(
51
- option => isSupported( option.name ) && !isDefault( option.name, locale )
52
- );
53
-
54
- // Once there is at least one `className` defined, we switch to alignment with classes.
55
- const shouldUseClasses = optionsToConvert.some( option => !!option.className );
56
-
57
- // Allow alignment attribute on all blocks.
58
- schema.extend( '$block', { allowAttributes: 'alignment' } );
59
- editor.model.schema.setAttributeProperties( 'alignment', { isFormatting: true } );
60
-
61
- if ( shouldUseClasses ) {
62
- editor.conversion.attributeToAttribute( buildClassDefinition( optionsToConvert ) );
63
- } else {
64
- // Downcast inline styles.
65
- editor.conversion.for( 'downcast' ).attributeToAttribute( buildDowncastInlineDefinition( optionsToConvert ) );
66
- }
67
-
68
- const upcastInlineDefinitions = buildUpcastInlineDefinitions( optionsToConvert );
69
-
70
- // Always upcast from inline styles.
71
- for ( const definition of upcastInlineDefinitions ) {
72
- editor.conversion.for( 'upcast' ).attributeToAttribute( definition );
73
- }
74
-
75
- const upcastCompatibilityDefinitions = buildUpcastCompatibilityDefinitions( optionsToConvert );
76
-
77
- // Always upcast from deprecated `align` attribute.
78
- for ( const definition of upcastCompatibilityDefinitions ) {
79
- editor.conversion.for( 'upcast' ).attributeToAttribute( definition );
80
- }
81
-
82
- editor.commands.add( 'alignment', new AlignmentCommand( editor ) );
83
- }
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ static get pluginName() {
20
+ return 'AlignmentEditing';
21
+ }
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ constructor(editor) {
26
+ super(editor);
27
+ editor.config.define('alignment', {
28
+ options: supportedOptions.map(option => ({ name: option }))
29
+ });
30
+ }
31
+ /**
32
+ * @inheritDoc
33
+ */
34
+ init() {
35
+ const editor = this.editor;
36
+ const locale = editor.locale;
37
+ const schema = editor.model.schema;
38
+ const options = normalizeAlignmentOptions(editor.config.get('alignment.options'));
39
+ // Filter out unsupported options and those that are redundant, e.g. `left` in LTR / `right` in RTL mode.
40
+ const optionsToConvert = options.filter(option => isSupported(option.name) && !isDefault(option.name, locale));
41
+ // Once there is at least one `className` defined, we switch to alignment with classes.
42
+ const shouldUseClasses = optionsToConvert.some(option => !!option.className);
43
+ // Allow alignment attribute on all blocks.
44
+ schema.extend('$block', { allowAttributes: 'alignment' });
45
+ editor.model.schema.setAttributeProperties('alignment', { isFormatting: true });
46
+ if (shouldUseClasses) {
47
+ editor.conversion.attributeToAttribute(buildClassDefinition(optionsToConvert));
48
+ }
49
+ else {
50
+ // Downcast inline styles.
51
+ editor.conversion.for('downcast').attributeToAttribute(buildDowncastInlineDefinition(optionsToConvert));
52
+ }
53
+ const upcastInlineDefinitions = buildUpcastInlineDefinitions(optionsToConvert);
54
+ // Always upcast from inline styles.
55
+ for (const definition of upcastInlineDefinitions) {
56
+ editor.conversion.for('upcast').attributeToAttribute(definition);
57
+ }
58
+ const upcastCompatibilityDefinitions = buildUpcastCompatibilityDefinitions(optionsToConvert);
59
+ // Always upcast from deprecated `align` attribute.
60
+ for (const definition of upcastCompatibilityDefinitions) {
61
+ editor.conversion.for('upcast').attributeToAttribute(definition);
62
+ }
63
+ editor.commands.add('alignment', new AlignmentCommand(editor));
64
+ }
84
65
  }
85
-
86
- // Prepare downcast conversion definition for inline alignment styling.
87
- // @private
88
- function buildDowncastInlineDefinition( options ) {
89
- const definition = {
90
- model: {
91
- key: 'alignment',
92
- values: options.map( option => option.name )
93
- },
94
- view: {}
95
- };
96
-
97
- for ( const { name } of options ) {
98
- definition.view[ name ] = {
99
- key: 'style',
100
- value: {
101
- 'text-align': name
102
- }
103
- };
104
- }
105
-
106
- return definition;
107
- }
108
-
109
- // Prepare upcast definitions for inline alignment styles.
110
- // @private
111
- function buildUpcastInlineDefinitions( options ) {
112
- const definitions = [];
113
-
114
- for ( const { name } of options ) {
115
- definitions.push( {
116
- view: {
117
- key: 'style',
118
- value: {
119
- 'text-align': name
120
- }
121
- },
122
- model: {
123
- key: 'alignment',
124
- value: name
125
- }
126
- } );
127
- }
128
-
129
- return definitions;
66
+ /**
67
+ * Prepare downcast conversion definition for inline alignment styling.
68
+ */
69
+ function buildDowncastInlineDefinition(options) {
70
+ const view = {};
71
+ for (const { name } of options) {
72
+ view[name] = {
73
+ key: 'style',
74
+ value: {
75
+ 'text-align': name
76
+ }
77
+ };
78
+ }
79
+ const definition = {
80
+ model: {
81
+ key: 'alignment',
82
+ values: options.map(option => option.name)
83
+ },
84
+ view
85
+ };
86
+ return definition;
130
87
  }
131
-
132
- // Prepare upcast definitions for deprecated `align` attribute.
133
- // @private
134
- function buildUpcastCompatibilityDefinitions( options ) {
135
- const definitions = [];
136
-
137
- for ( const { name } of options ) {
138
- definitions.push( {
139
- view: {
140
- key: 'align',
141
- value: name
142
- },
143
- model: {
144
- key: 'alignment',
145
- value: name
146
- }
147
- } );
148
- }
149
-
150
- return definitions;
88
+ /**
89
+ * Prepare upcast definitions for inline alignment styles.
90
+ */
91
+ function buildUpcastInlineDefinitions(options) {
92
+ const definitions = [];
93
+ for (const { name } of options) {
94
+ definitions.push({
95
+ view: {
96
+ key: 'style',
97
+ value: {
98
+ 'text-align': name
99
+ }
100
+ },
101
+ model: {
102
+ key: 'alignment',
103
+ value: name
104
+ }
105
+ });
106
+ }
107
+ return definitions;
151
108
  }
152
-
153
- // Prepare conversion definitions for upcast and downcast alignment with classes.
154
- // @private
155
- function buildClassDefinition( options ) {
156
- const definition = {
157
- model: {
158
- key: 'alignment',
159
- values: options.map( option => option.name )
160
- },
161
- view: {}
162
- };
163
-
164
- for ( const option of options ) {
165
- definition.view[ option.name ] = {
166
- key: 'class',
167
- value: option.className
168
- };
169
- }
170
-
171
- return definition;
109
+ /**
110
+ * Prepare upcast definitions for deprecated `align` attribute.
111
+ */
112
+ function buildUpcastCompatibilityDefinitions(options) {
113
+ const definitions = [];
114
+ for (const { name } of options) {
115
+ definitions.push({
116
+ view: {
117
+ key: 'align',
118
+ value: name
119
+ },
120
+ model: {
121
+ key: 'alignment',
122
+ value: name
123
+ }
124
+ });
125
+ }
126
+ return definitions;
172
127
  }
173
-
174
128
  /**
175
- * The alignment configuration format descriptor.
176
- *
177
- * const alignmentFormat = {
178
- * name: 'right',
179
- * className: 'my-align-right-class'
180
- * }
181
- *
182
- * @typedef {Object} module:alignment/alignmentediting~AlignmentFormat
183
- *
184
- * @property {'left'|'right'|'center'|'justify'} name One of the alignment names options.
185
- *
186
- * @property {String} className The CSS class used to represent the style in the view.
187
- * Used to override default, inline styling for alignment.
129
+ * Prepare conversion definitions for upcast and downcast alignment with classes.
188
130
  */
131
+ function buildClassDefinition(options) {
132
+ const view = {};
133
+ for (const option of options) {
134
+ view[option.name] = {
135
+ key: 'class',
136
+ value: option.className
137
+ };
138
+ }
139
+ const definition = {
140
+ model: {
141
+ key: 'alignment',
142
+ values: options.map(option => option.name)
143
+ },
144
+ view
145
+ };
146
+ return definition;
147
+ }
@@ -1,162 +1,124 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module alignment/alignmentui
8
7
  */
9
-
10
8
  import { Plugin, icons } from 'ckeditor5/src/core';
11
9
  import { ButtonView, createDropdown, addToolbarToDropdown } from 'ckeditor5/src/ui';
12
-
13
10
  import { isSupported, normalizeAlignmentOptions } from './utils';
14
-
15
- const iconsMap = new Map( [
16
- [ 'left', icons.alignLeft ],
17
- [ 'right', icons.alignRight ],
18
- [ 'center', icons.alignCenter ],
19
- [ 'justify', icons.alignJustify ]
20
- ] );
21
-
11
+ const iconsMap = new Map([
12
+ ['left', icons.alignLeft],
13
+ ['right', icons.alignRight],
14
+ ['center', icons.alignCenter],
15
+ ['justify', icons.alignJustify]
16
+ ]);
22
17
  /**
23
18
  * The default alignment UI plugin.
24
19
  *
25
20
  * It introduces the `'alignment:left'`, `'alignment:right'`, `'alignment:center'` and `'alignment:justify'` buttons
26
21
  * and the `'alignment'` dropdown.
27
- *
28
- * @extends module:core/plugin~Plugin
29
22
  */
30
23
  export default class AlignmentUI extends Plugin {
31
- /**
32
- * Returns the localized option titles provided by the plugin.
33
- *
34
- * The following localized titles corresponding with
35
- * {@link module:alignment/alignment~AlignmentConfig#options} are available:
36
- *
37
- * * `'left'`,
38
- * * `'right'`,
39
- * * `'center'`,
40
- * * `'justify'`.
41
- *
42
- * @readonly
43
- * @type {Object.<String,String>}
44
- */
45
- get localizedOptionTitles() {
46
- const t = this.editor.t;
47
-
48
- return {
49
- 'left': t( 'Align left' ),
50
- 'right': t( 'Align right' ),
51
- 'center': t( 'Align center' ),
52
- 'justify': t( 'Justify' )
53
- };
54
- }
55
-
56
- /**
57
- * @inheritDoc
58
- */
59
- static get pluginName() {
60
- return 'AlignmentUI';
61
- }
62
-
63
- /**
64
- * @inheritDoc
65
- */
66
- init() {
67
- const editor = this.editor;
68
- const componentFactory = editor.ui.componentFactory;
69
- const t = editor.t;
70
- const options = normalizeAlignmentOptions( editor.config.get( 'alignment.options' ) );
71
-
72
- options
73
- .map( option => option.name )
74
- .filter( isSupported )
75
- .forEach( option => this._addButton( option ) );
76
-
77
- componentFactory.add( 'alignment', locale => {
78
- const dropdownView = createDropdown( locale );
79
-
80
- // Add existing alignment buttons to dropdown's toolbar.
81
- const buttons = options.map( option => componentFactory.create( `alignment:${ option.name }` ) );
82
- addToolbarToDropdown( dropdownView, buttons, { enableActiveItemFocusOnDropdownOpen: true } );
83
-
84
- // Configure dropdown properties an behavior.
85
- dropdownView.buttonView.set( {
86
- label: t( 'Text alignment' ),
87
- tooltip: true
88
- } );
89
-
90
- dropdownView.toolbarView.isVertical = true;
91
- dropdownView.toolbarView.ariaLabel = t( 'Text alignment toolbar' );
92
-
93
- dropdownView.extendTemplate( {
94
- attributes: {
95
- class: 'ck-alignment-dropdown'
96
- }
97
- } );
98
-
99
- // The default icon depends on the direction of the content.
100
- const defaultIcon = locale.contentLanguageDirection === 'rtl' ? iconsMap.get( 'right' ) : iconsMap.get( 'left' );
101
-
102
- // Change icon to reflect current selection's alignment.
103
- dropdownView.buttonView.bind( 'icon' ).toMany( buttons, 'isOn', ( ...areActive ) => {
104
- // Get the index of an active button.
105
- const index = areActive.findIndex( value => value );
106
-
107
- // If none of the commands is active, display either defaultIcon or the first button's icon.
108
- if ( index < 0 ) {
109
- return defaultIcon;
110
- }
111
-
112
- // Return active button's icon.
113
- return buttons[ index ].icon;
114
- } );
115
-
116
- // Enable button if any of the buttons is enabled.
117
- dropdownView.bind( 'isEnabled' ).toMany( buttons, 'isEnabled', ( ...areEnabled ) => areEnabled.some( isEnabled => isEnabled ) );
118
-
119
- // Focus the editable after executing the command.
120
- // Overrides a default behaviour where the focus is moved to the dropdown button (#12125).
121
- this.listenTo( dropdownView, 'execute', () => {
122
- editor.editing.view.focus();
123
- } );
124
-
125
- return dropdownView;
126
- } );
127
- }
128
-
129
- /**
130
- * Helper method for initializing the button and linking it with an appropriate command.
131
- *
132
- * @private
133
- * @param {String} option The name of the alignment option for which the button is added.
134
- */
135
- _addButton( option ) {
136
- const editor = this.editor;
137
-
138
- editor.ui.componentFactory.add( `alignment:${ option }`, locale => {
139
- const command = editor.commands.get( 'alignment' );
140
- const buttonView = new ButtonView( locale );
141
-
142
- buttonView.set( {
143
- label: this.localizedOptionTitles[ option ],
144
- icon: iconsMap.get( option ),
145
- tooltip: true,
146
- isToggleable: true
147
- } );
148
-
149
- // Bind button model to command.
150
- buttonView.bind( 'isEnabled' ).to( command );
151
- buttonView.bind( 'isOn' ).to( command, 'value', value => value === option );
152
-
153
- // Execute command.
154
- this.listenTo( buttonView, 'execute', () => {
155
- editor.execute( 'alignment', { value: option } );
156
- editor.editing.view.focus();
157
- } );
158
-
159
- return buttonView;
160
- } );
161
- }
24
+ /**
25
+ * Returns the localized option titles provided by the plugin.
26
+ *
27
+ * The following localized titles corresponding with
28
+ * {@link module:alignment/alignment~AlignmentConfig#options} are available:
29
+ *
30
+ * * `'left'`,
31
+ * * `'right'`,
32
+ * * `'center'`,
33
+ * * `'justify'`.
34
+ *
35
+ * @readonly
36
+ */
37
+ get localizedOptionTitles() {
38
+ const t = this.editor.t;
39
+ return {
40
+ 'left': t('Align left'),
41
+ 'right': t('Align right'),
42
+ 'center': t('Align center'),
43
+ 'justify': t('Justify')
44
+ };
45
+ }
46
+ /**
47
+ * @inheritDoc
48
+ */
49
+ static get pluginName() {
50
+ return 'AlignmentUI';
51
+ }
52
+ /**
53
+ * @inheritDoc
54
+ */
55
+ init() {
56
+ const editor = this.editor;
57
+ const componentFactory = editor.ui.componentFactory;
58
+ const t = editor.t;
59
+ const options = normalizeAlignmentOptions(editor.config.get('alignment.options'));
60
+ options
61
+ .map(option => option.name)
62
+ .filter(isSupported)
63
+ .forEach(option => this._addButton(option));
64
+ componentFactory.add('alignment', locale => {
65
+ const dropdownView = createDropdown(locale);
66
+ // Add existing alignment buttons to dropdown's toolbar.
67
+ addToolbarToDropdown(dropdownView, () => options.map(option => componentFactory.create(`alignment:${option.name}`)), {
68
+ enableActiveItemFocusOnDropdownOpen: true,
69
+ isVertical: true,
70
+ ariaLabel: t('Text alignment toolbar')
71
+ });
72
+ // Configure dropdown properties an behavior.
73
+ dropdownView.buttonView.set({
74
+ label: t('Text alignment'),
75
+ tooltip: true
76
+ });
77
+ dropdownView.extendTemplate({
78
+ attributes: {
79
+ class: 'ck-alignment-dropdown'
80
+ }
81
+ });
82
+ // The default icon depends on the direction of the content.
83
+ const defaultIcon = locale.contentLanguageDirection === 'rtl' ? iconsMap.get('right') : iconsMap.get('left');
84
+ const command = editor.commands.get('alignment');
85
+ // Change icon to reflect current selection's alignment.
86
+ dropdownView.buttonView.bind('icon').to(command, 'value', value => iconsMap.get(value) || defaultIcon);
87
+ // Enable button if any of the buttons is enabled.
88
+ dropdownView.bind('isEnabled').to(command, 'isEnabled');
89
+ // Focus the editable after executing the command.
90
+ // Overrides a default behaviour where the focus is moved to the dropdown button (#12125).
91
+ this.listenTo(dropdownView, 'execute', () => {
92
+ editor.editing.view.focus();
93
+ });
94
+ return dropdownView;
95
+ });
96
+ }
97
+ /**
98
+ * Helper method for initializing the button and linking it with an appropriate command.
99
+ *
100
+ * @param option The name of the alignment option for which the button is added.
101
+ */
102
+ _addButton(option) {
103
+ const editor = this.editor;
104
+ editor.ui.componentFactory.add(`alignment:${option}`, locale => {
105
+ const command = editor.commands.get('alignment');
106
+ const buttonView = new ButtonView(locale);
107
+ buttonView.set({
108
+ label: this.localizedOptionTitles[option],
109
+ icon: iconsMap.get(option),
110
+ tooltip: true,
111
+ isToggleable: true
112
+ });
113
+ // Bind button model to command.
114
+ buttonView.bind('isEnabled').to(command);
115
+ buttonView.bind('isOn').to(command, 'value', value => value === option);
116
+ // Execute command.
117
+ this.listenTo(buttonView, 'execute', () => {
118
+ editor.execute('alignment', { value: option });
119
+ editor.editing.view.focus();
120
+ });
121
+ return buttonView;
122
+ });
123
+ }
162
124
  }
package/src/index.js CHANGED
@@ -1,12 +1,10 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module alignment
8
7
  */
9
-
10
8
  export { default as Alignment } from './alignment';
11
9
  export { default as AlignmentEditing } from './alignmentediting';
12
10
  export { default as AlignmentUI } from './alignmentui';