diff --git a/open-isle-cli/src/utils/user.js b/open-isle-cli/src/utils/user.js new file mode 100644 index 000000000..a7469067e --- /dev/null +++ b/open-isle-cli/src/utils/user.js @@ -0,0 +1,30 @@ +import { API_BASE_URL } from '../main' + +export async function fetchFollowings(username) { + if (!username) return [] + try { + const res = await fetch(`${API_BASE_URL}/api/users/${username}/following`) + return res.ok ? await res.json() : [] + } catch (e) { + return [] + } +} + +export async function fetchAdmins() { + try { + const res = await fetch(`${API_BASE_URL}/api/users/admins`) + return res.ok ? await res.json() : [] + } catch (e) { + return [] + } +} + +export async function searchUsers(keyword) { + if (!keyword) return [] + try { + const res = await fetch(`${API_BASE_URL}/api/search/users?keyword=${encodeURIComponent(keyword)}`) + return res.ok ? await res.json() : [] + } catch (e) { + return [] + } +} diff --git a/open-isle-cli/src/utils/vditor.js b/open-isle-cli/src/utils/vditor.js index 2a46a0a52..3b1a5b755 100644 --- a/open-isle-cli/src/utils/vditor.js +++ b/open-isle-cli/src/utils/vditor.js @@ -1,7 +1,8 @@ import Vditor from 'vditor' import 'vditor/dist/index.css' import { API_BASE_URL } from '../main' -import { getToken } from './auth' +import { getToken, authState } from './auth' +import { searchUsers, fetchFollowings, fetchAdmins } from './user' export function getEditorTheme() { return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic' @@ -19,11 +20,38 @@ export function createVditor(editorId, options = {}) { after } = options + const fetchMentions = async (value) => { + if (!value) { + const [followings, admins] = await Promise.all([ + fetchFollowings(authState.username), + fetchAdmins() + ]) + const combined = [...followings, ...admins] + const seen = new Set() + return combined.filter(u => { + if (seen.has(u.id)) return false + seen.add(u.id) + return true + }) + } + return searchUsers(value) + } + return new Vditor(editorId, { placeholder, height: 'auto', theme: getEditorTheme(), preview: Object.assign({ theme: { current: getPreviewTheme() } }, preview), + hint: { + delay: 200, + at: async (value) => { + const list = await fetchMentions(value) + return list.map(u => ({ + value: u.username, + html: `@${u.username}` + })) + } + }, cdn: 'https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/vditor', toolbar: [ 'emoji', diff --git a/open-isle-cli/src/views/MessagePageView.vue b/open-isle-cli/src/views/MessagePageView.vue index 93b7c64d2..675fbfc6f 100644 --- a/open-isle-cli/src/views/MessagePageView.vue +++ b/open-isle-cli/src/views/MessagePageView.vue @@ -147,6 +147,29 @@ + +