Skip to content

Commit

Permalink
codeblock: make execute async, add canExecute, use hljs to determine …
Browse files Browse the repository at this point in the history
…language
  • Loading branch information
progrium committed Nov 11, 2024
1 parent 30ece9b commit 1b98212
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/com/codeblock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { Node } from "../model/mod.ts";
export interface CodeExecutor {
// executes the source and returns an output string.
// exceptions in execution should be caught and returned as a string.
execute(source: string, options: ExecuteOptions): string;
async execute(source: string, options: ExecuteOptions): string;

canExecute(options: ExecuteOptions): boolean;
}

export interface ExecuteOptions {
Expand All @@ -14,11 +16,18 @@ export interface ExecuteOptions {

// defaultExecutor can be replaced with an external service, etc
export let defaultExecutor: CodeExecutor = {
execute: (source: string, options: ExecuteOptions): string => {
async execute(source: string, options: ExecuteOptions): Promise<string> {
if (options.language !== "javascript") {
return `Unsupported language: ${options.language}`;
}
return JSON.stringify(window.eval(source));
},

canExecute(options: ExecuteOptions): boolean {
if (options.language === "javascript") {
return true;
}
return false;
}
}

Expand All @@ -27,9 +36,11 @@ export let defaultExecutor: CodeExecutor = {
@component
export class CodeBlock {
code: string;
language: string;

constructor() {
this.code = "";
this.language = "";
}

childrenView() {
Expand Down Expand Up @@ -75,6 +86,7 @@ const CodeEditor = {
// let's do it by this hack.
editor.textContent = editor.textContent;
window.hljs.highlightBlock(editor);
snippet.language = window.hljs.highlightAuto(editor.textContent).language || "";
});
dom.jarEditor.updateCode(snippet.code);
dom.jarEditor.onUpdate(code => {
Expand Down

0 comments on commit 1b98212

Please sign in to comment.