Sha256: f0a0fb263136dfa4260f4f8d4d6abb299d79aa7f31a1d299de3732e698d0a5ad

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

import Mention from "@tiptap/extension-mention";

import { createSuggestionRenderer, createNodeView } from "src/decidim/editor/common/suggestion";

const searchUsers = async (queryText) => {
  const query = `{
    users(filter: { wildcard: "${queryText}" }) {
      nickname,
      name,
      avatarUrl,
      __typename,
      ...on UserGroup { membersCount }
    }
  }`;


  return fetch("/api", {
    method: "POST",
    cache: "no-cache",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ query })
  }).then((response) => {
    if (response.ok) {
      return response.json();
    }
    throw new Error("Could not retrieve data");
  }).then((json) => (json.data.users || []));
};

export default Mention.extend({
  addOptions() {
    const parentOptions = this.parent?.();

    return {
      ...parentOptions,
      renderLabel({ node }) {
        // The labels are formed based on the nicknames returned by the API
        // which already contain the suggestion character, so there is no need
        // to display it twice.
        return `${node.attrs.label ?? node.attrs.id}`
      },
      suggestion: {
        ...parentOptions?.suggestion,
        allowSpaces: true,
        items: async ({ query }) => {
          if (query.length < 2) {
            return [];
          }

          const data = await searchUsers(query);
          const sorted = data.sort((user) => user.nickname.slice(1));
          return sorted.slice(0, 5);
        },
        render: createSuggestionRenderer(this, {
          itemConverter: (user) => {
            return { id: user.nickname, label: `${user.nickname} (${user.name})` }
          }
        })
      }
    };
  },

  addNodeView() {
    return createNodeView(this);
  }
});

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-core-0.28.0 app/packs/src/decidim/editor/extensions/mention/index.js
decidim-core-0.28.0.rc5 app/packs/src/decidim/editor/extensions/mention/index.js
decidim-core-0.28.0.rc4 app/packs/src/decidim/editor/extensions/mention/index.js