@ckeditor/ckeditor5-code-block 39.0.1 → 40.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.
Files changed (83) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +3 -3
  3. package/build/code-block.js +1 -1
  4. package/build/code-block.js.map +1 -0
  5. package/lang/translations/af.po +1 -0
  6. package/lang/translations/ar.po +1 -0
  7. package/lang/translations/az.po +1 -0
  8. package/lang/translations/bg.po +1 -0
  9. package/lang/translations/bn.po +1 -0
  10. package/lang/translations/bs.po +1 -0
  11. package/lang/translations/ca.po +1 -0
  12. package/lang/translations/cs.po +1 -0
  13. package/lang/translations/da.po +1 -0
  14. package/lang/translations/de-ch.po +1 -0
  15. package/lang/translations/de.po +1 -0
  16. package/lang/translations/el.po +1 -0
  17. package/lang/translations/en-au.po +1 -0
  18. package/lang/translations/en.po +1 -0
  19. package/lang/translations/es-co.po +1 -0
  20. package/lang/translations/es.po +1 -0
  21. package/lang/translations/et.po +1 -0
  22. package/lang/translations/fa.po +1 -0
  23. package/lang/translations/fi.po +1 -0
  24. package/lang/translations/fr.po +1 -0
  25. package/lang/translations/gl.po +1 -0
  26. package/lang/translations/he.po +1 -0
  27. package/lang/translations/hi.po +1 -0
  28. package/lang/translations/hr.po +1 -0
  29. package/lang/translations/hu.po +1 -0
  30. package/lang/translations/id.po +1 -0
  31. package/lang/translations/it.po +1 -0
  32. package/lang/translations/ja.po +1 -0
  33. package/lang/translations/jv.po +1 -0
  34. package/lang/translations/ko.po +1 -0
  35. package/lang/translations/ku.po +1 -0
  36. package/lang/translations/lt.po +1 -0
  37. package/lang/translations/lv.po +1 -0
  38. package/lang/translations/ms.po +1 -0
  39. package/lang/translations/nl.po +1 -0
  40. package/lang/translations/no.po +1 -0
  41. package/lang/translations/pl.po +1 -0
  42. package/lang/translations/pt-br.po +1 -0
  43. package/lang/translations/pt.po +1 -0
  44. package/lang/translations/ro.po +1 -0
  45. package/lang/translations/ru.po +1 -0
  46. package/lang/translations/sk.po +1 -0
  47. package/lang/translations/sq.po +1 -0
  48. package/lang/translations/sr-latn.po +1 -0
  49. package/lang/translations/sr.po +1 -0
  50. package/lang/translations/sv.po +1 -0
  51. package/lang/translations/th.po +1 -0
  52. package/lang/translations/tk.po +1 -0
  53. package/lang/translations/tr.po +1 -0
  54. package/lang/translations/ug.po +1 -0
  55. package/lang/translations/uk.po +1 -0
  56. package/lang/translations/ur.po +1 -0
  57. package/lang/translations/uz.po +1 -0
  58. package/lang/translations/vi.po +1 -0
  59. package/lang/translations/zh-cn.po +1 -0
  60. package/lang/translations/zh.po +1 -0
  61. package/package.json +2 -6
  62. package/src/augmentation.d.ts +25 -25
  63. package/src/augmentation.js +5 -5
  64. package/src/codeblock.d.ts +29 -29
  65. package/src/codeblock.js +33 -33
  66. package/src/codeblockcommand.d.ts +60 -60
  67. package/src/codeblockcommand.js +138 -138
  68. package/src/codeblockconfig.d.ts +146 -146
  69. package/src/codeblockconfig.js +5 -5
  70. package/src/codeblockediting.d.ts +36 -36
  71. package/src/codeblockediting.js +382 -383
  72. package/src/codeblockui.d.ts +29 -29
  73. package/src/codeblockui.js +93 -93
  74. package/src/converters.d.ts +126 -126
  75. package/src/converters.js +277 -277
  76. package/src/indentcodeblockcommand.d.ts +33 -33
  77. package/src/indentcodeblockcommand.js +78 -78
  78. package/src/index.d.ts +15 -15
  79. package/src/index.js +11 -11
  80. package/src/outdentcodeblockcommand.d.ts +33 -33
  81. package/src/outdentcodeblockcommand.js +148 -148
  82. package/src/utils.d.ts +138 -138
  83. package/src/utils.js +209 -209
@@ -1,60 +1,60 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- import { Command, type Editor } from 'ckeditor5/src/core';
6
- /**
7
- * The code block command plugin.
8
- */
9
- export default class CodeBlockCommand extends Command {
10
- /**
11
- * Contains the last used language.
12
- */
13
- private _lastLanguage;
14
- /**
15
- * Contains language if any is selected, false otherwise.
16
- * @observable
17
- * @readonly
18
- */
19
- value: string | false;
20
- /**
21
- * @inheritDoc
22
- */
23
- constructor(editor: Editor);
24
- /**
25
- * @inheritDoc
26
- */
27
- refresh(): void;
28
- /**
29
- * Executes the command. When the command {@link #value is on}, all topmost code blocks within
30
- * the selection will be removed. If it is off, all selected blocks will be flattened and
31
- * wrapped by a code block.
32
- *
33
- * @fires execute
34
- * @param options Command options.
35
- * @param options.language The code block language.
36
- * @param options.forceValue If set, it will force the command behavior. If `true`, the command will apply a code block,
37
- * otherwise the command will remove the code block. If not set, the command will act basing on its current value.
38
- * @param options.usePreviousLanguageChoice If set on `true` and the `options.language` is not specified, the command
39
- * will apply the previous language (if the command was already executed) when inserting the `codeBlock` element.
40
- */
41
- execute(options?: {
42
- language?: string;
43
- forceValue?: boolean;
44
- usePreviousLanguageChoice?: boolean;
45
- }): void;
46
- /**
47
- * Checks the command's {@link #value}.
48
- *
49
- * @returns The current value.
50
- */
51
- private _getValue;
52
- /**
53
- * Checks whether the command can be enabled in the current context.
54
- *
55
- * @returns Whether the command should be enabled.
56
- */
57
- private _checkEnabled;
58
- private _applyCodeBlock;
59
- private _removeCodeBlock;
60
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import { Command, type Editor } from 'ckeditor5/src/core';
6
+ /**
7
+ * The code block command plugin.
8
+ */
9
+ export default class CodeBlockCommand extends Command {
10
+ /**
11
+ * Contains the last used language.
12
+ */
13
+ private _lastLanguage;
14
+ /**
15
+ * Contains language if any is selected, false otherwise.
16
+ * @observable
17
+ * @readonly
18
+ */
19
+ value: string | false;
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ constructor(editor: Editor);
24
+ /**
25
+ * @inheritDoc
26
+ */
27
+ refresh(): void;
28
+ /**
29
+ * Executes the command. When the command {@link #value is on}, all topmost code blocks within
30
+ * the selection will be removed. If it is off, all selected blocks will be flattened and
31
+ * wrapped by a code block.
32
+ *
33
+ * @fires execute
34
+ * @param options Command options.
35
+ * @param options.language The code block language.
36
+ * @param options.forceValue If set, it will force the command behavior. If `true`, the command will apply a code block,
37
+ * otherwise the command will remove the code block. If not set, the command will act basing on its current value.
38
+ * @param options.usePreviousLanguageChoice If set on `true` and the `options.language` is not specified, the command
39
+ * will apply the previous language (if the command was already executed) when inserting the `codeBlock` element.
40
+ */
41
+ execute(options?: {
42
+ language?: string;
43
+ forceValue?: boolean;
44
+ usePreviousLanguageChoice?: boolean;
45
+ }): void;
46
+ /**
47
+ * Checks the command's {@link #value}.
48
+ *
49
+ * @returns The current value.
50
+ */
51
+ private _getValue;
52
+ /**
53
+ * Checks whether the command can be enabled in the current context.
54
+ *
55
+ * @returns Whether the command should be enabled.
56
+ */
57
+ private _checkEnabled;
58
+ private _applyCodeBlock;
59
+ private _removeCodeBlock;
60
+ }
@@ -1,138 +1,138 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- import { Command } from 'ckeditor5/src/core';
6
- import { first } from 'ckeditor5/src/utils';
7
- import { getNormalizedAndLocalizedLanguageDefinitions, canBeCodeBlock } from './utils';
8
- /**
9
- * The code block command plugin.
10
- */
11
- export default class CodeBlockCommand extends Command {
12
- /**
13
- * @inheritDoc
14
- */
15
- constructor(editor) {
16
- super(editor);
17
- this._lastLanguage = null;
18
- }
19
- /**
20
- * @inheritDoc
21
- */
22
- refresh() {
23
- this.value = this._getValue();
24
- this.isEnabled = this._checkEnabled();
25
- }
26
- /**
27
- * Executes the command. When the command {@link #value is on}, all topmost code blocks within
28
- * the selection will be removed. If it is off, all selected blocks will be flattened and
29
- * wrapped by a code block.
30
- *
31
- * @fires execute
32
- * @param options Command options.
33
- * @param options.language The code block language.
34
- * @param options.forceValue If set, it will force the command behavior. If `true`, the command will apply a code block,
35
- * otherwise the command will remove the code block. If not set, the command will act basing on its current value.
36
- * @param options.usePreviousLanguageChoice If set on `true` and the `options.language` is not specified, the command
37
- * will apply the previous language (if the command was already executed) when inserting the `codeBlock` element.
38
- */
39
- execute(options = {}) {
40
- const editor = this.editor;
41
- const model = editor.model;
42
- const selection = model.document.selection;
43
- const normalizedLanguagesDefs = getNormalizedAndLocalizedLanguageDefinitions(editor);
44
- const firstLanguageInConfig = normalizedLanguagesDefs[0];
45
- const blocks = Array.from(selection.getSelectedBlocks());
46
- const value = options.forceValue == undefined ? !this.value : options.forceValue;
47
- const language = getLanguage(options, this._lastLanguage, firstLanguageInConfig.language);
48
- model.change(writer => {
49
- if (value) {
50
- this._applyCodeBlock(writer, blocks, language);
51
- }
52
- else {
53
- this._removeCodeBlock(writer, blocks);
54
- }
55
- });
56
- }
57
- /**
58
- * Checks the command's {@link #value}.
59
- *
60
- * @returns The current value.
61
- */
62
- _getValue() {
63
- const selection = this.editor.model.document.selection;
64
- const firstBlock = first(selection.getSelectedBlocks());
65
- const isCodeBlock = !!(firstBlock && firstBlock.is('element', 'codeBlock'));
66
- return isCodeBlock ? firstBlock.getAttribute('language') : false;
67
- }
68
- /**
69
- * Checks whether the command can be enabled in the current context.
70
- *
71
- * @returns Whether the command should be enabled.
72
- */
73
- _checkEnabled() {
74
- if (this.value) {
75
- return true;
76
- }
77
- const selection = this.editor.model.document.selection;
78
- const schema = this.editor.model.schema;
79
- const firstBlock = first(selection.getSelectedBlocks());
80
- if (!firstBlock) {
81
- return false;
82
- }
83
- return canBeCodeBlock(schema, firstBlock);
84
- }
85
- _applyCodeBlock(writer, blocks, language) {
86
- this._lastLanguage = language;
87
- const schema = this.editor.model.schema;
88
- const allowedBlocks = blocks.filter(block => canBeCodeBlock(schema, block));
89
- for (const block of allowedBlocks) {
90
- writer.rename(block, 'codeBlock');
91
- writer.setAttribute('language', language, block);
92
- schema.removeDisallowedAttributes([block], writer);
93
- // Remove children of the `codeBlock` element that are not allowed. See #9567.
94
- Array.from(block.getChildren())
95
- .filter(child => !schema.checkChild(block, child))
96
- .forEach(child => writer.remove(child));
97
- }
98
- allowedBlocks.reverse().forEach((currentBlock, i) => {
99
- const nextBlock = allowedBlocks[i + 1];
100
- if (currentBlock.previousSibling === nextBlock) {
101
- writer.appendElement('softBreak', nextBlock);
102
- writer.merge(writer.createPositionBefore(currentBlock));
103
- }
104
- });
105
- }
106
- _removeCodeBlock(writer, blocks) {
107
- const codeBlocks = blocks.filter(block => block.is('element', 'codeBlock'));
108
- for (const block of codeBlocks) {
109
- const range = writer.createRangeOn(block);
110
- for (const item of Array.from(range.getItems()).reverse()) {
111
- if (item.is('element', 'softBreak') && item.parent.is('element', 'codeBlock')) {
112
- const { position } = writer.split(writer.createPositionBefore(item));
113
- const elementAfter = position.nodeAfter;
114
- writer.rename(elementAfter, 'paragraph');
115
- writer.removeAttribute('language', elementAfter);
116
- writer.remove(item);
117
- }
118
- }
119
- writer.rename(block, 'paragraph');
120
- writer.removeAttribute('language', block);
121
- }
122
- }
123
- }
124
- /**
125
- * Picks the language for the new code block. If any language is passed as an option,
126
- * it will be returned. Else, if option usePreviousLanguageChoice is true and some
127
- * code block was already created (lastLanguage is not null) then previously used
128
- * language will be returned. If not, it will return default language.
129
- */
130
- function getLanguage(options, lastLanguage, defaultLanguage) {
131
- if (options.language) {
132
- return options.language;
133
- }
134
- if (options.usePreviousLanguageChoice && lastLanguage) {
135
- return lastLanguage;
136
- }
137
- return defaultLanguage;
138
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import { Command } from 'ckeditor5/src/core';
6
+ import { first } from 'ckeditor5/src/utils';
7
+ import { getNormalizedAndLocalizedLanguageDefinitions, canBeCodeBlock } from './utils';
8
+ /**
9
+ * The code block command plugin.
10
+ */
11
+ export default class CodeBlockCommand extends Command {
12
+ /**
13
+ * @inheritDoc
14
+ */
15
+ constructor(editor) {
16
+ super(editor);
17
+ this._lastLanguage = null;
18
+ }
19
+ /**
20
+ * @inheritDoc
21
+ */
22
+ refresh() {
23
+ this.value = this._getValue();
24
+ this.isEnabled = this._checkEnabled();
25
+ }
26
+ /**
27
+ * Executes the command. When the command {@link #value is on}, all topmost code blocks within
28
+ * the selection will be removed. If it is off, all selected blocks will be flattened and
29
+ * wrapped by a code block.
30
+ *
31
+ * @fires execute
32
+ * @param options Command options.
33
+ * @param options.language The code block language.
34
+ * @param options.forceValue If set, it will force the command behavior. If `true`, the command will apply a code block,
35
+ * otherwise the command will remove the code block. If not set, the command will act basing on its current value.
36
+ * @param options.usePreviousLanguageChoice If set on `true` and the `options.language` is not specified, the command
37
+ * will apply the previous language (if the command was already executed) when inserting the `codeBlock` element.
38
+ */
39
+ execute(options = {}) {
40
+ const editor = this.editor;
41
+ const model = editor.model;
42
+ const selection = model.document.selection;
43
+ const normalizedLanguagesDefs = getNormalizedAndLocalizedLanguageDefinitions(editor);
44
+ const firstLanguageInConfig = normalizedLanguagesDefs[0];
45
+ const blocks = Array.from(selection.getSelectedBlocks());
46
+ const value = options.forceValue == undefined ? !this.value : options.forceValue;
47
+ const language = getLanguage(options, this._lastLanguage, firstLanguageInConfig.language);
48
+ model.change(writer => {
49
+ if (value) {
50
+ this._applyCodeBlock(writer, blocks, language);
51
+ }
52
+ else {
53
+ this._removeCodeBlock(writer, blocks);
54
+ }
55
+ });
56
+ }
57
+ /**
58
+ * Checks the command's {@link #value}.
59
+ *
60
+ * @returns The current value.
61
+ */
62
+ _getValue() {
63
+ const selection = this.editor.model.document.selection;
64
+ const firstBlock = first(selection.getSelectedBlocks());
65
+ const isCodeBlock = !!(firstBlock && firstBlock.is('element', 'codeBlock'));
66
+ return isCodeBlock ? firstBlock.getAttribute('language') : false;
67
+ }
68
+ /**
69
+ * Checks whether the command can be enabled in the current context.
70
+ *
71
+ * @returns Whether the command should be enabled.
72
+ */
73
+ _checkEnabled() {
74
+ if (this.value) {
75
+ return true;
76
+ }
77
+ const selection = this.editor.model.document.selection;
78
+ const schema = this.editor.model.schema;
79
+ const firstBlock = first(selection.getSelectedBlocks());
80
+ if (!firstBlock) {
81
+ return false;
82
+ }
83
+ return canBeCodeBlock(schema, firstBlock);
84
+ }
85
+ _applyCodeBlock(writer, blocks, language) {
86
+ this._lastLanguage = language;
87
+ const schema = this.editor.model.schema;
88
+ const allowedBlocks = blocks.filter(block => canBeCodeBlock(schema, block));
89
+ for (const block of allowedBlocks) {
90
+ writer.rename(block, 'codeBlock');
91
+ writer.setAttribute('language', language, block);
92
+ schema.removeDisallowedAttributes([block], writer);
93
+ // Remove children of the `codeBlock` element that are not allowed. See #9567.
94
+ Array.from(block.getChildren())
95
+ .filter(child => !schema.checkChild(block, child))
96
+ .forEach(child => writer.remove(child));
97
+ }
98
+ allowedBlocks.reverse().forEach((currentBlock, i) => {
99
+ const nextBlock = allowedBlocks[i + 1];
100
+ if (currentBlock.previousSibling === nextBlock) {
101
+ writer.appendElement('softBreak', nextBlock);
102
+ writer.merge(writer.createPositionBefore(currentBlock));
103
+ }
104
+ });
105
+ }
106
+ _removeCodeBlock(writer, blocks) {
107
+ const codeBlocks = blocks.filter(block => block.is('element', 'codeBlock'));
108
+ for (const block of codeBlocks) {
109
+ const range = writer.createRangeOn(block);
110
+ for (const item of Array.from(range.getItems()).reverse()) {
111
+ if (item.is('element', 'softBreak') && item.parent.is('element', 'codeBlock')) {
112
+ const { position } = writer.split(writer.createPositionBefore(item));
113
+ const elementAfter = position.nodeAfter;
114
+ writer.rename(elementAfter, 'paragraph');
115
+ writer.removeAttribute('language', elementAfter);
116
+ writer.remove(item);
117
+ }
118
+ }
119
+ writer.rename(block, 'paragraph');
120
+ writer.removeAttribute('language', block);
121
+ }
122
+ }
123
+ }
124
+ /**
125
+ * Picks the language for the new code block. If any language is passed as an option,
126
+ * it will be returned. Else, if option usePreviousLanguageChoice is true and some
127
+ * code block was already created (lastLanguage is not null) then previously used
128
+ * language will be returned. If not, it will return default language.
129
+ */
130
+ function getLanguage(options, lastLanguage, defaultLanguage) {
131
+ if (options.language) {
132
+ return options.language;
133
+ }
134
+ if (options.usePreviousLanguageChoice && lastLanguage) {
135
+ return lastLanguage;
136
+ }
137
+ return defaultLanguage;
138
+ }