refactor: move useSpecialContent to a composable

This commit is contained in:
Shivam Mishra
2024-05-09 18:28:40 +05:30
parent 6129cb5f80
commit ecb28383c1
2 changed files with 78 additions and 48 deletions
@@ -0,0 +1,61 @@
import { replaceVariablesInMessage } from '@chatwoot/utils';
import {
MessageMarkdownTransformer,
messageSchema,
} from '@chatwoot/prosemirror-schema';
export default function useSpecialContent() {
const getMentionNode = (editorView, content, from, to) => {
const node = editorView.state.schema.nodes.mention.create({
userId: content.id,
userFullName: content.name,
});
return { node, from, to };
};
const getCannedResponseNode = (editorView, content, from, to, variables) => {
const updatedMessage = replaceVariablesInMessage({
message: content,
variables: variables,
});
const node = new MessageMarkdownTransformer(messageSchema).parse(
updatedMessage
);
from = node.textContent === updatedMessage ? from : from - 1;
return { node, from, to };
};
const getVariableNode = (editorView, content, from, to) => {
const node = editorView.state.schema.text(`{{${content}}}`);
return { node, from, to };
};
const getContentNode = (editorView, type, content, range, variables) => {
const methodMap = {
mention: getMentionNode,
cannedResponse: getCannedResponseNode,
variable: getVariableNode,
};
if (!methodMap[type]) {
return { node: null, from: range.from, to: range.to };
}
let { node, from, to } = methodMap[type](
editorView,
content,
range.from,
range.to,
variables
);
return { node, from, to };
};
return {
getContentNode,
};
}