Skip to content

Commit 0a16b00

Browse files
mejo-backportbot[bot]
authored andcommitted
fix(editorApi): use onMentionSearch callback if provided
fix(editorApi): use onMentionSearch callback if provided Required for injecting collective members as prefered users in Collectives. Contributes to #6881 Signed-off-by: Jonas <jonas@freesources.org> [skip ci]
1 parent ed5ed28 commit 0a16b00

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

src/EditorFactory.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ const createRichEditor = ({
4242
connection,
4343
relativePath,
4444
isEmbedded = false,
45+
mentionSearch = undefined,
4546
} = {}) => {
4647
return new Editor({
4748
editorProps,
4849
extensions: [
49-
RichText.configure({ connection, relativePath, isEmbedded }),
50+
RichText.configure({
51+
connection,
52+
relativePath,
53+
isEmbedded,
54+
mentionSearch,
55+
}),
5056
FocusTrap,
5157
...extensions,
5258
],

src/components/Editor.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
8585
import { File } from '@nextcloud/files'
8686
import { Collaboration } from '@tiptap/extension-collaboration'
8787
import { useElementSize } from '@vueuse/core'
88-
import { defineComponent, ref, shallowRef, watch } from 'vue'
88+
import { defineComponent, inject, ref, shallowRef, watch } from 'vue'
8989
import { Doc } from 'yjs'
9090
import Autofocus from '../extensions/Autofocus.js'
9191
@@ -239,12 +239,14 @@ export default defineComponent({
239239
Collaboration.configure({ document: ydoc }),
240240
CollaborationCaret.configure({ provider: { awareness } }),
241241
]
242+
const mentionSearch = inject(HOOK_MENTION_SEARCH)
242243
const editor = isRichEditor
243244
? createRichEditor({
244245
connection,
245246
relativePath: props.relativePath,
246247
extensions,
247248
isEmbedded: props.isEmbedded,
249+
mentionSearch,
248250
})
249251
: createPlainEditor({ language, extensions })
250252
provideEditor(editor)

src/components/Suggestion/Mention/suggestions.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,26 @@ export default ({ connection, options }) =>
1111
createSuggestions({
1212
listComponent: MentionList,
1313
items: async ({ query }) => {
14-
const users = await getUsers(query, { connection })
15-
return Object.entries(users).map(([id, label]) => ({ id, label }))
14+
let customUsers = {}
15+
if (typeof options?.mentionSearch === 'function') {
16+
customUsers = await options.mentionSearch(query)
17+
}
18+
19+
const entries = Object.entries(customUsers).map(([id, label]) => ({
20+
id,
21+
label,
22+
}))
23+
if (entries.length >= 6) {
24+
return entries
25+
}
26+
27+
const serverUsers = await getUsers(query, { connection })
28+
const serverEntries = Object.entries(serverUsers)
29+
.filter(([id]) => !(id in customUsers))
30+
.map(([id, label]) => ({ id, label }))
31+
.slice(0, 6 - entries.length)
32+
33+
return [...entries, ...serverEntries]
1634
},
1735

1836
command: ({ editor, range, props }) => {

src/editor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ window.OCA.Text.createEditor = async function ({
259259
onOutlineToggle = (visible) => {}, // deprecated, use `onTocToggle`
260260
onTocPin = (fileId, keep) => {},
261261
onFileInsert = undefined,
262-
onMentionSearch = undefined,
262+
onMentionSearch = undefined, // (query) => Promise<{ [id]: label }>
263263
onMentionInsert = undefined,
264264
openLinkHandler = undefined,
265265
onSearch = undefined,
@@ -283,8 +283,8 @@ window.OCA.Text.createEditor = async function ({
283283
return {
284284
[ACTION_ATTACHMENT_PROMPT]: onFileInsert,
285285
[EDITOR_UPLOAD]: !!sessionEditor,
286-
[HOOK_MENTION_SEARCH]: sessionEditor ? true : onMentionSearch,
287-
[HOOK_MENTION_INSERT]: sessionEditor ? true : onMentionInsert,
286+
[HOOK_MENTION_SEARCH]: onMentionSearch,
287+
[HOOK_MENTION_INSERT]: onMentionInsert,
288288
[OPEN_LINK_HANDLER]: {
289289
openLink: openLinkHandler || openLink,
290290
},

src/extensions/RichText.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default Extension.create({
6060
extensions: [],
6161
relativePath: null,
6262
isEmbedded: false,
63+
mentionSearch: undefined,
6364
}
6465
},
6566

@@ -107,6 +108,9 @@ export default Extension.create({
107108
Mention.configure({
108109
suggestion: MentionSuggestion({
109110
connection: this.options.connection,
111+
options: {
112+
mentionSearch: this.options.mentionSearch,
113+
},
110114
}),
111115
}),
112116
Search,

0 commit comments

Comments
 (0)