2017-07-25 15:01:53 -04:00
|
|
|
import AjaxCache from '~/lib/utils/ajax_cache';
|
2019-01-22 07:35:28 -05:00
|
|
|
import { trimFirstCharOfLineContent } from '~/diffs/store/utils';
|
2019-05-01 11:54:18 -04:00
|
|
|
import { sprintf, __ } from '~/locale';
|
2020-06-25 11:08:37 -04:00
|
|
|
import createGqClient, { fetchPolicies } from '~/lib/graphql';
|
2017-07-25 15:01:53 -04:00
|
|
|
|
2019-05-06 16:06:00 -04:00
|
|
|
// factory function because global flag makes RegExp stateful
|
|
|
|
const createQuickActionsRegex = () => /^\/\w+.*$/gm;
|
2017-07-25 15:01:53 -04:00
|
|
|
|
2018-08-29 07:37:12 -04:00
|
|
|
export const findNoteObjectById = (notes, id) => notes.filter(n => n.id === id)[0];
|
2017-07-25 15:01:53 -04:00
|
|
|
|
2018-03-16 16:16:21 -04:00
|
|
|
export const getQuickActionText = note => {
|
2019-05-01 11:54:18 -04:00
|
|
|
let text = __('Applying command');
|
2018-08-29 07:37:12 -04:00
|
|
|
const quickActions = AjaxCache.get(gl.GfmAutoComplete.dataSources.commands) || [];
|
2017-07-25 15:01:53 -04:00
|
|
|
|
2018-03-16 16:16:21 -04:00
|
|
|
const executedCommands = quickActions.filter(command => {
|
2017-07-25 15:01:53 -04:00
|
|
|
const commandRegex = new RegExp(`/${command.name}`);
|
|
|
|
return commandRegex.test(note);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (executedCommands && executedCommands.length) {
|
|
|
|
if (executedCommands.length > 1) {
|
2019-05-01 11:54:18 -04:00
|
|
|
text = __('Applying multiple commands');
|
2017-07-25 15:01:53 -04:00
|
|
|
} else {
|
|
|
|
const commandDescription = executedCommands[0].description.toLowerCase();
|
2019-08-05 10:31:40 -04:00
|
|
|
text = sprintf(__('Applying command to %{commandDescription}'), { commandDescription });
|
2017-07-25 15:01:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2019-05-06 16:06:00 -04:00
|
|
|
export const hasQuickActions = note => createQuickActionsRegex().test(note);
|
2017-07-25 15:01:53 -04:00
|
|
|
|
2019-05-06 16:06:00 -04:00
|
|
|
export const stripQuickActions = note => note.replace(createQuickActionsRegex(), '').trim();
|
2019-01-22 07:35:28 -05:00
|
|
|
|
|
|
|
export const prepareDiffLines = diffLines =>
|
|
|
|
diffLines.map(line => ({ ...trimFirstCharOfLineContent(line) }));
|
2020-06-25 11:08:37 -04:00
|
|
|
|
|
|
|
export const gqClient = createGqClient(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
fetchPolicy: fetchPolicies.NO_CACHE,
|
|
|
|
},
|
|
|
|
);
|