Skip to content

Commit 3e8a7ed

Browse files
authored
Merge pull request #8422 from nextcloud/backport/8371/stable33
[stable33] fix(editorApi): use onMentionSearch callback if provided
2 parents 08cae3e + 7ee0667 commit 3e8a7ed

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,18 @@ 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
9292
import { provideEditor } from '../composables/useEditor.ts'
9393
import { provideEditorFlags } from '../composables/useEditorFlags.ts'
94-
import { ATTACHMENT_RESOLVER, FILE, IS_MOBILE } from './Editor.provider.ts'
94+
import {
95+
ATTACHMENT_RESOLVER,
96+
FILE,
97+
HOOK_MENTION_SEARCH,
98+
IS_MOBILE,
99+
} from './Editor.provider.ts'
95100
import ReadonlyBar from './Menu/ReadonlyBar.vue'
96101
97102
import { generateRemoteUrl } from '@nextcloud/router'
@@ -239,12 +244,14 @@ export default defineComponent({
239244
Collaboration.configure({ document: ydoc }),
240245
CollaborationCaret.configure({ provider: { awareness } }),
241246
]
247+
const mentionSearch = inject(HOOK_MENTION_SEARCH)
242248
const editor = isRichEditor
243249
? createRichEditor({
244250
connection,
245251
relativePath: props.relativePath,
246252
extensions,
247253
isEmbedded: props.isEmbedded,
254+
mentionSearch,
248255
})
249256
: createPlainEditor({ language, extensions })
250257
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)