@ckeditor/ckeditor5-code-block 40.0.0 → 40.1.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/LICENSE.md +3 -3
- package/package.json +2 -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 +382 -382
- 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/src/codeblockediting.js
CHANGED
|
@@ -1,382 +1,382 @@
|
|
|
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/codeblockediting
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
-
import { ShiftEnter } from 'ckeditor5/src/enter';
|
|
10
|
-
import { UpcastWriter } from 'ckeditor5/src/engine';
|
|
11
|
-
import CodeBlockCommand from './codeblockcommand';
|
|
12
|
-
import IndentCodeBlockCommand from './indentcodeblockcommand';
|
|
13
|
-
import OutdentCodeBlockCommand from './outdentcodeblockcommand';
|
|
14
|
-
import { getNormalizedAndLocalizedLanguageDefinitions, getLeadingWhiteSpaces, rawSnippetTextToViewDocumentFragment } from './utils';
|
|
15
|
-
import { modelToViewCodeBlockInsertion, modelToDataViewSoftBreakInsertion, dataViewToModelCodeBlockInsertion, dataViewToModelTextNewlinesInsertion, dataViewToModelOrphanNodeConsumer } from './converters';
|
|
16
|
-
const DEFAULT_ELEMENT = 'paragraph';
|
|
17
|
-
/**
|
|
18
|
-
* The editing part of the code block feature.
|
|
19
|
-
*
|
|
20
|
-
* Introduces the `'codeBlock'` command and the `'codeBlock'` model element.
|
|
21
|
-
*/
|
|
22
|
-
export default class CodeBlockEditing extends Plugin {
|
|
23
|
-
/**
|
|
24
|
-
* @inheritDoc
|
|
25
|
-
*/
|
|
26
|
-
static get pluginName() {
|
|
27
|
-
return 'CodeBlockEditing';
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* @inheritDoc
|
|
31
|
-
*/
|
|
32
|
-
static get requires() {
|
|
33
|
-
return [ShiftEnter];
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* @inheritDoc
|
|
37
|
-
*/
|
|
38
|
-
constructor(editor) {
|
|
39
|
-
super(editor);
|
|
40
|
-
editor.config.define('codeBlock', {
|
|
41
|
-
languages: [
|
|
42
|
-
{ language: 'plaintext', label: 'Plain text' },
|
|
43
|
-
{ language: 'c', label: 'C' },
|
|
44
|
-
{ language: 'cs', label: 'C#' },
|
|
45
|
-
{ language: 'cpp', label: 'C++' },
|
|
46
|
-
{ language: 'css', label: 'CSS' },
|
|
47
|
-
{ language: 'diff', label: 'Diff' },
|
|
48
|
-
{ language: 'html', label: 'HTML' },
|
|
49
|
-
{ language: 'java', label: 'Java' },
|
|
50
|
-
{ language: 'javascript', label: 'JavaScript' },
|
|
51
|
-
{ language: 'php', label: 'PHP' },
|
|
52
|
-
{ language: 'python', label: 'Python' },
|
|
53
|
-
{ language: 'ruby', label: 'Ruby' },
|
|
54
|
-
{ language: 'typescript', label: 'TypeScript' },
|
|
55
|
-
{ language: 'xml', label: 'XML' }
|
|
56
|
-
],
|
|
57
|
-
// A single tab.
|
|
58
|
-
indentSequence: '\t'
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* @inheritDoc
|
|
63
|
-
*/
|
|
64
|
-
init() {
|
|
65
|
-
const editor = this.editor;
|
|
66
|
-
const schema = editor.model.schema;
|
|
67
|
-
const model = editor.model;
|
|
68
|
-
const view = editor.editing.view;
|
|
69
|
-
const documentListEditing = editor.plugins.has('DocumentListEditing') ?
|
|
70
|
-
editor.plugins.get('DocumentListEditing') : null;
|
|
71
|
-
const normalizedLanguagesDefs = getNormalizedAndLocalizedLanguageDefinitions(editor);
|
|
72
|
-
// The main command.
|
|
73
|
-
editor.commands.add('codeBlock', new CodeBlockCommand(editor));
|
|
74
|
-
// Commands that change the indentation.
|
|
75
|
-
editor.commands.add('indentCodeBlock', new IndentCodeBlockCommand(editor));
|
|
76
|
-
editor.commands.add('outdentCodeBlock', new OutdentCodeBlockCommand(editor));
|
|
77
|
-
this.listenTo(view.document, 'tab', (evt, data) => {
|
|
78
|
-
const commandName = data.shiftKey ? 'outdentCodeBlock' : 'indentCodeBlock';
|
|
79
|
-
const command = editor.commands.get(commandName);
|
|
80
|
-
if (!command.isEnabled) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
editor.execute(commandName);
|
|
84
|
-
data.stopPropagation();
|
|
85
|
-
data.preventDefault();
|
|
86
|
-
evt.stop();
|
|
87
|
-
}, { context: 'pre' });
|
|
88
|
-
schema.register('codeBlock', {
|
|
89
|
-
allowWhere: '$block',
|
|
90
|
-
allowChildren: '$text',
|
|
91
|
-
isBlock: true,
|
|
92
|
-
allowAttributes: ['language']
|
|
93
|
-
});
|
|
94
|
-
// Allow all list* attributes on `codeBlock` (integration with DocumentList).
|
|
95
|
-
// Disallow all attributes on $text inside `codeBlock`.
|
|
96
|
-
schema.addAttributeCheck((context, attributeName) => {
|
|
97
|
-
if (context.endsWith('codeBlock') &&
|
|
98
|
-
documentListEditing && documentListEditing.getListAttributeNames().includes(attributeName)) {
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
if (context.endsWith('codeBlock $text')) {
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
// Disallow object elements inside `codeBlock`. See #9567.
|
|
106
|
-
editor.model.schema.addChildCheck((context, childDefinition) => {
|
|
107
|
-
if (context.endsWith('codeBlock') && childDefinition.isObject) {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
// Conversion.
|
|
112
|
-
editor.editing.downcastDispatcher.on('insert:codeBlock', modelToViewCodeBlockInsertion(model, normalizedLanguagesDefs, true));
|
|
113
|
-
editor.data.downcastDispatcher.on('insert:codeBlock', modelToViewCodeBlockInsertion(model, normalizedLanguagesDefs));
|
|
114
|
-
editor.data.downcastDispatcher.on('insert:softBreak', modelToDataViewSoftBreakInsertion(model), { priority: 'high' });
|
|
115
|
-
editor.data.upcastDispatcher.on('element:code', dataViewToModelCodeBlockInsertion(view, normalizedLanguagesDefs));
|
|
116
|
-
editor.data.upcastDispatcher.on('text', dataViewToModelTextNewlinesInsertion());
|
|
117
|
-
editor.data.upcastDispatcher.on('element:pre', dataViewToModelOrphanNodeConsumer(), { priority: 'high' });
|
|
118
|
-
// Intercept the clipboard input (paste) when the selection is anchored in the code block and force the clipboard
|
|
119
|
-
// data to be pasted as a single plain text. Otherwise, the code lines will split the code block and
|
|
120
|
-
// "spill out" as separate paragraphs.
|
|
121
|
-
this.listenTo(editor.editing.view.document, 'clipboardInput', (evt, data) => {
|
|
122
|
-
let insertionRange = model.createRange(model.document.selection.anchor);
|
|
123
|
-
// Use target ranges in case this is a drop.
|
|
124
|
-
if (data.targetRanges) {
|
|
125
|
-
insertionRange = editor.editing.mapper.toModelRange(data.targetRanges[0]);
|
|
126
|
-
}
|
|
127
|
-
if (!insertionRange.start.parent.is('element', 'codeBlock')) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
const text = data.dataTransfer.getData('text/plain');
|
|
131
|
-
const writer = new UpcastWriter(editor.editing.view.document);
|
|
132
|
-
// Pass the view fragment to the default clipboardInput handler.
|
|
133
|
-
data.content = rawSnippetTextToViewDocumentFragment(writer, text);
|
|
134
|
-
});
|
|
135
|
-
// Make sure multi–line selection is always wrapped in a code block when `getSelectedContent()`
|
|
136
|
-
// is used (e.g. clipboard copy). Otherwise, only the raw text will be copied to the clipboard and,
|
|
137
|
-
// upon next paste, this bare text will not be inserted as a code block, which is not the best UX.
|
|
138
|
-
// Similarly, when the selection in a single line, the selected content should be an inline code
|
|
139
|
-
// so it can be pasted later on and retain it's preformatted nature.
|
|
140
|
-
this.listenTo(model, 'getSelectedContent', (evt, [selection]) => {
|
|
141
|
-
const anchor = selection.anchor;
|
|
142
|
-
if (selection.isCollapsed || !anchor.parent.is('element', 'codeBlock') || !anchor.hasSameParentAs(selection.focus)) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
model.change(writer => {
|
|
146
|
-
const docFragment = evt.return;
|
|
147
|
-
// fo[o<softBreak></softBreak>b]ar -> <codeBlock language="...">[o<softBreak></softBreak>b]<codeBlock>
|
|
148
|
-
if (anchor.parent.is('element') &&
|
|
149
|
-
(docFragment.childCount > 1 || selection.containsEntireContent(anchor.parent))) {
|
|
150
|
-
const codeBlock = writer.createElement('codeBlock', anchor.parent.getAttributes());
|
|
151
|
-
writer.append(docFragment, codeBlock);
|
|
152
|
-
const newDocumentFragment = writer.createDocumentFragment();
|
|
153
|
-
writer.append(codeBlock, newDocumentFragment);
|
|
154
|
-
evt.return = newDocumentFragment;
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
// "f[oo]" -> <$text code="true">oo</text>
|
|
158
|
-
const textNode = docFragment.getChild(0);
|
|
159
|
-
if (schema.checkAttribute(textNode, 'code')) {
|
|
160
|
-
writer.setAttribute('code', true, textNode);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* @inheritDoc
|
|
167
|
-
*/
|
|
168
|
-
afterInit() {
|
|
169
|
-
const editor = this.editor;
|
|
170
|
-
const commands = editor.commands;
|
|
171
|
-
const indent = commands.get('indent');
|
|
172
|
-
const outdent = commands.get('outdent');
|
|
173
|
-
if (indent) {
|
|
174
|
-
// Priority is highest due to integration with `IndentList` command of `List` plugin.
|
|
175
|
-
// If selection is in a code block we give priority to it. This way list item cannot be indented
|
|
176
|
-
// but if we would give priority to indenting list item then user would have to indent list item
|
|
177
|
-
// as much as possible and only then he could indent code block.
|
|
178
|
-
indent.registerChildCommand(commands.get('indentCodeBlock'), { priority: 'highest' });
|
|
179
|
-
}
|
|
180
|
-
if (outdent) {
|
|
181
|
-
outdent.registerChildCommand(commands.get('outdentCodeBlock'));
|
|
182
|
-
}
|
|
183
|
-
// Customize the response to the <kbd>Enter</kbd> and <kbd>Shift</kbd>+<kbd>Enter</kbd>
|
|
184
|
-
// key press when the selection is in the code block. Upon enter key press we can either
|
|
185
|
-
// leave the block if it's "two or three enters" in a row or create a new code block line, preserving
|
|
186
|
-
// previous line's indentation.
|
|
187
|
-
this.listenTo(editor.editing.view.document, 'enter', (evt, data) => {
|
|
188
|
-
const positionParent = editor.model.document.selection.getLastPosition().parent;
|
|
189
|
-
if (!positionParent.is('element', 'codeBlock')) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
if (!leaveBlockStartOnEnter(editor, data.isSoft) && !leaveBlockEndOnEnter(editor, data.isSoft)) {
|
|
193
|
-
breakLineOnEnter(editor);
|
|
194
|
-
}
|
|
195
|
-
data.preventDefault();
|
|
196
|
-
evt.stop();
|
|
197
|
-
}, { context: 'pre' });
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Normally, when the Enter (or Shift+Enter) key is pressed, a soft line break is to be added to the
|
|
202
|
-
* code block. Let's try to follow the indentation of the previous line when possible, for instance:
|
|
203
|
-
*
|
|
204
|
-
* ```html
|
|
205
|
-
* // Before pressing enter (or shift enter)
|
|
206
|
-
* <codeBlock>
|
|
207
|
-
* " foo()"[] // Indent of 4 spaces.
|
|
208
|
-
* </codeBlock>
|
|
209
|
-
*
|
|
210
|
-
* // After pressing:
|
|
211
|
-
* <codeBlock>
|
|
212
|
-
* " foo()" // Indent of 4 spaces.
|
|
213
|
-
* <softBreak></softBreak> // A new soft break created by pressing enter.
|
|
214
|
-
* " "[] // Retain the indent of 4 spaces.
|
|
215
|
-
* </codeBlock>
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
|
-
function breakLineOnEnter(editor) {
|
|
219
|
-
const model = editor.model;
|
|
220
|
-
const modelDoc = model.document;
|
|
221
|
-
const lastSelectionPosition = modelDoc.selection.getLastPosition();
|
|
222
|
-
const node = lastSelectionPosition.nodeBefore || lastSelectionPosition.textNode;
|
|
223
|
-
let leadingWhiteSpaces;
|
|
224
|
-
// Figure out the indentation (white space chars) at the beginning of the line.
|
|
225
|
-
if (node && node.is('$text')) {
|
|
226
|
-
leadingWhiteSpaces = getLeadingWhiteSpaces(node);
|
|
227
|
-
}
|
|
228
|
-
// Keeping everything in a change block for a single undo step.
|
|
229
|
-
editor.model.change(writer => {
|
|
230
|
-
editor.execute('shiftEnter');
|
|
231
|
-
// If the line before being broken in two had some indentation, let's retain it
|
|
232
|
-
// in the new line.
|
|
233
|
-
if (leadingWhiteSpaces) {
|
|
234
|
-
writer.insertText(leadingWhiteSpaces, modelDoc.selection.anchor);
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Leave the code block when Enter (but NOT Shift+Enter) has been pressed twice at the beginning
|
|
240
|
-
* of the code block:
|
|
241
|
-
*
|
|
242
|
-
* ```html
|
|
243
|
-
* // Before:
|
|
244
|
-
* <codeBlock>[]<softBreak></softBreak>foo</codeBlock>
|
|
245
|
-
*
|
|
246
|
-
* // After pressing:
|
|
247
|
-
* <paragraph>[]</paragraph><codeBlock>foo</codeBlock>
|
|
248
|
-
* ```
|
|
249
|
-
*
|
|
250
|
-
* @param isSoftEnter When `true`, enter was pressed along with <kbd>Shift</kbd>.
|
|
251
|
-
* @returns `true` when selection left the block. `false` if stayed.
|
|
252
|
-
*/
|
|
253
|
-
function leaveBlockStartOnEnter(editor, isSoftEnter) {
|
|
254
|
-
const model = editor.model;
|
|
255
|
-
const modelDoc = model.document;
|
|
256
|
-
const view = editor.editing.view;
|
|
257
|
-
const lastSelectionPosition = modelDoc.selection.getLastPosition();
|
|
258
|
-
const nodeAfter = lastSelectionPosition.nodeAfter;
|
|
259
|
-
if (isSoftEnter || !modelDoc.selection.isCollapsed || !lastSelectionPosition.isAtStart) {
|
|
260
|
-
return false;
|
|
261
|
-
}
|
|
262
|
-
if (!isSoftBreakNode(nodeAfter)) {
|
|
263
|
-
return false;
|
|
264
|
-
}
|
|
265
|
-
// We're doing everything in a single change block to have a single undo step.
|
|
266
|
-
editor.model.change(writer => {
|
|
267
|
-
// "Clone" the <codeBlock> in the standard way.
|
|
268
|
-
editor.execute('enter');
|
|
269
|
-
// The cloned block exists now before the original code block.
|
|
270
|
-
const newBlock = modelDoc.selection.anchor.parent.previousSibling;
|
|
271
|
-
// Make the cloned <codeBlock> a regular <paragraph> (with clean attributes, so no language).
|
|
272
|
-
writer.rename(newBlock, DEFAULT_ELEMENT);
|
|
273
|
-
writer.setSelection(newBlock, 'in');
|
|
274
|
-
editor.model.schema.removeDisallowedAttributes([newBlock], writer);
|
|
275
|
-
// Remove the <softBreak> that originally followed the selection position.
|
|
276
|
-
writer.remove(nodeAfter);
|
|
277
|
-
});
|
|
278
|
-
// Eye candy.
|
|
279
|
-
view.scrollToTheSelection();
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Leave the code block when Enter (but NOT Shift+Enter) has been pressed twice at the end
|
|
284
|
-
* of the code block:
|
|
285
|
-
*
|
|
286
|
-
* ```html
|
|
287
|
-
* // Before:
|
|
288
|
-
* <codeBlock>foo[]</codeBlock>
|
|
289
|
-
*
|
|
290
|
-
* // After first press:
|
|
291
|
-
* <codeBlock>foo<softBreak></softBreak>[]</codeBlock>
|
|
292
|
-
*
|
|
293
|
-
* // After second press:
|
|
294
|
-
* <codeBlock>foo</codeBlock><paragraph>[]</paragraph>
|
|
295
|
-
* ```
|
|
296
|
-
*
|
|
297
|
-
* @param isSoftEnter When `true`, enter was pressed along with <kbd>Shift</kbd>.
|
|
298
|
-
* @returns `true` when selection left the block. `false` if stayed.
|
|
299
|
-
*/
|
|
300
|
-
function leaveBlockEndOnEnter(editor, isSoftEnter) {
|
|
301
|
-
const model = editor.model;
|
|
302
|
-
const modelDoc = model.document;
|
|
303
|
-
const view = editor.editing.view;
|
|
304
|
-
const lastSelectionPosition = modelDoc.selection.getLastPosition();
|
|
305
|
-
const nodeBefore = lastSelectionPosition.nodeBefore;
|
|
306
|
-
let emptyLineRangeToRemoveOnEnter;
|
|
307
|
-
if (isSoftEnter || !modelDoc.selection.isCollapsed || !lastSelectionPosition.isAtEnd || !nodeBefore || !nodeBefore.previousSibling) {
|
|
308
|
-
return false;
|
|
309
|
-
}
|
|
310
|
-
// When the position is directly preceded by two soft breaks
|
|
311
|
-
//
|
|
312
|
-
// <codeBlock>foo<softBreak></softBreak><softBreak></softBreak>[]</codeBlock>
|
|
313
|
-
//
|
|
314
|
-
// it creates the following range that will be cleaned up before leaving:
|
|
315
|
-
//
|
|
316
|
-
// <codeBlock>foo[<softBreak></softBreak><softBreak></softBreak>]</codeBlock>
|
|
317
|
-
//
|
|
318
|
-
if (isSoftBreakNode(nodeBefore) && isSoftBreakNode(nodeBefore.previousSibling)) {
|
|
319
|
-
emptyLineRangeToRemoveOnEnter = model.createRange(model.createPositionBefore(nodeBefore.previousSibling), model.createPositionAfter(nodeBefore));
|
|
320
|
-
}
|
|
321
|
-
// When there's some text before the position that is
|
|
322
|
-
// preceded by two soft breaks and made purely of white–space characters
|
|
323
|
-
//
|
|
324
|
-
// <codeBlock>foo<softBreak></softBreak><softBreak></softBreak> []</codeBlock>
|
|
325
|
-
//
|
|
326
|
-
// it creates the following range to clean up before leaving:
|
|
327
|
-
//
|
|
328
|
-
// <codeBlock>foo[<softBreak></softBreak><softBreak></softBreak> ]</codeBlock>
|
|
329
|
-
//
|
|
330
|
-
else if (isEmptyishTextNode(nodeBefore) &&
|
|
331
|
-
isSoftBreakNode(nodeBefore.previousSibling) &&
|
|
332
|
-
isSoftBreakNode(nodeBefore.previousSibling.previousSibling)) {
|
|
333
|
-
emptyLineRangeToRemoveOnEnter = model.createRange(model.createPositionBefore(nodeBefore.previousSibling.previousSibling), model.createPositionAfter(nodeBefore));
|
|
334
|
-
}
|
|
335
|
-
// When there's some text before the position that is made purely of white–space characters
|
|
336
|
-
// and is preceded by some other text made purely of white–space characters
|
|
337
|
-
//
|
|
338
|
-
// <codeBlock>foo<softBreak></softBreak> <softBreak></softBreak> []</codeBlock>
|
|
339
|
-
//
|
|
340
|
-
// it creates the following range to clean up before leaving:
|
|
341
|
-
//
|
|
342
|
-
// <codeBlock>foo[<softBreak></softBreak> <softBreak></softBreak> ]</codeBlock>
|
|
343
|
-
//
|
|
344
|
-
else if (isEmptyishTextNode(nodeBefore) &&
|
|
345
|
-
isSoftBreakNode(nodeBefore.previousSibling) &&
|
|
346
|
-
isEmptyishTextNode(nodeBefore.previousSibling.previousSibling) &&
|
|
347
|
-
nodeBefore.previousSibling.previousSibling &&
|
|
348
|
-
isSoftBreakNode(nodeBefore.previousSibling.previousSibling.previousSibling)) {
|
|
349
|
-
emptyLineRangeToRemoveOnEnter = model.createRange(model.createPositionBefore(nodeBefore.previousSibling.previousSibling.previousSibling), model.createPositionAfter(nodeBefore));
|
|
350
|
-
}
|
|
351
|
-
// Not leaving the block in the following cases:
|
|
352
|
-
//
|
|
353
|
-
// <codeBlock> []</codeBlock>
|
|
354
|
-
// <codeBlock> a []</codeBlock>
|
|
355
|
-
// <codeBlock>foo<softBreak></softBreak>[]</codeBlock>
|
|
356
|
-
// <codeBlock>foo<softBreak></softBreak><softBreak></softBreak>bar[]</codeBlock>
|
|
357
|
-
// <codeBlock>foo<softBreak></softBreak><softBreak></softBreak> a []</codeBlock>
|
|
358
|
-
//
|
|
359
|
-
else {
|
|
360
|
-
return false;
|
|
361
|
-
}
|
|
362
|
-
// We're doing everything in a single change block to have a single undo step.
|
|
363
|
-
editor.model.change(writer => {
|
|
364
|
-
// Remove the last <softBreak>s and all white space characters that followed them.
|
|
365
|
-
writer.remove(emptyLineRangeToRemoveOnEnter);
|
|
366
|
-
// "Clone" the <codeBlock> in the standard way.
|
|
367
|
-
editor.execute('enter');
|
|
368
|
-
const newBlock = modelDoc.selection.anchor.parent;
|
|
369
|
-
// Make the cloned <codeBlock> a regular <paragraph> (with clean attributes, so no language).
|
|
370
|
-
writer.rename(newBlock, DEFAULT_ELEMENT);
|
|
371
|
-
editor.model.schema.removeDisallowedAttributes([newBlock], writer);
|
|
372
|
-
});
|
|
373
|
-
// Eye candy.
|
|
374
|
-
view.scrollToTheSelection();
|
|
375
|
-
return true;
|
|
376
|
-
}
|
|
377
|
-
function isEmptyishTextNode(node) {
|
|
378
|
-
return node && node.is('$text') && !node.data.match(/\S/);
|
|
379
|
-
}
|
|
380
|
-
function isSoftBreakNode(node) {
|
|
381
|
-
return node && node.is('element', 'softBreak');
|
|
382
|
-
}
|
|
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/codeblockediting
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from 'ckeditor5/src/core';
|
|
9
|
+
import { ShiftEnter } from 'ckeditor5/src/enter';
|
|
10
|
+
import { UpcastWriter } from 'ckeditor5/src/engine';
|
|
11
|
+
import CodeBlockCommand from './codeblockcommand';
|
|
12
|
+
import IndentCodeBlockCommand from './indentcodeblockcommand';
|
|
13
|
+
import OutdentCodeBlockCommand from './outdentcodeblockcommand';
|
|
14
|
+
import { getNormalizedAndLocalizedLanguageDefinitions, getLeadingWhiteSpaces, rawSnippetTextToViewDocumentFragment } from './utils';
|
|
15
|
+
import { modelToViewCodeBlockInsertion, modelToDataViewSoftBreakInsertion, dataViewToModelCodeBlockInsertion, dataViewToModelTextNewlinesInsertion, dataViewToModelOrphanNodeConsumer } from './converters';
|
|
16
|
+
const DEFAULT_ELEMENT = 'paragraph';
|
|
17
|
+
/**
|
|
18
|
+
* The editing part of the code block feature.
|
|
19
|
+
*
|
|
20
|
+
* Introduces the `'codeBlock'` command and the `'codeBlock'` model element.
|
|
21
|
+
*/
|
|
22
|
+
export default class CodeBlockEditing extends Plugin {
|
|
23
|
+
/**
|
|
24
|
+
* @inheritDoc
|
|
25
|
+
*/
|
|
26
|
+
static get pluginName() {
|
|
27
|
+
return 'CodeBlockEditing';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get requires() {
|
|
33
|
+
return [ShiftEnter];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @inheritDoc
|
|
37
|
+
*/
|
|
38
|
+
constructor(editor) {
|
|
39
|
+
super(editor);
|
|
40
|
+
editor.config.define('codeBlock', {
|
|
41
|
+
languages: [
|
|
42
|
+
{ language: 'plaintext', label: 'Plain text' },
|
|
43
|
+
{ language: 'c', label: 'C' },
|
|
44
|
+
{ language: 'cs', label: 'C#' },
|
|
45
|
+
{ language: 'cpp', label: 'C++' },
|
|
46
|
+
{ language: 'css', label: 'CSS' },
|
|
47
|
+
{ language: 'diff', label: 'Diff' },
|
|
48
|
+
{ language: 'html', label: 'HTML' },
|
|
49
|
+
{ language: 'java', label: 'Java' },
|
|
50
|
+
{ language: 'javascript', label: 'JavaScript' },
|
|
51
|
+
{ language: 'php', label: 'PHP' },
|
|
52
|
+
{ language: 'python', label: 'Python' },
|
|
53
|
+
{ language: 'ruby', label: 'Ruby' },
|
|
54
|
+
{ language: 'typescript', label: 'TypeScript' },
|
|
55
|
+
{ language: 'xml', label: 'XML' }
|
|
56
|
+
],
|
|
57
|
+
// A single tab.
|
|
58
|
+
indentSequence: '\t'
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @inheritDoc
|
|
63
|
+
*/
|
|
64
|
+
init() {
|
|
65
|
+
const editor = this.editor;
|
|
66
|
+
const schema = editor.model.schema;
|
|
67
|
+
const model = editor.model;
|
|
68
|
+
const view = editor.editing.view;
|
|
69
|
+
const documentListEditing = editor.plugins.has('DocumentListEditing') ?
|
|
70
|
+
editor.plugins.get('DocumentListEditing') : null;
|
|
71
|
+
const normalizedLanguagesDefs = getNormalizedAndLocalizedLanguageDefinitions(editor);
|
|
72
|
+
// The main command.
|
|
73
|
+
editor.commands.add('codeBlock', new CodeBlockCommand(editor));
|
|
74
|
+
// Commands that change the indentation.
|
|
75
|
+
editor.commands.add('indentCodeBlock', new IndentCodeBlockCommand(editor));
|
|
76
|
+
editor.commands.add('outdentCodeBlock', new OutdentCodeBlockCommand(editor));
|
|
77
|
+
this.listenTo(view.document, 'tab', (evt, data) => {
|
|
78
|
+
const commandName = data.shiftKey ? 'outdentCodeBlock' : 'indentCodeBlock';
|
|
79
|
+
const command = editor.commands.get(commandName);
|
|
80
|
+
if (!command.isEnabled) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
editor.execute(commandName);
|
|
84
|
+
data.stopPropagation();
|
|
85
|
+
data.preventDefault();
|
|
86
|
+
evt.stop();
|
|
87
|
+
}, { context: 'pre' });
|
|
88
|
+
schema.register('codeBlock', {
|
|
89
|
+
allowWhere: '$block',
|
|
90
|
+
allowChildren: '$text',
|
|
91
|
+
isBlock: true,
|
|
92
|
+
allowAttributes: ['language']
|
|
93
|
+
});
|
|
94
|
+
// Allow all list* attributes on `codeBlock` (integration with DocumentList).
|
|
95
|
+
// Disallow all attributes on $text inside `codeBlock`.
|
|
96
|
+
schema.addAttributeCheck((context, attributeName) => {
|
|
97
|
+
if (context.endsWith('codeBlock') &&
|
|
98
|
+
documentListEditing && documentListEditing.getListAttributeNames().includes(attributeName)) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
if (context.endsWith('codeBlock $text')) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
// Disallow object elements inside `codeBlock`. See #9567.
|
|
106
|
+
editor.model.schema.addChildCheck((context, childDefinition) => {
|
|
107
|
+
if (context.endsWith('codeBlock') && childDefinition.isObject) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
// Conversion.
|
|
112
|
+
editor.editing.downcastDispatcher.on('insert:codeBlock', modelToViewCodeBlockInsertion(model, normalizedLanguagesDefs, true));
|
|
113
|
+
editor.data.downcastDispatcher.on('insert:codeBlock', modelToViewCodeBlockInsertion(model, normalizedLanguagesDefs));
|
|
114
|
+
editor.data.downcastDispatcher.on('insert:softBreak', modelToDataViewSoftBreakInsertion(model), { priority: 'high' });
|
|
115
|
+
editor.data.upcastDispatcher.on('element:code', dataViewToModelCodeBlockInsertion(view, normalizedLanguagesDefs));
|
|
116
|
+
editor.data.upcastDispatcher.on('text', dataViewToModelTextNewlinesInsertion());
|
|
117
|
+
editor.data.upcastDispatcher.on('element:pre', dataViewToModelOrphanNodeConsumer(), { priority: 'high' });
|
|
118
|
+
// Intercept the clipboard input (paste) when the selection is anchored in the code block and force the clipboard
|
|
119
|
+
// data to be pasted as a single plain text. Otherwise, the code lines will split the code block and
|
|
120
|
+
// "spill out" as separate paragraphs.
|
|
121
|
+
this.listenTo(editor.editing.view.document, 'clipboardInput', (evt, data) => {
|
|
122
|
+
let insertionRange = model.createRange(model.document.selection.anchor);
|
|
123
|
+
// Use target ranges in case this is a drop.
|
|
124
|
+
if (data.targetRanges) {
|
|
125
|
+
insertionRange = editor.editing.mapper.toModelRange(data.targetRanges[0]);
|
|
126
|
+
}
|
|
127
|
+
if (!insertionRange.start.parent.is('element', 'codeBlock')) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const text = data.dataTransfer.getData('text/plain');
|
|
131
|
+
const writer = new UpcastWriter(editor.editing.view.document);
|
|
132
|
+
// Pass the view fragment to the default clipboardInput handler.
|
|
133
|
+
data.content = rawSnippetTextToViewDocumentFragment(writer, text);
|
|
134
|
+
});
|
|
135
|
+
// Make sure multi–line selection is always wrapped in a code block when `getSelectedContent()`
|
|
136
|
+
// is used (e.g. clipboard copy). Otherwise, only the raw text will be copied to the clipboard and,
|
|
137
|
+
// upon next paste, this bare text will not be inserted as a code block, which is not the best UX.
|
|
138
|
+
// Similarly, when the selection in a single line, the selected content should be an inline code
|
|
139
|
+
// so it can be pasted later on and retain it's preformatted nature.
|
|
140
|
+
this.listenTo(model, 'getSelectedContent', (evt, [selection]) => {
|
|
141
|
+
const anchor = selection.anchor;
|
|
142
|
+
if (selection.isCollapsed || !anchor.parent.is('element', 'codeBlock') || !anchor.hasSameParentAs(selection.focus)) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
model.change(writer => {
|
|
146
|
+
const docFragment = evt.return;
|
|
147
|
+
// fo[o<softBreak></softBreak>b]ar -> <codeBlock language="...">[o<softBreak></softBreak>b]<codeBlock>
|
|
148
|
+
if (anchor.parent.is('element') &&
|
|
149
|
+
(docFragment.childCount > 1 || selection.containsEntireContent(anchor.parent))) {
|
|
150
|
+
const codeBlock = writer.createElement('codeBlock', anchor.parent.getAttributes());
|
|
151
|
+
writer.append(docFragment, codeBlock);
|
|
152
|
+
const newDocumentFragment = writer.createDocumentFragment();
|
|
153
|
+
writer.append(codeBlock, newDocumentFragment);
|
|
154
|
+
evt.return = newDocumentFragment;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
// "f[oo]" -> <$text code="true">oo</text>
|
|
158
|
+
const textNode = docFragment.getChild(0);
|
|
159
|
+
if (schema.checkAttribute(textNode, 'code')) {
|
|
160
|
+
writer.setAttribute('code', true, textNode);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* @inheritDoc
|
|
167
|
+
*/
|
|
168
|
+
afterInit() {
|
|
169
|
+
const editor = this.editor;
|
|
170
|
+
const commands = editor.commands;
|
|
171
|
+
const indent = commands.get('indent');
|
|
172
|
+
const outdent = commands.get('outdent');
|
|
173
|
+
if (indent) {
|
|
174
|
+
// Priority is highest due to integration with `IndentList` command of `List` plugin.
|
|
175
|
+
// If selection is in a code block we give priority to it. This way list item cannot be indented
|
|
176
|
+
// but if we would give priority to indenting list item then user would have to indent list item
|
|
177
|
+
// as much as possible and only then he could indent code block.
|
|
178
|
+
indent.registerChildCommand(commands.get('indentCodeBlock'), { priority: 'highest' });
|
|
179
|
+
}
|
|
180
|
+
if (outdent) {
|
|
181
|
+
outdent.registerChildCommand(commands.get('outdentCodeBlock'));
|
|
182
|
+
}
|
|
183
|
+
// Customize the response to the <kbd>Enter</kbd> and <kbd>Shift</kbd>+<kbd>Enter</kbd>
|
|
184
|
+
// key press when the selection is in the code block. Upon enter key press we can either
|
|
185
|
+
// leave the block if it's "two or three enters" in a row or create a new code block line, preserving
|
|
186
|
+
// previous line's indentation.
|
|
187
|
+
this.listenTo(editor.editing.view.document, 'enter', (evt, data) => {
|
|
188
|
+
const positionParent = editor.model.document.selection.getLastPosition().parent;
|
|
189
|
+
if (!positionParent.is('element', 'codeBlock')) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (!leaveBlockStartOnEnter(editor, data.isSoft) && !leaveBlockEndOnEnter(editor, data.isSoft)) {
|
|
193
|
+
breakLineOnEnter(editor);
|
|
194
|
+
}
|
|
195
|
+
data.preventDefault();
|
|
196
|
+
evt.stop();
|
|
197
|
+
}, { context: 'pre' });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Normally, when the Enter (or Shift+Enter) key is pressed, a soft line break is to be added to the
|
|
202
|
+
* code block. Let's try to follow the indentation of the previous line when possible, for instance:
|
|
203
|
+
*
|
|
204
|
+
* ```html
|
|
205
|
+
* // Before pressing enter (or shift enter)
|
|
206
|
+
* <codeBlock>
|
|
207
|
+
* " foo()"[] // Indent of 4 spaces.
|
|
208
|
+
* </codeBlock>
|
|
209
|
+
*
|
|
210
|
+
* // After pressing:
|
|
211
|
+
* <codeBlock>
|
|
212
|
+
* " foo()" // Indent of 4 spaces.
|
|
213
|
+
* <softBreak></softBreak> // A new soft break created by pressing enter.
|
|
214
|
+
* " "[] // Retain the indent of 4 spaces.
|
|
215
|
+
* </codeBlock>
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
function breakLineOnEnter(editor) {
|
|
219
|
+
const model = editor.model;
|
|
220
|
+
const modelDoc = model.document;
|
|
221
|
+
const lastSelectionPosition = modelDoc.selection.getLastPosition();
|
|
222
|
+
const node = lastSelectionPosition.nodeBefore || lastSelectionPosition.textNode;
|
|
223
|
+
let leadingWhiteSpaces;
|
|
224
|
+
// Figure out the indentation (white space chars) at the beginning of the line.
|
|
225
|
+
if (node && node.is('$text')) {
|
|
226
|
+
leadingWhiteSpaces = getLeadingWhiteSpaces(node);
|
|
227
|
+
}
|
|
228
|
+
// Keeping everything in a change block for a single undo step.
|
|
229
|
+
editor.model.change(writer => {
|
|
230
|
+
editor.execute('shiftEnter');
|
|
231
|
+
// If the line before being broken in two had some indentation, let's retain it
|
|
232
|
+
// in the new line.
|
|
233
|
+
if (leadingWhiteSpaces) {
|
|
234
|
+
writer.insertText(leadingWhiteSpaces, modelDoc.selection.anchor);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Leave the code block when Enter (but NOT Shift+Enter) has been pressed twice at the beginning
|
|
240
|
+
* of the code block:
|
|
241
|
+
*
|
|
242
|
+
* ```html
|
|
243
|
+
* // Before:
|
|
244
|
+
* <codeBlock>[]<softBreak></softBreak>foo</codeBlock>
|
|
245
|
+
*
|
|
246
|
+
* // After pressing:
|
|
247
|
+
* <paragraph>[]</paragraph><codeBlock>foo</codeBlock>
|
|
248
|
+
* ```
|
|
249
|
+
*
|
|
250
|
+
* @param isSoftEnter When `true`, enter was pressed along with <kbd>Shift</kbd>.
|
|
251
|
+
* @returns `true` when selection left the block. `false` if stayed.
|
|
252
|
+
*/
|
|
253
|
+
function leaveBlockStartOnEnter(editor, isSoftEnter) {
|
|
254
|
+
const model = editor.model;
|
|
255
|
+
const modelDoc = model.document;
|
|
256
|
+
const view = editor.editing.view;
|
|
257
|
+
const lastSelectionPosition = modelDoc.selection.getLastPosition();
|
|
258
|
+
const nodeAfter = lastSelectionPosition.nodeAfter;
|
|
259
|
+
if (isSoftEnter || !modelDoc.selection.isCollapsed || !lastSelectionPosition.isAtStart) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
if (!isSoftBreakNode(nodeAfter)) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
// We're doing everything in a single change block to have a single undo step.
|
|
266
|
+
editor.model.change(writer => {
|
|
267
|
+
// "Clone" the <codeBlock> in the standard way.
|
|
268
|
+
editor.execute('enter');
|
|
269
|
+
// The cloned block exists now before the original code block.
|
|
270
|
+
const newBlock = modelDoc.selection.anchor.parent.previousSibling;
|
|
271
|
+
// Make the cloned <codeBlock> a regular <paragraph> (with clean attributes, so no language).
|
|
272
|
+
writer.rename(newBlock, DEFAULT_ELEMENT);
|
|
273
|
+
writer.setSelection(newBlock, 'in');
|
|
274
|
+
editor.model.schema.removeDisallowedAttributes([newBlock], writer);
|
|
275
|
+
// Remove the <softBreak> that originally followed the selection position.
|
|
276
|
+
writer.remove(nodeAfter);
|
|
277
|
+
});
|
|
278
|
+
// Eye candy.
|
|
279
|
+
view.scrollToTheSelection();
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Leave the code block when Enter (but NOT Shift+Enter) has been pressed twice at the end
|
|
284
|
+
* of the code block:
|
|
285
|
+
*
|
|
286
|
+
* ```html
|
|
287
|
+
* // Before:
|
|
288
|
+
* <codeBlock>foo[]</codeBlock>
|
|
289
|
+
*
|
|
290
|
+
* // After first press:
|
|
291
|
+
* <codeBlock>foo<softBreak></softBreak>[]</codeBlock>
|
|
292
|
+
*
|
|
293
|
+
* // After second press:
|
|
294
|
+
* <codeBlock>foo</codeBlock><paragraph>[]</paragraph>
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
* @param isSoftEnter When `true`, enter was pressed along with <kbd>Shift</kbd>.
|
|
298
|
+
* @returns `true` when selection left the block. `false` if stayed.
|
|
299
|
+
*/
|
|
300
|
+
function leaveBlockEndOnEnter(editor, isSoftEnter) {
|
|
301
|
+
const model = editor.model;
|
|
302
|
+
const modelDoc = model.document;
|
|
303
|
+
const view = editor.editing.view;
|
|
304
|
+
const lastSelectionPosition = modelDoc.selection.getLastPosition();
|
|
305
|
+
const nodeBefore = lastSelectionPosition.nodeBefore;
|
|
306
|
+
let emptyLineRangeToRemoveOnEnter;
|
|
307
|
+
if (isSoftEnter || !modelDoc.selection.isCollapsed || !lastSelectionPosition.isAtEnd || !nodeBefore || !nodeBefore.previousSibling) {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
// When the position is directly preceded by two soft breaks
|
|
311
|
+
//
|
|
312
|
+
// <codeBlock>foo<softBreak></softBreak><softBreak></softBreak>[]</codeBlock>
|
|
313
|
+
//
|
|
314
|
+
// it creates the following range that will be cleaned up before leaving:
|
|
315
|
+
//
|
|
316
|
+
// <codeBlock>foo[<softBreak></softBreak><softBreak></softBreak>]</codeBlock>
|
|
317
|
+
//
|
|
318
|
+
if (isSoftBreakNode(nodeBefore) && isSoftBreakNode(nodeBefore.previousSibling)) {
|
|
319
|
+
emptyLineRangeToRemoveOnEnter = model.createRange(model.createPositionBefore(nodeBefore.previousSibling), model.createPositionAfter(nodeBefore));
|
|
320
|
+
}
|
|
321
|
+
// When there's some text before the position that is
|
|
322
|
+
// preceded by two soft breaks and made purely of white–space characters
|
|
323
|
+
//
|
|
324
|
+
// <codeBlock>foo<softBreak></softBreak><softBreak></softBreak> []</codeBlock>
|
|
325
|
+
//
|
|
326
|
+
// it creates the following range to clean up before leaving:
|
|
327
|
+
//
|
|
328
|
+
// <codeBlock>foo[<softBreak></softBreak><softBreak></softBreak> ]</codeBlock>
|
|
329
|
+
//
|
|
330
|
+
else if (isEmptyishTextNode(nodeBefore) &&
|
|
331
|
+
isSoftBreakNode(nodeBefore.previousSibling) &&
|
|
332
|
+
isSoftBreakNode(nodeBefore.previousSibling.previousSibling)) {
|
|
333
|
+
emptyLineRangeToRemoveOnEnter = model.createRange(model.createPositionBefore(nodeBefore.previousSibling.previousSibling), model.createPositionAfter(nodeBefore));
|
|
334
|
+
}
|
|
335
|
+
// When there's some text before the position that is made purely of white–space characters
|
|
336
|
+
// and is preceded by some other text made purely of white–space characters
|
|
337
|
+
//
|
|
338
|
+
// <codeBlock>foo<softBreak></softBreak> <softBreak></softBreak> []</codeBlock>
|
|
339
|
+
//
|
|
340
|
+
// it creates the following range to clean up before leaving:
|
|
341
|
+
//
|
|
342
|
+
// <codeBlock>foo[<softBreak></softBreak> <softBreak></softBreak> ]</codeBlock>
|
|
343
|
+
//
|
|
344
|
+
else if (isEmptyishTextNode(nodeBefore) &&
|
|
345
|
+
isSoftBreakNode(nodeBefore.previousSibling) &&
|
|
346
|
+
isEmptyishTextNode(nodeBefore.previousSibling.previousSibling) &&
|
|
347
|
+
nodeBefore.previousSibling.previousSibling &&
|
|
348
|
+
isSoftBreakNode(nodeBefore.previousSibling.previousSibling.previousSibling)) {
|
|
349
|
+
emptyLineRangeToRemoveOnEnter = model.createRange(model.createPositionBefore(nodeBefore.previousSibling.previousSibling.previousSibling), model.createPositionAfter(nodeBefore));
|
|
350
|
+
}
|
|
351
|
+
// Not leaving the block in the following cases:
|
|
352
|
+
//
|
|
353
|
+
// <codeBlock> []</codeBlock>
|
|
354
|
+
// <codeBlock> a []</codeBlock>
|
|
355
|
+
// <codeBlock>foo<softBreak></softBreak>[]</codeBlock>
|
|
356
|
+
// <codeBlock>foo<softBreak></softBreak><softBreak></softBreak>bar[]</codeBlock>
|
|
357
|
+
// <codeBlock>foo<softBreak></softBreak><softBreak></softBreak> a []</codeBlock>
|
|
358
|
+
//
|
|
359
|
+
else {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
// We're doing everything in a single change block to have a single undo step.
|
|
363
|
+
editor.model.change(writer => {
|
|
364
|
+
// Remove the last <softBreak>s and all white space characters that followed them.
|
|
365
|
+
writer.remove(emptyLineRangeToRemoveOnEnter);
|
|
366
|
+
// "Clone" the <codeBlock> in the standard way.
|
|
367
|
+
editor.execute('enter');
|
|
368
|
+
const newBlock = modelDoc.selection.anchor.parent;
|
|
369
|
+
// Make the cloned <codeBlock> a regular <paragraph> (with clean attributes, so no language).
|
|
370
|
+
writer.rename(newBlock, DEFAULT_ELEMENT);
|
|
371
|
+
editor.model.schema.removeDisallowedAttributes([newBlock], writer);
|
|
372
|
+
});
|
|
373
|
+
// Eye candy.
|
|
374
|
+
view.scrollToTheSelection();
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
function isEmptyishTextNode(node) {
|
|
378
|
+
return node && node.is('$text') && !node.data.match(/\S/);
|
|
379
|
+
}
|
|
380
|
+
function isSoftBreakNode(node) {
|
|
381
|
+
return node && node.is('element', 'softBreak');
|
|
382
|
+
}
|