@codespark/react 1.0.6 → 1.0.7
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/codemirror.d.ts +6249 -3
- package/dist/codemirror.js +5 -5
- package/dist/index.d.ts +6256 -10
- package/dist/index.js +38 -38
- package/dist/monaco.d.ts +6250 -4
- package/dist/monaco.js +22 -22
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -131,10 +131,10 @@ var Workspace = class extends OPFS {
|
|
|
131
131
|
constructor(config) {
|
|
132
132
|
super();
|
|
133
133
|
this.config = config;
|
|
134
|
-
const { id, OPFS
|
|
134
|
+
const { id, OPFS = false, files } = config;
|
|
135
135
|
this.id = id || "";
|
|
136
136
|
this.initialFiles = { ...files };
|
|
137
|
-
if (OPFS
|
|
137
|
+
if (OPFS) super.initOPFS(this.files);
|
|
138
138
|
}
|
|
139
139
|
notifyListeners() {
|
|
140
140
|
this.listeners.forEach((fn) => fn());
|
|
@@ -320,14 +320,14 @@ var Workspace = class extends OPFS {
|
|
|
320
320
|
* @returns Workspace state including files, compiled code, and the workspace instance
|
|
321
321
|
*/
|
|
322
322
|
function useWorkspace(init) {
|
|
323
|
-
const uid
|
|
323
|
+
const uid = useId();
|
|
324
324
|
const context = useCodespark();
|
|
325
325
|
const workspace = useMemo(() => {
|
|
326
326
|
let ws;
|
|
327
327
|
if (init instanceof Workspace) ws = init;
|
|
328
328
|
else if (init) ws = new Workspace(init);
|
|
329
329
|
else ws = context?.workspace;
|
|
330
|
-
if (!ws?.id) ws?.[INTERNAL_SET_ID](`workspace${uid
|
|
330
|
+
if (!ws?.id) ws?.[INTERNAL_SET_ID](`workspace${uid}`);
|
|
331
331
|
return ws;
|
|
332
332
|
}, []);
|
|
333
333
|
if (!workspace) throw Error("Can not find any workspace instance. Make sure provide a workspace during runtime.");
|
|
@@ -344,9 +344,9 @@ function useWorkspace(init) {
|
|
|
344
344
|
const currentFile = useSyncExternalStore(subscribe, () => workspace.currentFile, () => workspace.currentFile);
|
|
345
345
|
const derivedState = useMemo(() => {
|
|
346
346
|
if (context) {
|
|
347
|
-
const { fileTree
|
|
347
|
+
const { fileTree, vendor, compiled, compileError } = context;
|
|
348
348
|
return {
|
|
349
|
-
fileTree
|
|
349
|
+
fileTree,
|
|
350
350
|
vendor,
|
|
351
351
|
compiled,
|
|
352
352
|
compileError
|
|
@@ -393,13 +393,13 @@ function useWorkspace(init) {
|
|
|
393
393
|
return root;
|
|
394
394
|
})();
|
|
395
395
|
try {
|
|
396
|
-
framework.analyze(
|
|
397
|
-
const compiled = framework.compile();
|
|
396
|
+
framework.analyze(files);
|
|
397
|
+
const compiled = framework.compile(workspace.entry);
|
|
398
398
|
workspace[INTERNAL_EMIT]("compiled", compiled);
|
|
399
399
|
const modules = framework.getOutput(LoaderType.ESModule);
|
|
400
400
|
const styles = framework.getOutput(LoaderType.Style);
|
|
401
401
|
const scripts = framework.getOutput(LoaderType.Script);
|
|
402
|
-
const externals = modules.flatMap(({ externals
|
|
402
|
+
const externals = modules.flatMap(({ externals }) => externals).reduce((acc, { name, imported }) => {
|
|
403
403
|
if (!acc[name]) acc[name] = /* @__PURE__ */ new Set();
|
|
404
404
|
imported.forEach((i) => acc[name].add(i));
|
|
405
405
|
return acc;
|
|
@@ -542,10 +542,10 @@ function CodesparkProvider(props) {
|
|
|
542
542
|
|
|
543
543
|
//#endregion
|
|
544
544
|
//#region src/lib/editor-adapter/index.ts
|
|
545
|
-
let EditorEngine = /* @__PURE__ */ function(EditorEngine
|
|
546
|
-
EditorEngine
|
|
547
|
-
EditorEngine
|
|
548
|
-
return EditorEngine
|
|
545
|
+
let EditorEngine = /* @__PURE__ */ function(EditorEngine) {
|
|
546
|
+
EditorEngine[EditorEngine["Monaco"] = 0] = "Monaco";
|
|
547
|
+
EditorEngine[EditorEngine["CodeMirror"] = 1] = "CodeMirror";
|
|
548
|
+
return EditorEngine;
|
|
549
549
|
}({});
|
|
550
550
|
|
|
551
551
|
//#endregion
|
|
@@ -574,7 +574,7 @@ const buttonVariants = cva("inline-flex items-center justify-center gap-2 whites
|
|
|
574
574
|
size: "default"
|
|
575
575
|
}
|
|
576
576
|
});
|
|
577
|
-
const Button = React.forwardRef(function Button
|
|
577
|
+
const Button = React.forwardRef(function Button({ className, variant = "default", size = "default", asChild = false, ...props }, ref) {
|
|
578
578
|
return /* @__PURE__ */ jsx(asChild ? Slot.Root : "button", {
|
|
579
579
|
"data-slot": "button",
|
|
580
580
|
"data-variant": variant,
|
|
@@ -731,7 +731,7 @@ const LANGUAGE_EXTENSIONS = {
|
|
|
731
731
|
};
|
|
732
732
|
const CodeMirror = {
|
|
733
733
|
kind: EditorEngine.CodeMirror,
|
|
734
|
-
Component: memo(function CodeMirror
|
|
734
|
+
Component: memo(function CodeMirror(props) {
|
|
735
735
|
const { id, basicSetup, extensions = [], width, height, fontFamily, lang, onMount, ...rest } = props;
|
|
736
736
|
const [mounted, setMounted] = useState(false);
|
|
737
737
|
const editorRef = useRef(null);
|
|
@@ -794,8 +794,8 @@ function CodesparkEditor(props) {
|
|
|
794
794
|
entry: "./App.tsx",
|
|
795
795
|
files: { "./App.tsx": value }
|
|
796
796
|
}));
|
|
797
|
-
const uid
|
|
798
|
-
const editorId = id ?? `editor${uid
|
|
797
|
+
const uid = useId();
|
|
798
|
+
const editorId = id ?? `editor${uid}`;
|
|
799
799
|
const editorRef = useRef(null);
|
|
800
800
|
const currentFileRef = useLatest(currentFile);
|
|
801
801
|
const { copyToClipboard, isCopied } = useCopyToClipboard();
|
|
@@ -838,14 +838,14 @@ function CodesparkEditor(props) {
|
|
|
838
838
|
workspace[INTERNAL_REGISTER_EDITOR](editorId, adapter);
|
|
839
839
|
}, []);
|
|
840
840
|
const renderEditor = () => {
|
|
841
|
-
const id
|
|
841
|
+
const id = `${workspace.id}${editorId}`;
|
|
842
842
|
if (editor.kind === EditorEngine.Monaco && propsTypeGuard(props, editor, EditorEngine.Monaco)) {
|
|
843
843
|
const { height, width, wrapperProps, options, onChange, onMount } = props;
|
|
844
844
|
const { fontFamily = globalFontFamily } = options || {};
|
|
845
845
|
return /* @__PURE__ */ jsx(editor.Component, {
|
|
846
|
-
id
|
|
846
|
+
id,
|
|
847
847
|
value: value || currentFile.code,
|
|
848
|
-
path: `file:///${id
|
|
848
|
+
path: `file:///${id}/${currentFile.path.replace(/^(\.\.?\/)+/, "")}`,
|
|
849
849
|
theme: theme === "light" ? "vitesse-light" : "vitesse-dark",
|
|
850
850
|
files,
|
|
851
851
|
imports: vendor.imports,
|
|
@@ -876,7 +876,7 @@ function CodesparkEditor(props) {
|
|
|
876
876
|
if (editor.kind === EditorEngine.CodeMirror && propsTypeGuard(props, editor, EditorEngine.CodeMirror)) {
|
|
877
877
|
const { height, width, basicSetup, fontFamily, readOnly, onChange, onMount } = props;
|
|
878
878
|
return /* @__PURE__ */ jsx(editor.Component, {
|
|
879
|
-
id
|
|
879
|
+
id,
|
|
880
880
|
className,
|
|
881
881
|
value: value || currentFile.code,
|
|
882
882
|
height: height ?? "200px",
|
|
@@ -1165,11 +1165,11 @@ function useInjections(children) {
|
|
|
1165
1165
|
Children.forEach(children, (child) => {
|
|
1166
1166
|
if (isValidElement(child)) {
|
|
1167
1167
|
if (child.type === Style) {
|
|
1168
|
-
const { children
|
|
1169
|
-
result.push(`<style${serializeAttributes(attrs)}>${children
|
|
1168
|
+
const { children, ...attrs } = child.props;
|
|
1169
|
+
result.push(`<style${serializeAttributes(attrs)}>${children?.trim() || ""}</style>`);
|
|
1170
1170
|
} else if (child.type === Script) {
|
|
1171
|
-
const { children
|
|
1172
|
-
result.push(`<script${serializeAttributes(attrs)}>${children
|
|
1171
|
+
const { children, ...attrs } = child.props;
|
|
1172
|
+
result.push(`<script${serializeAttributes(attrs)}>${children?.trim() || ""}<\/script>`);
|
|
1173
1173
|
} else if (child.type === Link) {
|
|
1174
1174
|
const attrs = child.props;
|
|
1175
1175
|
result.push(`<link${serializeAttributes(attrs)} />`);
|
|
@@ -1428,13 +1428,13 @@ var TailwindCSSJit = class TailwindCSSJit {
|
|
|
1428
1428
|
return this.env.querySelectorAll(`style[type="${TailwindCSSJit.STYLE_TYPE}"]`);
|
|
1429
1429
|
}
|
|
1430
1430
|
async createCompiler() {
|
|
1431
|
-
let css
|
|
1432
|
-
for (const sheet of this.stylesheets) css
|
|
1433
|
-
if (!css
|
|
1434
|
-
if (this.lastCss === css
|
|
1435
|
-
this.lastCss = css
|
|
1431
|
+
let css = "";
|
|
1432
|
+
for (const sheet of this.stylesheets) css += sheet.textContent + "\n";
|
|
1433
|
+
if (!css.includes("@import")) css = `@import "tailwindcss";${css}`;
|
|
1434
|
+
if (this.lastCss === css) return;
|
|
1435
|
+
this.lastCss = css;
|
|
1436
1436
|
try {
|
|
1437
|
-
this.compiler = await tailwindcss.compile(css
|
|
1437
|
+
this.compiler = await tailwindcss.compile(css, {
|
|
1438
1438
|
base: "/",
|
|
1439
1439
|
loadStylesheet: this.loadStylesheet,
|
|
1440
1440
|
loadModule: this.loadModule
|
|
@@ -1522,8 +1522,8 @@ function useTailwindCSS() {
|
|
|
1522
1522
|
});
|
|
1523
1523
|
};
|
|
1524
1524
|
const rebuild = async (kind) => {
|
|
1525
|
-
const css
|
|
1526
|
-
if (css
|
|
1525
|
+
const css = await jitRef.current?.rebuild(kind);
|
|
1526
|
+
if (css !== void 0 && sheetRef.current) sheetRef.current.textContent = css;
|
|
1527
1527
|
};
|
|
1528
1528
|
const mount = (doc) => {
|
|
1529
1529
|
jitRef.current ??= new TailwindCSSJit(doc);
|
|
@@ -1623,7 +1623,7 @@ body {
|
|
|
1623
1623
|
function CodesparkPreview(props) {
|
|
1624
1624
|
const { imports: globalImports, theme: globalTheme } = useConfig();
|
|
1625
1625
|
const { workspace: contextWorkspace, imports: contextImports, theme: contextTheme, framework: contextFramework } = useCodespark() || {};
|
|
1626
|
-
const { code = "", framework = contextFramework, className, tailwindcss
|
|
1626
|
+
const { code = "", framework = contextFramework, className, tailwindcss = true, preflight = true, imports, theme = contextTheme ?? globalTheme ?? "light", children, height, onError, onLoad, onRendered, onConsole } = props;
|
|
1627
1627
|
const { compiled, vendor, workspace } = useWorkspace(props.workspace ?? contextWorkspace ?? new Workspace({
|
|
1628
1628
|
entry: "./App.tsx",
|
|
1629
1629
|
files: { "./App.tsx": code },
|
|
@@ -1653,12 +1653,12 @@ function CodesparkPreview(props) {
|
|
|
1653
1653
|
onConsole
|
|
1654
1654
|
});
|
|
1655
1655
|
useEffect(() => {
|
|
1656
|
-
if (!tailwindcss
|
|
1656
|
+
if (!tailwindcss) unmountTailwind();
|
|
1657
1657
|
else readyRef.current.promise.then((doc) => {
|
|
1658
1658
|
if (doc) mountTailwind(doc);
|
|
1659
1659
|
});
|
|
1660
1660
|
return unmountTailwind;
|
|
1661
|
-
}, [tailwindcss
|
|
1661
|
+
}, [tailwindcss]);
|
|
1662
1662
|
useEffect(() => {
|
|
1663
1663
|
if (typeof window === "undefined" || !code) return;
|
|
1664
1664
|
workspace.setFile(workspace.entry, code);
|
|
@@ -1704,7 +1704,7 @@ registerFramework(react);
|
|
|
1704
1704
|
* Supports both single-file mode (via `code` prop) and multi-file mode (via `files` prop).
|
|
1705
1705
|
*/
|
|
1706
1706
|
function Codespark(props) {
|
|
1707
|
-
const { code, files, name = "./App.tsx", theme, editor, framework = "react", showEditor = true, showPreview = true, showFileExplorer = true, readonly: readOnly, className, toolbox, tailwindcss
|
|
1707
|
+
const { code, files, name = "./App.tsx", theme, editor, framework = "react", showEditor = true, showPreview = true, showFileExplorer = true, readonly: readOnly, className, toolbox, tailwindcss, onConsole, onError, children, defaultExpanded, getWorkspace, editorHeight, previewHeight, orientation = "vertical", preflight } = props;
|
|
1708
1708
|
const { workspace, fileTree, compileError } = useWorkspace({
|
|
1709
1709
|
entry: name,
|
|
1710
1710
|
files: files ?? { [name]: code || "" },
|
|
@@ -1757,7 +1757,7 @@ function Codespark(props) {
|
|
|
1757
1757
|
children: [/* @__PURE__ */ jsxs("div", {
|
|
1758
1758
|
className: cn("border-border relative", showPreview && showEditor ? orientation === "vertical" ? "border-b" : "border-l" : "", orientation === "horizontal" && "order-2"),
|
|
1759
1759
|
children: [showPreview ? /* @__PURE__ */ jsx(CodesparkPreview, {
|
|
1760
|
-
tailwindcss
|
|
1760
|
+
tailwindcss,
|
|
1761
1761
|
onConsole,
|
|
1762
1762
|
onError: (error) => {
|
|
1763
1763
|
onError?.(error);
|