-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolid-repl.tsx
More file actions
133 lines (112 loc) · 3.23 KB
/
solid-repl.tsx
File metadata and controls
133 lines (112 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Show } from 'solid-js/web';
import { JSX, createMemo, splitProps } from 'solid-js';
import { compressToURL } from '@amoutonbrady/lz-string';
function uid() {
const [ts, rand] = [Date.now(), Math.random()].map((value) =>
value.toString(36),
);
return (ts + rand).replace(/\./g, '');
}
function childrensToArray<T = unknown>(children: unknown | unknown[]): T[] {
return [...(Array.isArray(children) ? children : [children])];
}
function formatCode(code: string) {
const lines = code.split('\n');
let mindent: number | null = null;
let result = code.replace(/\\\n[ \t]*/g, '').replace(/\\`/g, '`');
for (const line of lines) {
const m = line.match(/^(\s+)\S+/);
if (!m) continue;
const indent = m[1].length;
mindent = mindent ? Math.min(mindent, indent) : indent;
}
if (mindent !== null) {
result = lines
.map((line) => (line[0] === ' ' ? line.slice(mindent) : line))
.join('\n');
}
return result.trim().replace(/\\n/g, '\n');
}
export const ReplTab = (props: {
name: string;
children?: unknown | unknown[];
}) => {
const id = uid();
return createMemo(() => {
const source = childrensToArray(props.children).join('');
return {
id,
name: props.name,
source: formatCode(source),
type: 'tsx',
} as unknown as JSX.Element;
});
};
export const Repl = (props: ReplOptions) => {
const [internal, external] = splitProps(props, [
'data',
'height',
'baseUrl',
'children',
'isInteractive',
'withHeader',
'edtiableTabs',
'withActionBar',
'layout',
]);
const tabs = createMemo(() => {
if (!internal.children) return [];
return childrensToArray<() => Tab>(internal.children).map((tab) => tab());
});
const src = createMemo(() => {
const url = new URL(internal.baseUrl || 'https://playground.solidjs.com');
if (!internal.withHeader) url.searchParams.set('noHeader', 'true');
if (!internal.isInteractive) url.searchParams.set('noInteractive', 'true');
if (internal.data) url.searchParams.set('data', internal.data);
if (!internal.edtiableTabs) url.searchParams.set('noEditableTabs', 'true')
if (!internal.withActionBar) url.searchParams.set('noActionBar', 'true')
if (internal.layout === 'vertical') url.searchParams.set('isHorizontal', 'true')
if (tabs().length) {
url.hash = compressToURL(JSON.stringify(tabs()));
}
return url.toString();
});
return (
<Show
when={internal.data || tabs().length}
fallback={<p>The REPL needs to have at least one tab</p>}
>
<iframe
{...external}
title="Solid REPL"
src={src()}
style={{
width: '100%',
height:
typeof internal.height === 'number'
? `${internal.height}px`
: internal.height,
border: 0,
}}
/>
</Show>
);
};
export interface ReplOptions
extends JSX.IframeHTMLAttributes<HTMLIFrameElement> {
baseUrl?: string;
height?: number | string;
isInteractive?: boolean;
withHeader?: boolean;
withActionBar?: boolean;
layout?: 'vertical' | 'responsive';
edtiableTabs?: boolean;
data?: string;
children?: any;
}
export interface Tab {
id: string;
name: string;
type: string;
source: string;
}