@ckeditor/ckeditor5-code-block 0.0.0-nightly-20240509.0 → 0.0.0-nightly-20240510.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/dist/index.js CHANGED
@@ -224,26 +224,37 @@ import { createDropdown, SplitButtonView, addListToDropdown, MenuBarMenuView, Me
224
224
  return t('Leaving code snippet');
225
225
  }
226
226
 
227
- class CodeBlockCommand extends Command {
227
+ /**
228
+ * The code block command plugin.
229
+ */ class CodeBlockCommand extends Command {
230
+ /**
231
+ * Contains the last used language.
232
+ */ _lastLanguage;
233
+ /**
234
+ * @inheritDoc
235
+ */ constructor(editor){
236
+ super(editor);
237
+ this._lastLanguage = null;
238
+ }
228
239
  /**
229
- * @inheritDoc
230
- */ refresh() {
240
+ * @inheritDoc
241
+ */ refresh() {
231
242
  this.value = this._getValue();
232
243
  this.isEnabled = this._checkEnabled();
233
244
  }
234
245
  /**
235
- * Executes the command. When the command {@link #value is on}, all topmost code blocks within
236
- * the selection will be removed. If it is off, all selected blocks will be flattened and
237
- * wrapped by a code block.
238
- *
239
- * @fires execute
240
- * @param options Command options.
241
- * @param options.language The code block language.
242
- * @param options.forceValue If set, it will force the command behavior. If `true`, the command will apply a code block,
243
- * otherwise the command will remove the code block. If not set, the command will act basing on its current value.
244
- * @param options.usePreviousLanguageChoice If set on `true` and the `options.language` is not specified, the command
245
- * will apply the previous language (if the command was already executed) when inserting the `codeBlock` element.
246
- */ execute(options = {}) {
246
+ * Executes the command. When the command {@link #value is on}, all topmost code blocks within
247
+ * the selection will be removed. If it is off, all selected blocks will be flattened and
248
+ * wrapped by a code block.
249
+ *
250
+ * @fires execute
251
+ * @param options Command options.
252
+ * @param options.language The code block language.
253
+ * @param options.forceValue If set, it will force the command behavior. If `true`, the command will apply a code block,
254
+ * otherwise the command will remove the code block. If not set, the command will act basing on its current value.
255
+ * @param options.usePreviousLanguageChoice If set on `true` and the `options.language` is not specified, the command
256
+ * will apply the previous language (if the command was already executed) when inserting the `codeBlock` element.
257
+ */ execute(options = {}) {
247
258
  const editor = this.editor;
248
259
  const model = editor.model;
249
260
  const selection = model.document.selection;
@@ -261,20 +272,20 @@ class CodeBlockCommand extends Command {
261
272
  });
262
273
  }
263
274
  /**
264
- * Checks the command's {@link #value}.
265
- *
266
- * @returns The current value.
267
- */ _getValue() {
275
+ * Checks the command's {@link #value}.
276
+ *
277
+ * @returns The current value.
278
+ */ _getValue() {
268
279
  const selection = this.editor.model.document.selection;
269
280
  const firstBlock = first(selection.getSelectedBlocks());
270
281
  const isCodeBlock = !!(firstBlock && firstBlock.is('element', 'codeBlock'));
271
282
  return isCodeBlock ? firstBlock.getAttribute('language') : false;
272
283
  }
273
284
  /**
274
- * Checks whether the command can be enabled in the current context.
275
- *
276
- * @returns Whether the command should be enabled.
277
- */ _checkEnabled() {
285
+ * Checks whether the command can be enabled in the current context.
286
+ *
287
+ * @returns Whether the command should be enabled.
288
+ */ _checkEnabled() {
278
289
  if (this.value) {
279
290
  return true;
280
291
  }
@@ -324,12 +335,6 @@ class CodeBlockCommand extends Command {
324
335
  writer.removeAttribute('language', block);
325
336
  }
326
337
  }
327
- /**
328
- * @inheritDoc
329
- */ constructor(editor){
330
- super(editor);
331
- this._lastLanguage = null;
332
- }
333
338
  }
334
339
  /**
335
340
  * Picks the language for the new code block. If any language is passed as an option,
@@ -346,18 +351,27 @@ class CodeBlockCommand extends Command {
346
351
  return defaultLanguage;
347
352
  }
348
353
 
349
- class IndentCodeBlockCommand extends Command {
354
+ /**
355
+ * The code block indentation increase command plugin.
356
+ */ class IndentCodeBlockCommand extends Command {
350
357
  /**
351
- * @inheritDoc
352
- */ refresh() {
358
+ * A sequence of characters added to the line when the command is executed.
359
+ */ _indentSequence;
360
+ constructor(editor){
361
+ super(editor);
362
+ this._indentSequence = editor.config.get('codeBlock.indentSequence');
363
+ }
364
+ /**
365
+ * @inheritDoc
366
+ */ refresh() {
353
367
  this.isEnabled = this._checkEnabled();
354
368
  }
355
369
  /**
356
- * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
357
- * code lines in the selection will be increased.
358
- *
359
- * @fires execute
360
- */ execute() {
370
+ * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
371
+ * code lines in the selection will be increased.
372
+ *
373
+ * @fires execute
374
+ */ execute() {
361
375
  const editor = this.editor;
362
376
  const model = editor.model;
363
377
  model.change((writer)=>{
@@ -395,8 +409,8 @@ class IndentCodeBlockCommand extends Command {
395
409
  });
396
410
  }
397
411
  /**
398
- * Checks whether the command can be enabled in the current context.
399
- */ _checkEnabled() {
412
+ * Checks whether the command can be enabled in the current context.
413
+ */ _checkEnabled() {
400
414
  if (!this._indentSequence) {
401
415
  return false;
402
416
  }
@@ -404,24 +418,29 @@ class IndentCodeBlockCommand extends Command {
404
418
  // because you can always indent code lines.
405
419
  return isModelSelectionInCodeBlock(this.editor.model.document.selection);
406
420
  }
421
+ }
422
+
423
+ /**
424
+ * The code block indentation decrease command plugin.
425
+ */ class OutdentCodeBlockCommand extends Command {
426
+ /**
427
+ * A sequence of characters removed from the line when the command is executed.
428
+ */ _indentSequence;
407
429
  constructor(editor){
408
430
  super(editor);
409
431
  this._indentSequence = editor.config.get('codeBlock.indentSequence');
410
432
  }
411
- }
412
-
413
- class OutdentCodeBlockCommand extends Command {
414
433
  /**
415
- * @inheritDoc
416
- */ refresh() {
434
+ * @inheritDoc
435
+ */ refresh() {
417
436
  this.isEnabled = this._checkEnabled();
418
437
  }
419
438
  /**
420
- * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
421
- * code lines in the selection will be decreased.
422
- *
423
- * @fires execute
424
- */ execute() {
439
+ * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
440
+ * code lines in the selection will be decreased.
441
+ *
442
+ * @fires execute
443
+ */ execute() {
425
444
  const editor = this.editor;
426
445
  const model = editor.model;
427
446
  model.change(()=>{
@@ -460,11 +479,11 @@ class OutdentCodeBlockCommand extends Command {
460
479
  });
461
480
  }
462
481
  /**
463
- * Checks whether the command can be enabled in the current context.
464
- *
465
- * @private
466
- * @returns {Boolean} Whether the command should be enabled.
467
- */ _checkEnabled() {
482
+ * Checks whether the command can be enabled in the current context.
483
+ *
484
+ * @private
485
+ * @returns {Boolean} Whether the command should be enabled.
486
+ */ _checkEnabled() {
468
487
  if (!this._indentSequence) {
469
488
  return false;
470
489
  }
@@ -478,10 +497,6 @@ class OutdentCodeBlockCommand extends Command {
478
497
  return getLastOutdentableSequenceRange(model, position, this._indentSequence);
479
498
  });
480
499
  }
481
- constructor(editor){
482
- super(editor);
483
- this._indentSequence = editor.config.get('codeBlock.indentSequence');
484
- }
485
500
  }
486
501
  // For a position coming from `getIndentOutdentPositions()`, it returns the range representing
487
502
  // the last occurrence of the indent sequence among the leading whitespaces of the code line the
@@ -824,22 +839,93 @@ function getCodeLineTextNodeAtPosition(position) {
824
839
  }
825
840
 
826
841
  const DEFAULT_ELEMENT = 'paragraph';
827
- class CodeBlockEditing extends Plugin {
842
+ /**
843
+ * The editing part of the code block feature.
844
+ *
845
+ * Introduces the `'codeBlock'` command and the `'codeBlock'` model element.
846
+ */ class CodeBlockEditing extends Plugin {
828
847
  /**
829
- * @inheritDoc
830
- */ static get pluginName() {
848
+ * @inheritDoc
849
+ */ static get pluginName() {
831
850
  return 'CodeBlockEditing';
832
851
  }
833
852
  /**
834
- * @inheritDoc
835
- */ static get requires() {
853
+ * @inheritDoc
854
+ */ static get requires() {
836
855
  return [
837
856
  ShiftEnter
838
857
  ];
839
858
  }
840
859
  /**
841
- * @inheritDoc
842
- */ init() {
860
+ * @inheritDoc
861
+ */ constructor(editor){
862
+ super(editor);
863
+ editor.config.define('codeBlock', {
864
+ languages: [
865
+ {
866
+ language: 'plaintext',
867
+ label: 'Plain text'
868
+ },
869
+ {
870
+ language: 'c',
871
+ label: 'C'
872
+ },
873
+ {
874
+ language: 'cs',
875
+ label: 'C#'
876
+ },
877
+ {
878
+ language: 'cpp',
879
+ label: 'C++'
880
+ },
881
+ {
882
+ language: 'css',
883
+ label: 'CSS'
884
+ },
885
+ {
886
+ language: 'diff',
887
+ label: 'Diff'
888
+ },
889
+ {
890
+ language: 'html',
891
+ label: 'HTML'
892
+ },
893
+ {
894
+ language: 'java',
895
+ label: 'Java'
896
+ },
897
+ {
898
+ language: 'javascript',
899
+ label: 'JavaScript'
900
+ },
901
+ {
902
+ language: 'php',
903
+ label: 'PHP'
904
+ },
905
+ {
906
+ language: 'python',
907
+ label: 'Python'
908
+ },
909
+ {
910
+ language: 'ruby',
911
+ label: 'Ruby'
912
+ },
913
+ {
914
+ language: 'typescript',
915
+ label: 'TypeScript'
916
+ },
917
+ {
918
+ language: 'xml',
919
+ label: 'XML'
920
+ }
921
+ ],
922
+ // A single tab.
923
+ indentSequence: '\t'
924
+ });
925
+ }
926
+ /**
927
+ * @inheritDoc
928
+ */ init() {
843
929
  const editor = this.editor;
844
930
  const schema = editor.model.schema;
845
931
  const model = editor.model;
@@ -946,8 +1032,8 @@ class CodeBlockEditing extends Plugin {
946
1032
  });
947
1033
  }
948
1034
  /**
949
- * @inheritDoc
950
- */ afterInit() {
1035
+ * @inheritDoc
1036
+ */ afterInit() {
951
1037
  const editor = this.editor;
952
1038
  const commands = editor.commands;
953
1039
  const indent = commands.get('indent');
@@ -984,11 +1070,11 @@ class CodeBlockEditing extends Plugin {
984
1070
  this._initAriaAnnouncements();
985
1071
  }
986
1072
  /**
987
- * Observe when user enters or leaves code block and set proper aria value in global live announcer.
988
- * This allows screen readers to indicate when the user has entered and left the specified code block.
989
- *
990
- * @internal
991
- */ _initAriaAnnouncements() {
1073
+ * Observe when user enters or leaves code block and set proper aria value in global live announcer.
1074
+ * This allows screen readers to indicate when the user has entered and left the specified code block.
1075
+ *
1076
+ * @internal
1077
+ */ _initAriaAnnouncements() {
992
1078
  const { model, ui, t } = this.editor;
993
1079
  const languageDefs = getNormalizedAndLocalizedLanguageDefinitions(this.editor);
994
1080
  let lastFocusedCodeBlock = null;
@@ -1006,73 +1092,6 @@ class CodeBlockEditing extends Plugin {
1006
1092
  lastFocusedCodeBlock = focusParent;
1007
1093
  });
1008
1094
  }
1009
- /**
1010
- * @inheritDoc
1011
- */ constructor(editor){
1012
- super(editor);
1013
- editor.config.define('codeBlock', {
1014
- languages: [
1015
- {
1016
- language: 'plaintext',
1017
- label: 'Plain text'
1018
- },
1019
- {
1020
- language: 'c',
1021
- label: 'C'
1022
- },
1023
- {
1024
- language: 'cs',
1025
- label: 'C#'
1026
- },
1027
- {
1028
- language: 'cpp',
1029
- label: 'C++'
1030
- },
1031
- {
1032
- language: 'css',
1033
- label: 'CSS'
1034
- },
1035
- {
1036
- language: 'diff',
1037
- label: 'Diff'
1038
- },
1039
- {
1040
- language: 'html',
1041
- label: 'HTML'
1042
- },
1043
- {
1044
- language: 'java',
1045
- label: 'Java'
1046
- },
1047
- {
1048
- language: 'javascript',
1049
- label: 'JavaScript'
1050
- },
1051
- {
1052
- language: 'php',
1053
- label: 'PHP'
1054
- },
1055
- {
1056
- language: 'python',
1057
- label: 'Python'
1058
- },
1059
- {
1060
- language: 'ruby',
1061
- label: 'Ruby'
1062
- },
1063
- {
1064
- language: 'typescript',
1065
- label: 'TypeScript'
1066
- },
1067
- {
1068
- language: 'xml',
1069
- label: 'XML'
1070
- }
1071
- ],
1072
- // A single tab.
1073
- indentSequence: '\t'
1074
- });
1075
- }
1076
1095
  }
1077
1096
  /**
1078
1097
  * Normally, when the Enter (or Shift+Enter) key is pressed, a soft line break is to be added to the
@@ -1224,15 +1243,19 @@ function isSoftBreakNode(node) {
1224
1243
  return node && node.is('element', 'softBreak');
1225
1244
  }
1226
1245
 
1227
- class CodeBlockUI extends Plugin {
1246
+ /**
1247
+ * The code block UI plugin.
1248
+ *
1249
+ * Introduces the `'codeBlock'` dropdown.
1250
+ */ class CodeBlockUI extends Plugin {
1228
1251
  /**
1229
- * @inheritDoc
1230
- */ static get pluginName() {
1252
+ * @inheritDoc
1253
+ */ static get pluginName() {
1231
1254
  return 'CodeBlockUI';
1232
1255
  }
1233
1256
  /**
1234
- * @inheritDoc
1235
- */ init() {
1257
+ * @inheritDoc
1258
+ */ init() {
1236
1259
  const editor = this.editor;
1237
1260
  const t = editor.t;
1238
1261
  const componentFactory = editor.ui.componentFactory;
@@ -1303,9 +1326,9 @@ class CodeBlockUI extends Plugin {
1303
1326
  });
1304
1327
  }
1305
1328
  /**
1306
- * A helper returning a collection of the `codeBlock` dropdown items representing languages
1307
- * available for the user to choose from.
1308
- */ _getLanguageListItemDefinitions(normalizedLanguageDefs) {
1329
+ * A helper returning a collection of the `codeBlock` dropdown items representing languages
1330
+ * available for the user to choose from.
1331
+ */ _getLanguageListItemDefinitions(normalizedLanguageDefs) {
1309
1332
  const editor = this.editor;
1310
1333
  const command = editor.commands.get('codeBlock');
1311
1334
  const itemDefinitions = new Collection();
@@ -1328,18 +1351,26 @@ class CodeBlockUI extends Plugin {
1328
1351
  }
1329
1352
  }
1330
1353
 
1331
- class CodeBlock extends Plugin {
1354
+ /**
1355
+ * The code block plugin.
1356
+ *
1357
+ * For more information about this feature check the {@glink api/code-block package page} and the
1358
+ * {@glink features/code-blocks code block} feature guide.
1359
+ *
1360
+ * This is a "glue" plugin that loads the {@link module:code-block/codeblockediting~CodeBlockEditing code block editing feature}
1361
+ * and the {@link module:code-block/codeblockui~CodeBlockUI code block UI feature}.
1362
+ */ class CodeBlock extends Plugin {
1332
1363
  /**
1333
- * @inheritDoc
1334
- */ static get requires() {
1364
+ * @inheritDoc
1365
+ */ static get requires() {
1335
1366
  return [
1336
1367
  CodeBlockEditing,
1337
1368
  CodeBlockUI
1338
1369
  ];
1339
1370
  }
1340
1371
  /**
1341
- * @inheritDoc
1342
- */ static get pluginName() {
1372
+ * @inheritDoc
1373
+ */ static get pluginName() {
1343
1374
  return 'CodeBlock';
1344
1375
  }
1345
1376
  }