Skip to content

Commit

Permalink
feat(click-to-react-component): support jb ide
Browse files Browse the repository at this point in the history
You must make sure that the plugin is installed and that the IDE has a project open when you use it.
  • Loading branch information
alanhe421 committed Dec 24, 2024
1 parent 03e05cd commit 0e30c98
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
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']
6 changes: 5 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,10 @@
export { ClickToComponent } from './ClickToComponent'
export { JB_EDITORS } from './getUrl'

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

0 comments on commit 0e30c98

Please sign in to comment.