debian-mirror-gitlab/app/assets/javascripts/notes/stores/utils.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-04-17 20:07:23 +05:30
import { trimFirstCharOfLineContent } from '~/diffs/store/utils'; // eslint-disable-line import/no-deprecated
2020-07-28 23:09:34 +05:30
import createGqClient, { fetchPolicies } from '~/lib/graphql';
2021-03-11 19:13:27 +05:30
import AjaxCache from '~/lib/utils/ajax_cache';
import { sprintf, __ } from '~/locale';
2018-03-17 18:26:18 +05:30
2019-09-04 21:01:54 +05:30
// factory function because global flag makes RegExp stateful
const createQuickActionsRegex = () => /^\/\w+.*$/gm;
2018-03-17 18:26:18 +05:30
2021-03-08 18:12:59 +05:30
export const findNoteObjectById = (notes, id) => notes.filter((n) => n.id === id)[0];
2018-03-17 18:26:18 +05:30
2021-03-08 18:12:59 +05:30
export const getQuickActionText = (note) => {
2019-07-31 22:56:46 +05:30
let text = __('Applying command');
2018-11-20 20:47:30 +05:30
const quickActions = AjaxCache.get(gl.GfmAutoComplete.dataSources.commands) || [];
2018-03-17 18:26:18 +05:30
2021-03-08 18:12:59 +05:30
const executedCommands = quickActions.filter((command) => {
2018-03-17 18:26:18 +05:30
const commandRegex = new RegExp(`/${command.name}`);
return commandRegex.test(note);
});
if (executedCommands && executedCommands.length) {
if (executedCommands.length > 1) {
2019-07-31 22:56:46 +05:30
text = __('Applying multiple commands');
2018-03-17 18:26:18 +05:30
} else {
const commandDescription = executedCommands[0].description.toLowerCase();
2019-10-12 21:52:04 +05:30
text = sprintf(__('Applying command to %{commandDescription}'), { commandDescription });
2018-03-17 18:26:18 +05:30
}
}
return text;
};
2021-03-08 18:12:59 +05:30
export const hasQuickActions = (note) => createQuickActionsRegex().test(note);
2018-03-17 18:26:18 +05:30
2021-03-08 18:12:59 +05:30
export const stripQuickActions = (note) => note.replace(createQuickActionsRegex(), '').trim();
2019-03-02 22:35:43 +05:30
2021-03-08 18:12:59 +05:30
export const prepareDiffLines = (diffLines) =>
2021-04-17 20:07:23 +05:30
diffLines.map((line) => ({ ...trimFirstCharOfLineContent(line) })); // eslint-disable-line import/no-deprecated
2020-07-28 23:09:34 +05:30
export const gqClient = createGqClient(
{},
{
fetchPolicy: fetchPolicies.NO_CACHE,
},
);