@ckeditor/ckeditor5-indent 47.5.0 → 47.6.0-alpha.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
@@ -7,6 +7,7 @@ import { ButtonView, MenuBarMenuListItemButtonView } from '@ckeditor/ckeditor5-u
7
7
  import { IconIndent, IconOutdent } from '@ckeditor/ckeditor5-icons/dist/index.js';
8
8
  import { addMarginStylesRules } from '@ckeditor/ckeditor5-engine/dist/index.js';
9
9
  import { first } from '@ckeditor/ckeditor5-utils/dist/index.js';
10
+ import { _isListItemBlock } from '@ckeditor/ckeditor5-list/dist/index.js';
10
11
 
11
12
  /**
12
13
  * The indent editing feature.
@@ -267,6 +268,9 @@ import { first } from '@ckeditor/ckeditor5-utils/dist/index.js';
267
268
  */ getNextIndent(indentAttributeValue) {
268
269
  const currentOffset = parseFloat(indentAttributeValue || '0');
269
270
  const isSameUnit = !indentAttributeValue || indentAttributeValue.endsWith(this.unit);
271
+ if (currentOffset < 0) {
272
+ return;
273
+ }
270
274
  if (!isSameUnit) {
271
275
  return this.isForward ? this.offset + this.unit : undefined;
272
276
  }
@@ -316,10 +320,513 @@ import { first } from '@ckeditor/ckeditor5-utils/dist/index.js';
316
320
  */ getNextIndent(indentAttributeValue) {
317
321
  const currentIndex = this.classes.indexOf(indentAttributeValue);
318
322
  const indexStep = this.isForward ? 1 : -1;
319
- return this.classes[currentIndex + indexStep];
323
+ const nextIndex = currentIndex + indexStep;
324
+ const nextIndexClamped = Math.min(nextIndex, this.classes.length - 1);
325
+ return this.classes[nextIndexClamped];
326
+ }
327
+ }
328
+
329
+ /**
330
+ * The indent block list command.
331
+ *
332
+ * The command is registered by the {@link module:indent/integrations/indentblocklistintegration~IndentBlockListIntegration} as
333
+ * `'indentBlockList'` for indenting lists and `'outdentBlockList'` for outdenting lists.
334
+ *
335
+ * To increase/decrease block indentation of the list the selection must be at the start of the first top–level list item
336
+ * in the list.
337
+ *
338
+ * To increase block indentation of the list, execute the command:
339
+ *
340
+ * ```ts
341
+ * editor.execute( 'indentBlockList' );
342
+ * ```
343
+ *
344
+ * To decrease block indentation of the list, execute the command:
345
+ *
346
+ * ```ts
347
+ * editor.execute( 'outdentBlockList' );
348
+ * ```
349
+ */ class IndentBlockListCommand extends Command {
350
+ /**
351
+ * The command's indentation behavior.
352
+ */ _indentBehavior;
353
+ /**
354
+ * Creates an instance of the command.
355
+ */ constructor(editor, indentBehavior){
356
+ super(editor);
357
+ this._indentBehavior = indentBehavior;
358
+ }
359
+ /**
360
+ * @inheritDoc
361
+ */ refresh() {
362
+ const listItem = this._getFirstListItemIfSelectionIsAtListStart(this.editor.model.document.selection);
363
+ this.isEnabled = !!listItem && this._indentBehavior.checkEnabled(listItem.getAttribute('blockIndentList'));
364
+ }
365
+ /**
366
+ * Executes the command.
367
+ *
368
+ * @fires execute
369
+ * @param options Command options.
370
+ * @param options.firstListOnly When `true`, indentation is applied only to the first list at the beginning of the selection.
371
+ * When `false` or omitted, indentation is applied to all lists of the selection.
372
+ */ execute(options = {}) {
373
+ const editor = this.editor;
374
+ const model = editor.model;
375
+ const selection = model.document.selection;
376
+ model.change((writer)=>{
377
+ const listItem = this._getFirstListItemIfSelectionIsAtListStart(selection);
378
+ const listItems = [];
379
+ if (!options.firstListOnly) {
380
+ const blocks = Array.from(selection.getSelectedBlocks());
381
+ for (const block of blocks){
382
+ if (_isListItemBlock(block) && block.getAttribute('listIndent') === 0 && model.schema.checkAttribute(block, 'blockIndentList')) {
383
+ listItems.push(block);
384
+ }
385
+ }
386
+ } else {
387
+ listItems.push(listItem);
388
+ }
389
+ for (const item of listItems){
390
+ const currentIndent = item.getAttribute('blockIndentList');
391
+ const nextIndent = this._indentBehavior.getNextIndent(currentIndent);
392
+ if (nextIndent) {
393
+ writer.setAttribute('blockIndentList', nextIndent, item);
394
+ } else {
395
+ writer.removeAttribute('blockIndentList', item);
396
+ }
397
+ }
398
+ });
399
+ }
400
+ /**
401
+ * Returns the list item at the beginning of the current selection if it is the first top–level list item in the list.
402
+ * Otherwise, returns `null`.
403
+ */ _getFirstListItemIfSelectionIsAtListStart(selection) {
404
+ const position = selection.getFirstPosition();
405
+ const listUtils = this.editor.plugins.get('ListUtils');
406
+ const parent = position.parent;
407
+ const schema = this.editor.model.schema;
408
+ if (position.isAtStart && _isListItemBlock(parent) && parent.getAttribute('listIndent') == 0 && schema.checkAttribute(parent, 'blockIndentList') && listUtils.isFirstListItemInList(parent)) {
409
+ return parent;
410
+ }
411
+ return null;
412
+ }
413
+ }
414
+
415
+ /**
416
+ * The indent block list item command.
417
+ *
418
+ * The command is registered by the {@link module:indent/integrations/indentblocklistintegration~IndentBlockListIntegration} as
419
+ * `'indentBlockListItem'` for indenting list items and `'outdentBlockListItem'` for outdenting list items.
420
+ *
421
+ * It's only possible to reset the block indentation of a list item to `0`.
422
+ * This means that if a list item has negative block indentation, the command will only allow forward indentation
423
+ * to make it possible to reset it to `0` and if a list item has positive block indentation, the command
424
+ * will only allow backward indentation to make it possible to reset it to `0`.
425
+ *
426
+ * To increase block indentation of the list item, execute the command:
427
+ *
428
+ * ```ts
429
+ * editor.execute( 'indentBlockListItem' );
430
+ * ```
431
+ *
432
+ * To decrease block indentation of the list item, execute the command:
433
+ *
434
+ * ```ts
435
+ * editor.execute( 'outdentBlockListItem' );
436
+ * ```
437
+ */ class IndentBlockListItemCommand extends Command {
438
+ /**
439
+ * The command's indentation behavior.
440
+ */ _indentBehavior;
441
+ /**
442
+ * Creates an instance of the command.
443
+ */ constructor(editor, indentBehavior){
444
+ super(editor);
445
+ this._indentBehavior = indentBehavior;
446
+ }
447
+ /**
448
+ * @inheritDoc
449
+ */ refresh() {
450
+ this.isEnabled = this._getAffectedListItems().length > 0;
451
+ }
452
+ /**
453
+ * @inheritDoc
454
+ */ execute() {
455
+ const editor = this.editor;
456
+ const model = editor.model;
457
+ model.change((writer)=>{
458
+ for (const block of this._getAffectedListItems()){
459
+ writer.removeAttribute('blockIndentListItem', block);
460
+ }
461
+ });
462
+ }
463
+ /**
464
+ * Returns an array of list items which block indentation should be changed.
465
+ */ _getAffectedListItems() {
466
+ const model = this.editor.model;
467
+ const selection = model.document.selection;
468
+ const listUtils = this.editor.plugins.get('ListUtils');
469
+ const blocksInSelection = Array.from(selection.getSelectedBlocks());
470
+ const expandedBlocks = listUtils.expandListBlocksToCompleteItems(blocksInSelection);
471
+ return expandedBlocks.filter((block)=>this._isIndentationChangeAllowed(block));
472
+ }
473
+ /**
474
+ * Returns `true` if changing the block indentation is allowed for the given list item.
475
+ *
476
+ * Indentation of a list item is only allowed if it moves toward zero. This means that:
477
+ * - when currentIndent = 0, the command should be disabled
478
+ * - when currentIndent < 0, only forward indentation should be allowed
479
+ * - when currentIndent > 0, only backward indentation should be allowed
480
+ *
481
+ * For classes-based indentation, the command should be enabled if there is a class to be removed.
482
+ */ _isIndentationChangeAllowed(element) {
483
+ if (!element.hasAttribute('blockIndentListItem')) {
484
+ return false;
485
+ }
486
+ const currentIndent = parseFloat(element.getAttribute('blockIndentListItem'));
487
+ // Class based indent, allow only outdent.
488
+ // TODO find a better way, probably use behavior to find out.
489
+ if (isNaN(currentIndent)) {
490
+ return !this._indentBehavior.isForward;
491
+ }
492
+ return this._indentBehavior.isForward && currentIndent < 0 || !this._indentBehavior.isForward && currentIndent > 0;
320
493
  }
321
494
  }
322
495
 
496
+ /**
497
+ * This integration enables using block indentation feature with lists.
498
+ */ class IndentBlockListIntegration extends Plugin {
499
+ /**
500
+ * @inheritDoc
501
+ */ static get pluginName() {
502
+ return 'IndentBlockListIntegration';
503
+ }
504
+ /**
505
+ * @inheritDoc
506
+ */ static get isOfficialPlugin() {
507
+ return true;
508
+ }
509
+ /**
510
+ * @inheritDoc
511
+ */ init() {
512
+ const editor = this.editor;
513
+ if (!this.editor.plugins.has('ListEditing')) {
514
+ return;
515
+ }
516
+ const config = editor.config.get('indentBlock');
517
+ if (config.classes && config.classes.length) {
518
+ this._setupConversionUsingClassesForListBlock(config.classes);
519
+ this._setupConversionUsingClassesForListItemBlock(config.classes);
520
+ editor.commands.add('indentBlockList', new IndentBlockListCommand(editor, new IndentUsingClasses({
521
+ direction: 'forward',
522
+ classes: config.classes
523
+ })));
524
+ editor.commands.add('outdentBlockList', new IndentBlockListCommand(editor, new IndentUsingClasses({
525
+ direction: 'backward',
526
+ classes: config.classes
527
+ })));
528
+ editor.commands.add('indentBlockListItem', new IndentBlockListItemCommand(editor, new IndentUsingClasses({
529
+ direction: 'forward',
530
+ classes: config.classes
531
+ })));
532
+ editor.commands.add('outdentBlockListItem', new IndentBlockListItemCommand(editor, new IndentUsingClasses({
533
+ direction: 'backward',
534
+ classes: config.classes
535
+ })));
536
+ } else {
537
+ editor.data.addStyleProcessorRules(addMarginStylesRules);
538
+ this._setupConversionUsingOffsetForListBlock();
539
+ this._setupConversionUsingOffsetForListItemBlock();
540
+ editor.commands.add('indentBlockList', new IndentBlockListCommand(editor, new IndentUsingOffset({
541
+ direction: 'forward',
542
+ offset: config.offset,
543
+ unit: config.unit
544
+ })));
545
+ editor.commands.add('outdentBlockList', new IndentBlockListCommand(editor, new IndentUsingOffset({
546
+ direction: 'backward',
547
+ offset: config.offset,
548
+ unit: config.unit
549
+ })));
550
+ editor.commands.add('indentBlockListItem', new IndentBlockListItemCommand(editor, new IndentUsingOffset({
551
+ direction: 'forward',
552
+ offset: config.offset,
553
+ unit: config.unit
554
+ })));
555
+ editor.commands.add('outdentBlockListItem', new IndentBlockListItemCommand(editor, new IndentUsingOffset({
556
+ direction: 'backward',
557
+ offset: config.offset,
558
+ unit: config.unit
559
+ })));
560
+ }
561
+ const listEditing = editor.plugins.get('ListEditing');
562
+ // Make sure that all items in a single list (items at the same level & listType) have the same blockIndentList attribute value.
563
+ listEditing.on('postFixer', (evt, { listNodes, writer })=>{
564
+ for (const { node, previousNodeInList } of listNodes){
565
+ // This is a first item of a nested list.
566
+ if (!previousNodeInList) {
567
+ continue;
568
+ }
569
+ if (previousNodeInList.getAttribute('listType') != node.getAttribute('listType')) {
570
+ continue;
571
+ }
572
+ if (ensureIndentValuesConsistency('blockIndentList', node, previousNodeInList, writer)) {
573
+ evt.return = true;
574
+ }
575
+ if (previousNodeInList.getAttribute('listItemId') != node.getAttribute('listItemId')) {
576
+ continue;
577
+ }
578
+ if (ensureIndentValuesConsistency('blockIndentListItem', node, previousNodeInList, writer)) {
579
+ evt.return = true;
580
+ }
581
+ }
582
+ });
583
+ this.listenTo(editor.editing.view.document, 'tab', (evt, data)=>{
584
+ const commandName = data.shiftKey ? 'outdentBlockList' : 'indentBlockList';
585
+ const command = this.editor.commands.get(commandName);
586
+ if (command.isEnabled) {
587
+ editor.execute(commandName, {
588
+ firstListOnly: true
589
+ });
590
+ data.stopPropagation();
591
+ data.preventDefault();
592
+ evt.stop();
593
+ }
594
+ }, {
595
+ context: 'li',
596
+ priority: 'high'
597
+ });
598
+ }
599
+ /**
600
+ * @inheritDoc
601
+ */ afterInit() {
602
+ const editor = this.editor;
603
+ const model = editor.model;
604
+ const schema = model.schema;
605
+ if (!editor.plugins.has('ListEditing')) {
606
+ return;
607
+ }
608
+ // Schema registration.
609
+ schema.extend('$listItem', {
610
+ allowAttributes: [
611
+ 'blockIndentList',
612
+ 'blockIndentListItem'
613
+ ]
614
+ });
615
+ schema.setAttributeProperties('blockIndentList', {
616
+ isFormatting: true
617
+ });
618
+ schema.setAttributeProperties('blockIndentListItem', {
619
+ isFormatting: true
620
+ });
621
+ model.schema.addAttributeCheck((context)=>{
622
+ const item = context.last;
623
+ if (!item.getAttribute('listItemId')) {
624
+ return false;
625
+ }
626
+ }, 'blockIndentList');
627
+ model.schema.addAttributeCheck((context)=>{
628
+ const item = context.last;
629
+ if (!item.getAttribute('listItemId')) {
630
+ return false;
631
+ }
632
+ }, 'blockIndentListItem');
633
+ // Clear blockIndentList and blockIndentListItem when list indent changes.
634
+ const clearBlockIndentAttributesOnListIndentChange = (_evt, changedBlocks)=>{
635
+ editor.model.change((writer)=>{
636
+ for (const node of changedBlocks){
637
+ if (node.hasAttribute('listItemId')) {
638
+ if (node.hasAttribute('blockIndentList')) {
639
+ writer.removeAttribute('blockIndentList', node);
640
+ }
641
+ if (node.hasAttribute('blockIndentListItem')) {
642
+ writer.removeAttribute('blockIndentListItem', node);
643
+ }
644
+ }
645
+ }
646
+ });
647
+ };
648
+ const indentListCommand = editor.commands.get('indentList');
649
+ const outdentListCommand = editor.commands.get('outdentList');
650
+ if (indentListCommand) {
651
+ this.listenTo(indentListCommand, 'afterExecute', clearBlockIndentAttributesOnListIndentChange);
652
+ }
653
+ if (outdentListCommand) {
654
+ this.listenTo(outdentListCommand, 'afterExecute', clearBlockIndentAttributesOnListIndentChange);
655
+ }
656
+ // Register list block indent commands in multi command.
657
+ const indentCommand = editor.commands.get('indent');
658
+ const outdentCommand = editor.commands.get('outdent');
659
+ indentCommand.registerChildCommand(editor.commands.get('indentBlockList'));
660
+ outdentCommand.registerChildCommand(editor.commands.get('outdentBlockList'));
661
+ indentCommand.registerChildCommand(editor.commands.get('indentBlockListItem'));
662
+ outdentCommand.registerChildCommand(editor.commands.get('outdentBlockListItem'));
663
+ }
664
+ /**
665
+ * Setups conversion for list block indent using offset indents.
666
+ */ _setupConversionUsingOffsetForListBlock() {
667
+ const editor = this.editor;
668
+ const conversion = editor.conversion;
669
+ const locale = editor.locale;
670
+ const marginProperty = locale.contentLanguageDirection === 'rtl' ? 'margin-right' : 'margin-left';
671
+ const listEditing = editor.plugins.get('ListEditing');
672
+ conversion.for('upcast').add((dispatcher)=>{
673
+ dispatcher.on('element:ol', listBlockIndentUpcastConverter('blockIndentList', marginProperty));
674
+ dispatcher.on('element:ul', listBlockIndentUpcastConverter('blockIndentList', marginProperty));
675
+ });
676
+ listEditing.registerDowncastStrategy({
677
+ scope: 'list',
678
+ attributeName: 'blockIndentList',
679
+ setAttributeOnDowncast (writer, value, element) {
680
+ if (value) {
681
+ writer.setStyle(marginProperty, value, element);
682
+ }
683
+ }
684
+ });
685
+ }
686
+ /**
687
+ * Setups conversion for list item block indent using offset indents.
688
+ */ _setupConversionUsingOffsetForListItemBlock() {
689
+ const editor = this.editor;
690
+ const locale = editor.locale;
691
+ const conversion = editor.conversion;
692
+ const marginProperty = locale.contentLanguageDirection === 'rtl' ? 'margin-right' : 'margin-left';
693
+ const listEditing = editor.plugins.get('ListEditing');
694
+ conversion.for('upcast').add((dispatcher)=>{
695
+ dispatcher.on('element:li', listBlockIndentUpcastConverter('blockIndentListItem', marginProperty), {
696
+ priority: 'low'
697
+ });
698
+ });
699
+ listEditing.registerDowncastStrategy({
700
+ scope: 'item',
701
+ attributeName: 'blockIndentListItem',
702
+ setAttributeOnDowncast (writer, value, element) {
703
+ if (value) {
704
+ writer.setStyle(marginProperty, value, element);
705
+ }
706
+ }
707
+ });
708
+ }
709
+ /**
710
+ * Setups conversion for list block indent using classes.
711
+ */ _setupConversionUsingClassesForListBlock(classes) {
712
+ const editor = this.editor;
713
+ const conversion = editor.conversion;
714
+ const listEditing = editor.plugins.get('ListEditing');
715
+ conversion.for('upcast').add((dispatcher)=>{
716
+ dispatcher.on('element:ol', listBlockIndentUpcastConverterUsingClasses('blockIndentList', classes));
717
+ dispatcher.on('element:ul', listBlockIndentUpcastConverterUsingClasses('blockIndentList', classes));
718
+ });
719
+ listEditing.registerDowncastStrategy({
720
+ scope: 'list',
721
+ attributeName: 'blockIndentList',
722
+ setAttributeOnDowncast (writer, value, element) {
723
+ if (value) {
724
+ writer.addClass(value, element);
725
+ }
726
+ }
727
+ });
728
+ }
729
+ /**
730
+ * Setups conversion for list item block indent using classes.
731
+ */ _setupConversionUsingClassesForListItemBlock(classes) {
732
+ const editor = this.editor;
733
+ const conversion = editor.conversion;
734
+ const listEditing = editor.plugins.get('ListEditing');
735
+ conversion.for('upcast').add((dispatcher)=>{
736
+ dispatcher.on('element:li', listBlockIndentUpcastConverterUsingClasses('blockIndentListItem', classes), {
737
+ priority: 'low'
738
+ });
739
+ });
740
+ listEditing.registerDowncastStrategy({
741
+ scope: 'item',
742
+ attributeName: 'blockIndentListItem',
743
+ setAttributeOnDowncast (writer, value, element) {
744
+ if (value) {
745
+ writer.addClass(value, element);
746
+ }
747
+ }
748
+ });
749
+ }
750
+ }
751
+ function listBlockIndentUpcastConverterUsingClasses(attributeName, classes) {
752
+ return (evt, data, conversionApi)=>{
753
+ const { writer, consumable } = conversionApi;
754
+ if (!data.modelRange) {
755
+ Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
756
+ }
757
+ const viewClasses = Array.from(data.viewItem.getClassNames());
758
+ const matchedClass = classes.find((cls)=>viewClasses.includes(cls));
759
+ if (matchedClass === undefined) {
760
+ return;
761
+ }
762
+ let applied = false;
763
+ let indentLevel;
764
+ for (const item of data.modelRange.getItems({
765
+ shallow: true
766
+ })){
767
+ if (indentLevel === undefined) {
768
+ indentLevel = item.getAttribute('listIndent');
769
+ }
770
+ if (item.hasAttribute(attributeName)) {
771
+ continue;
772
+ }
773
+ if (item.getAttribute('listIndent') !== indentLevel) {
774
+ continue;
775
+ }
776
+ writer.setAttribute(attributeName, matchedClass, item);
777
+ applied = true;
778
+ }
779
+ if (applied) {
780
+ consumable.consume(data.viewItem, {
781
+ classes: matchedClass
782
+ });
783
+ }
784
+ };
785
+ }
786
+ function listBlockIndentUpcastConverter(attributeName, marginProperty) {
787
+ return (evt, data, conversionApi)=>{
788
+ const { writer, consumable } = conversionApi;
789
+ if (!data.modelRange) {
790
+ Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
791
+ }
792
+ const marginValue = data.viewItem.getStyle(marginProperty);
793
+ let applied = false;
794
+ let indentLevel;
795
+ for (const item of data.modelRange.getItems({
796
+ shallow: true
797
+ })){
798
+ if (indentLevel === undefined) {
799
+ indentLevel = item.getAttribute('listIndent');
800
+ }
801
+ if (item.hasAttribute(attributeName)) {
802
+ continue;
803
+ }
804
+ if (item.getAttribute('listIndent') !== indentLevel) {
805
+ continue;
806
+ }
807
+ writer.setAttribute(attributeName, marginValue, item);
808
+ applied = true;
809
+ }
810
+ if (applied) {
811
+ consumable.consume(data.viewItem, {
812
+ styles: marginProperty
813
+ });
814
+ }
815
+ };
816
+ }
817
+ function ensureIndentValuesConsistency(attributeName, node, previousNodeInList, writer) {
818
+ const prevNodeIndentListValue = previousNodeInList.getAttribute(attributeName);
819
+ if (node.getAttribute(attributeName) === prevNodeIndentListValue) {
820
+ return false;
821
+ }
822
+ if (prevNodeIndentListValue) {
823
+ writer.setAttribute(attributeName, prevNodeIndentListValue, node);
824
+ } else {
825
+ writer.removeAttribute(attributeName, node);
826
+ }
827
+ return true;
828
+ }
829
+
323
830
  const DEFAULT_ELEMENTS = [
324
831
  'paragraph',
325
832
  'heading1',
@@ -356,6 +863,13 @@ const DEFAULT_ELEMENTS = [
356
863
  */ static get isOfficialPlugin() {
357
864
  return true;
358
865
  }
866
+ /**
867
+ * @inheritDoc
868
+ */ static get requires() {
869
+ return [
870
+ IndentBlockListIntegration
871
+ ];
872
+ }
359
873
  /**
360
874
  * @inheritDoc
361
875
  */ init() {
@@ -467,5 +981,5 @@ const DEFAULT_ELEMENTS = [
467
981
  }
468
982
  }
469
983
 
470
- export { Indent, IndentBlock, IndentBlockCommand, IndentEditing, IndentUI, IndentUsingClasses as _IndentUsingClasses, IndentUsingOffset as _IndentUsingOffset };
984
+ export { Indent, IndentBlock, IndentBlockCommand, IndentBlockListCommand, IndentBlockListIntegration, IndentBlockListItemCommand, IndentEditing, IndentUI, IndentUsingClasses as _IndentUsingClasses, IndentUsingOffset as _IndentUsingOffset };
471
985
  //# sourceMappingURL=index.js.map