All files / app/assets/javascripts/editor/extensions source_editor_markdown_ext.js

86.66% Statements 52/60
72% Branches 18/25
81.25% Functions 13/16
87.27% Lines 48/55

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          54x       27x 27x 27x 27x       11x 1x 1x   1x 4x   1x       27x 297x 108x                     297x               27x         80x   6x 6x 6x 6x 3x   3x 3x   3x 1x 1x   3x       6x     5x 5x     10x 10x 10x 10x                                                                                             5x 5x 3x   2x         2x   2x   4x 2x 2x             2x 2x   2x       2x          
import { insertMarkdownText } from '~/lib/utils/text_markdown';
import { EDITOR_TOOLBAR_BUTTON_GROUPS, EXTENSION_MARKDOWN_BUTTONS } from '../constants';
 
export class EditorMarkdownExtension {
  static get extensionName() {
    return 'EditorMarkdown';
  }
 
  onSetup(instance) {
    this.toolbarButtons = [];
    this.actions = [];
    Eif (instance.toolbar) {
      this.setupToolbar(instance);
    }
  }
  onBeforeUnuse(instance) {
    const ids = this.toolbarButtons.map((item) => item.id);
    Eif (instance.toolbar) {
      instance.toolbar.removeItems(ids);
    }
    this.actions.forEach((action) => {
      action.dispose();
    });
    this.actions = [];
  }
 
  setupToolbar(instance) {
    this.toolbarButtons = EXTENSION_MARKDOWN_BUTTONS.map((btn) => {
      if (btn.data.mdShortcuts) {
        this.actions.push(
          instance.addAction({
            id: btn.id,
            label: btn.label,
            keybindings: btn.data.mdShortcuts,
            run(inst) {
              inst.insertMarkdown(btn.data);
            },
          }),
        );
      }
      return {
        ...btn,
        icon: btn.id,
        group: EDITOR_TOOLBAR_BUTTON_GROUPS.edit,
        category: 'tertiary',
        onClick: (e) => instance.insertMarkdown(e),
      };
    });
    instance.toolbar.addItems(this.toolbarButtons);
  }
 
  // eslint-disable-next-line class-methods-use-this
  provides() {
    return {
      getSelectedText: (instance, selection = instance.getSelection()) => {
        const { startLineNumber, endLineNumber, startColumn, endColumn } = selection;
        const valArray = instance.getValue().split('\n');
        let text = '';
        if (startLineNumber === endLineNumber) {
          text = valArray[startLineNumber - 1].slice(startColumn - 1, endColumn - 1);
        } else {
          const startLineText = valArray[startLineNumber - 1].slice(startColumn - 1);
          const endLineText = valArray[endLineNumber - 1].slice(0, endColumn - 1);
 
          for (let i = startLineNumber, k = endLineNumber - 1; i < k; i += 1) {
            text += `${valArray[i]}`;
            Iif (i !== k - 1) text += `\n`;
          }
          text = text
            ? [startLineText, text, endLineText].join('\n')
            : [startLineText, endLineText].join('\n');
        }
        return text;
      },
      replaceSelectedText: (instance, text, select) => {
        const forceMoveMarkers = !select;
        instance.executeEdits('', [{ range: instance.getSelection(), text, forceMoveMarkers }]);
      },
      moveCursor: (instance, dx = 0, dy = 0) => {
        const pos = instance.getPosition();
        pos.column += dx;
        pos.lineNumber += dy;
        instance.setPosition(pos);
      },
      insertMarkdown: (instance, e) => {
        const { mdTag: tag, mdBlock: blockTag, mdPrepend, mdSelect: select } =
          e.currentTarget?.dataset || e;
 
        insertMarkdownText({
          tag,
          blockTag,
          wrap: !mdPrepend,
          select,
          selected: instance.getSelectedText(),
          text: instance.getValue(),
          editor: instance,
        });
        instance.focus();
      },
      /**
       * Adjust existing selection to select text within the original selection.
       * - If `selectedText` is not supplied, we fetch selected text with
       *
       * ALGORITHM:
       *
       * MULTI-LINE SELECTION
       * 1. Find line that contains `toSelect` text.
       * 2. Using the index of this line and the position of `toSelect` text in it,
       * construct:
       *   * newStartLineNumber
       *   * newStartColumn
       *
       * SINGLE-LINE SELECTION
       * 1. Use `startLineNumber` from the current selection as `newStartLineNumber`
       * 2. Find the position of `toSelect` text in it to get `newStartColumn`
       *
       * 3. `newEndLineNumber` — Since this method is supposed to be used with
       * markdown decorators that are pretty short, the `newEndLineNumber` is
       * suggested to be assumed the same as the startLine.
       * 4. `newEndColumn` — pretty obvious
       * 5. Adjust the start and end positions of the current selection
       * 6. Re-set selection on the instance
       *
       * @param {module:source_editor_instance~EditorInstance} instance - The Source Editor instance. Is passed automatically.
       * @param {string} toSelect - New text to select within current selection.
       * @param {string} selectedText - Currently selected text. It's just a
       * shortcut: If it's not supplied, we fetch selected text from the instance
       */
      selectWithinSelection: (instance, toSelect, selectedText) => {
        const currentSelection = instance.getSelection();
        if (currentSelection.isEmpty() || !toSelect) {
          return;
        }
        const text = selectedText || instance.getSelectedText(currentSelection);
        let lineShift;
        let newStartLineNumber;
        let newStartColumn;
 
        const textLines = text.split('\n');
 
        if (textLines.length > 1) {
          // Multi-line selection
          lineShift = textLines.findIndex((line) => line.indexOf(toSelect) !== -1);
          newStartLineNumber = currentSelection.startLineNumber + lineShift;
          newStartColumn = textLines[lineShift].indexOf(toSelect) + 1;
        } else E{
          // Single-line selection
          newStartLineNumber = currentSelection.startLineNumber;
          newStartColumn = currentSelection.startColumn + text.indexOf(toSelect);
        }
 
        const newEndLineNumber = newStartLineNumber;
        const newEndColumn = newStartColumn + toSelect.length;
 
        const newSelection = currentSelection
          .setStartPosition(newStartLineNumber, newStartColumn)
          .setEndPosition(newEndLineNumber, newEndColumn);
 
        instance.setSelection(newSelection);
      },
    };
  }
}