0.0.245 • Published 10 months ago

@tscircuit/runframe v0.0.245

Weekly downloads
-
License
-
Repository
-
Last release
10 months ago

@tscircuit/runframe

tscircuitView Examples

A React component that runs tscircuit inside a webworker and shows you the PCB, Schematic, 3D and other previews.

  • Automatically imports other snippets and libraries if imported
  • Runs inside webworker (doesn't block the main thread)
  • Converts typescript to javascript using babel

Usage

import { RunFrame } from "@tscircuit/runframe/runner"

const App = () => (
  <RunFrame
    fsMap={{
      "main.tsx": `
circuit.add(
  <resistor resistance="1k" />
)
`,
    }}
    entrypoint="main.tsx"

    // Listen to events
    onCircuitJsonChange={(circuitJson) => void}
    onRenderingFinished={({ circuitJson }) => void}
    onRenderEvent={(event) => void}
    onError={(error) => void}
  />
)

Using CircuitJsonPreview Directly

If you already have circuit JSON and just want to display it:

import { CircuitJsonPreview } from "@tscircuit/runframe/preview"

const App = () => (
  <CircuitJsonPreview
    circuitJson={myCircuitJson}
    // Optional props
    showCodeTab={false}      // Show/hide the code editor tab
    showJsonTab={true}       // Show/hide the raw JSON viewer tab
    className="h-screen"     // Container className
    headerClassName="px-4"   // Header className
    isFullScreen={false}     // Toggle fullscreen mode
    onToggleFullScreen={() => void}  // Fullscreen toggle callback
  />
)

The CircuitJsonPreview component provides:

  • PCB view with interactive component placement
  • Schematic view
  • Assembly view
  • 3D view
  • Bill of Materials (BOM) table
  • Circuit JSON viewer
  • Error display

Providing the Blob URL (to avoid loading webworker from CDN)

import evalWebWorkerBlobUrl from "@tscircuit/eval/blob-url"

Standalone Iframe

RunFrame can be embedded in an iframe, allowing you to evaluate tscircuit code from a parent application. There are two main ways to integrate RunFrame: using the React component or creating a standalone iframe implementation.

<!DOCTYPE html>
<html>
  <head>
    <title>RunFrame Host</title>
  </head>
  <body>
    <iframe
      id="runframe"
      src="https://runframe.tscircuit.com/iframe.html"
      style="width: 100%; height: 600px; border: none;"
    ></iframe>

    <script>
      const iframe = document.getElementById("runframe")

      // Listen for ready message from iframe
      window.addEventListener("message", (event) => {
        if (event.data?.runframe_type === "runframe_ready_to_receive") {
          // Send circuit configuration
          iframe.contentWindow.postMessage(
            {
              runframe_type: "runframe_props_changed",
              runframe_props: {
                fsMap: {
                  "main.tsx": `circuit.add(<resistor resistance="1k" />)`,
                },
                entrypoint: "main.tsx",
              },
            },
            "*"
          )
        }
      })
    </script>
  </body>
</html>

Listening for events from the iframe

window.addEventListener("message", (event) => {
  if (event.data?.runframe_type === "runframe_event") {
    const { type } = event.data.runframe_event
    if (type === "error") {
      console.error(event.data.runframe_event.error_message)
    }
  }
})

Development

File-Server Interaction with Edit Events

Edit events can be a complex to understand, because it can take some time for rendering to complete, edit events are applied for an approximate render before the circuit is fully rendered. The sequence diagram below can be used as a reference to understand how the edit events are applied and queued for rendering.

sequenceDiagram
    participant CLI
    participant FileServer as File Server
    participant BrowserRunframe as Browser Runframe
    participant Viewer as Schematic/PCB Viewer

    BrowserRunframe->>Viewer: Initial Circuit JSON

    Note over Viewer: Edit event created
    Viewer->>BrowserRunframe: onEditEvent
    Note over BrowserRunframe: newEditEvent._applied=false<br/>editEvents.push(newEditEvent)
    BrowserRunframe->>Viewer: editEvents={editEvents}

    BrowserRunframe->>FileServer: /files/get manual-edits.json
    FileServer-->>BrowserRunframe: manual-edits.json content

    Note over BrowserRunframe: Apply edit events to manual-edits.json<br/>Mark editEvent._applied = true

    BrowserRunframe->>FileServer: /files/update manual-edits.json
    FileServer->>CLI: FILE_UPDATED event

    Note over CLI: Update manual-edits.json on filesystem

    rect rgb(200, 230, 255)
        Note over BrowserRunframe: Start re-render

        Note over Viewer: Edit event created
        Viewer->>BrowserRunframe: onEditEvent
        Note over BrowserRunframe: newEditEvent._applied=false<br/>editEvents.push(newEditEvent)
        BrowserRunframe->>Viewer: editEvents={editEvents}<br/>both unapplied & applied<br/>edit events are sent

        Note over BrowserRunframe: Render complete
    end

    Note over BrowserRunframe: Remove all applied editEvents

    BrowserRunframe->>Viewer: editEvents={editEvents}<br/>circuitJson={circuitJson}<br/>Send unapplied editEvents list<br/>+ Circuit JSON

    Note over BrowserRunframe: If there are unapplied editEvents, repeat

<RunFrameWithApi /> and <RunFrameForCli /> Event Bus

The event bus for the RunFrame is accessible from the @tscircuit/file-server /events/list endpoint. RunFrame creates the following events:

Event NameDescription
FILE_UPDATEDA file was updated
REQUEST_TO_SAVE_SNIPPETBrowser request to save the current circuit to snippets
FAILED_TO_SAVE_SNIPPETFailed to save the current snippet to Registry
SNIPPET_SAVEDSnippet was saved to Registry
0.0.241

10 months ago

0.0.240

10 months ago

0.0.245

10 months ago

0.0.244

10 months ago

0.0.243

10 months ago

0.0.242

10 months ago

0.0.216

10 months ago

0.0.219

10 months ago

0.0.218

10 months ago

0.0.217

10 months ago

0.0.227

10 months ago

0.0.226

10 months ago

0.0.225

10 months ago

0.0.224

10 months ago

0.0.229

10 months ago

0.0.228

10 months ago

0.0.223

10 months ago

0.0.222

10 months ago

0.0.221

10 months ago

0.0.220

10 months ago

0.0.238

10 months ago

0.0.237

10 months ago

0.0.236

10 months ago

0.0.235

10 months ago

0.0.239

10 months ago

0.0.230

10 months ago

0.0.234

10 months ago

0.0.233

10 months ago

0.0.232

10 months ago

0.0.231

10 months ago

0.0.205

10 months ago

0.0.204

10 months ago

0.0.203

10 months ago

0.0.202

10 months ago

0.0.209

10 months ago

0.0.208

10 months ago

0.0.207

10 months ago

0.0.206

10 months ago

0.0.201

10 months ago

0.0.215

10 months ago

0.0.214

10 months ago

0.0.213

10 months ago

0.0.212

10 months ago

0.0.211

10 months ago

0.0.210

10 months ago

0.0.200

10 months ago

0.0.197

10 months ago

0.0.196

10 months ago

0.0.195

10 months ago

0.0.194

10 months ago

0.0.199

10 months ago

0.0.198

10 months ago

0.0.193

10 months ago

0.0.192

10 months ago

0.0.191

10 months ago

0.0.190

10 months ago

0.0.189

10 months ago

0.0.188

10 months ago

0.0.159

11 months ago

0.0.158

11 months ago

0.0.157

11 months ago

0.0.156

11 months ago

0.0.155

11 months ago

0.0.154

11 months ago

0.0.169

11 months ago

0.0.164

11 months ago

0.0.163

11 months ago

0.0.162

11 months ago

0.0.161

11 months ago

0.0.168

11 months ago

0.0.167

11 months ago

0.0.166

11 months ago

0.0.165

11 months ago

0.0.160

11 months ago

0.0.175

10 months ago

0.0.174

10 months ago

0.0.173

10 months ago

0.0.172

10 months ago

0.0.179

10 months ago

0.0.178

10 months ago

0.0.177

10 months ago

0.0.176

10 months ago

0.0.171

10 months ago

0.0.170

10 months ago

0.0.186

10 months ago

0.0.185

10 months ago

0.0.184

10 months ago

0.0.183

10 months ago

0.0.187

10 months ago

0.0.182

10 months ago

0.0.181

10 months ago

0.0.180

10 months ago

0.0.153

11 months ago

0.0.152

11 months ago

0.0.151

11 months ago

0.0.150

11 months ago

0.0.128

11 months ago

0.0.127

11 months ago

0.0.126

11 months ago

0.0.125

11 months ago

0.0.129

11 months ago

0.0.124

11 months ago

0.0.123

11 months ago

0.0.139

11 months ago

0.0.138

11 months ago

0.0.137

11 months ago

0.0.136

11 months ago

0.0.131

11 months ago

0.0.130

11 months ago

0.0.135

11 months ago

0.0.134

11 months ago

0.0.133

11 months ago

0.0.132

11 months ago

0.0.149

11 months ago

0.0.148

11 months ago

0.0.147

11 months ago

0.0.142

11 months ago

0.0.141

11 months ago

0.0.140

11 months ago

0.0.146

11 months ago

0.0.145

11 months ago

0.0.144

11 months ago

0.0.143

11 months ago

0.0.117

11 months ago

0.0.116

11 months ago

0.0.115

11 months ago

0.0.114

11 months ago

0.0.119

11 months ago

0.0.118

11 months ago

0.0.113

11 months ago

0.0.120

11 months ago

0.0.122

11 months ago

0.0.121

11 months ago

0.0.106

11 months ago

0.0.105

11 months ago

0.0.104

11 months ago

0.0.103

11 months ago

0.0.109

11 months ago

0.0.108

11 months ago

0.0.107

11 months ago

0.0.102

11 months ago

0.0.101

11 months ago

0.0.100

11 months ago

0.0.112

11 months ago

0.0.111

11 months ago

0.0.110

11 months ago

0.0.95

12 months ago

0.0.96

11 months ago

0.0.97

11 months ago

0.0.98

11 months ago

0.0.99

11 months ago

0.0.90

12 months ago

0.0.91

12 months ago

0.0.92

12 months ago

0.0.93

12 months ago

0.0.94

12 months ago

0.0.84

12 months ago

0.0.85

12 months ago

0.0.86

12 months ago

0.0.87

12 months ago

0.0.88

12 months ago

0.0.89

12 months ago

0.0.80

12 months ago

0.0.81

12 months ago

0.0.82

12 months ago

0.0.83

12 months ago

0.0.73

12 months ago

0.0.74

12 months ago

0.0.75

12 months ago

0.0.76

12 months ago

0.0.77

12 months ago

0.0.78

12 months ago

0.0.79

12 months ago

0.0.70

12 months ago

0.0.71

12 months ago

0.0.72

12 months ago

0.0.62

12 months ago

0.0.63

12 months ago

0.0.64

12 months ago

0.0.65

12 months ago

0.0.66

12 months ago

0.0.67

12 months ago

0.0.68

12 months ago

0.0.69

12 months ago

0.0.40

12 months ago

0.0.41

12 months ago

0.0.42

12 months ago

0.0.43

12 months ago

0.0.44

12 months ago

0.0.45

12 months ago

0.0.46

12 months ago

0.0.47

12 months ago

0.0.37

12 months ago

0.0.38

12 months ago

0.0.39

12 months ago

0.0.30

12 months ago

0.0.31

12 months ago

0.0.32

12 months ago

0.0.33

12 months ago

0.0.34

12 months ago

0.0.35

12 months ago

0.0.36

12 months ago

0.0.29

12 months ago

0.0.60

12 months ago

0.0.61

12 months ago

0.0.59

12 months ago

0.0.51

12 months ago

0.0.52

12 months ago

0.0.53

12 months ago

0.0.54

12 months ago

0.0.55

12 months ago

0.0.56

12 months ago

0.0.57

12 months ago

0.0.58

12 months ago

0.0.50

12 months ago

0.0.48

12 months ago

0.0.49

12 months ago

0.0.20

12 months ago

0.0.21

12 months ago

0.0.22

12 months ago

0.0.23

12 months ago

0.0.24

12 months ago

0.0.25

12 months ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.19

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.26

12 months ago

0.0.27

12 months ago

0.0.28

12 months ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago