@tscircuit/fake-snippets 0.0.97 → 0.0.98
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.js +1 -1
- package/package.json +1 -1
- package/src/App.tsx +10 -1
- package/src/components/ViewPackagePage/components/ShikiCodeViewer.tsx +15 -2
- package/src/components/ViewPackagePage/components/tab-views/3d-view.tsx +14 -1
- package/src/components/package-port/CodeEditor.tsx +76 -9
- package/src/components/package-port/CodeEditorHeader.tsx +12 -7
- package/src/hooks/use-create-package-release-mutation.ts +2 -2
- package/src/lib/constants.ts +11 -0
- package/src/lib/utils/resolveRelativePath.ts +40 -0
package/package.json
CHANGED
package/src/App.tsx
CHANGED
|
@@ -122,6 +122,7 @@ class ErrorBoundary extends React.Component<
|
|
|
122
122
|
cleanup = () => {
|
|
123
123
|
if (this.visibilityHandler) {
|
|
124
124
|
document.removeEventListener("visibilitychange", this.visibilityHandler)
|
|
125
|
+
window.removeEventListener("focus", this.visibilityHandler)
|
|
125
126
|
this.visibilityHandler = undefined
|
|
126
127
|
}
|
|
127
128
|
if (this.reloadTimeout) {
|
|
@@ -144,12 +145,20 @@ class ErrorBoundary extends React.Component<
|
|
|
144
145
|
this.cleanup() // Clean up any existing handlers
|
|
145
146
|
|
|
146
147
|
this.visibilityHandler = () => {
|
|
147
|
-
if (
|
|
148
|
+
if (
|
|
149
|
+
document.visibilityState === "visible" &&
|
|
150
|
+
this.state.hasError &&
|
|
151
|
+
!this.state.reloading
|
|
152
|
+
) {
|
|
148
153
|
this.performReload()
|
|
149
154
|
}
|
|
150
155
|
}
|
|
151
156
|
|
|
152
157
|
document.addEventListener("visibilitychange", this.visibilityHandler)
|
|
158
|
+
window.addEventListener("focus", this.visibilityHandler)
|
|
159
|
+
|
|
160
|
+
// In case the tab is already visible when the error occurs
|
|
161
|
+
this.visibilityHandler()
|
|
153
162
|
}
|
|
154
163
|
|
|
155
164
|
render() {
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { useShikiHighlighter } from "@/hooks/use-shiki-highlighter"
|
|
2
2
|
import { useEffect, useMemo } from "react"
|
|
3
3
|
import { useQuery } from "react-query"
|
|
4
|
+
import { Skeleton } from "@/components/ui/skeleton"
|
|
5
|
+
|
|
6
|
+
// Pre-randomized array to avoid flickering on re-renders
|
|
7
|
+
const SKELETON_WIDTHS = ["w-2/3", "w-1/4", "w-5/6", "w-1/3", "w-1/2", "w-3/4"]
|
|
4
8
|
|
|
5
9
|
export const ShikiCodeViewer = ({
|
|
6
10
|
code,
|
|
7
11
|
filePath,
|
|
8
|
-
}: {
|
|
12
|
+
}: {
|
|
13
|
+
code: string
|
|
14
|
+
filePath: string
|
|
15
|
+
}) => {
|
|
9
16
|
const { highlighter } = useShikiHighlighter()
|
|
10
17
|
|
|
11
18
|
const html = useMemo(
|
|
@@ -18,7 +25,13 @@ export const ShikiCodeViewer = ({
|
|
|
18
25
|
)
|
|
19
26
|
|
|
20
27
|
if (!html) {
|
|
21
|
-
return
|
|
28
|
+
return (
|
|
29
|
+
<div className="text-sm p-4">
|
|
30
|
+
{SKELETON_WIDTHS.map((w, i) => (
|
|
31
|
+
<Skeleton key={i} className={`h-4 mb-2 ${w}`} />
|
|
32
|
+
))}
|
|
33
|
+
</div>
|
|
34
|
+
)
|
|
22
35
|
}
|
|
23
36
|
|
|
24
37
|
return (
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CadViewer } from "@tscircuit/runframe"
|
|
2
2
|
import { useCurrentPackageCircuitJson } from "../../hooks/use-current-package-circuit-json"
|
|
3
|
+
import { Suspense } from "react"
|
|
3
4
|
|
|
4
5
|
export default function ThreeDView() {
|
|
5
6
|
const { circuitJson, isLoading, error } = useCurrentPackageCircuitJson()
|
|
@@ -24,7 +25,19 @@ export default function ThreeDView() {
|
|
|
24
25
|
|
|
25
26
|
return (
|
|
26
27
|
<div className="h-[620px]">
|
|
27
|
-
<
|
|
28
|
+
<Suspense
|
|
29
|
+
fallback={
|
|
30
|
+
<div className="flex justify-center items-center h-full">
|
|
31
|
+
<div className="w-48">
|
|
32
|
+
<div className="loading">
|
|
33
|
+
<div className="loading-bar"></div>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
}
|
|
38
|
+
>
|
|
39
|
+
<CadViewer clickToInteractEnabled circuitJson={circuitJson} />
|
|
40
|
+
</Suspense>
|
|
28
41
|
</div>
|
|
29
42
|
)
|
|
30
43
|
}
|
|
@@ -15,7 +15,10 @@ import { getImportsFromCode } from "@tscircuit/prompt-benchmarks/code-runner-uti
|
|
|
15
15
|
import type { ATABootstrapConfig } from "@typescript/ata"
|
|
16
16
|
import { setupTypeAcquisition } from "@typescript/ata"
|
|
17
17
|
import { linter } from "@codemirror/lint"
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
TSCI_PACKAGE_PATTERN,
|
|
20
|
+
LOCAL_FILE_IMPORT_PATTERN,
|
|
21
|
+
} from "@/lib/constants"
|
|
19
22
|
import {
|
|
20
23
|
createSystem,
|
|
21
24
|
createVirtualTypeScriptEnvironment,
|
|
@@ -29,7 +32,6 @@ import tsModule from "typescript"
|
|
|
29
32
|
import CodeEditorHeader, {
|
|
30
33
|
FileName,
|
|
31
34
|
} from "@/components/package-port/CodeEditorHeader"
|
|
32
|
-
import { useCodeCompletionApi } from "@/hooks/use-code-completion-ai-api"
|
|
33
35
|
import FileSidebar from "../FileSidebar"
|
|
34
36
|
import { findTargetFile } from "@/lib/utils/findTargetFile"
|
|
35
37
|
import type { PackageFile } from "@/types/package"
|
|
@@ -44,6 +46,7 @@ import {
|
|
|
44
46
|
} from "@/hooks/useFileManagement"
|
|
45
47
|
import { isHiddenFile } from "../ViewPackagePage/utils/is-hidden-file"
|
|
46
48
|
import { inlineCopilot } from "codemirror-copilot"
|
|
49
|
+
import { resolveRelativePath } from "@/lib/utils/resolveRelativePath"
|
|
47
50
|
|
|
48
51
|
const defaultImports = `
|
|
49
52
|
import React from "@types/react/jsx-runtime"
|
|
@@ -417,11 +420,13 @@ export const CodeEditor = ({
|
|
|
417
420
|
const lineStart = line.from
|
|
418
421
|
const lineEnd = line.to
|
|
419
422
|
const lineText = view.state.sliceDoc(lineStart, lineEnd)
|
|
420
|
-
|
|
423
|
+
|
|
424
|
+
// Check for TSCI package imports
|
|
425
|
+
const packageMatches = Array.from(
|
|
421
426
|
lineText.matchAll(TSCI_PACKAGE_PATTERN),
|
|
422
427
|
)
|
|
423
428
|
|
|
424
|
-
for (const match of
|
|
429
|
+
for (const match of packageMatches) {
|
|
425
430
|
if (match.index !== undefined) {
|
|
426
431
|
const start = lineStart + match.index
|
|
427
432
|
const end = start + match[0].length
|
|
@@ -484,10 +489,12 @@ export const CodeEditor = ({
|
|
|
484
489
|
const lineStart = line.from
|
|
485
490
|
const lineEnd = line.to
|
|
486
491
|
const lineText = view.state.sliceDoc(lineStart, lineEnd)
|
|
487
|
-
|
|
492
|
+
|
|
493
|
+
// Check for TSCI package imports first
|
|
494
|
+
const packageMatches = Array.from(
|
|
488
495
|
lineText.matchAll(TSCI_PACKAGE_PATTERN),
|
|
489
496
|
)
|
|
490
|
-
for (const match of
|
|
497
|
+
for (const match of packageMatches) {
|
|
491
498
|
if (match.index !== undefined) {
|
|
492
499
|
const start = lineStart + match.index
|
|
493
500
|
const end = start + match[0].length
|
|
@@ -502,6 +509,42 @@ export const CodeEditor = ({
|
|
|
502
509
|
}
|
|
503
510
|
}
|
|
504
511
|
}
|
|
512
|
+
|
|
513
|
+
// Check for local file imports
|
|
514
|
+
const localFileMatches = Array.from(
|
|
515
|
+
lineText.matchAll(LOCAL_FILE_IMPORT_PATTERN),
|
|
516
|
+
)
|
|
517
|
+
for (const match of localFileMatches) {
|
|
518
|
+
if (match.index !== undefined) {
|
|
519
|
+
const start = lineStart + match.index
|
|
520
|
+
const end = start + match[0].length
|
|
521
|
+
if (pos >= start && pos <= end) {
|
|
522
|
+
const relativePath = match[0]
|
|
523
|
+
const resolvedPath = resolveRelativePath(
|
|
524
|
+
relativePath,
|
|
525
|
+
currentFile || "",
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
// Add common extensions if not present
|
|
529
|
+
let targetPath = resolvedPath
|
|
530
|
+
if (!targetPath.includes(".")) {
|
|
531
|
+
const extensions = [".tsx", ".ts", ".js", ".jsx"]
|
|
532
|
+
for (const ext of extensions) {
|
|
533
|
+
if (fileMap[`${targetPath}${ext}`]) {
|
|
534
|
+
targetPath = `${targetPath}${ext}`
|
|
535
|
+
break
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (fileMap[targetPath]) {
|
|
541
|
+
onFileSelect(targetPath)
|
|
542
|
+
return true
|
|
543
|
+
}
|
|
544
|
+
return !!fileMap[targetPath]
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
505
548
|
return false
|
|
506
549
|
},
|
|
507
550
|
keydown: (event) => {
|
|
@@ -529,6 +572,12 @@ export const CodeEditor = ({
|
|
|
529
572
|
overflow: "auto",
|
|
530
573
|
zIndex: "9999",
|
|
531
574
|
},
|
|
575
|
+
".cm-import:hover": {
|
|
576
|
+
textDecoration: "underline",
|
|
577
|
+
textDecorationColor: "#aa1111",
|
|
578
|
+
textUnderlineOffset: "1px",
|
|
579
|
+
filter: "brightness(0.7)",
|
|
580
|
+
},
|
|
532
581
|
}),
|
|
533
582
|
EditorView.decorations.of((view) => {
|
|
534
583
|
const decorations = []
|
|
@@ -536,14 +585,32 @@ export const CodeEditor = ({
|
|
|
536
585
|
for (let pos = from; pos < to; ) {
|
|
537
586
|
const line = view.state.doc.lineAt(pos)
|
|
538
587
|
const lineText = line.text
|
|
539
|
-
|
|
540
|
-
|
|
588
|
+
|
|
589
|
+
// Add decorations for TSCI package imports
|
|
590
|
+
const packageMatches = lineText.matchAll(TSCI_PACKAGE_PATTERN)
|
|
591
|
+
for (const match of packageMatches) {
|
|
592
|
+
if (match.index !== undefined) {
|
|
593
|
+
const start = line.from + match.index
|
|
594
|
+
const end = start + match[0].length
|
|
595
|
+
decorations.push(
|
|
596
|
+
Decoration.mark({
|
|
597
|
+
class: "cm-import cursor-pointer",
|
|
598
|
+
}).range(start, end),
|
|
599
|
+
)
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// Add decorations for local file imports
|
|
604
|
+
const localFileMatches = lineText.matchAll(
|
|
605
|
+
LOCAL_FILE_IMPORT_PATTERN,
|
|
606
|
+
)
|
|
607
|
+
for (const match of localFileMatches) {
|
|
541
608
|
if (match.index !== undefined) {
|
|
542
609
|
const start = line.from + match.index
|
|
543
610
|
const end = start + match[0].length
|
|
544
611
|
decorations.push(
|
|
545
612
|
Decoration.mark({
|
|
546
|
-
class: "cm-
|
|
613
|
+
class: "cm-import cursor-pointer",
|
|
547
614
|
}).range(start, end),
|
|
548
615
|
)
|
|
549
616
|
}
|
|
@@ -29,6 +29,7 @@ import { convertRawEasyToTsx, fetchEasyEDAComponent } from "easyeda/browser"
|
|
|
29
29
|
import { ComponentSearchResult } from "@tscircuit/runframe/runner"
|
|
30
30
|
import { usePackagesBaseApiUrl } from "@/hooks/use-packages-base-api-url"
|
|
31
31
|
import { ICreateFileProps, ICreateFileResult } from "@/hooks/useFileManagement"
|
|
32
|
+
import { useGlobalStore } from "@/hooks/use-global-store"
|
|
32
33
|
|
|
33
34
|
export type FileName = string
|
|
34
35
|
|
|
@@ -61,6 +62,7 @@ export const CodeEditorHeader: React.FC<CodeEditorHeaderProps> = ({
|
|
|
61
62
|
const [sidebarOpen, setSidebarOpen] = fileSidebarState
|
|
62
63
|
const API_BASE = usePackagesBaseApiUrl()
|
|
63
64
|
const [aiAutocompleteEnabled, setAiAutocompleteEnabled] = aiAutocompleteState
|
|
65
|
+
const session = useGlobalStore((s) => s.session)
|
|
64
66
|
|
|
65
67
|
const handleFormatFile = useCallback(() => {
|
|
66
68
|
if (!window.prettier || !window.prettierPlugins) return
|
|
@@ -157,18 +159,21 @@ export const CodeEditorHeader: React.FC<CodeEditorHeaderProps> = ({
|
|
|
157
159
|
updateFileContent(currentFile, newContent)
|
|
158
160
|
}
|
|
159
161
|
if (component.source == "jlcpcb") {
|
|
162
|
+
if (!session?.token) {
|
|
163
|
+
throw new Error("You need to be logged in to import jlcpcb component")
|
|
164
|
+
}
|
|
160
165
|
const jlcpcbComponent = await fetchEasyEDAComponent("C1", {
|
|
161
166
|
fetch: ((url, options: any) => {
|
|
162
167
|
return fetch(`${API_BASE}/proxy`, {
|
|
163
|
-
|
|
168
|
+
body: options.body,
|
|
169
|
+
method: options.method,
|
|
164
170
|
headers: {
|
|
165
|
-
|
|
171
|
+
authority: options.headers.authority,
|
|
172
|
+
Authorization: `Bearer ${session?.token}`,
|
|
166
173
|
"X-Target-Url": url.toString(),
|
|
167
|
-
"X-Sender-
|
|
168
|
-
"X-Sender-
|
|
169
|
-
"
|
|
170
|
-
"X-Sender-User-Agent": options?.headers?.userAgent ?? "",
|
|
171
|
-
"X-Sender-Cookie": options?.headers?.cookie ?? "",
|
|
174
|
+
"X-Sender-Host": options.headers.origin,
|
|
175
|
+
"X-Sender-Origin": options.headers.origin,
|
|
176
|
+
"content-type": options.headers["content-type"],
|
|
172
177
|
},
|
|
173
178
|
})
|
|
174
179
|
}) as typeof fetch,
|
|
@@ -42,9 +42,9 @@ export const useCreatePackageReleaseMutation = ({
|
|
|
42
42
|
resolvedPkgName = pkgName
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
// Default version to
|
|
45
|
+
// Default version to 0.0.1 when it contains no digits
|
|
46
46
|
if (!resolvedVersion || !/[0-9]/.test(resolvedVersion)) {
|
|
47
|
-
resolvedVersion = "
|
|
47
|
+
resolvedVersion = "0.0.1"
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
const normalizedPackageNameWithVersion =
|
package/src/lib/constants.ts
CHANGED
|
@@ -10,3 +10,14 @@
|
|
|
10
10
|
*/
|
|
11
11
|
export const TSCI_PACKAGE_PATTERN =
|
|
12
12
|
/@tsci\/[a-zA-Z][a-zA-Z0-9]*(?:--?[a-zA-Z0-9]+)*(?:\.[a-zA-Z][a-zA-Z0-9_]*(?:--?[a-zA-Z0-9_]+)*)*/g
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Regular expression pattern for matching local file imports
|
|
16
|
+
* Rules:
|
|
17
|
+
* - Must start with ./ or ../
|
|
18
|
+
* - Can contain letters, numbers, dots, dashes, underscores, and forward slashes
|
|
19
|
+
* - Can optionally end with file extensions like .ts, .tsx, .js, .jsx, .json
|
|
20
|
+
* - Captures the full relative path
|
|
21
|
+
*/
|
|
22
|
+
export const LOCAL_FILE_IMPORT_PATTERN =
|
|
23
|
+
/(?:\.\.?\/[a-zA-Z0-9._\-\/]*(?:\.(?:ts|tsx|js|jsx|json))?)/g
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves a relative file path to an absolute path based on the current file
|
|
3
|
+
*/
|
|
4
|
+
export const resolveRelativePath = (
|
|
5
|
+
relativePath: string,
|
|
6
|
+
currentFilePath: string,
|
|
7
|
+
): string => {
|
|
8
|
+
if (!currentFilePath) return relativePath
|
|
9
|
+
|
|
10
|
+
const currentDir = currentFilePath.includes("/")
|
|
11
|
+
? currentFilePath.substring(0, currentFilePath.lastIndexOf("/"))
|
|
12
|
+
: ""
|
|
13
|
+
|
|
14
|
+
if (relativePath.startsWith("./")) {
|
|
15
|
+
return currentDir
|
|
16
|
+
? `${currentDir}/${relativePath.slice(2)}`
|
|
17
|
+
: relativePath.slice(2)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (relativePath.startsWith("../")) {
|
|
21
|
+
const parts = currentDir.split("/").filter((p) => p !== "")
|
|
22
|
+
const relativeParts = relativePath.split("/").filter((p) => p !== "")
|
|
23
|
+
|
|
24
|
+
let upCount = 0
|
|
25
|
+
for (const part of relativeParts) {
|
|
26
|
+
if (part === "..") {
|
|
27
|
+
upCount++
|
|
28
|
+
} else {
|
|
29
|
+
break
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const resultParts = parts.slice(0, Math.max(0, parts.length - upCount))
|
|
34
|
+
const remainingParts = relativeParts.slice(upCount)
|
|
35
|
+
|
|
36
|
+
return [...resultParts, ...remainingParts].join("/")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return relativePath
|
|
40
|
+
}
|