Skip to content

Commit

Permalink
Fix can't open file in VS Code 1.84 (#75)
Browse files Browse the repository at this point in the history
fix: vscode url with absolute path
  • Loading branch information
zjffun authored Nov 25, 2023
1 parent 6772192 commit aca85a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/click-to-react-component/src/ClickToComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as React from 'react'
import { ContextMenu } from './ContextMenu.js'
import { getPathToSource } from './getPathToSource.js'
import { getSourceForElement } from './getSourceForElement.js'
import { getUrl } from './getUrl.js'

export const State = /** @type {const} */ ({
IDLE: 'IDLE',
Expand Down Expand Up @@ -41,7 +42,10 @@ export function ClickToComponent({ editor = 'vscode' }) {
if (state === State.HOVER && target instanceof HTMLElement) {
const source = getSourceForElement(target)
const path = getPathToSource(source)
const url = `${editor}://file/${path}`
const url = getUrl({
editor,
pathToSource: path,
})

event.preventDefault()
window.location.assign(url)
Expand All @@ -55,7 +59,11 @@ export function ClickToComponent({ editor = 'vscode' }) {
const onClose = React.useCallback(
function handleClose(returnValue) {
if (returnValue) {
const url = `${editor}://file/${returnValue}`
const url = getUrl({
editor,
pathToSource: returnValue,
})

window.location.assign(url)
}

Expand Down
13 changes: 13 additions & 0 deletions packages/click-to-react-component/src/getUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param {Object} param
* @param {string} param.editor
* @param {string} param.pathToSource
*/
export function getUrl({ editor, pathToSource }) {
// Fix https://github.com/microsoft/vscode/issues/197319
if (pathToSource[0] === '/') {
return `${editor}://file${pathToSource}`
}

return `${editor}://file/${pathToSource}`
}

0 comments on commit aca85a8

Please sign in to comment.