From 933a897ae69e99500bc0f11dd028dbba8c264750 Mon Sep 17 00:00:00 2001 From: sanchezcarlosjr <24639141+sanchezcarlosjr@users.noreply.github.com> Date: Sun, 19 Nov 2023 08:55:21 -0800 Subject: [PATCH] feat: solve html entities when it would be python --- .../languages/JavaScriptMainThread.ts | 3 ++- .../notebook/cellTypes/languages/Python.ts | 20 ++-------------- src/app/notebook/cellTypes/stringToHTML.ts | 23 ++++++++++++++++++- src/app/notebook/notebook.component.html | 3 ++- src/app/notebook/notebook.component.ts | 6 +++++ 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/app/notebook/cellTypes/languages/JavaScriptMainThread.ts b/src/app/notebook/cellTypes/languages/JavaScriptMainThread.ts index b447105..be65137 100644 --- a/src/app/notebook/cellTypes/languages/JavaScriptMainThread.ts +++ b/src/app/notebook/cellTypes/languages/JavaScriptMainThread.ts @@ -1,6 +1,7 @@ import { exec } from "../../shell/exec"; import { JavaScript } from "./JavaScript"; import '../../shell/protocols'; +import {stringify} from "../stringToHTML"; export class JavaScriptMainThread extends JavaScript { override dispatchShellRun() { @@ -14,4 +15,4 @@ export class JavaScriptMainThread extends JavaScript { override get name() { return 'javascript main thread'; } -} \ No newline at end of file +} diff --git a/src/app/notebook/cellTypes/languages/Python.ts b/src/app/notebook/cellTypes/languages/Python.ts index 28b83a4..e1fbd53 100644 --- a/src/app/notebook/cellTypes/languages/Python.ts +++ b/src/app/notebook/cellTypes/languages/Python.ts @@ -2,23 +2,7 @@ import {Language} from "./language"; import {Observable, shareReplay} from "rxjs"; import {Extension} from "@codemirror/state"; import {python} from "@codemirror/lang-python"; - -/** - * @param {string} key - * @param {any} value - */ -function jsonStringifyToObjectReplacer(key: string, value: any) { - if (value && value.toObject) { - return value.toObject(); - } - if (value && value.toJs) { - return value.toString(); - } - if (value && value.toJSON) { - return value.toJSON(); - } - return value; -} +import {jsonStringifyToObjectReplacer, stringify} from "../stringToHTML"; const pyodide = new Observable<{ @@ -79,7 +63,7 @@ ${this.mostRecentCode}`; await instance.runPythonAsync(code) .then((output: any) => { if (output !== undefined) { - this.write(JSON.stringify(output, jsonStringifyToObjectReplacer)); + this.write(stringify(JSON.stringify(output, jsonStringifyToObjectReplacer))); } this.stop(); }).catch((e: any) => { diff --git a/src/app/notebook/cellTypes/stringToHTML.ts b/src/app/notebook/cellTypes/stringToHTML.ts index c072e25..4971474 100644 --- a/src/app/notebook/cellTypes/stringToHTML.ts +++ b/src/app/notebook/cellTypes/stringToHTML.ts @@ -6,6 +6,27 @@ */ export function stringToHTML(str: string): ChildNode { const parser = new DOMParser(); - const doc = parser.parseFromString(str.replace(/[\u00A0-\u9999<>&]/g, i => '&#'+i.charCodeAt(0)+';'), 'text/html'); + const doc = parser.parseFromString(str, 'text/html'); return doc.body.firstChild as ChildNode; } + +/** + * @param {string} key + * @param {any} value + */ +export function jsonStringifyToObjectReplacer(key: string, value: any) { + if (value && value.toObject) { + return value.toObject(); + } + if (value && value.toJs) { + return value.toString().replace(/[\u00A0-\u9999<>&]/g, (i: string) => '&#' + i.charCodeAt(0) + ';'); + } + if (value && value.toJSON) { + return value.toJSON(); + } + return value; +} + +export function stringify(value: any) { + return JSON.stringify(value, jsonStringifyToObjectReplacer); +} diff --git a/src/app/notebook/notebook.component.html b/src/app/notebook/notebook.component.html index bb449b2..ea8f3f9 100644 --- a/src/app/notebook/notebook.component.html +++ b/src/app/notebook/notebook.component.html @@ -1,4 +1,4 @@ - + @@ -16,6 +16,7 @@ + diff --git a/src/app/notebook/notebook.component.ts b/src/app/notebook/notebook.component.ts index 84d1aa7..71b00e2 100644 --- a/src/app/notebook/notebook.component.ts +++ b/src/app/notebook/notebook.component.ts @@ -41,6 +41,7 @@ export class NotebookComponent implements OnInit { isMode2: boolean = true; loading: boolean = true; name: string = ""; + hideMainToolbar = false; constructor( private injector: Injector, private _snackBar: MatSnackBar, @@ -52,6 +53,7 @@ export class NotebookComponent implements OnInit { async ngOnInit() { const EditorJS = await import("@editorjs/editorjs"); this.isMode2 = url.read("m") === "2"; + this.hideMainToolbar = this.isMode2; const editor = new EditorJS.default({ holder: 'editor-js', autofocus: true, @@ -272,4 +274,8 @@ export class NotebookComponent implements OnInit { openRecent() { this.dialog.open(HistoryComponent); } + + modeZen() { + this.hideMainToolbar = true; + } }