-
Hi @ianstormtaylor , Regarding your mentions example, is there a specific way to get a list of users who were mentioned? Ideally I just need a JSON list of all the people mentioned (people's names or an ID). If anyone else has any ideas, please feel free to jump in as well. I'm very curious if there's an easy way to do this. Many thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
A little bit more background: I've replicated the mentions example, and ideally I want to take the JSON being stored in the database and get the mentions. The only problem I have is that it's not easy navigating a nested JSON structure where I don't know what the keys are. Say if I had a table inside a table with some mentions in that inner table, I'm unsure as to the best way of finding these mentions. If you need any further clarification, please let me know. |
Beta Was this translation helpful? Give feedback.
-
Finally came up with a solution for this a few months ago using lodash to assist with generating an unique list of all the people mentioned in a "comment" from a SlateJS editor. /**
* Routine to recursively traverse a SlateJS JSON structure to find any nodes with the type === "mention".
* As this is difficult to accomplish reliably, this function uses lodash to find all JSON nodes where
* type === "mention", and then performs a flattenDeep operation to give us an array of people mentioned
* in the SlateJS structure.
* @param tree The JSON structure to parse. NOTE: the "content" node of a SlateJS JSON structure must be the root node.
* @returns Array of objects representing the mentions that were found in the input JSON structure.
*/
function recursiveFindMentions(tree: any) {
function recurse(nodes: any) {
return _.map(nodes, function (node: any) {
// Ignore any non-mention nodes.
if (node.type !== "mention") return recurse(node.children);
// Remaining nodes are mentions. Include all required keys in the JSON object.
return [
_.pick(node, ['type', 'person', 'id']),
recurse(node.children) // Get the next node in the JSON structure.
];
});
}
// Return only one of each mention.
return _.uniqBy(_.flattenDeep(recurse(tree)), (mention: any) => mention.id);
} ...where each mention node has a structure that gets defined in the SlateJS editor like so: type MentionElement = {
type: 'mention'
person: string,
id: number
} |
Beta Was this translation helpful? Give feedback.
Finally came up with a solution for this a few months ago using lodash to assist with generating an unique list of all the people mentioned in a "comment" from a SlateJS editor.