@ckeditor/ckeditor5-code-block 35.4.0 → 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,115 +1,88 @@
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 code-block/codeblockui
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
9
  import { Collection } from 'ckeditor5/src/utils';
12
10
  import { Model, SplitButtonView, createDropdown, addListToDropdown } from 'ckeditor5/src/ui';
13
-
14
11
  import { getNormalizedAndLocalizedLanguageDefinitions } from './utils';
15
-
16
12
  import codeBlockIcon from '../theme/icons/codeblock.svg';
17
13
  import '../theme/codeblock.css';
18
-
19
14
  /**
20
15
  * The code block UI plugin.
21
16
  *
22
17
  * Introduces the `'codeBlock'` dropdown.
23
- *
24
- * @extends module:core/plugin~Plugin
25
18
  */
26
19
  export default class CodeBlockUI extends Plugin {
27
- /**
28
- * @inheritDoc
29
- */
30
- static get pluginName() {
31
- return 'CodeBlockUI';
32
- }
33
-
34
- /**
35
- * @inheritDoc
36
- */
37
- init() {
38
- const editor = this.editor;
39
- const t = editor.t;
40
- const componentFactory = editor.ui.componentFactory;
41
- const normalizedLanguageDefs = getNormalizedAndLocalizedLanguageDefinitions( editor );
42
-
43
- componentFactory.add( 'codeBlock', locale => {
44
- const command = editor.commands.get( 'codeBlock' );
45
- const dropdownView = createDropdown( locale, SplitButtonView );
46
- const splitButtonView = dropdownView.buttonView;
47
-
48
- splitButtonView.set( {
49
- label: t( 'Insert code block' ),
50
- tooltip: true,
51
- icon: codeBlockIcon,
52
- isToggleable: true
53
- } );
54
-
55
- splitButtonView.bind( 'isOn' ).to( command, 'value', value => !!value );
56
-
57
- splitButtonView.on( 'execute', () => {
58
- editor.execute( 'codeBlock', {
59
- usePreviousLanguageChoice: true
60
- } );
61
-
62
- editor.editing.view.focus();
63
- } );
64
-
65
- dropdownView.on( 'execute', evt => {
66
- editor.execute( 'codeBlock', {
67
- language: evt.source._codeBlockLanguage,
68
- forceValue: true
69
- } );
70
-
71
- editor.editing.view.focus();
72
- } );
73
-
74
- dropdownView.class = 'ck-code-block-dropdown';
75
- dropdownView.bind( 'isEnabled' ).to( command );
76
-
77
- addListToDropdown( dropdownView, this._getLanguageListItemDefinitions( normalizedLanguageDefs ) );
78
-
79
- return dropdownView;
80
- } );
81
- }
82
-
83
- /**
84
- * A helper returning a collection of the `codeBlock` dropdown items representing languages
85
- * available for the user to choose from.
86
- *
87
- * @private
88
- * @param {Array.<module:code-block/codeblock~CodeBlockLanguageDefinition>} normalizedLanguageDefs
89
- * @returns {Iterable.<module:ui/dropdown/utils~ListDropdownItemDefinition>}
90
- */
91
- _getLanguageListItemDefinitions( normalizedLanguageDefs ) {
92
- const editor = this.editor;
93
- const command = editor.commands.get( 'codeBlock' );
94
- const itemDefinitions = new Collection();
95
-
96
- for ( const languageDef of normalizedLanguageDefs ) {
97
- const definition = {
98
- type: 'button',
99
- model: new Model( {
100
- _codeBlockLanguage: languageDef.language,
101
- label: languageDef.label,
102
- withText: true
103
- } )
104
- };
105
-
106
- definition.model.bind( 'isOn' ).to( command, 'value', value => {
107
- return value === definition.model._codeBlockLanguage;
108
- } );
109
-
110
- itemDefinitions.add( definition );
111
- }
112
-
113
- return itemDefinitions;
114
- }
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ static get pluginName() {
24
+ return 'CodeBlockUI';
25
+ }
26
+ /**
27
+ * @inheritDoc
28
+ */
29
+ init() {
30
+ const editor = this.editor;
31
+ const t = editor.t;
32
+ const componentFactory = editor.ui.componentFactory;
33
+ const normalizedLanguageDefs = getNormalizedAndLocalizedLanguageDefinitions(editor);
34
+ componentFactory.add('codeBlock', locale => {
35
+ const command = editor.commands.get('codeBlock');
36
+ const dropdownView = createDropdown(locale, SplitButtonView);
37
+ const splitButtonView = dropdownView.buttonView;
38
+ splitButtonView.set({
39
+ label: t('Insert code block'),
40
+ tooltip: true,
41
+ icon: codeBlockIcon,
42
+ isToggleable: true
43
+ });
44
+ splitButtonView.bind('isOn').to(command, 'value', value => !!value);
45
+ splitButtonView.on('execute', () => {
46
+ editor.execute('codeBlock', {
47
+ usePreviousLanguageChoice: true
48
+ });
49
+ editor.editing.view.focus();
50
+ });
51
+ dropdownView.on('execute', evt => {
52
+ editor.execute('codeBlock', {
53
+ language: evt.source._codeBlockLanguage,
54
+ forceValue: true
55
+ });
56
+ editor.editing.view.focus();
57
+ });
58
+ dropdownView.class = 'ck-code-block-dropdown';
59
+ dropdownView.bind('isEnabled').to(command);
60
+ addListToDropdown(dropdownView, () => this._getLanguageListItemDefinitions(normalizedLanguageDefs));
61
+ return dropdownView;
62
+ });
63
+ }
64
+ /**
65
+ * A helper returning a collection of the `codeBlock` dropdown items representing languages
66
+ * available for the user to choose from.
67
+ */
68
+ _getLanguageListItemDefinitions(normalizedLanguageDefs) {
69
+ const editor = this.editor;
70
+ const command = editor.commands.get('codeBlock');
71
+ const itemDefinitions = new Collection();
72
+ for (const languageDef of normalizedLanguageDefs) {
73
+ const definition = {
74
+ type: 'button',
75
+ model: new Model({
76
+ _codeBlockLanguage: languageDef.language,
77
+ label: languageDef.label,
78
+ withText: true
79
+ })
80
+ };
81
+ definition.model.bind('isOn').to(command, 'value', value => {
82
+ return value === definition.model._codeBlockLanguage;
83
+ });
84
+ itemDefinitions.add(definition);
85
+ }
86
+ return itemDefinitions;
87
+ }
115
88
  }