@ckeditor/ckeditor5-code-block 38.1.0 → 38.1.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.
@@ -1,78 +1,78 @@
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/indentcodeblockcommand
7
- */
8
- import { Command } from 'ckeditor5/src/core';
9
- import { getIndentOutdentPositions, isModelSelectionInCodeBlock } from './utils';
10
- /**
11
- * The code block indentation increase command plugin.
12
- */
13
- export default class IndentCodeBlockCommand extends Command {
14
- constructor(editor) {
15
- super(editor);
16
- this._indentSequence = editor.config.get('codeBlock.indentSequence');
17
- }
18
- /**
19
- * @inheritDoc
20
- */
21
- refresh() {
22
- this.isEnabled = this._checkEnabled();
23
- }
24
- /**
25
- * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
26
- * code lines in the selection will be increased.
27
- *
28
- * @fires execute
29
- */
30
- execute() {
31
- const editor = this.editor;
32
- const model = editor.model;
33
- model.change(writer => {
34
- const positions = getIndentOutdentPositions(model);
35
- // Indent all positions, for instance assuming the indent sequence is 4x space (" "):
36
- //
37
- // <codeBlock>^foo</codeBlock> -> <codeBlock> foo</codeBlock>
38
- //
39
- // <codeBlock>foo^bar</codeBlock> -> <codeBlock>foo bar</codeBlock>
40
- //
41
- // Also, when there is more than one position:
42
- //
43
- // <codeBlock>
44
- // ^foobar
45
- // <softBreak></softBreak>
46
- // ^bazqux
47
- // </codeBlock>
48
- //
49
- // ->
50
- //
51
- // <codeBlock>
52
- // foobar
53
- // <softBreak></softBreak>
54
- // bazqux
55
- // </codeBlock>
56
- //
57
- for (const position of positions) {
58
- const indentSequenceTextElement = writer.createText(this._indentSequence);
59
- // Previously insertion was done by writer.insertText(). It was changed to insertContent() to enable
60
- // integration of code block with track changes. It's the easiest way of integration because insertContent()
61
- // is already integrated with track changes, but if it ever cause any troubles it can be reverted, however
62
- // some additional work will be required in track changes integration of code block.
63
- model.insertContent(indentSequenceTextElement, position);
64
- }
65
- });
66
- }
67
- /**
68
- * Checks whether the command can be enabled in the current context.
69
- */
70
- _checkEnabled() {
71
- if (!this._indentSequence) {
72
- return false;
73
- }
74
- // Indent (forward) command is always enabled when there's any code block in the selection
75
- // because you can always indent code lines.
76
- return isModelSelectionInCodeBlock(this.editor.model.document.selection);
77
- }
78
- }
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/indentcodeblockcommand
7
+ */
8
+ import { Command } from 'ckeditor5/src/core';
9
+ import { getIndentOutdentPositions, isModelSelectionInCodeBlock } from './utils';
10
+ /**
11
+ * The code block indentation increase command plugin.
12
+ */
13
+ export default class IndentCodeBlockCommand extends Command {
14
+ constructor(editor) {
15
+ super(editor);
16
+ this._indentSequence = editor.config.get('codeBlock.indentSequence');
17
+ }
18
+ /**
19
+ * @inheritDoc
20
+ */
21
+ refresh() {
22
+ this.isEnabled = this._checkEnabled();
23
+ }
24
+ /**
25
+ * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
26
+ * code lines in the selection will be increased.
27
+ *
28
+ * @fires execute
29
+ */
30
+ execute() {
31
+ const editor = this.editor;
32
+ const model = editor.model;
33
+ model.change(writer => {
34
+ const positions = getIndentOutdentPositions(model);
35
+ // Indent all positions, for instance assuming the indent sequence is 4x space (" "):
36
+ //
37
+ // <codeBlock>^foo</codeBlock> -> <codeBlock> foo</codeBlock>
38
+ //
39
+ // <codeBlock>foo^bar</codeBlock> -> <codeBlock>foo bar</codeBlock>
40
+ //
41
+ // Also, when there is more than one position:
42
+ //
43
+ // <codeBlock>
44
+ // ^foobar
45
+ // <softBreak></softBreak>
46
+ // ^bazqux
47
+ // </codeBlock>
48
+ //
49
+ // ->
50
+ //
51
+ // <codeBlock>
52
+ // foobar
53
+ // <softBreak></softBreak>
54
+ // bazqux
55
+ // </codeBlock>
56
+ //
57
+ for (const position of positions) {
58
+ const indentSequenceTextElement = writer.createText(this._indentSequence);
59
+ // Previously insertion was done by writer.insertText(). It was changed to insertContent() to enable
60
+ // integration of code block with track changes. It's the easiest way of integration because insertContent()
61
+ // is already integrated with track changes, but if it ever cause any troubles it can be reverted, however
62
+ // some additional work will be required in track changes integration of code block.
63
+ model.insertContent(indentSequenceTextElement, position);
64
+ }
65
+ });
66
+ }
67
+ /**
68
+ * Checks whether the command can be enabled in the current context.
69
+ */
70
+ _checkEnabled() {
71
+ if (!this._indentSequence) {
72
+ return false;
73
+ }
74
+ // Indent (forward) command is always enabled when there's any code block in the selection
75
+ // because you can always indent code lines.
76
+ return isModelSelectionInCodeBlock(this.editor.model.document.selection);
77
+ }
78
+ }
package/src/index.d.ts CHANGED
@@ -1,15 +1,15 @@
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
7
- */
8
- export { default as CodeBlock } from './codeblock';
9
- export { default as CodeBlockEditing } from './codeblockediting';
10
- export { default as CodeBlockUI } from './codeblockui';
11
- export type { default as CodeBlockCommand } from './codeblockcommand';
12
- export type { default as IndentCodeBlockCommand } from './indentcodeblockcommand';
13
- export type { default as OutdentCodeBlockCommand } from './outdentcodeblockcommand';
14
- export type { CodeBlockConfig } from './codeblockconfig';
15
- import './augmentation';
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
7
+ */
8
+ export { default as CodeBlock } from './codeblock';
9
+ export { default as CodeBlockEditing } from './codeblockediting';
10
+ export { default as CodeBlockUI } from './codeblockui';
11
+ export type { default as CodeBlockCommand } from './codeblockcommand';
12
+ export type { default as IndentCodeBlockCommand } from './indentcodeblockcommand';
13
+ export type { default as OutdentCodeBlockCommand } from './outdentcodeblockcommand';
14
+ export type { CodeBlockConfig } from './codeblockconfig';
15
+ import './augmentation';
package/src/index.js CHANGED
@@ -1,11 +1,11 @@
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
7
- */
8
- export { default as CodeBlock } from './codeblock';
9
- export { default as CodeBlockEditing } from './codeblockediting';
10
- export { default as CodeBlockUI } from './codeblockui';
11
- import './augmentation';
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
7
+ */
8
+ export { default as CodeBlock } from './codeblock';
9
+ export { default as CodeBlockEditing } from './codeblockediting';
10
+ export { default as CodeBlockUI } from './codeblockui';
11
+ import './augmentation';
@@ -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
- import { Command, type Editor } from 'ckeditor5/src/core';
6
- /**
7
- * The code block indentation decrease command plugin.
8
- */
9
- export default class OutdentCodeBlockCommand extends Command {
10
- /**
11
- * A sequence of characters removed from the line when the command is executed.
12
- */
13
- private readonly _indentSequence;
14
- constructor(editor: Editor);
15
- /**
16
- * @inheritDoc
17
- */
18
- refresh(): void;
19
- /**
20
- * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
21
- * code lines in the selection will be decreased.
22
- *
23
- * @fires execute
24
- */
25
- execute(): void;
26
- /**
27
- * Checks whether the command can be enabled in the current context.
28
- *
29
- * @private
30
- * @returns {Boolean} Whether the command should be enabled.
31
- */
32
- private _checkEnabled;
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
+ import { Command, type Editor } from 'ckeditor5/src/core';
6
+ /**
7
+ * The code block indentation decrease command plugin.
8
+ */
9
+ export default class OutdentCodeBlockCommand extends Command {
10
+ /**
11
+ * A sequence of characters removed from the line when the command is executed.
12
+ */
13
+ private readonly _indentSequence;
14
+ constructor(editor: Editor);
15
+ /**
16
+ * @inheritDoc
17
+ */
18
+ refresh(): void;
19
+ /**
20
+ * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
21
+ * code lines in the selection will be decreased.
22
+ *
23
+ * @fires execute
24
+ */
25
+ execute(): void;
26
+ /**
27
+ * Checks whether the command can be enabled in the current context.
28
+ *
29
+ * @private
30
+ * @returns {Boolean} Whether the command should be enabled.
31
+ */
32
+ private _checkEnabled;
33
+ }
@@ -1,148 +1,148 @@
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 { getLeadingWhiteSpaces, getIndentOutdentPositions, isModelSelectionInCodeBlock } from './utils';
7
- /**
8
- * The code block indentation decrease command plugin.
9
- */
10
- export default class OutdentCodeBlockCommand extends Command {
11
- constructor(editor) {
12
- super(editor);
13
- this._indentSequence = editor.config.get('codeBlock.indentSequence');
14
- }
15
- /**
16
- * @inheritDoc
17
- */
18
- refresh() {
19
- this.isEnabled = this._checkEnabled();
20
- }
21
- /**
22
- * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
23
- * code lines in the selection will be decreased.
24
- *
25
- * @fires execute
26
- */
27
- execute() {
28
- const editor = this.editor;
29
- const model = editor.model;
30
- model.change(() => {
31
- const positions = getIndentOutdentPositions(model);
32
- // Outdent all positions, for instance assuming the indent sequence is 4x space (" "):
33
- //
34
- // <codeBlock>^foo</codeBlock> -> <codeBlock>foo</codeBlock>
35
- //
36
- // <codeBlock> ^bar</codeBlock> -> <codeBlock>bar</codeBlock>
37
- //
38
- // Also, when there is more than one position:
39
- //
40
- // <codeBlock>
41
- // ^foobar
42
- // <softBreak></softBreak>
43
- // ^bazqux
44
- // </codeBlock>
45
- //
46
- // ->
47
- //
48
- // <codeBlock>
49
- // foobar
50
- // <softBreak></softBreak>
51
- // bazqux
52
- // </codeBlock>
53
- for (const position of positions) {
54
- const range = getLastOutdentableSequenceRange(model, position, this._indentSequence);
55
- if (range) {
56
- // Previously deletion was done by writer.remove(). It was changed to deleteContent() to enable
57
- // integration of code block with track changes. It's the easiest way of integration because deleteContent()
58
- // is already integrated with track changes, but if it ever cause any troubles it can be reverted, however
59
- // some additional work will be required in track changes integration of code block.
60
- model.deleteContent(model.createSelection(range));
61
- }
62
- }
63
- });
64
- }
65
- /**
66
- * Checks whether the command can be enabled in the current context.
67
- *
68
- * @private
69
- * @returns {Boolean} Whether the command should be enabled.
70
- */
71
- _checkEnabled() {
72
- if (!this._indentSequence) {
73
- return false;
74
- }
75
- const model = this.editor.model;
76
- if (!isModelSelectionInCodeBlock(model.document.selection)) {
77
- return false;
78
- }
79
- // Outdent command can execute only when there is an indent character sequence
80
- // in some of the lines.
81
- return getIndentOutdentPositions(model).some(position => {
82
- return getLastOutdentableSequenceRange(model, position, this._indentSequence);
83
- });
84
- }
85
- }
86
- // For a position coming from `getIndentOutdentPositions()`, it returns the range representing
87
- // the last occurrence of the indent sequence among the leading whitespaces of the code line the
88
- // position represents.
89
- //
90
- // For instance, assuming the indent sequence is 4x space (" "):
91
- //
92
- // <codeBlock>foo^</codeBlock> -> null
93
- // <codeBlock>foo^<softBreak></softBreak>bar</codeBlock> -> null
94
- // <codeBlock> ^foo</codeBlock> -> null
95
- // <codeBlock> ^foo</codeBlock> -> <codeBlock> [ ]foo</codeBlock>
96
- // <codeBlock> ^foo bar</codeBlock> -> <codeBlock>[ ]foo bar</codeBlock>
97
- //
98
- // @param {<module:engine/model/model~Model>} model
99
- // @param {<module:engine/model/position~Position>} position
100
- // @param {String} sequence
101
- // @returns {<module:engine/model/range~Range>|null}
102
- function getLastOutdentableSequenceRange(model, position, sequence) {
103
- // Positions start before each text node (code line). Get the node corresponding to the position.
104
- const nodeAtPosition = getCodeLineTextNodeAtPosition(position);
105
- if (!nodeAtPosition) {
106
- return null;
107
- }
108
- const leadingWhiteSpaces = getLeadingWhiteSpaces(nodeAtPosition);
109
- const lastIndexOfSequence = leadingWhiteSpaces.lastIndexOf(sequence);
110
- // For instance, assuming the indent sequence is 4x space (" "):
111
- //
112
- // <codeBlock> ^foo</codeBlock> -> null
113
- //
114
- if (lastIndexOfSequence + sequence.length !== leadingWhiteSpaces.length) {
115
- return null;
116
- }
117
- // For instance, assuming the indent sequence is 4x space (" "):
118
- //
119
- // <codeBlock> ^foo</codeBlock> -> null
120
- //
121
- if (lastIndexOfSequence === -1) {
122
- return null;
123
- }
124
- const { parent, startOffset } = nodeAtPosition;
125
- // Create a range that contains the **last** indent sequence among the leading whitespaces
126
- // of the line.
127
- //
128
- // For instance, assuming the indent sequence is 4x space (" "):
129
- //
130
- // <codeBlock> ^foo</codeBlock> -> <codeBlock> [ ]foo</codeBlock>
131
- //
132
- return model.createRange(model.createPositionAt(parent, startOffset + lastIndexOfSequence), model.createPositionAt(parent, startOffset + lastIndexOfSequence + sequence.length));
133
- }
134
- function getCodeLineTextNodeAtPosition(position) {
135
- // Positions start before each text node (code line). Get the node corresponding to the position.
136
- let nodeAtPosition = position.parent.getChild(position.index);
137
- // <codeBlock>foo^</codeBlock>
138
- // <codeBlock>foo^<softBreak></softBreak>bar</codeBlock>
139
- if (!nodeAtPosition || nodeAtPosition.is('element', 'softBreak')) {
140
- nodeAtPosition = position.nodeBefore;
141
- }
142
- // <codeBlock>^</codeBlock>
143
- // <codeBlock>foo^<softBreak></softBreak>bar</codeBlock>
144
- if (!nodeAtPosition || nodeAtPosition.is('element', 'softBreak')) {
145
- return null;
146
- }
147
- return nodeAtPosition;
148
- }
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 { getLeadingWhiteSpaces, getIndentOutdentPositions, isModelSelectionInCodeBlock } from './utils';
7
+ /**
8
+ * The code block indentation decrease command plugin.
9
+ */
10
+ export default class OutdentCodeBlockCommand extends Command {
11
+ constructor(editor) {
12
+ super(editor);
13
+ this._indentSequence = editor.config.get('codeBlock.indentSequence');
14
+ }
15
+ /**
16
+ * @inheritDoc
17
+ */
18
+ refresh() {
19
+ this.isEnabled = this._checkEnabled();
20
+ }
21
+ /**
22
+ * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
23
+ * code lines in the selection will be decreased.
24
+ *
25
+ * @fires execute
26
+ */
27
+ execute() {
28
+ const editor = this.editor;
29
+ const model = editor.model;
30
+ model.change(() => {
31
+ const positions = getIndentOutdentPositions(model);
32
+ // Outdent all positions, for instance assuming the indent sequence is 4x space (" "):
33
+ //
34
+ // <codeBlock>^foo</codeBlock> -> <codeBlock>foo</codeBlock>
35
+ //
36
+ // <codeBlock> ^bar</codeBlock> -> <codeBlock>bar</codeBlock>
37
+ //
38
+ // Also, when there is more than one position:
39
+ //
40
+ // <codeBlock>
41
+ // ^foobar
42
+ // <softBreak></softBreak>
43
+ // ^bazqux
44
+ // </codeBlock>
45
+ //
46
+ // ->
47
+ //
48
+ // <codeBlock>
49
+ // foobar
50
+ // <softBreak></softBreak>
51
+ // bazqux
52
+ // </codeBlock>
53
+ for (const position of positions) {
54
+ const range = getLastOutdentableSequenceRange(model, position, this._indentSequence);
55
+ if (range) {
56
+ // Previously deletion was done by writer.remove(). It was changed to deleteContent() to enable
57
+ // integration of code block with track changes. It's the easiest way of integration because deleteContent()
58
+ // is already integrated with track changes, but if it ever cause any troubles it can be reverted, however
59
+ // some additional work will be required in track changes integration of code block.
60
+ model.deleteContent(model.createSelection(range));
61
+ }
62
+ }
63
+ });
64
+ }
65
+ /**
66
+ * Checks whether the command can be enabled in the current context.
67
+ *
68
+ * @private
69
+ * @returns {Boolean} Whether the command should be enabled.
70
+ */
71
+ _checkEnabled() {
72
+ if (!this._indentSequence) {
73
+ return false;
74
+ }
75
+ const model = this.editor.model;
76
+ if (!isModelSelectionInCodeBlock(model.document.selection)) {
77
+ return false;
78
+ }
79
+ // Outdent command can execute only when there is an indent character sequence
80
+ // in some of the lines.
81
+ return getIndentOutdentPositions(model).some(position => {
82
+ return getLastOutdentableSequenceRange(model, position, this._indentSequence);
83
+ });
84
+ }
85
+ }
86
+ // For a position coming from `getIndentOutdentPositions()`, it returns the range representing
87
+ // the last occurrence of the indent sequence among the leading whitespaces of the code line the
88
+ // position represents.
89
+ //
90
+ // For instance, assuming the indent sequence is 4x space (" "):
91
+ //
92
+ // <codeBlock>foo^</codeBlock> -> null
93
+ // <codeBlock>foo^<softBreak></softBreak>bar</codeBlock> -> null
94
+ // <codeBlock> ^foo</codeBlock> -> null
95
+ // <codeBlock> ^foo</codeBlock> -> <codeBlock> [ ]foo</codeBlock>
96
+ // <codeBlock> ^foo bar</codeBlock> -> <codeBlock>[ ]foo bar</codeBlock>
97
+ //
98
+ // @param {<module:engine/model/model~Model>} model
99
+ // @param {<module:engine/model/position~Position>} position
100
+ // @param {String} sequence
101
+ // @returns {<module:engine/model/range~Range>|null}
102
+ function getLastOutdentableSequenceRange(model, position, sequence) {
103
+ // Positions start before each text node (code line). Get the node corresponding to the position.
104
+ const nodeAtPosition = getCodeLineTextNodeAtPosition(position);
105
+ if (!nodeAtPosition) {
106
+ return null;
107
+ }
108
+ const leadingWhiteSpaces = getLeadingWhiteSpaces(nodeAtPosition);
109
+ const lastIndexOfSequence = leadingWhiteSpaces.lastIndexOf(sequence);
110
+ // For instance, assuming the indent sequence is 4x space (" "):
111
+ //
112
+ // <codeBlock> ^foo</codeBlock> -> null
113
+ //
114
+ if (lastIndexOfSequence + sequence.length !== leadingWhiteSpaces.length) {
115
+ return null;
116
+ }
117
+ // For instance, assuming the indent sequence is 4x space (" "):
118
+ //
119
+ // <codeBlock> ^foo</codeBlock> -> null
120
+ //
121
+ if (lastIndexOfSequence === -1) {
122
+ return null;
123
+ }
124
+ const { parent, startOffset } = nodeAtPosition;
125
+ // Create a range that contains the **last** indent sequence among the leading whitespaces
126
+ // of the line.
127
+ //
128
+ // For instance, assuming the indent sequence is 4x space (" "):
129
+ //
130
+ // <codeBlock> ^foo</codeBlock> -> <codeBlock> [ ]foo</codeBlock>
131
+ //
132
+ return model.createRange(model.createPositionAt(parent, startOffset + lastIndexOfSequence), model.createPositionAt(parent, startOffset + lastIndexOfSequence + sequence.length));
133
+ }
134
+ function getCodeLineTextNodeAtPosition(position) {
135
+ // Positions start before each text node (code line). Get the node corresponding to the position.
136
+ let nodeAtPosition = position.parent.getChild(position.index);
137
+ // <codeBlock>foo^</codeBlock>
138
+ // <codeBlock>foo^<softBreak></softBreak>bar</codeBlock>
139
+ if (!nodeAtPosition || nodeAtPosition.is('element', 'softBreak')) {
140
+ nodeAtPosition = position.nodeBefore;
141
+ }
142
+ // <codeBlock>^</codeBlock>
143
+ // <codeBlock>foo^<softBreak></softBreak>bar</codeBlock>
144
+ if (!nodeAtPosition || nodeAtPosition.is('element', 'softBreak')) {
145
+ return null;
146
+ }
147
+ return nodeAtPosition;
148
+ }