2021-02-18 13:10:41 -05:00
|
|
|
import { trimFirstCharOfLineContent } from '~/diffs/store/utils'; // eslint-disable-line import/no-deprecated
|
2020-06-25 11:08:37 -04:00
|
|
|
import createGqClient, { fetchPolicies } from '~/lib/graphql';
|
2021-02-14 13:09:20 -05:00
|
|
|
import AjaxCache from '~/lib/utils/ajax_cache';
|
|
|
|
import { sprintf, __ } from '~/locale';
|
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
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
export const findNoteObjectById = (notes, id) => notes.filter((n) => n.id === id)[0];
|
2017-07-25 15:01:53 -04:00
|
|
|
|
2020-12-23 16:10:24 -05: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
|
|
|
|
2020-12-23 16:10:24 -05: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;
|
|
|
|
};
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
export const hasQuickActions = (note) => createQuickActionsRegex().test(note);
|
2017-07-25 15:01:53 -04:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
export const stripQuickActions = (note) => note.replace(createQuickActionsRegex(), '').trim();
|
2019-01-22 07:35:28 -05:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
export const prepareDiffLines = (diffLines) =>
|
2021-02-18 13:10:41 -05:00
|
|
|
diffLines.map((line) => ({ ...trimFirstCharOfLineContent(line) })); // eslint-disable-line import/no-deprecated
|
2020-06-25 11:08:37 -04:00
|
|
|
|
|
|
|
export const gqClient = createGqClient(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
fetchPolicy: fetchPolicies.NO_CACHE,
|
|
|
|
},
|
|
|
|
);
|