Skip to content

Commit

Permalink
Inline Console
Browse files Browse the repository at this point in the history
  • Loading branch information
wandyezj committed Sep 24, 2024
1 parent 0e1c64b commit 7309070
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/core/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export const settingsMetadata = {
defaultValue: "",
} as SettingString,

/**
* Show in run console.log in inline text.
*/
enableRunInlineConsole: {
name: "Enable Run Inline Console",
tooltip: "Useful for simple debugging without F12",
type: "boolean",
defaultValue: true,
} as SettingBoolean,

/**
* Show the samples in the open menu.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/core/settings/enableRunInlineConsole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getSetting } from "../setting";

/**
* Enable inline console
*/
export function enableRunInlineConsole(): boolean {
const enable = getSetting("enableRunInlineConsole");
return enable;
}
24 changes: 23 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { loadCurrentSnipToRun } from "./core/storage";
import { parseLibraries } from "./core/parseLibraries";
import { compileCode } from "./core/compileCode";
import { enableRunInlineConsole } from "./core/settings/enableRunInlineConsole";

console.log("run");

Expand Down Expand Up @@ -88,10 +89,31 @@ async function runSnip() {
}
// TODO: will need to compile TypeScript, where should this be done?
const ts = snip.files["typescript"].content;
const { js, issues } = compileCode(ts);
const results = compileCode(ts);
const { issues } = results;
let { js } = results;
console.log("Issues");
console.log(issues);

const displayConsole = enableRunInlineConsole();
if (displayConsole) {
html = html + `\n<br/><div id="console"></div>`;
js = `
const _____originalConsole = console;
(function () {
const console = {
log: function () {
_____originalConsole.log.apply(_____originalConsole, arguments);
const consoleDiv = document.getElementById("console");
const newLine = document.createElement("div");
newLine.textContent = Array.from(arguments).join(" ");
consoleDiv.appendChild(newLine);
}
};
${js}
})();`;
}

// Loading order matters
await loadLibraries(libraries);
loadCss(css);
Expand Down

0 comments on commit 7309070

Please sign in to comment.