-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mention: Add text attribute #1322
Conversation
👍 looking for such an option |
@tomhrtly Do you know if there is any option to override the current mention render value? because i am returning an object as user. Basically instead of
i like to return
So its not possible to configure |
@nadar Can you not pass the specific value you need into the data attributes that already exist? I'm not sure exactly what you're trying to achieve |
I think i am trying to do the same as you, maintain a NAME and an ID. But then display the NAME of the user. Sorry i don't want to be off-topic in your PR. renderText() decides what should be rendered as value inside the element, and in my case the selection returns an object with NAME (or TEXT) and ID |
i know its still off-topic, but i found the solution to customize the output when working with an object as mention response value. Maybe others are google for such a solution and are beginners like me. So decided to post the solution here, if it helps someone it was worth it :-) Its not possible to override the extensions renderHTML({ node, HTMLAttributes }) {
return [
'span',
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
`@${node.attrs.name}` // <----
]
}, Where name is an attribute defined previously: addAttributes() {
return {
name: {
default: null,
parseHTML: element => {
return {
name: element.getAttribute('data-mention-name'),
}
},
renderHTML: attributes => {
if (!attributes.name) {
return {}
}
return {
'data-mention-name': attributes.name,
}
},
},
id: {
default: null,
parseHTML: element => {
return {
id: element.getAttribute('data-mention-id'),
}
},
renderHTML: attributes => {
if (!attributes.id) {
return {}
}
return {
'data-mention-id': attributes.id,
}
},
},
}
},
}) As people often need context, here is the full solution const CustomMention = Mention.extend({
renderHTML({ node, HTMLAttributes }) {
return [
'span',
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
`@${node.attrs.name}`
]
},
addAttributes() {
return {
name: {
default: null,
parseHTML: element => {
return {
name: element.getAttribute('data-mention-name'),
}
},
renderHTML: attributes => {
if (!attributes.name) {
return {}
}
return {
'data-mention-name': attributes.name,
}
},
},
id: {
default: null,
parseHTML: element => {
return {
id: element.getAttribute('data-mention-id'),
}
},
renderHTML: attributes => {
if (!attributes.id) {
return {}
}
return {
'data-mention-id': attributes.id,
}
},
},
}
},
}) and then define the editor with the custom mention extension new Editor({
extensions: [
StarterKit,
CustomMention.configure({
HTMLAttributes: {
class: 'mention',
},
suggestion: {
items: async function(query) {
const response = await this.$axios('users?query=' + query)
var values = []
response.data.forEach(user => {
values.push({id: userr.user_id, name: userr.user.firstname})
})
return values
}.bind(this),
//.... here goes the rest from the example in the docs I could send a PR for the docs, if you are interested in such extensive guide examples @hanspagel |
BTW: |
@philippkuehn What changes do you need me to make to get this PR approved? What's the best way forward? |
Issue #1316 also mentions this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, I think I would like to merge that, however I would want to rename "text" to "label". Can you change this?
@philippkuehn All done |
Great! Another thing I’m not quite sure about: Currently the id is stored via the attribute |
@philippkuehn In my use case it would, but other developers using this may not be passing the ID but a username instead so |
id = identifier = the thing that identifies it, so +1 for data-id from me 🙃 |
Happy to change to |
Just a suggestion but would it be worth making the label dynamic by not storing it as an attribute? If the above use cases are similar to mine, I prefer to have the label get updated when the data does. By storing it as an attribute it is similar to hard-coding it with the value it first had when chosen. Where as if a user's name changes, this won't be reflected in the HTML. If going down this route, perhaps it would be best to just supply a new |
Mention.configure({
label: id => MY_LABELS[id],
}).extend({
renderHTML({node, HTMLAttributes}){
return [
'span',
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
this.options.label(node.attrs.id),
]
},
}), |
@tomhrtly I’ve added |
@shadow-light Yeah you are right. I’ve learned that rendering mentions is very individual. But I like your idea and added a So right now there are some ways to render mentions.
|
Why didn't I get the right variables?
But here's the variable:
Everything else is default. "name": "@tiptap/extension-mention", |
@HeebeLee did you find out how to win this ? |
I think there is a bug here, there is a mention of plug-ins. Its input
must be String or {id:{id:’any’, label:’any’}}, so I copied and modified
the plug-in to meet my format.
You can choose a way, although the current data structure is not
good-looking, but it can still be satisfied.
…On Tue, Jul 20, 2021 at 10:50 PM Oleg Zinoviev ***@***.***> wrote:
@HeebeLee <https://github.com/HeebeLee> did you find out how to win this ?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1322 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACV6CILJMZOJJDNB2A7NUYTTYWEMPANCNFSM445ZUVOQ>
.
|
@HeebeLee I've won this by minor changing in our custom MentionList.vue component:
Then it works like a charm. It wasn't very obvious at first.. |
This is our code. |
This PR allows you to change the text that is to be rendered in the DOM when a selection is made from the suggestion.
This is so that you may want the
data-mention
attribute to use a user's ID but the user's name would be displayed instead of the ID e.g. "@johnsmith" instead of "@1".