refac
This commit is contained in:
1 parent
285a65ef39
commit
74d976c2f7
1 file changed
+21
-7
@@ -270,6 +270,13 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getMentionText = ({ node, suggestion }) => {
|
||||||
|
const id = node.attrs.id ?? '';
|
||||||
|
const label = node.attrs.label ?? id;
|
||||||
|
const char = id.includes('|') ? '$' : (suggestion?.char ?? '@');
|
||||||
|
return `${char}${label}`;
|
||||||
|
};
|
||||||
|
|
||||||
export let onSelectionUpdate = (e) => {};
|
export let onSelectionUpdate = (e) => {};
|
||||||
|
|
||||||
export let id = '';
|
export let id = '';
|
||||||
@@ -455,9 +462,6 @@
|
|||||||
if (text === '') {
|
if (text === '') {
|
||||||
editor.commands.clearContent();
|
editor.commands.clearContent();
|
||||||
} else {
|
} else {
|
||||||
// Regex to find serialized mention tags: <@id>, <#id>, <$id|label>
|
|
||||||
const mentionReG = /<([@#$])([\w.\-:/]+)(?:\|([^>]*))?>/g;
|
|
||||||
|
|
||||||
// Convert each line to a <p>, replacing mention tags with proper
|
// Convert each line to a <p>, replacing mention tags with proper
|
||||||
// TipTap mention spans that the editor's DOMParser will recognise.
|
// TipTap mention spans that the editor's DOMParser will recognise.
|
||||||
const lines = text.split('\n');
|
const lines = text.split('\n');
|
||||||
@@ -470,10 +474,14 @@
|
|||||||
const escaped = line.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
const escaped = line.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||||
// Now replace the escaped mention patterns back into real spans
|
// Now replace the escaped mention patterns back into real spans
|
||||||
const withMentions = escaped.replace(
|
const withMentions = escaped.replace(
|
||||||
/<([@#$])([\w.\-:/]+)(?:\|([^&]*?))?>/g,
|
/<([@#$])([\w.\-:/]+)(?:\|([^&]*?))?>|<\/([\w.\-:/]+)\|([^&]*?)>/g,
|
||||||
(_, ch, id, label) => {
|
(_, ch, id, label, slashSkillId, slashSkillLabel) => {
|
||||||
const display = label?.length ? label : id;
|
const mentionChar = ch || '$';
|
||||||
return `<span class="mention" data-type="mention" data-id="${id}" data-label="${display}" data-mention-suggestion-char="${ch}">${ch}${display}</span>`;
|
const mentionId = id || slashSkillId;
|
||||||
|
const display = (label || slashSkillLabel)?.length
|
||||||
|
? label || slashSkillLabel
|
||||||
|
: mentionId;
|
||||||
|
return `<span class="mention" data-type="mention" data-id="${mentionId}" data-label="${display}" data-mention-suggestion-char="${mentionChar}">${mentionChar}${display}</span>`;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return `<p>${withMentions}</p>`;
|
return `<p>${withMentions}</p>`;
|
||||||
@@ -782,6 +790,12 @@
|
|||||||
? [
|
? [
|
||||||
Mention.configure({
|
Mention.configure({
|
||||||
HTMLAttributes: { class: 'mention' },
|
HTMLAttributes: { class: 'mention' },
|
||||||
|
renderText: getMentionText,
|
||||||
|
renderHTML: ({ options, node, suggestion }) => [
|
||||||
|
'span',
|
||||||
|
options.HTMLAttributes,
|
||||||
|
getMentionText({ node, suggestion })
|
||||||
|
],
|
||||||
suggestions: suggestions
|
suggestions: suggestions
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in new issue
Block a user