@ckeditor/ckeditor5-code-block 0.0.0-nightly-next-20260118.0 → 0.0.0-nightly-20260119.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.
- package/build/code-block.js +5 -0
- package/build/translations/af.js +1 -0
- package/build/translations/ar.js +1 -0
- package/build/translations/ast.js +1 -0
- package/build/translations/az.js +1 -0
- package/build/translations/be.js +1 -0
- package/build/translations/bg.js +1 -0
- package/build/translations/bn.js +1 -0
- package/build/translations/bs.js +1 -0
- package/build/translations/ca.js +1 -0
- package/build/translations/cs.js +1 -0
- package/build/translations/da.js +1 -0
- package/build/translations/de-ch.js +1 -0
- package/build/translations/de.js +1 -0
- package/build/translations/el.js +1 -0
- package/build/translations/en-au.js +1 -0
- package/build/translations/en-gb.js +1 -0
- package/build/translations/eo.js +1 -0
- package/build/translations/es-co.js +1 -0
- package/build/translations/es.js +1 -0
- package/build/translations/et.js +1 -0
- package/build/translations/eu.js +1 -0
- package/build/translations/fa.js +1 -0
- package/build/translations/fi.js +1 -0
- package/build/translations/fr.js +1 -0
- package/build/translations/gl.js +1 -0
- package/build/translations/gu.js +1 -0
- package/build/translations/he.js +1 -0
- package/build/translations/hi.js +1 -0
- package/build/translations/hr.js +1 -0
- package/build/translations/hu.js +1 -0
- package/build/translations/hy.js +1 -0
- package/build/translations/id.js +1 -0
- package/build/translations/it.js +1 -0
- package/build/translations/ja.js +1 -0
- package/build/translations/jv.js +1 -0
- package/build/translations/kk.js +1 -0
- package/build/translations/km.js +1 -0
- package/build/translations/kn.js +1 -0
- package/build/translations/ko.js +1 -0
- package/build/translations/ku.js +1 -0
- package/build/translations/lt.js +1 -0
- package/build/translations/lv.js +1 -0
- package/build/translations/ms.js +1 -0
- package/build/translations/nb.js +1 -0
- package/build/translations/ne.js +1 -0
- package/build/translations/nl.js +1 -0
- package/build/translations/no.js +1 -0
- package/build/translations/oc.js +1 -0
- package/build/translations/pl.js +1 -0
- package/build/translations/pt-br.js +1 -0
- package/build/translations/pt.js +1 -0
- package/build/translations/ro.js +1 -0
- package/build/translations/ru.js +1 -0
- package/build/translations/si.js +1 -0
- package/build/translations/sk.js +1 -0
- package/build/translations/sl.js +1 -0
- package/build/translations/sq.js +1 -0
- package/build/translations/sr-latn.js +1 -0
- package/build/translations/sr.js +1 -0
- package/build/translations/sv.js +1 -0
- package/build/translations/th.js +1 -0
- package/build/translations/ti.js +1 -0
- package/build/translations/tk.js +1 -0
- package/build/translations/tr.js +1 -0
- package/build/translations/tt.js +1 -0
- package/build/translations/ug.js +1 -0
- package/build/translations/uk.js +1 -0
- package/build/translations/ur.js +1 -0
- package/build/translations/uz.js +1 -0
- package/build/translations/vi.js +1 -0
- package/build/translations/zh-cn.js +1 -0
- package/build/translations/zh.js +1 -0
- package/ckeditor5-metadata.json +1 -1
- package/dist/index.js.map +1 -1
- package/lang/contexts.json +9 -0
- package/lang/translations/af.po +40 -0
- package/lang/translations/ar.po +40 -0
- package/lang/translations/ast.po +40 -0
- package/lang/translations/az.po +40 -0
- package/lang/translations/be.po +40 -0
- package/lang/translations/bg.po +40 -0
- package/lang/translations/bn.po +40 -0
- package/lang/translations/bs.po +40 -0
- package/lang/translations/ca.po +40 -0
- package/lang/translations/cs.po +40 -0
- package/lang/translations/da.po +40 -0
- package/lang/translations/de-ch.po +40 -0
- package/lang/translations/de.po +40 -0
- package/lang/translations/el.po +40 -0
- package/lang/translations/en-au.po +40 -0
- package/lang/translations/en-gb.po +40 -0
- package/lang/translations/en.po +40 -0
- package/lang/translations/eo.po +40 -0
- package/lang/translations/es-co.po +40 -0
- package/lang/translations/es.po +40 -0
- package/lang/translations/et.po +40 -0
- package/lang/translations/eu.po +40 -0
- package/lang/translations/fa.po +40 -0
- package/lang/translations/fi.po +40 -0
- package/lang/translations/fr.po +40 -0
- package/lang/translations/gl.po +40 -0
- package/lang/translations/gu.po +40 -0
- package/lang/translations/he.po +40 -0
- package/lang/translations/hi.po +40 -0
- package/lang/translations/hr.po +40 -0
- package/lang/translations/hu.po +40 -0
- package/lang/translations/hy.po +40 -0
- package/lang/translations/id.po +40 -0
- package/lang/translations/it.po +40 -0
- package/lang/translations/ja.po +40 -0
- package/lang/translations/jv.po +40 -0
- package/lang/translations/kk.po +40 -0
- package/lang/translations/km.po +40 -0
- package/lang/translations/kn.po +40 -0
- package/lang/translations/ko.po +40 -0
- package/lang/translations/ku.po +40 -0
- package/lang/translations/lt.po +40 -0
- package/lang/translations/lv.po +40 -0
- package/lang/translations/ms.po +40 -0
- package/lang/translations/nb.po +40 -0
- package/lang/translations/ne.po +40 -0
- package/lang/translations/nl.po +40 -0
- package/lang/translations/no.po +40 -0
- package/lang/translations/oc.po +40 -0
- package/lang/translations/pl.po +40 -0
- package/lang/translations/pt-br.po +40 -0
- package/lang/translations/pt.po +40 -0
- package/lang/translations/ro.po +40 -0
- package/lang/translations/ru.po +40 -0
- package/lang/translations/si.po +40 -0
- package/lang/translations/sk.po +40 -0
- package/lang/translations/sl.po +40 -0
- package/lang/translations/sq.po +40 -0
- package/lang/translations/sr-latn.po +40 -0
- package/lang/translations/sr.po +40 -0
- package/lang/translations/sv.po +40 -0
- package/lang/translations/th.po +40 -0
- package/lang/translations/ti.po +40 -0
- package/lang/translations/tk.po +40 -0
- package/lang/translations/tr.po +40 -0
- package/lang/translations/tt.po +40 -0
- package/lang/translations/ug.po +40 -0
- package/lang/translations/uk.po +40 -0
- package/lang/translations/ur.po +40 -0
- package/lang/translations/uz.po +40 -0
- package/lang/translations/vi.po +40 -0
- package/lang/translations/zh-cn.po +40 -0
- package/lang/translations/zh.po +40 -0
- package/package.json +49 -25
- package/src/augmentation.js +5 -0
- package/{dist → src}/codeblock.d.ts +1 -1
- package/src/codeblock.js +39 -0
- package/{dist → src}/codeblockcommand.d.ts +1 -1
- package/src/codeblockcommand.js +142 -0
- package/src/codeblockconfig.js +5 -0
- package/{dist → src}/codeblockediting.d.ts +2 -2
- package/src/codeblockediting.js +430 -0
- package/{dist → src}/codeblockui.d.ts +1 -1
- package/src/codeblockui.js +134 -0
- package/{dist → src}/converters.d.ts +2 -2
- package/src/converters.js +285 -0
- package/{dist → src}/indentcodeblockcommand.d.ts +1 -1
- package/src/indentcodeblockcommand.js +82 -0
- package/src/index.js +16 -0
- package/{dist → src}/outdentcodeblockcommand.d.ts +1 -1
- package/src/outdentcodeblockcommand.js +137 -0
- package/{dist → src}/utils.d.ts +3 -3
- package/src/utils.js +296 -0
- package/theme/codeblock.css +40 -0
- /package/{dist → src}/augmentation.d.ts +0 -0
- /package/{dist → src}/codeblockconfig.d.ts +0 -0
- /package/{dist → src}/index.d.ts +0 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
+
*/
|
|
5
|
+
import { Command } from 'ckeditor5/src/core.js';
|
|
6
|
+
import { getLeadingWhiteSpaces, getIndentOutdentPositions, isModelSelectionInCodeBlock, getTextNodeAtLineStart } from './utils.js';
|
|
7
|
+
/**
|
|
8
|
+
* The code block indentation decrease command plugin.
|
|
9
|
+
*/
|
|
10
|
+
export class OutdentCodeBlockCommand extends Command {
|
|
11
|
+
/**
|
|
12
|
+
* A sequence of characters removed from the line when the command is executed.
|
|
13
|
+
*/
|
|
14
|
+
_indentSequence;
|
|
15
|
+
constructor(editor) {
|
|
16
|
+
super(editor);
|
|
17
|
+
this._indentSequence = editor.config.get('codeBlock.indentSequence');
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @inheritDoc
|
|
21
|
+
*/
|
|
22
|
+
refresh() {
|
|
23
|
+
this.isEnabled = this._checkEnabled();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
|
|
27
|
+
* code lines in the selection will be decreased.
|
|
28
|
+
*
|
|
29
|
+
* @fires execute
|
|
30
|
+
*/
|
|
31
|
+
execute() {
|
|
32
|
+
const editor = this.editor;
|
|
33
|
+
const model = editor.model;
|
|
34
|
+
model.change(() => {
|
|
35
|
+
const positions = getIndentOutdentPositions(model);
|
|
36
|
+
// Outdent all positions, for instance assuming the indent sequence is 4x space (" "):
|
|
37
|
+
//
|
|
38
|
+
// <codeBlock>^foo</codeBlock> -> <codeBlock>foo</codeBlock>
|
|
39
|
+
//
|
|
40
|
+
// <codeBlock> ^bar</codeBlock> -> <codeBlock>bar</codeBlock>
|
|
41
|
+
//
|
|
42
|
+
// Also, when there is more than one position:
|
|
43
|
+
//
|
|
44
|
+
// <codeBlock>
|
|
45
|
+
// ^foobar
|
|
46
|
+
// <softBreak></softBreak>
|
|
47
|
+
// ^bazqux
|
|
48
|
+
// </codeBlock>
|
|
49
|
+
//
|
|
50
|
+
// ->
|
|
51
|
+
//
|
|
52
|
+
// <codeBlock>
|
|
53
|
+
// foobar
|
|
54
|
+
// <softBreak></softBreak>
|
|
55
|
+
// bazqux
|
|
56
|
+
// </codeBlock>
|
|
57
|
+
for (const position of positions) {
|
|
58
|
+
const range = getLastOutdentableSequenceRange(model, position, this._indentSequence);
|
|
59
|
+
if (range) {
|
|
60
|
+
// Previously deletion was done by writer.remove(). It was changed to deleteContent() to enable
|
|
61
|
+
// integration of code block with track changes. It's the easiest way of integration because deleteContent()
|
|
62
|
+
// is already integrated with track changes, but if it ever cause any troubles it can be reverted, however
|
|
63
|
+
// some additional work will be required in track changes integration of code block.
|
|
64
|
+
model.deleteContent(model.createSelection(range));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Checks whether the command can be enabled in the current context.
|
|
71
|
+
*
|
|
72
|
+
* @private
|
|
73
|
+
* @returns {Boolean} Whether the command should be enabled.
|
|
74
|
+
*/
|
|
75
|
+
_checkEnabled() {
|
|
76
|
+
if (!this._indentSequence) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const model = this.editor.model;
|
|
80
|
+
if (!isModelSelectionInCodeBlock(model.document.selection)) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
// Outdent command can execute only when there is an indent character sequence
|
|
84
|
+
// in some of the lines.
|
|
85
|
+
return getIndentOutdentPositions(model).some(position => {
|
|
86
|
+
return getLastOutdentableSequenceRange(model, position, this._indentSequence);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// For a position coming from `getIndentOutdentPositions()`, it returns the range representing
|
|
91
|
+
// the last occurrence of the indent sequence among the leading whitespaces of the code line the
|
|
92
|
+
// position represents.
|
|
93
|
+
//
|
|
94
|
+
// For instance, assuming the indent sequence is 4x space (" "):
|
|
95
|
+
//
|
|
96
|
+
// <codeBlock>foo^</codeBlock> -> null
|
|
97
|
+
// <codeBlock>foo^<softBreak></softBreak>bar</codeBlock> -> null
|
|
98
|
+
// <codeBlock> ^foo</codeBlock> -> null
|
|
99
|
+
// <codeBlock> ^foo</codeBlock> -> <codeBlock> [ ]foo</codeBlock>
|
|
100
|
+
// <codeBlock> ^foo bar</codeBlock> -> <codeBlock>[ ]foo bar</codeBlock>
|
|
101
|
+
//
|
|
102
|
+
// @param {<module:engine/model/model~Model>} model
|
|
103
|
+
// @param {<module:engine/model/position~ModelPosition>} position
|
|
104
|
+
// @param {String} sequence
|
|
105
|
+
// @returns {<module:engine/model/range~ModelRange>|null}
|
|
106
|
+
function getLastOutdentableSequenceRange(model, position, sequence) {
|
|
107
|
+
// Positions start before each text node (code line). Get the node corresponding to the position.
|
|
108
|
+
const nodeAtPosition = getTextNodeAtLineStart(position, model);
|
|
109
|
+
if (!nodeAtPosition) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const leadingWhiteSpaces = getLeadingWhiteSpaces(nodeAtPosition);
|
|
113
|
+
const lastIndexOfSequence = leadingWhiteSpaces.lastIndexOf(sequence);
|
|
114
|
+
// For instance, assuming the indent sequence is 4x space (" "):
|
|
115
|
+
//
|
|
116
|
+
// <codeBlock> ^foo</codeBlock> -> null
|
|
117
|
+
//
|
|
118
|
+
if (lastIndexOfSequence + sequence.length !== leadingWhiteSpaces.length) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
// For instance, assuming the indent sequence is 4x space (" "):
|
|
122
|
+
//
|
|
123
|
+
// <codeBlock> ^foo</codeBlock> -> null
|
|
124
|
+
//
|
|
125
|
+
if (lastIndexOfSequence === -1) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const { parent, startOffset } = nodeAtPosition;
|
|
129
|
+
// Create a range that contains the **last** indent sequence among the leading whitespaces
|
|
130
|
+
// of the line.
|
|
131
|
+
//
|
|
132
|
+
// For instance, assuming the indent sequence is 4x space (" "):
|
|
133
|
+
//
|
|
134
|
+
// <codeBlock> ^foo</codeBlock> -> <codeBlock> [ ]foo</codeBlock>
|
|
135
|
+
//
|
|
136
|
+
return model.createRange(model.createPositionAt(parent, startOffset + lastIndexOfSequence), model.createPositionAt(parent, startOffset + lastIndexOfSequence + sequence.length));
|
|
137
|
+
}
|
package/{dist → src}/utils.d.ts
RENAMED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* @module code-block/utils
|
|
7
7
|
*/
|
|
8
|
-
import type { Editor } from '
|
|
8
|
+
import type { Editor } from 'ckeditor5/src/core.js';
|
|
9
9
|
import type { CodeBlockLanguageDefinition } from './codeblockconfig.js';
|
|
10
|
-
import type { ModelDocumentSelection, ModelElement, Model, ModelPosition, ModelSchema, ModelText, ViewUpcastWriter, ViewDocumentFragment } from '
|
|
11
|
-
import { type LocaleTranslate } from '
|
|
10
|
+
import type { ModelDocumentSelection, ModelElement, Model, ModelPosition, ModelSchema, ModelText, ViewUpcastWriter, ViewDocumentFragment } from 'ckeditor5/src/engine.js';
|
|
11
|
+
import { type LocaleTranslate } from 'ckeditor5/src/utils.js';
|
|
12
12
|
/**
|
|
13
13
|
* Returns code block languages as defined in `config.codeBlock.languages` but processed:
|
|
14
14
|
*
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
+
*/
|
|
5
|
+
import { first } from 'ckeditor5/src/utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Returns code block languages as defined in `config.codeBlock.languages` but processed:
|
|
8
|
+
*
|
|
9
|
+
* * To consider the editor localization, i.e. to display {@link module:code-block/codeblockconfig~CodeBlockLanguageDefinition}
|
|
10
|
+
* in the correct language. There is no way to use {@link module:utils/locale~Locale#t} when the user
|
|
11
|
+
* configuration is defined because the editor does not exist yet.
|
|
12
|
+
* * To make sure each definition has a CSS class associated with it even if not specified
|
|
13
|
+
* in the original configuration.
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export function getNormalizedAndLocalizedLanguageDefinitions(editor) {
|
|
18
|
+
const t = editor.t;
|
|
19
|
+
const languageDefs = editor.config.get('codeBlock.languages');
|
|
20
|
+
for (const def of languageDefs) {
|
|
21
|
+
if (def.label === 'Plain text') {
|
|
22
|
+
def.label = t('Plain text');
|
|
23
|
+
}
|
|
24
|
+
if (def.class === undefined) {
|
|
25
|
+
def.class = `language-${def.language}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return languageDefs;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns an object associating certain language definition properties with others. For instance:
|
|
32
|
+
*
|
|
33
|
+
* For:
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const definitions = {
|
|
37
|
+
* { language: 'php', class: 'language-php', label: 'PHP' },
|
|
38
|
+
* { language: 'javascript', class: 'js', label: 'JavaScript' },
|
|
39
|
+
* };
|
|
40
|
+
*
|
|
41
|
+
* getPropertyAssociation( definitions, 'class', 'language' );
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* returns:
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* {
|
|
48
|
+
* 'language-php': 'php',
|
|
49
|
+
* 'js': 'javascript'
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* and
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* getPropertyAssociation( definitions, 'language', 'label' );
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* returns:
|
|
60
|
+
*
|
|
61
|
+
* ```ts
|
|
62
|
+
* {
|
|
63
|
+
* 'php': 'PHP',
|
|
64
|
+
* 'javascript': 'JavaScript'
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
export function getPropertyAssociation(languageDefs, key, value) {
|
|
71
|
+
const association = {};
|
|
72
|
+
for (const def of languageDefs) {
|
|
73
|
+
if (key === 'class') {
|
|
74
|
+
// Only the first class is considered.
|
|
75
|
+
const newKey = (def[key]).split(' ').shift();
|
|
76
|
+
association[newKey] = def[value];
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
association[def[key]] = def[value];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return association;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* For a given model text node, it returns white spaces that precede other characters in that node.
|
|
86
|
+
* This corresponds to the indentation part of the code block line.
|
|
87
|
+
*
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
export function getLeadingWhiteSpaces(textNode) {
|
|
91
|
+
return textNode.data.match(/^(\s*)/)[0];
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* For plain text containing the code (a snippet), it returns a document fragment containing
|
|
95
|
+
* view text nodes separated by `<br>` elements (in place of new line characters "\n"), for instance:
|
|
96
|
+
*
|
|
97
|
+
* Input:
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
* "foo()\n
|
|
101
|
+
* bar()"
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* Output:
|
|
105
|
+
*
|
|
106
|
+
* ```html
|
|
107
|
+
* <DocumentFragment>
|
|
108
|
+
* "foo()"
|
|
109
|
+
* <br/>
|
|
110
|
+
* "bar()"
|
|
111
|
+
* </DocumentFragment>
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @param text The raw code text to be converted.
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
export function rawSnippetTextToViewDocumentFragment(writer, text) {
|
|
118
|
+
const fragment = writer.createDocumentFragment();
|
|
119
|
+
const textLines = text.split('\n');
|
|
120
|
+
const items = textLines.reduce((nodes, line, lineIndex) => {
|
|
121
|
+
nodes.push(line);
|
|
122
|
+
if (lineIndex < textLines.length - 1) {
|
|
123
|
+
nodes.push(writer.createElement('br'));
|
|
124
|
+
}
|
|
125
|
+
return nodes;
|
|
126
|
+
}, []);
|
|
127
|
+
writer.appendChild(items, fragment);
|
|
128
|
+
return fragment;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Returns an array of all model positions within the selection that represent code block lines.
|
|
132
|
+
*
|
|
133
|
+
* If the selection is collapsed, it returns the exact selection anchor position:
|
|
134
|
+
*
|
|
135
|
+
* ```html
|
|
136
|
+
* <codeBlock>[]foo</codeBlock> -> <codeBlock>^foo</codeBlock>
|
|
137
|
+
* <codeBlock>foo[]bar</codeBlock> -> <codeBlock>foo^bar</codeBlock>
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* Otherwise, it returns positions **before** each text node belonging to all code blocks contained by the selection:
|
|
141
|
+
*
|
|
142
|
+
* ```html
|
|
143
|
+
* <codeBlock> <codeBlock>
|
|
144
|
+
* foo[bar ^foobar
|
|
145
|
+
* <softBreak></softBreak> -> <softBreak></softBreak>
|
|
146
|
+
* baz]qux ^bazqux
|
|
147
|
+
* </codeBlock> </codeBlock>
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* It also works across other non–code blocks:
|
|
151
|
+
*
|
|
152
|
+
* ```html
|
|
153
|
+
* <codeBlock> <codeBlock>
|
|
154
|
+
* foo[bar ^foobar
|
|
155
|
+
* </codeBlock> </codeBlock>
|
|
156
|
+
* <paragraph>text</paragraph> -> <paragraph>text</paragraph>
|
|
157
|
+
* <codeBlock> <codeBlock>
|
|
158
|
+
* baz]qux ^bazqux
|
|
159
|
+
* </codeBlock> </codeBlock>
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* **Note:** The positions are in reverse order so they do not get outdated when iterating over them and
|
|
163
|
+
* the writer inserts or removes elements at the same time.
|
|
164
|
+
*
|
|
165
|
+
* **Note:** The position is located after the leading white spaces in the text node.
|
|
166
|
+
*
|
|
167
|
+
* @internal
|
|
168
|
+
*/
|
|
169
|
+
export function getIndentOutdentPositions(model) {
|
|
170
|
+
const selection = model.document.selection;
|
|
171
|
+
const positions = [];
|
|
172
|
+
// When the selection is collapsed, there's only one position we can indent or outdent.
|
|
173
|
+
if (selection.isCollapsed) {
|
|
174
|
+
return [selection.anchor];
|
|
175
|
+
}
|
|
176
|
+
// When the selection is NOT collapsed, collect all positions starting before text nodes
|
|
177
|
+
// (code lines) in any <codeBlock> within the selection.
|
|
178
|
+
// Walk backward so positions we are about to collect here do not get outdated when
|
|
179
|
+
// inserting or deleting using the writer.
|
|
180
|
+
const walker = selection.getFirstRange().getWalker({
|
|
181
|
+
ignoreElementEnd: true,
|
|
182
|
+
direction: 'backward'
|
|
183
|
+
});
|
|
184
|
+
for (const { item } of walker) {
|
|
185
|
+
let node = item.is('$textProxy') ? item.textNode : item;
|
|
186
|
+
const parent = node.parent;
|
|
187
|
+
if (!parent.is('element', 'codeBlock') || node.is('element', 'softBreak')) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
// For each item in code block, move backwards until the beginning of the line it is in is found.
|
|
191
|
+
while (node.previousSibling && !node.previousSibling.is('element', 'softBreak')) {
|
|
192
|
+
node = node.previousSibling;
|
|
193
|
+
}
|
|
194
|
+
// Take the leading white spaces into account (only for text nodes).
|
|
195
|
+
const startOffset = !node.is('$text') ? node.startOffset : node.startOffset + getLeadingWhiteSpaces(node).length;
|
|
196
|
+
const position = model.createPositionAt(parent, startOffset);
|
|
197
|
+
// Do not add the same position twice. Unfortunately using set doesn't deduplicate positions because
|
|
198
|
+
// they are different objects.
|
|
199
|
+
if (positions.every(pos => !pos.isEqual(position))) {
|
|
200
|
+
positions.push(position);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return positions;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Checks if any of the blocks within the model selection is a code block.
|
|
207
|
+
*
|
|
208
|
+
* @internal
|
|
209
|
+
*/
|
|
210
|
+
export function isModelSelectionInCodeBlock(selection) {
|
|
211
|
+
const firstBlock = first(selection.getSelectedBlocks());
|
|
212
|
+
return !!firstBlock && firstBlock.is('element', 'codeBlock');
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Checks if an {@link module:engine/model/element~ModelElement Element} can become a code block.
|
|
216
|
+
*
|
|
217
|
+
* @param schema Model's schema.
|
|
218
|
+
* @param element The element to be checked.
|
|
219
|
+
* @returns Check result.
|
|
220
|
+
* @internal
|
|
221
|
+
*/
|
|
222
|
+
export function canBeCodeBlock(schema, element) {
|
|
223
|
+
if (element.is('rootElement') || schema.isLimit(element)) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
return schema.checkChild(element.parent, 'codeBlock');
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Get the translated message read by the screen reader when you enter or exit an element with your cursor.
|
|
230
|
+
*
|
|
231
|
+
* @internal
|
|
232
|
+
*/
|
|
233
|
+
export function getCodeBlockAriaAnnouncement(t, languageDefs, element, direction) {
|
|
234
|
+
const languagesToLabels = getPropertyAssociation(languageDefs, 'language', 'label');
|
|
235
|
+
const codeBlockLanguage = element.getAttribute('language');
|
|
236
|
+
if (codeBlockLanguage in languagesToLabels) {
|
|
237
|
+
const language = languagesToLabels[codeBlockLanguage];
|
|
238
|
+
if (direction === 'enter') {
|
|
239
|
+
return t('Entering %0 code snippet', language);
|
|
240
|
+
}
|
|
241
|
+
return t('Leaving %0 code snippet', language);
|
|
242
|
+
}
|
|
243
|
+
if (direction === 'enter') {
|
|
244
|
+
return t('Entering code snippet');
|
|
245
|
+
}
|
|
246
|
+
return t('Leaving code snippet');
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* For given position, finds the closest position that is at the beginning of a line of code and returns a text node that is at the
|
|
250
|
+
* beginning of the line (or `null` if there's no text node at the beginning of a given line).
|
|
251
|
+
*
|
|
252
|
+
* Line beings at the start of a code block element and after each `softBreak` element.
|
|
253
|
+
*
|
|
254
|
+
* Note: even though code block doesn't allow inline elements other than `<softBreak>` by default, some features may overwrite this rule,
|
|
255
|
+
* so such inline elements are taken into account.
|
|
256
|
+
*
|
|
257
|
+
* Some examples of expected results:
|
|
258
|
+
*
|
|
259
|
+
* ```
|
|
260
|
+
* <codeBlock>^</codeBlock> -> null
|
|
261
|
+
* <codeBlock>^foobar</codeBlock> -> <codeBlock>[foobar]</codeBlock>
|
|
262
|
+
* <codeBlock>foobar^</codeBlock> -> <codeBlock>[foobar]</codeBlock>
|
|
263
|
+
* <codeBlock>foo^bar</codeBlock> -> <codeBlock>[foobar]</codeBlock>
|
|
264
|
+
* <codeBlock>foo^<softBreak />bar</codeBlock> -> <codeBlock>[foo]<softBreak />bar</codeBlock>
|
|
265
|
+
* <codeBlock>foo<softBreak />bar^</codeBlock> -> <codeBlock>foo<softBreak />[bar]</codeBlock>
|
|
266
|
+
* <codeBlock>foo<softBreak />b^ar</codeBlock> -> <codeBlock>foo<softBreak />[bar]</codeBlock>
|
|
267
|
+
* <codeBlock>foo<softBreak />^bar</codeBlock> -> <codeBlock>foo<softBreak />[bar]</codeBlock>
|
|
268
|
+
* <codeBlock>^<element /></codeBlock> -> null
|
|
269
|
+
* <codeBlock><element />^</codeBlock> -> null
|
|
270
|
+
* <codeBlock>foo^<element /></codeBlock> -> <codeBlock>[foo]<element /></codeBlock>
|
|
271
|
+
* <codeBlock>foo<element />^</codeBlock> -> <codeBlock>[foo]<element /></codeBlock>
|
|
272
|
+
* <codeBlock>foo<element />bar^</codeBlock> -> <codeBlock>[foo]<element />bar</codeBlock>
|
|
273
|
+
* <codeBlock><element />bar^</codeBlock> -> null
|
|
274
|
+
* <codeBlock>foo<softBreak />^<softBreak /></codeBlock> -> null
|
|
275
|
+
* <codeBlock>foo<softBreak />^<element /></codeBlock> -> null
|
|
276
|
+
* <codeBlock>foo<softBreak /><element />^</codeBlock> -> null
|
|
277
|
+
* <codeBlock>foo<softBreak />bar<element />^</codeBlock> -> <codeBlock>foo<softBreak />[bar]<element /></codeBlock>
|
|
278
|
+
* <codeBlock>foo<softBreak /><element />ba^r</codeBlock> -> null
|
|
279
|
+
* ```
|
|
280
|
+
*
|
|
281
|
+
* @internal
|
|
282
|
+
*/
|
|
283
|
+
export function getTextNodeAtLineStart(position, model) {
|
|
284
|
+
// First, move position before a text node, if it is inside a text node.
|
|
285
|
+
if (position.textNode) {
|
|
286
|
+
position = model.createPositionBefore(position.textNode);
|
|
287
|
+
}
|
|
288
|
+
// Then, jump-back the position until it is before a `softBreak` or at the beginning of the `codeBlock`.
|
|
289
|
+
while (position.nodeBefore && !position.nodeBefore.is('element', 'softBreak')) {
|
|
290
|
+
position = model.createPositionBefore(position.nodeBefore);
|
|
291
|
+
}
|
|
292
|
+
// Now, the position is at the beginning of a line.
|
|
293
|
+
// Return a text node after the position, if there is one.
|
|
294
|
+
const nodeAtStart = position.nodeAfter;
|
|
295
|
+
return nodeAtStart && nodeAtStart.is('$text') ? nodeAtStart : null;
|
|
296
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
.ck-content pre {
|
|
7
|
+
padding: 1em;
|
|
8
|
+
color: hsl(0, 0%, 20.8%);
|
|
9
|
+
background: hsla(0, 0%, 78%, 0.3);
|
|
10
|
+
border: 1px solid hsl(0, 0%, 77%);
|
|
11
|
+
border-radius: 2px;
|
|
12
|
+
|
|
13
|
+
/* Code block are language direction–agnostic. */
|
|
14
|
+
text-align: left;
|
|
15
|
+
direction: ltr;
|
|
16
|
+
|
|
17
|
+
tab-size: 4;
|
|
18
|
+
white-space: pre-wrap;
|
|
19
|
+
|
|
20
|
+
/* Don't inherit the style, e.g. when in a block quote. */
|
|
21
|
+
font-style: normal;
|
|
22
|
+
|
|
23
|
+
/* Don't let the code be squashed e.g. when in a table cell. */
|
|
24
|
+
min-width: 200px;
|
|
25
|
+
|
|
26
|
+
& code {
|
|
27
|
+
background: unset;
|
|
28
|
+
padding: 0;
|
|
29
|
+
border-radius: 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.ck.ck-editor__editable pre {
|
|
34
|
+
position: relative;
|
|
35
|
+
|
|
36
|
+
&[data-language]::after {
|
|
37
|
+
content: attr(data-language);
|
|
38
|
+
position: absolute;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
File without changes
|
|
File without changes
|
/package/{dist → src}/index.d.ts
RENAMED
|
File without changes
|