From 895378a96bf8a6c7662ed02509c37b8d8a95db0b Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 5 Sep 2023 19:28:36 +0200 Subject: [PATCH 1/5] fix(front): fix fetching and rendering profile mentions and fetching tags Signed-off-by: Thomas Citharel --- js/src/components/Editor/Mention.ts | 53 ++++++++------- js/src/components/Editor/MentionList.vue | 83 +++++++++--------------- js/src/components/TextEditor.vue | 4 ++ js/src/composition/apollo/tags.ts | 31 ++++----- js/src/views/Event/EventView.vue | 4 ++ 5 files changed, 83 insertions(+), 92 deletions(-) diff --git a/js/src/components/Editor/Mention.ts b/js/src/components/Editor/Mention.ts index b866df575..82c134d4b 100644 --- a/js/src/components/Editor/Mention.ts +++ b/js/src/components/Editor/Mention.ts @@ -2,37 +2,32 @@ import { SEARCH_PERSONS } from "@/graphql/search"; import { VueRenderer } from "@tiptap/vue-3"; import tippy from "tippy.js"; import MentionList from "./MentionList.vue"; -import { apolloClient } from "@/vue-apollo"; +import { apolloClient, waitApolloQuery } from "@/vue-apollo"; import { IPerson } from "@/types/actor"; import pDebounce from "p-debounce"; import { MentionOptions } from "@tiptap/extension-mention"; import { Editor } from "@tiptap/core"; import { provideApolloClient, useQuery } from "@vue/apollo-composable"; import { Paginate } from "@/types/paginate"; -import { onError } from "@apollo/client/link/error"; -const fetchItems = (query: string): Promise => { - return new Promise((resolve, reject) => { - const { onResult } = provideApolloClient(apolloClient)(() => { - return useQuery<{ searchPersons: Paginate }>( - SEARCH_PERSONS, - () => ({ - variables: { - searchText: query, - }, - }) - ); - }); - - onResult(({ data }) => { - resolve(data.searchPersons.elements); - }); - - onError(reject); - }); - - // // TipTap doesn't handle async for onFilter, hence the following line. - // return result.data.searchPersons.elements; +const fetchItems = async (query: string): Promise => { + try { + if (query === "") return []; + const res = await waitApolloQuery( + provideApolloClient(apolloClient)(() => { + return useQuery< + { searchPersons: Paginate }, + { searchText: string } + >(SEARCH_PERSONS, () => ({ + searchText: query, + })); + }) + ); + return res.data.searchPersons.elements; + } catch (e) { + console.error(e); + return []; + } }; const debouncedFetchItems = pDebounce(fetchItems, 200); @@ -68,6 +63,10 @@ const mentionOptions: MentionOptions = { editor: props.editor, }); + if (!props.clientRect) { + return; + } + popup = tippy("body", { getReferenceClientRect: props.clientRect, appendTo: () => document.body, @@ -86,6 +85,12 @@ const mentionOptions: MentionOptions = { }); }, onKeyDown(props: any) { + if (props.event.key === "Escape") { + popup[0].hide(); + + return true; + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return component.ref?.onKeyDown(props); diff --git a/js/src/components/Editor/MentionList.vue b/js/src/components/Editor/MentionList.vue index f22e16530..441cf881f 100644 --- a/js/src/components/Editor/MentionList.vue +++ b/js/src/components/Editor/MentionList.vue @@ -1,8 +1,8 @@