Skip to content

Commit 9508b2e

Browse files
committed
initial solid-sdk commit
1 parent f78c69e commit 9508b2e

6 files changed

Lines changed: 121 additions & 57 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 {{me}}
3+
Copyright (c) 2026 - Current Hatem Hosny
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
11
<p>
2-
<img width="100%" src="https://assets.solidjs.com/banner?type={{name_of_lib}}&background=tiles&project=%20" alt="{{name_of_lib}}">
2+
<img width="100%" src="https://assets.solidjs.com/banner?type=@live-codes/solid-sdk&background=tiles&project=%20" alt="@live-codes/solid-sdk">
33
</p>
44

5-
# {{name_of_lib}}
5+
# @live-codes/solid-sdk
66

77
[![pnpm](https://img.shields.io/badge/maintained%20with-pnpm-cc00ff.svg?style=for-the-badge&logo=pnpm)](https://pnpm.io/)
88

9-
{{desc_of_lib}}
10-
11-
> **Note** After using this template, you have to search and replace all `{{name_of_lib}}` and similar strings
12-
> with appropriate texts.
13-
>
14-
> `{{name_of_lib}}` should be a **kebab-case** string representing the name of you monorepo.
15-
>
16-
> `{{desc_of_lib}}` should be a **Normal case** string with the description of the repository.
17-
>
18-
> `{{me}}` should be a **kebab-case** string from your profile URL.
19-
20-
## Quick start
21-
22-
Install it:
23-
24-
```bash
25-
npm i {{name_of_lib}}
26-
# or
27-
yarn add {{name_of_lib}}
28-
# or
29-
pnpm add {{name_of_lib}}
30-
```
31-
32-
Use it:
33-
34-
```tsx
35-
import {{name_of_lib}} from '{{name_of_lib}}'
36-
```
9+
Solid SDK for LiveCodes

dev/vite.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export default defineConfig({
88
src: path.resolve(__dirname, '../src'),
99
},
1010
},
11+
build: {
12+
rollupOptions: {
13+
external: ['solid-js', 'livecodes'],
14+
},
15+
},
1116
plugins: [
1217
solidPlugin(),
1318
{

package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"name": "{{name_of_lib}}",
3-
"version": "0.0.0",
4-
"description": "{{desc_of_lib}}",
2+
"name": "@live-codes/solid-sdk",
3+
"version": "0.2.0",
4+
"description": "Solid SDK for LiveCodes",
55
"license": "MIT",
6-
"author": "{{me}}",
6+
"author": "Hatem Hosny",
77
"contributors": [],
88
"repository": {
99
"type": "git",
10-
"url": "git+https://github.com/{{me}}/{{name_of_lib}}.git"
10+
"url": "git+https://github.com/live-codes/solid-sdk.git"
1111
},
12-
"homepage": "https://github.com/{{me}}/{{name_of_lib}}#readme",
12+
"homepage": "https://github.com/live-codes/solid-sdk#readme",
1313
"bugs": {
14-
"url": "https://github.com/{{me}}/{{name_of_lib}}/issues"
14+
"url": "https://github.com/live-codes/solid-sdk/issues"
1515
},
1616
"files": [
1717
"dist"
@@ -83,5 +83,8 @@
8383
"engines": {
8484
"node": ">=18",
8585
"pnpm": ">=9.0.0"
86+
},
87+
"dependencies": {
88+
"livecodes": "^0.13.0"
8689
}
8790
}

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.tsx

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,100 @@
1-
import { Accessor, Component, createComputed, createSignal } from 'solid-js'
1+
import { createEffect, on, onCleanup, onMount } from 'solid-js'
2+
import type { Component, JSX } from 'solid-js'
3+
import type { createPlayground, EmbedOptions, Playground } from 'livecodes'
24

3-
export function createHello(): [Accessor<string>, (to: string) => void] {
4-
const [hello, setHello] = createSignal('Hello World!')
5-
6-
return [hello, (to: string) => setHello(`Hello ${to}!`)]
5+
export interface Props extends EmbedOptions {
6+
class?: string
7+
style?: JSX.CSSProperties
8+
height?: string
9+
sdkReady?: (sdk: Playground) => void
710
}
811

9-
export const Hello: Component<{ to?: string }> = props => {
10-
const [hello, setHello] = createHello()
12+
export const createComponent =
13+
(createPlaygroundFn: typeof createPlayground): Component<Props> =>
14+
props => {
15+
let containerRef!: HTMLDivElement
16+
let playground: Playground | undefined
17+
let configCache = JSON.stringify(props.config || '')
18+
let otherOptionsCache = ''
1119

12-
// Console calls will be removed in production if `dropConsole` is enabled
20+
const getOtherOptions = (): Omit<EmbedOptions, 'config'> => {
21+
const opts: Record<string, unknown> = {}
22+
if (props.appUrl !== undefined) opts.appUrl = props.appUrl
23+
if (props.import !== undefined) opts.import = props.import
24+
if (props.headless !== undefined) opts.headless = props.headless
25+
if (props.lite !== undefined) opts.lite = props.lite
26+
if (props.loading !== undefined) opts.loading = props.loading
27+
if (props.params !== undefined) opts.params = props.params
28+
if (props.template !== undefined) opts.template = props.template
29+
if (props.view !== undefined) opts.view = props.view
30+
return opts as Omit<EmbedOptions, 'config'>
31+
}
1332

14-
// eslint-disable-next-line no-console
15-
console.log('Hello World!')
33+
const initOrUpdate = () => {
34+
if (!containerRef) return
1635

17-
createComputed(() => {
18-
if (typeof props.to === 'string') setHello(props.to)
19-
})
36+
const config = props.config
37+
const sdkReady = props.sdkReady
38+
const otherOptions = getOtherOptions()
39+
const currentOtherOptionsStr = JSON.stringify(otherOptions)
2040

21-
return (
22-
<>
23-
<div>{hello()}</div>
24-
</>
25-
)
26-
}
41+
if (!playground || otherOptionsCache !== currentOtherOptionsStr) {
42+
otherOptionsCache = currentOtherOptionsStr
43+
configCache = JSON.stringify(config || '')
44+
playground?.destroy()
45+
createPlaygroundFn(containerRef, { config, ...otherOptions }).then(sdk => {
46+
playground = sdk
47+
if (typeof sdkReady === 'function') {
48+
sdkReady(sdk)
49+
}
50+
})
51+
} else {
52+
const currentConfigStr = JSON.stringify(config)
53+
if (configCache === currentConfigStr) return
54+
configCache = currentConfigStr
55+
56+
if (typeof config === 'string') {
57+
fetch(config)
58+
.then(res => res.json())
59+
.then(json => {
60+
playground?.setConfig(json)
61+
})
62+
} else if (config) {
63+
playground.setConfig(config)
64+
}
65+
}
66+
}
67+
68+
onMount(() => {
69+
initOrUpdate()
70+
})
71+
72+
createEffect(
73+
on(
74+
() => [
75+
props.config,
76+
props.appUrl,
77+
props.import,
78+
props.headless,
79+
props.lite,
80+
props.loading,
81+
props.params,
82+
props.template,
83+
props.view,
84+
props.sdkReady,
85+
],
86+
() => {
87+
initOrUpdate()
88+
},
89+
{ defer: true },
90+
),
91+
)
92+
93+
onCleanup(() => {
94+
playground?.destroy()
95+
})
96+
97+
return (
98+
<div ref={containerRef} class={props.class} style={props.style} data-height={props.height} />
99+
)
100+
}

0 commit comments

Comments
 (0)