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

25.71% Statements 9/35
25% Branches 4/16
35.71% Functions 5/14
27.27% Lines 9/33

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                      616x                                                                                   616x   616x                   616x                                                                                                                           170x             56x   56x 616x                   56x                                                              
import { Node } from '@tiptap/core';
import { VueRenderer } from '@tiptap/vue-2';
import tippy from 'tippy.js';
import Suggestion from '@tiptap/suggestion';
import { PluginKey } from '@tiptap/pm/state';
import { uniqueId } from 'lodash';
import SuggestionsDropdown from '../components/suggestions_dropdown.vue';
 
function createSuggestionPlugin({
  editor,
  char,
  limit = 5,
  nodeType,
  referenceType,
  cache = true,
  insertionMap = {},
  serializer,
  autocompleteHelper,
}) {
  return Suggestion({
    editor,
    char,
    pluginKey: new PluginKey(uniqueId('suggestions')),
 
    command: ({ editor: tiptapEditor, range, props }) => {
      let content;
 
      if (nodeType === 'link') {
        content = [
          {
            type: 'text',
            text: props.text,
            marks: [{ type: 'link', attrs: props }],
          },
        ];
      } else {
        content = [
          { type: nodeType, attrs: props },
          { type: 'text', text: ` ${insertionMap[props.text] || ''}` },
        ];
      }
 
      tiptapEditor.chain().focus().insertContentAt(range, content).run();
    },
 
    async items({ query, editor: tiptapEditor }) {
      const slice = tiptapEditor.state.doc.slice(0, tiptapEditor.state.selection.to);
      const markdownLine = serializer.serialize({ doc: slice.content }).split('\n').pop();
 
      return autocompleteHelper
        .getDataSource(referenceType, {
          command: markdownLine.match(/\/\w+/)?.[0],
          cache,
          limit,
        })
        .search(query);
    },
 
    render: () => {
      let component;
      let popup;
      let isHidden = false;
 
      const onUpdate = (props) => {
        component?.updateProps({ ...props, loading: false });
 
        if (!props.clientRect) return;
 
        popup?.[0].setProps({
          getReferenceClientRect: props.clientRect,
        });
      };
 
      return {
        onBeforeStart: (props) => {
          component = new VueRenderer(SuggestionsDropdown, {
            propsData: {
              ...props,
              char,
              nodeType,
              nodeProps: { referenceType },
              loading: true,
            },
            editor: props.editor,
          });
 
          if (!props.clientRect) {
            return;
          }
 
          popup = tippy('body', {
            getReferenceClientRect: props.clientRect,
            appendTo: () => document.body,
            onHide: () => {
              isHidden = true;
            },
            onShow: () => {
              isHidden = false;
            },
            content: component.element,
            showOnCreate: true,
            interactive: true,
            trigger: 'manual',
            placement: 'bottom-start',
          });
        },
 
        onStart: onUpdate,
        onUpdate,
 
        onKeyDown(props) {
          if (isHidden) return false;
 
          if (props.event.key === 'Escape') {
            popup?.[0].hide();
 
            return true;
          }
 
          return component?.ref?.onKeyDown(props);
        },
 
        onExit() {
          popup?.[0].destroy();
          component?.destroy();
        },
      };
    },
  });
}
 
export default Node.create({
  name: 'suggestions',
 
  addOptions() {
    return {
      autocompleteHelper: {},
      serializer: null,
    };
  },
 
  addProseMirrorPlugins() {
    const { serializer, autocompleteHelper } = this.options;
 
    const createPlugin = (char, nodeType, referenceType, options = {}) =>
      createSuggestionPlugin({
        editor: this.editor,
        char,
        nodeType,
        referenceType,
        serializer,
        autocompleteHelper,
        ...options,
      });
 
    return [
      createPlugin('@', 'reference', 'user', { limit: 10 }),
      createPlugin('#', 'reference', 'issue'),
      createPlugin('$', 'reference', 'snippet'),
      createPlugin('~', 'referenceLabel', 'label', { limit: 20 }),
      createPlugin('&', 'reference', 'epic'),
      createPlugin('!', 'reference', 'merge_request'),
      createPlugin('[vulnerability:', 'reference', 'vulnerability'),
      createPlugin('%', 'reference', 'milestone'),
      createPlugin(':', 'emoji', 'emoji'),
      createPlugin('[[', 'link', 'wiki'),
      createPlugin('/', 'reference', 'command', {
        cache: false,
        limit: 100,
        insertionMap: {
          '/label': '~',
          '/unlabel': '~',
          '/relabel': '~',
          '/assign': '@',
          '/unassign': '@',
          '/reassign': '@',
          '/cc': '@',
          '/assign_reviewer': '@',
          '/unassign_reviewer': '@',
          '/reassign_reviewer': '@',
          '/milestone': '%',
        },
      }),
    ];
  },
});