diff --git a/src/EditorFactory.js b/src/EditorFactory.js index 9ac41d3acd2..50015b72160 100644 --- a/src/EditorFactory.js +++ b/src/EditorFactory.js @@ -42,6 +42,7 @@ const createRichEditor = ({ connection, relativePath, isEmbedded = false, + mentionSearch = undefined, noLazyImages = false, } = {}) => { return new Editor({ @@ -51,6 +52,7 @@ const createRichEditor = ({ connection, relativePath, isEmbedded, + mentionSearch, noLazyImages, }), FocusTrap, diff --git a/src/components/Editor.vue b/src/components/Editor.vue index e9794bb4c26..c2d5386bf35 100644 --- a/src/components/Editor.vue +++ b/src/components/Editor.vue @@ -88,13 +88,18 @@ import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus' import { File } from '@nextcloud/files' import { Collaboration } from '@tiptap/extension-collaboration' import { useElementSize } from '@vueuse/core' -import { defineComponent, ref, shallowRef, watch } from 'vue' +import { defineComponent, inject, ref, shallowRef, watch } from 'vue' import { Doc } from 'yjs' import Autofocus from '../extensions/Autofocus.js' import { provideEditor } from '../composables/useEditor.ts' import { provideEditorFlags } from '../composables/useEditorFlags.ts' -import { ATTACHMENT_RESOLVER, FILE, IS_MOBILE } from './Editor.provider.ts' +import { + ATTACHMENT_RESOLVER, + FILE, + HOOK_MENTION_SEARCH, + IS_MOBILE, +} from './Editor.provider.ts' import ReadonlyBar from './Menu/ReadonlyBar.vue' import { generateRemoteUrl } from '@nextcloud/router' @@ -248,12 +253,14 @@ export default defineComponent({ Collaboration.configure({ document: ydoc }), CollaborationCursor.configure({ provider: { awareness } }), ] + const mentionSearch = inject(HOOK_MENTION_SEARCH) const editor = isRichEditor ? createRichEditor({ connection, relativePath: props.relativePath, extensions, isEmbedded: props.isEmbedded, + mentionSearch, noLazyImages: props.noLazyImages, }) : createPlainEditor({ language, extensions }) diff --git a/src/components/Suggestion/Mention/suggestions.js b/src/components/Suggestion/Mention/suggestions.js index bdaf567116b..57e357b6a20 100644 --- a/src/components/Suggestion/Mention/suggestions.js +++ b/src/components/Suggestion/Mention/suggestions.js @@ -11,8 +11,26 @@ export default ({ connection, options }) => createSuggestions({ listComponent: MentionList, items: async ({ query }) => { - const users = await getUsers(query, { connection }) - return Object.entries(users).map(([id, label]) => ({ id, label })) + let customUsers = {} + if (typeof options?.mentionSearch === 'function') { + customUsers = await options.mentionSearch(query) + } + + const entries = Object.entries(customUsers).map(([id, label]) => ({ + id, + label, + })) + if (entries.length >= 6) { + return entries + } + + const serverUsers = await getUsers(query, { connection }) + const serverEntries = Object.entries(serverUsers) + .filter(([id]) => !(id in customUsers)) + .map(([id, label]) => ({ id, label })) + .slice(0, 6 - entries.length) + + return [...entries, ...serverEntries] }, command: ({ editor, range, props }) => { diff --git a/src/editor.js b/src/editor.js index 9467b286910..fb7dad95e38 100644 --- a/src/editor.js +++ b/src/editor.js @@ -255,7 +255,7 @@ window.OCA.Text.createEditor = async function ({ onUpdate = ({ markdown }) => {}, onOutlineToggle = (visible) => {}, onFileInsert = undefined, - onMentionSearch = undefined, + onMentionSearch = undefined, // (query) => Promise<{ [id]: label }> onMentionInsert = undefined, openLinkHandler = undefined, onSearch = undefined, @@ -282,8 +282,8 @@ window.OCA.Text.createEditor = async function ({ return { [ACTION_ATTACHMENT_PROMPT]: onFileInsert, [EDITOR_UPLOAD]: !!sessionEditor, - [HOOK_MENTION_SEARCH]: sessionEditor ? true : onMentionSearch, - [HOOK_MENTION_INSERT]: sessionEditor ? true : onMentionInsert, + [HOOK_MENTION_SEARCH]: onMentionSearch, + [HOOK_MENTION_INSERT]: onMentionInsert, [OPEN_LINK_HANDLER]: { openLink: openLinkHandler || openLink, }, diff --git a/src/extensions/RichText.js b/src/extensions/RichText.js index 06d262d8b45..2a1a07a6374 100644 --- a/src/extensions/RichText.js +++ b/src/extensions/RichText.js @@ -64,6 +64,7 @@ export default Extension.create({ extensions: [], relativePath: null, isEmbedded: false, + mentionSearch: undefined, noLazyImages: false, } }, @@ -112,6 +113,9 @@ export default Extension.create({ Mention.configure({ suggestion: MentionSuggestion({ connection: this.options.connection, + options: { + mentionSearch: this.options.mentionSearch, + }, }), }), Search,