utils.js 984 Bytes
Newer Older
1 2 3 4
import AjaxCache from '~/lib/utils/ajax_cache';

const REGEX_QUICK_ACTIONS = /^\/\w+.*$/gm;

Fatih Acet's avatar
Fatih Acet committed
5 6
export const findNoteObjectById = (notes, id) =>
  notes.filter(n => n.id === id)[0];
7

Fatih Acet's avatar
Fatih Acet committed
8
export const getQuickActionText = note => {
9
  let text = 'Applying command';
Fatih Acet's avatar
Fatih Acet committed
10 11
  const quickActions =
    AjaxCache.get(gl.GfmAutoComplete.dataSources.commands) || [];
12

Fatih Acet's avatar
Fatih Acet committed
13
  const executedCommands = quickActions.filter(command => {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    const commandRegex = new RegExp(`/${command.name}`);
    return commandRegex.test(note);
  });

  if (executedCommands && executedCommands.length) {
    if (executedCommands.length > 1) {
      text = 'Applying multiple commands';
    } else {
      const commandDescription = executedCommands[0].description.toLowerCase();
      text = `Applying command to ${commandDescription}`;
    }
  }

  return text;
};

export const hasQuickActions = note => REGEX_QUICK_ACTIONS.test(note);

Fatih Acet's avatar
Fatih Acet committed
32 33
export const stripQuickActions = note =>
  note.replace(REGEX_QUICK_ACTIONS, '').trim();