-
Hello, |
Beta Was this translation helpful? Give feedback.
Answered by
zgrybus
Mar 22, 2023
Replies: 1 comment 1 reply
-
If someone is curious how do I manage to do that, here is my code: useLinkActions hook export const useLinkActions = () => {
const { getSelectedMarkPosition } = useSelectedMarkPosition();
const getLinkUpdateTransaction = useCallback(
(view: EditorView, text: string, href: string) => {
const { state } = view;
const linkPosition = getSelectedMarkPosition(view, linkSchema.type());
if (linkPosition) {
const link = linkSchema.type().create({ href: href });
const node = state.schema.text(text).mark([link]);
return state.tr.replaceRangeWith(
linkPosition.start,
linkPosition.end,
node
);
}
},
[getSelectedMarkPosition]
);
return { getLinkCreationTransaction, getLinkUpdateTransaction };
}; useSelectedMarkPosition hook export const useSelectedMarkPosition = () => {
const getSelectedMarkPosition = useCallback(
(view: EditorView, markType: MarkType) => {
const { state } = view;
const { selection } = state;
return findChildrenByMark(state.doc, markType)
.map(link => ({
end: link.pos + link.node.nodeSize,
start: link.pos,
}))
.find(({ start, end }) => selection.from > start && selection.to < end);
},
[]
);
return { getSelectedMarkPosition };
};
const MyComp = () => {
const { getLinkUpdateTransaction } = useLinkActions()
const [getEditor] = useInstance();
const onTextLinkChange = (text) => {
const editor = getEditor();
if (editor) {
editor.action(ctx => {
const view = ctx.get(editorViewCtx);
const updateTransaction = getLinkUpdateTransaction(view, text, href);
if (updateTransaction) {
view.dispatch(updateTransaction);
}
});
}
};
return (..)
} Does it make sense @Saul-Mirone ? 🤔 Is it ok from your perspective? |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
zgrybus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If someone is curious how do I manage to do that, here is my code:
useLinkActions hook