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

Support for onOpenComponent callback #29

Closed
wants to merge 4 commits into from

Conversation

frantic
Copy link
Contributor

@frantic frantic commented Apr 27, 2022

This PR adds support for onOpenFile callback. When set, it will be called instead of opening the file with the default editor. This change will enable the support for the long tail of editors and will allow other products to build on top of click-to-component.

Usage

<ClickToComponent onOpenComponent={(source) => alert(source.fileName)} />

Fixes #28

@changeset-bot
Copy link

changeset-bot bot commented Apr 27, 2022

🦋 Changeset detected

Latest commit: 44a52e3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
click-to-react-component Minor
remix-5961 Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -44,12 +44,16 @@ export function ClickToComponent({ editor = 'vscode' }) {
const url = `${editor}://file/${path}`

event.preventDefault()
window.open(url)
if (onOpenFile) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! Thanks for taking the initiative! This definitely seems like a need.

What do you think about moving it up one level before the path is crafted. Like, something that accepts { editor, source }?

Such as onOpenComponent({ source }) or similar?

I learned that different editors have different URL schemes, so it'd give the opportunity for people to customize it.

Then, our default implementation would look like:

onOpenComponent = ({ editor, source }) => {
  const path = getPathToSource(source)
  const url = `${editor}://file/${path}`

  window.open(url);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, source has a file name, line & column. So e.g. for vim it would be +{line} {file}. The only drawback here is the user would have to manually import and call getPathToSource or write their own.

Is there a reason you have editor in onOpenComponent: ({ editor, source }) => void? This prop is passed by the same callsite that sets the callback, so they should already know the editor.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, no real reason. You're right – no sense adding it to an API unless it actually makes sense.

Based off of #26 , I think these are actually two distinct needs. So you can ignore my Qs :)

We'll probably end up with something like:

<ClickToComponent
  getURLFromSource={customMapper}
  onOpenURL={customOpener}
/>

@frantic
Copy link
Contributor Author

frantic commented Apr 28, 2022

I've renamed onOpenFile to onOpenComponent and made the callback accept Source instead of path. The users who want an advanced control over what happens can provide their own implementation that formats it and calls external API (via window.open, postMessage, fetch, etc.)

@frantic frantic changed the title Support for onOpenFile callback Support for onOpenComponent callback Apr 28, 2022
@frantic
Copy link
Contributor Author

frantic commented May 2, 2022

@ericclemmons do you think we can move forward with this? Would you rather see a different approach?

@zjffun
Copy link
Contributor

zjffun commented Aug 5, 2023

Great, as far as I know this method can support the following four situations:

@frantic frantic closed this Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for a custom onClick for ClickToComponent
3 participants