diff --git a/app/assets/javascripts/awards_handler.js b/app/assets/javascripts/awards_handler.js index 288acd1b2f1..ab95e00b740 100644 --- a/app/assets/javascripts/awards_handler.js +++ b/app/assets/javascripts/awards_handler.js @@ -560,7 +560,7 @@ export class AwardsHandler { } findMatchingEmojiElements(query) { - const emojiMatches = this.emoji.searchEmoji(query, { match: 'fuzzy' }).map(({ name }) => name); + const emojiMatches = this.emoji.searchEmoji(query).map((x) => x.emoji.name); const $emojiElements = $('.emoji-menu-list:not(.frequent-emojis) [data-name]'); const $matchingElements = $emojiElements.filter( (i, elm) => emojiMatches.indexOf(elm.dataset.name) >= 0, diff --git a/app/assets/javascripts/behaviors/gl_emoji.js b/app/assets/javascripts/behaviors/gl_emoji.js index 0553990ecd8..65a6d1388e7 100644 --- a/app/assets/javascripts/behaviors/gl_emoji.js +++ b/app/assets/javascripts/behaviors/gl_emoji.js @@ -1,6 +1,12 @@ import 'document-register-element'; import isEmojiUnicodeSupported from '../emoji/support'; -import { initEmojiMap, getEmojiInfo, emojiFallbackImageSrc, emojiImageTag } from '../emoji'; +import { + initEmojiMap, + getEmojiInfo, + emojiFallbackImageSrc, + emojiImageTag, + FALLBACK_EMOJI_KEY, +} from '../emoji'; class GlEmoji extends HTMLElement { connectedCallback() { @@ -17,7 +23,7 @@ class GlEmoji extends HTMLElement { if (emojiInfo) { if (name !== emojiInfo.name) { - if (emojiInfo.fallback && this.innerHTML) { + if (emojiInfo.name === FALLBACK_EMOJI_KEY && this.innerHTML) { return; // When fallback emoji is used, but there is a provided, use the instead } diff --git a/app/assets/javascripts/captcha/captcha_modal.vue b/app/assets/javascripts/captcha/captcha_modal.vue new file mode 100644 index 00000000000..00ea06db0cf --- /dev/null +++ b/app/assets/javascripts/captcha/captcha_modal.vue @@ -0,0 +1,110 @@ + + diff --git a/app/assets/javascripts/captcha/init_recaptcha_script.js b/app/assets/javascripts/captcha/init_recaptcha_script.js index b9df7604ed1..f546eef7d84 100644 --- a/app/assets/javascripts/captcha/init_recaptcha_script.js +++ b/app/assets/javascripts/captcha/init_recaptcha_script.js @@ -28,11 +28,11 @@ export const initRecaptchaScript = memoize(() => { return new Promise((resolve) => { // This global callback resolves the Promise and is passed by name to the reCAPTCHA script. - window[RECAPTCHA_ONLOAD_CALLBACK_NAME] = (val) => { + window[RECAPTCHA_ONLOAD_CALLBACK_NAME] = () => { // Let's clean up after ourselves. This is also important for testing, because `window` is NOT cleared between tests. // https://github.com/facebook/jest/issues/1224#issuecomment-444586798. delete window[RECAPTCHA_ONLOAD_CALLBACK_NAME]; - resolve(val); + resolve(window.grecaptcha); }; appendRecaptchaScript(); }); diff --git a/app/assets/javascripts/emoji/index.js b/app/assets/javascripts/emoji/index.js index 8deb6f59e5d..73820b4e429 100644 --- a/app/assets/javascripts/emoji/index.js +++ b/app/assets/javascripts/emoji/index.js @@ -1,10 +1,11 @@ -import fuzzaldrinPlus from 'fuzzaldrin-plus'; +import { escape, minBy } from 'lodash'; import emojiAliases from 'emojis/aliases.json'; import axios from '../lib/utils/axios_utils'; import AccessorUtilities from '../lib/utils/accessor'; let emojiMap = null; let validEmojiNames = null; +export const FALLBACK_EMOJI_KEY = 'grey_question'; export const EMOJI_VERSION = '1'; @@ -30,23 +31,17 @@ async function loadEmoji() { return data; } +async function loadEmojiWithNames() { + return Object.entries(await loadEmoji()).reduce((acc, [key, value]) => { + acc[key] = { ...value, name: key }; + + return acc; + }, {}); +} + async function prepareEmojiMap() { - emojiMap = await loadEmoji(); - + emojiMap = await loadEmojiWithNames(); validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)]; - - Object.keys(emojiMap).forEach((name) => { - emojiMap[name].aliases = []; - emojiMap[name].name = name; - }); - Object.entries(emojiAliases).forEach(([alias, name]) => { - // This check, `if (name in emojiMap)` is necessary during testing. In - // production, it shouldn't be necessary, because at no point should there - // be an entry in aliases.json with no corresponding entry in emojis.json. - // However, during testing, the endpoint for emojis.json is mocked with a - // small dataset, whereas aliases.json is always `import`ed directly. - if (name in emojiMap) emojiMap[name].aliases.push(alias); - }); } export function initEmojiMap() { @@ -63,156 +58,101 @@ export function getValidEmojiNames() { } export function isEmojiNameValid(name) { - return validEmojiNames.indexOf(name) >= 0; + if (!emojiMap) { + // eslint-disable-next-line @gitlab/require-i18n-strings + throw new Error('The emoji map is uninitialized or initialization has not completed'); + } + + return name in emojiMap || name in emojiAliases; } export function getAllEmoji() { return emojiMap; } -/** - * Retrieves an emoji by name or alias. - * - * Note: `initEmojiMap` must have been called and completed before this method - * can safely be called. - * - * @param {String} query The emoji name - * @param {Boolean} fallback If true, a fallback emoji will be returned if the - * named emoji does not exist. Defaults to false. - * @returns {Object} The matching emoji. - */ -export function getEmoji(query, fallback = false) { - // TODO https://gitlab.com/gitlab-org/gitlab/-/issues/268208 - const fallbackEmoji = emojiMap.grey_question; - if (!query) { - return fallback ? fallbackEmoji : null; - } +function getAliasesMatchingQuery(query) { + return Object.keys(emojiAliases) + .filter((alias) => alias.includes(query)) + .reduce((map, alias) => { + const emojiName = emojiAliases[alias]; + const score = alias.indexOf(query); - if (!emojiMap) { - // eslint-disable-next-line @gitlab/require-i18n-strings - throw new Error('The emoji map is uninitialized or initialization has not completed'); - } + const prev = map.get(emojiName); + // overwrite if we beat the previous score or we're more alphabetical + const shouldSet = + !prev || + prev.score > score || + (prev.score === score && prev.alias.localeCompare(alias) > 0); - const lowercaseQuery = query.toLowerCase(); - const name = normalizeEmojiName(lowercaseQuery); + if (shouldSet) { + map.set(emojiName, { score, alias }); + } - if (name in emojiMap) { - return emojiMap[name]; - } - - return fallback ? fallbackEmoji : null; + return map; + }, new Map()); } -const searchMatchers = { - // Fuzzy matching compares using a fuzzy matching library - fuzzy: (value, query) => { - const score = fuzzaldrinPlus.score(value, query) > 0; - return { score, success: score > 0 }; - }, - // Contains matching compares by indexOf - contains: (value, query) => { - const index = value.indexOf(query.toLowerCase()); - return { index, success: index >= 0 }; - }, - // Exact matching compares by equality - exact: (value, query) => { - return { success: value === query.toLowerCase() }; - }, -}; - -const searchPredicates = { - // Search by name - name: (matcher, query) => (emoji) => { - const m = matcher(emoji.name, query); - return [{ ...m, emoji, field: emoji.name }]; - }, - // Search by alias - alias: (matcher, query) => (emoji) => - emoji.aliases.map((alias) => { - const m = matcher(alias, query); - return { ...m, emoji, field: alias }; - }), - // Search by description - description: (matcher, query) => (emoji) => { - const m = matcher(emoji.d, query); - return [{ ...m, emoji, field: emoji.d }]; - }, - // Search by unicode value (always exact) - unicode: (matcher, query) => (emoji) => { - return [{ emoji, field: emoji.e, success: emoji.e === query }]; - }, -}; - -/** - * Searches emoji by name, aliases, description, and unicode value and returns - * an array of matches. - * - * Behavior is undefined if `opts.fields` is empty or if `opts.match` is fuzzy - * and the query is empty. - * - * Note: `initEmojiMap` must have been called and completed before this method - * can safely be called. - * - * @param {String} query Search query. - * @param {Object} opts Search options (optional). - * @param {String[]} opts.fields Fields to search. Choices are 'name', 'alias', - * 'description', and 'unicode' (value). Default is all (four) fields. - * @param {String} opts.match Search method to use. Choices are 'exact', - * 'contains', or 'fuzzy'. All methods are case-insensitive. Exact matching (the - * default) compares by equality. Contains matching compares by indexOf. Fuzzy - * matching compares using a fuzzy matching library. - * @param {Boolean} opts.fallback If true, a fallback emoji will be returned if - * the result set is empty. Defaults to false. - * @param {Boolean} opts.raw Returns the raw match data instead of just the - * matching emoji. - * @returns {Object[]} A list of emoji that match the query. - */ -export function searchEmoji(query, opts) { - if (!emojiMap) { - // eslint-disable-next-line @gitlab/require-i18n-strings - throw new Error('The emoji map is uninitialized or initialization has not completed'); +function getUnicodeMatch(emoji, query) { + if (emoji.e === query) { + return { score: 0, field: 'e', fieldValue: emoji.name, emoji }; } - const { - fields = ['name', 'alias', 'description', 'unicode'], - match = 'exact', - fallback = false, - raw = false, - } = opts || {}; + return null; +} - const fallbackEmoji = emojiMap.grey_question; - - if (fallbackEmoji) { - fallbackEmoji.fallback = true; +function getDescriptionMatch(emoji, query) { + if (emoji.d.includes(query)) { + return { score: emoji.d.indexOf(query), field: 'd', fieldValue: emoji.d, emoji }; } - if (!query) { - if (fallback) { - return raw ? [{ emoji: fallbackEmoji }] : [fallbackEmoji]; - } + return null; +} - return []; +function getAliasMatch(emoji, matchingAliases) { + if (matchingAliases.has(emoji.name)) { + const { score, alias } = matchingAliases.get(emoji.name); + + return { score, field: 'alias', fieldValue: alias, emoji }; } - // optimization for an exact match in name and alias - if (match === 'exact' && new Set([...fields, 'name', 'alias']).size === 2) { - const emoji = getEmoji(query, fallback); - return emoji ? [emoji] : []; + return null; +} + +function getNameMatch(emoji, query) { + if (emoji.name.includes(query)) { + return { + score: emoji.name.indexOf(query), + field: 'name', + fieldValue: emoji.name, + emoji, + }; } - const matcher = searchMatchers[match] || searchMatchers.exact; - const predicates = fields.map((f) => searchPredicates[f](matcher, query)); + return null; +} - const results = Object.values(emojiMap) - .flatMap((emoji) => predicates.flatMap((predicate) => predicate(emoji))) - .filter((r) => r.success); +export function searchEmoji(query) { + const lowercaseQuery = query ? `${query}`.toLowerCase() : ''; - // Fallback to question mark for unknown emojis - if (fallback && results.length === 0) { - return raw ? [{ emoji: fallbackEmoji }] : [fallbackEmoji]; - } + const matchingAliases = getAliasesMatchingQuery(lowercaseQuery); - return raw ? results : results.map((r) => r.emoji); + return Object.values(emojiMap) + .map((emoji) => { + const matches = [ + getUnicodeMatch(emoji, query), + getDescriptionMatch(emoji, lowercaseQuery), + getAliasMatch(emoji, matchingAliases), + getNameMatch(emoji, lowercaseQuery), + ].filter(Boolean); + + return minBy(matches, (x) => x.score); + }) + .filter(Boolean); +} + +export function sortEmoji(items) { + // Sort results by index of and string comparison + return [...items].sort((a, b) => a.score - b.score || a.fieldValue.localeCompare(b.fieldValue)); } let emojiCategoryMap; @@ -238,11 +178,28 @@ export function getEmojiCategoryMap() { return emojiCategoryMap; } -export function getEmojiInfo(query) { - return searchEmoji(query, { - fields: ['name', 'alias'], - fallback: true, - })[0]; +/** + * Retrieves an emoji by name + * + * @param {String} query The emoji name + * @param {Boolean} fallback If true, a fallback emoji will be returned if the + * named emoji does not exist. + * @returns {Object} The matching emoji. + */ +export function getEmojiInfo(query, fallback = true) { + if (!emojiMap) { + // eslint-disable-next-line @gitlab/require-i18n-strings + throw new Error('The emoji map is uninitialized or initialization has not completed'); + } + + const lowercaseQuery = query ? `${query}`.toLowerCase() : ''; + const name = normalizeEmojiName(lowercaseQuery); + + if (name in emojiMap) { + return emojiMap[name]; + } + + return fallback ? emojiMap[FALLBACK_EMOJI_KEY] : null; } export function emojiFallbackImageSrc(inputName) { @@ -262,12 +219,8 @@ export function glEmojiTag(inputName, options) { const fallbackSpriteClass = `emoji-${name}`; const fallbackSpriteAttribute = opts.sprite - ? `data-fallback-sprite-class="${fallbackSpriteClass}"` + ? `data-fallback-sprite-class="${escape(fallbackSpriteClass)}" ` : ''; - return ` - - `; + return ``; } diff --git a/app/assets/javascripts/gfm_auto_complete.js b/app/assets/javascripts/gfm_auto_complete.js index febb108ec71..949540d38d4 100644 --- a/app/assets/javascripts/gfm_auto_complete.js +++ b/app/assets/javascripts/gfm_auto_complete.js @@ -190,59 +190,43 @@ class GfmAutoComplete { } setupEmoji($input) { - const self = this; - const { filter, ...defaults } = this.getDefaultCallbacks(); + const fetchData = this.fetchData.bind(this); // Emoji $input.atwho({ at: ':', - displayTpl(value) { - let tmpl = GfmAutoComplete.Loading.template; - if (value && value.name) { - tmpl = GfmAutoComplete.Emoji.templateFunction(value.name); - } - return tmpl; - }, + displayTpl: GfmAutoComplete.Emoji.templateFunction, insertTpl: GfmAutoComplete.Emoji.insertTemplateFunction, skipSpecialCharacterTest: true, data: GfmAutoComplete.defaultLoadingData, callbacks: { - ...defaults, + ...this.getDefaultCallbacks(), matcher(flag, subtext) { const regexp = new RegExp(`(?:[^${glRegexp.unicodeLetters}0-9:]|\n|^):([^:]*)$`, 'gi'); const match = regexp.exec(subtext); return match && match.length ? match[1] : null; }, - filter(query, items, searchKey) { - const filtered = filter.call(this, query, items, searchKey); - if (query.length === 0 || GfmAutoComplete.isLoading(items)) { - return filtered; + filter(query, items) { + if (GfmAutoComplete.isLoading(items)) { + fetchData(this.$inputor, this.at); + return items; } - // map from value to " is of ", arranged by emoji - const emojis = {}; - filtered.forEach(({ name: value }) => { - self.emojiLookup[value].forEach(({ emoji: { name }, kind }) => { - let entry = emojis[name]; - if (!entry) { - entry = {}; - emojis[name] = entry; - } - if (!(kind in entry) || value.localeCompare(entry[kind]) < 0) { - entry[kind] = value; - } - }); - }); + return GfmAutoComplete.Emoji.filter(query); + }, + sorter(query, items) { + this.setting.highlightFirst = this.setting.alwaysHighlightFirst || query.length > 0; + if (GfmAutoComplete.isLoading(items)) { + this.setting.highlightFirst = false; + return items; + } - // collate results to list, prefering name > unicode > alias > description - const results = []; - Object.values(emojis).forEach(({ name, unicode, alias, description }) => { - results.push(name || unicode || alias || description); - }); + if (query.length === 0) { + return items; + } - // return to the form atwho wants - return results.map((name) => ({ name })); + return GfmAutoComplete.Emoji.sorter(items); }, }, }); @@ -674,32 +658,7 @@ class GfmAutoComplete { async loadEmojiData($input, at) { await Emoji.initEmojiMap(); - // All the emoji - const emojis = Emoji.getAllEmoji(); - - // Add all of the fields to atwho's database - this.loadData($input, at, [ - ...Object.keys(emojis), // Names - ...Object.values(emojis).flatMap(({ aliases }) => aliases), // Aliases - ...Object.values(emojis).map(({ e }) => e), // Unicode values - ...Object.values(emojis).map(({ d }) => d), // Descriptions - ]); - - // Construct a lookup that can correlate a value to " is the of " - const lookup = {}; - const add = (key, kind, emoji) => { - if (!(key in lookup)) { - lookup[key] = []; - } - lookup[key].push({ kind, emoji }); - }; - Object.values(emojis).forEach((emoji) => { - add(emoji.name, 'name', emoji); - add(emoji.d, 'description', emoji); - add(emoji.e, 'unicode', emoji); - emoji.aliases.forEach((a) => add(a, 'alias', emoji)); - }); - this.emojiLookup = lookup; + this.loadData($input, at, ['loaded']); GfmAutoComplete.glEmojiTag = Emoji.glEmojiTag; } @@ -772,36 +731,38 @@ GfmAutoComplete.typesWithBackendFiltering = ['vulnerabilities']; GfmAutoComplete.isTypeWithBackendFiltering = (type) => GfmAutoComplete.typesWithBackendFiltering.includes(GfmAutoComplete.atTypeMap[type]); -function findEmoji(name) { - return Emoji.searchEmoji(name, { match: 'contains', raw: true }).sort((a, b) => { - if (a.index !== b.index) { - return a.index - b.index; - } - return a.field.localeCompare(b.field); - }); -} - // Emoji GfmAutoComplete.glEmojiTag = null; GfmAutoComplete.Emoji = { insertTemplateFunction(value) { - const results = findEmoji(value.name); - if (results.length) { - return `:${results[0].emoji.name}:`; - } - return `:${value.name}:`; + return `:${value.emoji.name}:`; }, - templateFunction(name) { - // glEmojiTag helper is loaded on-demand in fetchData() - if (!GfmAutoComplete.glEmojiTag) return `
  • ${name}
  • `; - - const results = findEmoji(name); - if (!results.length) { - return `
  • ${name} ${GfmAutoComplete.glEmojiTag(name)}
  • `; + templateFunction(item) { + if (GfmAutoComplete.isLoading(item)) { + return GfmAutoComplete.Loading.template; } - const { field, emoji } = results[0]; - return `
  • ${field} ${GfmAutoComplete.glEmojiTag(emoji.name)}
  • `; + const escapedFieldValue = escape(item.fieldValue); + if (!GfmAutoComplete.glEmojiTag) { + return `
  • ${escapedFieldValue}
  • `; + } + + return `
  • ${escapedFieldValue} ${GfmAutoComplete.glEmojiTag(item.emoji.name)}
  • `; + }, + filter(query) { + if (query.length === 0) { + return Object.values(Emoji.getAllEmoji()) + .map((emoji) => ({ + emoji, + fieldValue: emoji.name, + })) + .slice(0, 20); + } + + return Emoji.searchEmoji(query); + }, + sorter(items) { + return Emoji.sortEmoji(items); }, }; // Team Members diff --git a/app/assets/javascripts/snippets/components/edit.vue b/app/assets/javascripts/snippets/components/edit.vue index ffb5e242973..629f9b03255 100644 --- a/app/assets/javascripts/snippets/components/edit.vue +++ b/app/assets/javascripts/snippets/components/edit.vue @@ -32,6 +32,7 @@ export default { SnippetBlobActionsEdit, TitleField, FormFooterActions, + CaptchaModal: () => import('~/captcha/captcha_modal.vue'), GlButton, GlLoadingIcon, }, @@ -66,12 +67,25 @@ export default { description: '', visibilityLevel: this.selectedLevel, }, + captchaResponse: '', + needsCaptchaResponse: false, + captchaSiteKey: '', + spamLogId: '', }; }, computed: { hasBlobChanges() { return this.actions.length > 0; }, + hasNoChanges() { + return ( + this.actions.every( + (action) => !action.content && !action.filePath && !action.previousPath, + ) && + !this.snippet.title && + !this.snippet.description + ); + }, hasValidBlobs() { return this.actions.every((x) => x.content); }, @@ -88,6 +102,8 @@ export default { description: this.snippet.description, visibilityLevel: this.snippet.visibilityLevel, blobActions: this.actions, + ...(this.spamLogId && { spamLogId: this.spamLogId }), + ...(this.captchaResponse && { captchaResponse: this.captchaResponse }), }; }, saveButtonLabel() { @@ -116,7 +132,7 @@ export default { onBeforeUnload(e = {}) { const returnValue = __('Are you sure you want to lose unsaved changes?'); - if (!this.hasBlobChanges || this.isUpdating) return undefined; + if (!this.hasBlobChanges || this.hasNoChanges || this.isUpdating) return undefined; Object.assign(e, { returnValue }); return returnValue; @@ -159,6 +175,13 @@ export default { .then(({ data }) => { const baseObj = this.newSnippet ? data?.createSnippet : data?.updateSnippet; + if (baseObj.needsCaptchaResponse) { + // If we need a captcha response, start process for receiving captcha response. + // We will resubmit after the response is obtained. + this.requestCaptchaResponse(baseObj.captchaSiteKey, baseObj.spamLogId); + return; + } + const errors = baseObj?.errors; if (errors.length) { this.flashAPIFailure(errors[0]); @@ -173,6 +196,35 @@ export default { updateActions(actions) { this.actions = actions; }, + /** + * Start process for getting captcha response from user + * + * @param captchaSiteKey Stored in data and used to display the captcha. + * @param spamLogId Stored in data and included when the form is re-submitted. + */ + requestCaptchaResponse(captchaSiteKey, spamLogId) { + this.captchaSiteKey = captchaSiteKey; + this.spamLogId = spamLogId; + this.needsCaptchaResponse = true; + }, + /** + * Handle the captcha response from the user + * + * @param captchaResponse The captchaResponse value emitted from the modal. + */ + receivedCaptchaResponse(captchaResponse) { + this.needsCaptchaResponse = false; + this.captchaResponse = captchaResponse; + + if (this.captchaResponse) { + // If the user solved the captcha resubmit the form. + this.handleFormSubmit(); + } else { + // If the user didn't solve the captcha (e.g. they just closed the modal), + // finish the update and allow them to continue editing or manually resubmit the form. + this.isUpdating = false; + } + }, }, }; @@ -190,6 +242,11 @@ export default { class="loading-animation prepend-top-20 gl-mb-6" />