From 19a49ca10aea6a2701e12461efd01bc12f733415 Mon Sep 17 00:00:00 2001 From: Okiki Ojo Date: Sat, 18 Jun 2022 09:22:40 +0000 Subject: [PATCH] chore: ... --- .../MainSection/EditorSection/Editor.tsx | 4 ++-- .../EditorSection/EditorButtons.tsx | 6 ++--- .../website/src/scripts/workers/other-ts.ts | 22 ++++++------------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/packages/website/src/components/MainSection/EditorSection/Editor.tsx b/packages/website/src/components/MainSection/EditorSection/Editor.tsx index 7afaf99..22ea0da 100644 --- a/packages/website/src/components/MainSection/EditorSection/Editor.tsx +++ b/packages/website/src/components/MainSection/EditorSection/Editor.tsx @@ -8,12 +8,12 @@ import { state, setState, initial } from "../store"; import { OtherTSWorkerClient } from "../../../scripts/clients/other-ts-client"; +let otherTSWorker = "document" in globalThis && new OtherTSWorkerClient(); + export function Editor(props?: ComponentProps<'div'>) { let ref: HTMLDivElement = null; let loadingRef: HTMLDivElement = null; - let otherTSWorker = "document" in globalThis && new OtherTSWorkerClient(); - onMount(() => { (async () => { diff --git a/packages/website/src/components/MainSection/EditorSection/EditorButtons.tsx b/packages/website/src/components/MainSection/EditorSection/EditorButtons.tsx index 1302ac3..843d693 100644 --- a/packages/website/src/components/MainSection/EditorSection/EditorButtons.tsx +++ b/packages/website/src/components/MainSection/EditorSection/EditorButtons.tsx @@ -141,12 +141,12 @@ export function EditorButtons() { (async () => { const worker = state.monaco.workers.other; const thisWorker = await worker.getWorker(); - + // @ts-ignore - thisWorker.writeFile(model.uri.toString(), model.getValue()); + const formattedCode = await thisWorker.format(model.uri.toString(), model.getValue()); // @ts-ignore - const formattedCode = await thisWorker.format(model.uri.toString()); + console.log(await thisWorker.getShareableURL(model.uri.toString(), formattedCode)) state.monaco.editor.setValue(formattedCode); })(); } catch (e) { diff --git a/packages/website/src/scripts/workers/other-ts.ts b/packages/website/src/scripts/workers/other-ts.ts index 62830b2..8c710b6 100644 --- a/packages/website/src/scripts/workers/other-ts.ts +++ b/packages/website/src/scripts/workers/other-ts.ts @@ -169,25 +169,17 @@ export class OtherTSWorker { this._inlayHintsOptions = createData.inlayHintsOptions; } - writeFile(fileName, text) { - let file = fileName.replace("file://", "/"); - // host.createFile(fileName, text); - system.writeFile(file, text); - console.log(fsMap.get(file), file); - console.log(system.readFile(file)); - console.log(program.getSourceFiles().map(x => x.fileName)); + createFile(fileName, content) { + return ts.createSourceFile(fileName, content, compilerOpts.target, false); } - async format(fileName) { - const source = program.getSourceFile(fileName); - - // program + async format(fileName, content) { if (!formatter) formatter = await getFormatter(); - return await Promise.resolve(formatter.formatText(fileName, source.getFullText())); + return await Promise.resolve(formatter.formatText(fileName, content)); } - async getShareableURL(fileName, config = "{}") { - const source = program.getSourceFile(fileName); + async getShareableURL(fileName, content, config = "{}") { + const source = this.createFile(fileName, content); config = JSON.parse(config ? config : "{}") ?? {}; // Basically only keep the config options that have changed from the default @@ -214,7 +206,7 @@ export class OtherTSWorker { ImportExportStatements.push({ kind: isImport ? "import" : "export", clause: clause?.getText() ?? "*", - module: node.moduleSpecifier.getText(), + module: node.moduleSpecifier?.getText(), pos: { start: node.pos, end: node.end, -- knotmirror 0.0.1