diff --git a/docs/components/CodeExample.client.vue b/docs/components/CodeExample.client.vue index e4ea3f6..a62d489 100644 --- a/docs/components/CodeExample.client.vue +++ b/docs/components/CodeExample.client.vue @@ -7,7 +7,7 @@ const code = ref("") const el = ref(null) const value = await import(`../examples/${props.file}.ts?raw`) code.value = value.default -const result = ref() +const result = ref>([]) const error = ref("") const stopWatch = watch(el, () => { @@ -31,6 +31,7 @@ const stopWatch = watch(el, () => { lineNumbers: "off", glyphMargin: false, folding: false, + renderLineHighlight: "none", }) stopWatch() @@ -55,7 +56,7 @@ const stopWatch = watch(el, () => { if (code === "") return code = processPlaygroundCode(code) if (worker) worker.terminate() - result.value = "" + result.value = [] error.value = "" worker = new Worker( @@ -64,20 +65,32 @@ const stopWatch = watch(el, () => { type: "module", } ) + worker.postMessage(code) - worker.onmessage = (e) => { - result.value = e.data + worker.onmessage = (e: { + data: { lineNumber: number; value: unknown } + }) => { + result.value[e.data.lineNumber] ??= [] + if (e.data.value instanceof Date) { + result.value[e.data.lineNumber].push( + `Date: ${e.data.value.toISOString()}` + ) + } else { + result.value[e.data.lineNumber].push(String(e.data.value)) + } } worker.onerror = (e) => { - result.value = "" + result.value = [] error.value = "Your code contains errors." } } + let debounceTimer: NodeJS.Timeout | number | null = null editor.onDidChangeModelContent(() => { code.value = editor.getValue() updateSize() - runInsideWorker(code.value) + if (debounceTimer) clearTimeout(debounceTimer) + debounceTimer = setTimeout(() => runInsideWorker(code.value), 500) }) updateSize() @@ -89,8 +102,12 @@ const stopWatch = watch(el, () => {