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

feat(click-to-react-component): support jb ide #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/four-geckos-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'click-to-react-component': minor
---

support jb ide
2 changes: 1 addition & 1 deletion packages/click-to-react-component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
[Create React App](https://create-react-app.dev/),
& [Vite](https://github.com/vitejs/vite/tree/main/packages/plugin-react)
that use [@babel/plugin-transform-react-jsx-source](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-jsx-source)
- Supports `vscode` & `vscode-insiders` & `cursor` [URL handling](https://code.visualstudio.com/docs/editor/command-line#_opening-vs-code-with-urls)
- Supports `vscode`, `vscode-insiders`, `cursor` [URL handling](https://code.visualstudio.com/docs/editor/command-line#_opening-vs-code-with-urls), and JB IDEs like `WebStorm` (requires [IDE Remote Control](https://plugins.jetbrains.com/plugin/19991-ide-remote-control))
- Automatically **tree-shaken** from `production` builds
- Keyboard navigation in context menu (e.g. <kbd>←</kbd>, <kbd>→</kbd>, <kbd>⏎</kbd>)
- More context & faster than using React DevTools:
Expand Down
28 changes: 18 additions & 10 deletions packages/click-to-react-component/src/ClickToComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ export function ClickToComponent({ editor = 'vscode', pathModifier }) {
)
}
const path = getPathToSource(source, pathModifier)
const url = getUrl({
const [url, isURLScheme] = getUrl({
editor,
pathToSource: path,
})

event.preventDefault()
window.location.assign(url)

if (isURLScheme) {
window.location.assign(url)
} else {
fetch(url);
}
setState(State.IDLE)
}
},
Expand All @@ -78,12 +81,16 @@ export function ClickToComponent({ editor = 'vscode', pathModifier }) {
const onClose = React.useCallback(
function handleClose(returnValue) {
if (returnValue) {
const url = getUrl({
const [url,isURLScheme] = getUrl({
editor,
pathToSource: returnValue,
})

window.location.assign(url)
if (isURLScheme) {
window.location.assign(url)
} else {
fetch(url);
}
}

setState(State.IDLE)
Expand Down Expand Up @@ -247,11 +254,12 @@ export function ClickToComponent({ editor = 'vscode', pathModifier }) {
</style>
<${FloatingPortal} key="click-to-component-portal">
${html`<${ContextMenu}
key="click-to-component-contextmenu"
onClose=${onClose}
pathModifier=${pathModifier}
/>`}
${html`
<${ContextMenu}
key="click-to-component-contextmenu"
onClose=${onClose}
pathModifier=${pathModifier}
/>`}
</${FloatingPortal}
`
}
12 changes: 10 additions & 2 deletions packages/click-to-react-component/src/getUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
* @param {Object} param
* @param {string} param.editor
* @param {string} param.pathToSource
* @returns {[string, boolean]} bool isURLScheme
*/
export function getUrl({ editor, pathToSource }) {
if (JB_EDITORS.includes(editor)) {
// @see https://github.com/JetBrains/intellij-community/blob/a77365debaadcf00b888a977d89512f3f0f3cf9e/platform/built-in-server/src/org/jetbrains/ide/OpenFileHttpService.kt#L52-L59
return [`http://localhost:63342/api/file/${pathToSource}`, false]
}
// Fix https://github.com/microsoft/vscode/issues/197319
if (pathToSource[0] === '/') {
return `${editor}://file${pathToSource}`
return [`${editor}://file${pathToSource}`, true]
}

return `${editor}://file/${pathToSource}`
return [`${editor}://file/${pathToSource}`, true]
}

export const JB_EDITORS = ['idea', 'appcode', 'clion', 'pycharm', 'phpstorm',
'rubymine', 'webstorm', 'rider', 'goland', 'rustrover']
5 changes: 4 additions & 1 deletion packages/click-to-react-component/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export { ClickToComponent } from './ClickToComponent'

export type Editor = 'vscode' | 'vscode-insiders' | 'cursor' | string
const JB_EDITORS = ['idea', 'appcode', 'clion', 'pycharm', 'phpstorm',
'rubymine', 'webstorm', 'rider', 'goland', 'rustrover'] as const

export type Editor = 'vscode' | 'vscode-insiders' | 'cursor' | typeof JB_EDITORS[number] | string

export type PathModifier = (path: string) => string

Expand Down