Skip to content
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

Add a callback for click link in link-dialog plugin. #335

Merged
merged 5 commits into from
Jan 30, 2024
Merged

Add a callback for click link in link-dialog plugin. #335

merged 5 commits into from
Jan 30, 2024

Conversation

newproplus
Copy link
Contributor

Add a callback for click link in link-dialog plugin.

So, developers can get the url that user clicked, and do some more actions.

@newproplus
Copy link
Contributor Author

demo code like:

export const MdxEditor: React.FC<MdxEditorComponentProps> = ({ content, onEmitChange, onEmitOpen }) => {
	const onChange = (aaa: string) => {
		if (onEmitChange) onEmitChange(aaa)
	}

	const allPlugins = () => [
		linkDialogPlugin({
			onClickLinkCallback: (url: string) => {
				onEmitOpen(url)
			},
		}),
	]

	return (
		<MDXEditor
			markdown={content}
			onChange={onChange}
			plugins={allPlugins()}
		/>
	)
}

@petyosi
Copy link
Contributor

petyosi commented Jan 30, 2024

Question for you: would it be easier if we have something like this:

type LinkPreviewDialogClickHandler = React.MouseEventHandler<HTMLAnchorElement>
// ... 
const onLinkPreviewDialogClick$ =  Cell<LinkPreviewDialogClickHandler | null>(null)
// ...
<a href={linkDialogState.url} onClick={useCellValue(onLinkPreviewDialogClick$)}

Then your code would look like:

		linkDialogPlugin({
			onPreviewDialogLinkClick: (e) => {
                                  e.preventDefault();
				onEmitOpen(e.target.url);
			},
		}),

Let me know if this makes sense.

@newproplus
Copy link
Contributor Author

sorry, @petyosi I tred to use const onLinkPreviewDialogClick$ = Cell<LinkPreviewDialogClickHandler | null>(null), it will cause error when I clicked on the link:

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

If I chang it to export let onLinkPreviewDialogClick$: LinkPreviewDialogClickHandler | null = null, and just call onLinkPreviewDialogClick$, it works.

Meybe just return a url string to callback funtion is more easier.

@petyosi petyosi merged commit 95e55cd into mdx-editor:main Jan 30, 2024
Copy link

🎉 This PR is included in version 2.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@petyosi
Copy link
Contributor

petyosi commented Jan 30, 2024

I updated your PR to follow the gurx pattern.

Your previous implementation was overriding a module-level variable value. Take a look at how it works now - it's great that you're actively contributing to the project, so understanding the internal state management is going to be of good help.

@newproplus newproplus deleted the plugin-link-dialog-add-ClickLinkCallback branch January 30, 2024 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants