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

35.71% Statements 5/14
33.33% Branches 2/6
62.5% Functions 5/8
38.46% Lines 5/13

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                629x     10x           90x       10x               90x                                        
import { Node, mergeAttributes } from '@tiptap/core';
 
export default Node.create({
  name: 'descriptionItem',
  content: 'block+',
  defining: true,
 
  addAttributes() {
    return {
      isTerm: {
        default: true,
        parseHTML: (element) => element.tagName.toLowerCase() === 'dt',
      },
    };
  },
 
  parseHTML() {
    return [{ tag: 'dt' }, { tag: 'dd' }];
  },
 
  renderHTML({ HTMLAttributes: { isTerm, ...HTMLAttributes } }) {
    return [
      'li',
      mergeAttributes(HTMLAttributes, { class: isTerm ? 'dl-term' : 'dl-description' }),
      0,
    ];
  },
 
  addKeyboardShortcuts() {
    return {
      Enter: () => {
        return this.editor.commands.splitListItem('descriptionItem');
      },
      Tab: () => {
        const { isTerm } = this.editor.getAttributes('descriptionItem');
        if (isTerm)
          return this.editor.commands.updateAttributes('descriptionItem', { isTerm: !isTerm });
 
        return false;
      },
      'Shift-Tab': () => {
        const { isTerm } = this.editor.getAttributes('descriptionItem');
        if (isTerm) return this.editor.commands.liftListItem('descriptionItem');
 
        return this.editor.commands.updateAttributes('descriptionItem', { isTerm: true });
      },
    };
  },
});