You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Leaving this here as a rough start to writing the docs for adding mention support:
We are going to be @mentioning Users, who have names.
The Rails Stuff
Let's do the railsy stuff first, and update our User model to be quack like ActionText::Attachments
# app/models/user.rbclassUser < ApplicationRecord# Used when editingdefto_trix_content_attachment_partial_path"user_mentions/trix_content_attachment"end# Used when displayingdefto_attachable_partial_path"user_mentions/attachable"end# A custom content type for easy queryingdefattachable_content_type"application/vnd.active_record.user"endend
Those two partials are used by action text to render the attachment in two places: First within the editor:
We also monkey patch ActionText to export these attachments as span tags, instead of figure tags. This is because TipTap really wants to convert figures into their own blocks, and not display them inline (which we really want for our mentions).
# app/controllers/user_mentions_controller.rbclassUserMentionsController < ApplicationControllerdefindex# Use your own search logic here, but something like@q=User.ransack({name_cont: params[:query]})@users=@q.result.distinct.limit(5)respond_todo |format|
format.jsonendendend
You can use this extension directly if you want. Or (in our case) if you need to have more than one mention functionality, you can extend it. For example, you could @mention users, and #mention tags:
// app/javascript/rhino-editor/extensions/UserMention.jsimportActionTextAttachmentMentionfrom'./ActionTextAttachmentMention'import{PluginKey}from'@tiptap/pm/state'importSuggestionfrom'@tiptap/suggestion'// When using multiple mention extensions at the same time// you must make two things unique:// * The name of the extension// * The pluginKey for the Suggestion plugin// // We do that here.// Everything else is configured at a higher level.constUserMention=ActionTextAttachmentMention.extend({name: 'UserMention',addProseMirrorPlugins(){return[Suggestion({editor: this.editor,pluginKey: newPluginKey('UserMentionSuggestion'),
...this.options.suggestion,}),]},})exportdefaultUserMention
All that's left is to configure the extension with your editor:
Leaving this here as a rough start to writing the docs for adding mention support:
We are going to be @mentioning Users, who have names.
The Rails Stuff
Let's do the railsy stuff first, and update our User model to be quack like ActionText::Attachments
Those two partials are used by action text to render the attachment in two places: First within the editor:
Second, when rendered within the application in our views. In this case: we want to display a link to the user (but this could be whatever you want)
We also monkey patch ActionText to export these attachments as
span
tags, instead offigure
tags. This is because TipTap really wants to convert figures into their own blocks, and not display them inline (which we really want for our mentions).Now let's build the endpoint for for searching users. Routes first:
And the controller:
With views that renders the JSON:
With the three important bits that our attachments care about:
Storing the Attachments
We are going to store the attachments in the database. Create a model that looks like:
And then we hook into ActionText to maintain these records everytime an ActionText is created/updated:
And finally, a little CSS:
The JavaScript
We didn't use Vue or React, but rather a custom
LitElements
. Make sure you enable decorators in your build environmentOur final view component looks like:
We extend the Mention extension to integrate with Action Text Attachments.
You can use this extension directly if you want. Or (in our case) if you need to have more than one mention functionality, you can extend it. For example, you could
@mention
users, and#mention
tags:All that's left is to configure the extension with your editor:
Note: We had to disable some of Rhino's default extensions due to #112
The text was updated successfully, but these errors were encountered: