@ckeditor/ckeditor5-code-block 38.1.1 → 38.2.0-alpha.1
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.
- package/package.json +3 -2
- package/src/augmentation.d.ts +25 -25
- package/src/augmentation.js +5 -5
- package/src/codeblock.d.ts +29 -29
- package/src/codeblock.js +33 -33
- package/src/codeblockcommand.d.ts +60 -60
- package/src/codeblockcommand.js +138 -138
- package/src/codeblockconfig.d.ts +146 -146
- package/src/codeblockconfig.js +5 -5
- package/src/codeblockediting.d.ts +36 -36
- package/src/codeblockediting.js +383 -383
- package/src/codeblockui.d.ts +29 -29
- package/src/codeblockui.js +93 -93
- package/src/converters.d.ts +126 -126
- package/src/converters.js +277 -277
- package/src/indentcodeblockcommand.d.ts +33 -33
- package/src/indentcodeblockcommand.js +78 -78
- package/src/index.d.ts +15 -15
- package/src/index.js +11 -11
- package/src/outdentcodeblockcommand.d.ts +33 -33
- package/src/outdentcodeblockcommand.js +148 -148
- package/src/utils.d.ts +138 -138
- package/src/utils.js +209 -209
- package/build/code-block.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckeditor/ckeditor5-code-block",
|
|
3
|
-
"version": "38.
|
|
3
|
+
"version": "38.2.0-alpha.1",
|
|
4
4
|
"description": "Code block feature for CKEditor 5.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ckeditor",
|
|
@@ -11,8 +11,9 @@
|
|
|
11
11
|
"ckeditor5-dll"
|
|
12
12
|
],
|
|
13
13
|
"main": "src/index.js",
|
|
14
|
+
"type": "module",
|
|
14
15
|
"dependencies": {
|
|
15
|
-
"ckeditor5": "38.
|
|
16
|
+
"ckeditor5": "38.2.0-alpha.1"
|
|
16
17
|
},
|
|
17
18
|
"engines": {
|
|
18
19
|
"node": ">=16.0.0",
|
package/src/augmentation.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
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 type { CodeBlock, CodeBlockCommand, CodeBlockConfig, CodeBlockEditing, CodeBlockUI, IndentCodeBlockCommand, OutdentCodeBlockCommand } from './index';
|
|
6
|
-
declare module '@ckeditor/ckeditor5-core' {
|
|
7
|
-
interface EditorConfig {
|
|
8
|
-
/**
|
|
9
|
-
* The configuration of the {@link module:code-block/codeblock~CodeBlock} feature.
|
|
10
|
-
*
|
|
11
|
-
* Read more in {@link module:code-block/codeblockconfig~CodeBlockConfig}.
|
|
12
|
-
*/
|
|
13
|
-
codeBlock?: CodeBlockConfig;
|
|
14
|
-
}
|
|
15
|
-
interface PluginsMap {
|
|
16
|
-
[CodeBlock.pluginName]: CodeBlock;
|
|
17
|
-
[CodeBlockEditing.pluginName]: CodeBlockEditing;
|
|
18
|
-
[CodeBlockUI.pluginName]: CodeBlockUI;
|
|
19
|
-
}
|
|
20
|
-
interface CommandsMap {
|
|
21
|
-
codeBlock: CodeBlockCommand;
|
|
22
|
-
indentCodeBlock: IndentCodeBlockCommand;
|
|
23
|
-
outdentCodeBlock: OutdentCodeBlockCommand;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
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 type { CodeBlock, CodeBlockCommand, CodeBlockConfig, CodeBlockEditing, CodeBlockUI, IndentCodeBlockCommand, OutdentCodeBlockCommand } from './index.js';
|
|
6
|
+
declare module '@ckeditor/ckeditor5-core' {
|
|
7
|
+
interface EditorConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The configuration of the {@link module:code-block/codeblock~CodeBlock} feature.
|
|
10
|
+
*
|
|
11
|
+
* Read more in {@link module:code-block/codeblockconfig~CodeBlockConfig}.
|
|
12
|
+
*/
|
|
13
|
+
codeBlock?: CodeBlockConfig;
|
|
14
|
+
}
|
|
15
|
+
interface PluginsMap {
|
|
16
|
+
[CodeBlock.pluginName]: CodeBlock;
|
|
17
|
+
[CodeBlockEditing.pluginName]: CodeBlockEditing;
|
|
18
|
+
[CodeBlockUI.pluginName]: CodeBlockUI;
|
|
19
|
+
}
|
|
20
|
+
interface CommandsMap {
|
|
21
|
+
codeBlock: CodeBlockCommand;
|
|
22
|
+
indentCodeBlock: IndentCodeBlockCommand;
|
|
23
|
+
outdentCodeBlock: OutdentCodeBlockCommand;
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/augmentation.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
|
-
export {};
|
|
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
|
+
export {};
|
package/src/codeblock.d.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
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
|
-
/**
|
|
6
|
-
* @module code-block/codeblock
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
-
import CodeBlockEditing from './codeblockediting';
|
|
10
|
-
import CodeBlockUI from './codeblockui';
|
|
11
|
-
/**
|
|
12
|
-
* The code block plugin.
|
|
13
|
-
*
|
|
14
|
-
* For more information about this feature check the {@glink api/code-block package page} and the
|
|
15
|
-
* {@glink features/code-blocks code block} feature guide.
|
|
16
|
-
*
|
|
17
|
-
* This is a "glue" plugin that loads the {@link module:code-block/codeblockediting~CodeBlockEditing code block editing feature}
|
|
18
|
-
* and the {@link module:code-block/codeblockui~CodeBlockUI code block UI feature}.
|
|
19
|
-
*/
|
|
20
|
-
export default class CodeBlock extends Plugin {
|
|
21
|
-
/**
|
|
22
|
-
* @inheritDoc
|
|
23
|
-
*/
|
|
24
|
-
static get requires(): readonly [typeof CodeBlockEditing, typeof CodeBlockUI];
|
|
25
|
-
/**
|
|
26
|
-
* @inheritDoc
|
|
27
|
-
*/
|
|
28
|
-
static get pluginName(): "CodeBlock";
|
|
29
|
-
}
|
|
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
|
+
/**
|
|
6
|
+
* @module code-block/codeblock
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
|
9
|
+
import CodeBlockEditing from './codeblockediting.js';
|
|
10
|
+
import CodeBlockUI from './codeblockui.js';
|
|
11
|
+
/**
|
|
12
|
+
* The code block plugin.
|
|
13
|
+
*
|
|
14
|
+
* For more information about this feature check the {@glink api/code-block package page} and the
|
|
15
|
+
* {@glink features/code-blocks code block} feature guide.
|
|
16
|
+
*
|
|
17
|
+
* This is a "glue" plugin that loads the {@link module:code-block/codeblockediting~CodeBlockEditing code block editing feature}
|
|
18
|
+
* and the {@link module:code-block/codeblockui~CodeBlockUI code block UI feature}.
|
|
19
|
+
*/
|
|
20
|
+
export default class CodeBlock extends Plugin {
|
|
21
|
+
/**
|
|
22
|
+
* @inheritDoc
|
|
23
|
+
*/
|
|
24
|
+
static get requires(): readonly [typeof CodeBlockEditing, typeof CodeBlockUI];
|
|
25
|
+
/**
|
|
26
|
+
* @inheritDoc
|
|
27
|
+
*/
|
|
28
|
+
static get pluginName(): "CodeBlock";
|
|
29
|
+
}
|
package/src/codeblock.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
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
|
-
/**
|
|
6
|
-
* @module code-block/codeblock
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
-
import CodeBlockEditing from './codeblockediting';
|
|
10
|
-
import CodeBlockUI from './codeblockui';
|
|
11
|
-
/**
|
|
12
|
-
* The code block plugin.
|
|
13
|
-
*
|
|
14
|
-
* For more information about this feature check the {@glink api/code-block package page} and the
|
|
15
|
-
* {@glink features/code-blocks code block} feature guide.
|
|
16
|
-
*
|
|
17
|
-
* This is a "glue" plugin that loads the {@link module:code-block/codeblockediting~CodeBlockEditing code block editing feature}
|
|
18
|
-
* and the {@link module:code-block/codeblockui~CodeBlockUI code block UI feature}.
|
|
19
|
-
*/
|
|
20
|
-
export default class CodeBlock extends Plugin {
|
|
21
|
-
/**
|
|
22
|
-
* @inheritDoc
|
|
23
|
-
*/
|
|
24
|
-
static get requires() {
|
|
25
|
-
return [CodeBlockEditing, CodeBlockUI];
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* @inheritDoc
|
|
29
|
-
*/
|
|
30
|
-
static get pluginName() {
|
|
31
|
-
return 'CodeBlock';
|
|
32
|
-
}
|
|
33
|
-
}
|
|
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
|
+
/**
|
|
6
|
+
* @module code-block/codeblock
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
|
9
|
+
import CodeBlockEditing from './codeblockediting.js';
|
|
10
|
+
import CodeBlockUI from './codeblockui.js';
|
|
11
|
+
/**
|
|
12
|
+
* The code block plugin.
|
|
13
|
+
*
|
|
14
|
+
* For more information about this feature check the {@glink api/code-block package page} and the
|
|
15
|
+
* {@glink features/code-blocks code block} feature guide.
|
|
16
|
+
*
|
|
17
|
+
* This is a "glue" plugin that loads the {@link module:code-block/codeblockediting~CodeBlockEditing code block editing feature}
|
|
18
|
+
* and the {@link module:code-block/codeblockui~CodeBlockUI code block UI feature}.
|
|
19
|
+
*/
|
|
20
|
+
export default class CodeBlock extends Plugin {
|
|
21
|
+
/**
|
|
22
|
+
* @inheritDoc
|
|
23
|
+
*/
|
|
24
|
+
static get requires() {
|
|
25
|
+
return [CodeBlockEditing, CodeBlockUI];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @inheritDoc
|
|
29
|
+
*/
|
|
30
|
+
static get pluginName() {
|
|
31
|
+
return 'CodeBlock';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -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.js';
|
|
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
|
+
}
|
package/src/codeblockcommand.js
CHANGED
|
@@ -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.js';
|
|
6
|
+
import { first } from 'ckeditor5/src/utils.js';
|
|
7
|
+
import { getNormalizedAndLocalizedLanguageDefinitions, canBeCodeBlock } from './utils.js';
|
|
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
|
+
}
|