All files / app/assets/javascripts/content_editor/extensions description_item.js

90.47% Statements 19/21
91.66% Branches 11/12
88.88% Functions 8/9
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                251x               1453x                 79x       16x                   79x   1x   1x     4x   3x 3x 2x   1x     4x   3x 3x   1x          
import { Node, mergeAttributes } from '@tiptap/core';
 
export default Node.create({
  name: 'descriptionItem',
  content: 'block+',
  defining: true,
 
  addOptions() {
    return {
      HTMLAttributes: {
        dir: 'auto',
      },
    };
  },
 
  addAttributes() {
    return {
      isTerm: {
        default: true,
        parseHTML: (element) => element.tagName.toLowerCase() === 'dt',
      },
    };
  },
 
  parseHTML() {
    return [{ tag: 'dt' }, { tag: 'dd' }];
  },
 
  renderHTML({ HTMLAttributes: { isTerm, ...HTMLAttributes } }) {
    return [
      'li',
      mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
        class: isTerm ? 'dl-term' : 'dl-description',
      }),
      0,
    ];
  },
 
  addKeyboardShortcuts() {
    return {
      Enter: () => {
        Iif (!this.editor.isActive('descriptionItem')) return false;
 
        return this.editor.commands.splitListItem('descriptionItem');
      },
      Tab: () => {
        if (!this.editor.isActive('descriptionItem')) return false;
 
        const { isTerm } = this.editor.getAttributes('descriptionItem');
        if (isTerm)
          return this.editor.commands.updateAttributes('descriptionItem', { isTerm: !isTerm });
 
        return false;
      },
      'Shift-Tab': () => {
        if (!this.editor.isActive('descriptionItem')) return false;
 
        const { isTerm } = this.editor.getAttributes('descriptionItem');
        if (isTerm) return this.editor.commands.liftListItem('descriptionItem');
 
        return this.editor.commands.updateAttributes('descriptionItem', { isTerm: true });
      },
    };
  },
});