From e5dd7194ba2d884c32f27e498fddda05a73c25e5 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Wed, 23 Aug 2023 09:47:19 +0200 Subject: [PATCH] adding universe encoding --- index.html | 2 +- src/AppSettings.ts | 146 +++++++++++++++++++++++---------------------- src/main.ts | 50 ++++++++++++++-- 3 files changed, 122 insertions(+), 76 deletions(-) diff --git a/index.html b/index.html index d6d006b..34894c1 100644 --- a/index.html +++ b/index.html @@ -35,7 +35,7 @@
- + diff --git a/src/AppSettings.ts b/src/AppSettings.ts index 3bfb82a..09731f8 100644 --- a/src/AppSettings.ts +++ b/src/AppSettings.ts @@ -1,39 +1,39 @@ -import { tutorial_universe } from "./universes/tutorial" +import { tutorial_universe } from "./universes/tutorial"; -export type Universes = { [key: string]: Universe } +export type Universes = { [key: string]: Universe }; export interface Universe { /** * Universe is a collection of files. - * + * * @param global - Global file * @param locals - Local files * @param init - Init file * @param notes - Notes file */ - global: File - locals: { [key: number]: File } - init: File - notes: File + global: File; + locals: { [key: number]: File }; + init: File; + notes: File; } export interface File { /** * A File is a set of the same text in different states. - * + * * @param candidate - The text that is being edited * @param committed - The text that has been committed (e.g. stable) * @param evaluations - The number of times the text has been evaluated */ - candidate: string - committed?: string - evaluations?: number + candidate: string; + committed?: string; + evaluations?: number; } export interface Settings { /** * Settings for the Topos application. - * + * * @param vimMode - Whether or not to use vim keybindings * @param theme - The name of the theme to use * @param font - The name of the font to use @@ -42,60 +42,58 @@ export interface Settings { * @param selected_universe - The name of the selected universe * @param line_numbers - Whether or not to show line numbers */ - vimMode: boolean - theme: string - font: string - font_size: number - universes: Universes - selected_universe: string - line_numbers: boolean + vimMode: boolean; + theme: string; + font: string; + font_size: number; + universes: Universes; + selected_universe: string; + line_numbers: boolean; } export const template_universe = { global: { candidate: "", committed: "", evaluations: 0 }, locals: { - 1: { candidate: "", committed: "", evaluations: 0}, - 2: { candidate: "", committed: "", evaluations: 0}, - 3: { candidate: "", committed: "", evaluations: 0}, - 4: { candidate: "", committed: "", evaluations: 0}, - 5: { candidate: "", committed: "", evaluations: 0}, - 6: { candidate: "", committed: "", evaluations: 0}, - 7: { candidate: "", committed: "", evaluations: 0}, - 8: { candidate: "", committed: "", evaluations: 0}, - 9: { candidate: "", committed: "", evaluations: 0}, + 1: { candidate: "", committed: "", evaluations: 0 }, + 2: { candidate: "", committed: "", evaluations: 0 }, + 3: { candidate: "", committed: "", evaluations: 0 }, + 4: { candidate: "", committed: "", evaluations: 0 }, + 5: { candidate: "", committed: "", evaluations: 0 }, + 6: { candidate: "", committed: "", evaluations: 0 }, + 7: { candidate: "", committed: "", evaluations: 0 }, + 8: { candidate: "", committed: "", evaluations: 0 }, + 9: { candidate: "", committed: "", evaluations: 0 }, }, init: { candidate: "", committed: "", evaluations: 0 }, notes: { candidate: "" }, -} +}; export const template_universes = { - "Default": { + Default: { global: { candidate: "", committed: "", evaluations: 0 }, locals: { - 1: { candidate: "", committed: "", evaluations: 0}, - 2: { candidate: "", committed: "", evaluations: 0}, - 3: { candidate: "", committed: "", evaluations: 0}, - 4: { candidate: "", committed: "", evaluations: 0}, - 5: { candidate: "", committed: "", evaluations: 0}, - 6: { candidate: "", committed: "", evaluations: 0}, - 7: { candidate: "", committed: "", evaluations: 0}, - 8: { candidate: "", committed: "", evaluations: 0}, - 9: { candidate: "", committed: "", evaluations: 0}, + 1: { candidate: "", committed: "", evaluations: 0 }, + 2: { candidate: "", committed: "", evaluations: 0 }, + 3: { candidate: "", committed: "", evaluations: 0 }, + 4: { candidate: "", committed: "", evaluations: 0 }, + 5: { candidate: "", committed: "", evaluations: 0 }, + 6: { candidate: "", committed: "", evaluations: 0 }, + 7: { candidate: "", committed: "", evaluations: 0 }, + 8: { candidate: "", committed: "", evaluations: 0 }, + 9: { candidate: "", committed: "", evaluations: 0 }, }, init: { candidate: "", committed: "", evaluations: 0 }, notes: { candidate: "// NOTES" }, }, - "Help": tutorial_universe, -} - - -export class AppSettings { + Help: tutorial_universe, +}; +export class AppSettings { /** * AppSettings is a class that stores the settings for the Topos application. * It is in charge of reading and writing to local storage and exposing that * information to the main application. - * + * * @param vimMode - Whether or not to use vim keybindings * @param theme - The name of the theme to use * @param font - The name of the font to use @@ -105,33 +103,36 @@ export class AppSettings { * @param line_numbers - Whether or not to show line numbers */ - public vimMode: boolean = false - public theme: string = "materialDark" - public font: string = "SpaceMono" - public font_size: number = 22 - public universes: Universes - public selected_universe: string = "Default" - public line_numbers: boolean = true + public vimMode: boolean = false; + public theme: string = "materialDark"; + public font: string = "SpaceMono"; + public font_size: number = 22; + public universes: Universes; + public selected_universe: string = "Default"; + public line_numbers: boolean = true; constructor() { + const settingsFromStorage = JSON.parse( + localStorage.getItem("topos") || "{}" + ); - const settingsFromStorage = JSON.parse(localStorage.getItem('topos') || "{}"); - - if (settingsFromStorage && Object.keys(settingsFromStorage).length !== 0) { + if (settingsFromStorage && Object.keys(settingsFromStorage).length !== 0) { // let settings = JSON.parse(localStorage.getItem("topos") as string) - this.vimMode = settingsFromStorage.vimMode - this.theme = settingsFromStorage.theme - this.font = settingsFromStorage.font - this.font_size = settingsFromStorage.font_size - this.universes = settingsFromStorage.universes - this.selected_universe = settingsFromStorage.selected_universe - this.line_numbers = settingsFromStorage.line_numbers + this.vimMode = settingsFromStorage.vimMode; + this.theme = settingsFromStorage.theme; + this.font = settingsFromStorage.font; + this.font_size = settingsFromStorage.font_size; + this.universes = settingsFromStorage.universes; + this.selected_universe = settingsFromStorage.selected_universe; + this.line_numbers = settingsFromStorage.line_numbers; } else { - this.universes = template_universes + this.universes = template_universes; } - } + get_universe(universe_name: string) { + this.universes.universe_name; + } get data(): Settings { /** @@ -144,15 +145,18 @@ export class AppSettings { font_size: this.font_size, universes: this.universes, selected_universe: this.selected_universe, - line_numbers: this.line_numbers - } + line_numbers: this.line_numbers, + }; } - saveApplicationToLocalStorage(universes: Universes, settings: Settings): void{ + saveApplicationToLocalStorage( + universes: Universes, + settings: Settings + ): void { /** * Main method to store the application to local storage. - * - * @param universes - The universes to save + * + * @param universes - The universes to save * @param settings - The settings to save */ this.universes = universes; @@ -161,6 +165,6 @@ export class AppSettings { this.font_size = settings.font_size; this.selected_universe = settings.selected_universe; this.line_numbers = settings.line_numbers; - localStorage.setItem('topos', JSON.stringify(this.data)) + localStorage.setItem("topos", JSON.stringify(this.data)); } -} \ No newline at end of file +} diff --git a/src/main.ts b/src/main.ts index d4cdb19..2e900b9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import { javascript } from "@codemirror/lang-javascript"; import { oneDark } from "@codemirror/theme-one-dark"; import { markdown } from "@codemirror/lang-markdown"; import { Extension, Prec } from "@codemirror/state"; -import {indentWithTab} from "@codemirror/commands" +import { indentWithTab } from "@codemirror/commands"; import { vim } from "@replit/codemirror-vim"; import { AppSettings } from "./AppSettings"; import { editorSetup } from "./EditorSetup"; @@ -164,6 +164,11 @@ export class Editor { "vim-mode" ) as HTMLButtonElement; + // Share button + share_button: HTMLElement = document.getElementById( + "share_button" + ) as HTMLElement; + // Error line error_line: HTMLElement = document.getElementById( "error_line" @@ -229,9 +234,16 @@ export class Editor { ...this.editorExtensions, EditorView.lineWrapping, dynamicPlugins.of(this.userPlugins), - Prec.highest(keymap.of([ - { key:"Ctrl-Enter", run: ()=>{return true} } - ])), + Prec.highest( + keymap.of([ + { + key: "Ctrl-Enter", + run: () => { + return true; + }, + }, + ]) + ), keymap.of([indentWithTab]), ], doc: this.universes[this.selected_universe].locals[this.local_index] @@ -484,6 +496,20 @@ export class Editor { this.settings.font_size = parseInt(new_value); }); + this.share_button.addEventListener("click", () => { + this.share_button.classList.add("animate-spin"); + setInterval( + () => this.share_button.classList.remove("animate-spin"), + 1000 + ); + // trigger a manual save + this.currentFile().candidate = app.view.state.doc.toString(); + this.currentFile().committed = app.view.state.doc.toString(); + this.settings.saveApplicationToLocalStorage(app.universes, app.settings); + // encode as a blob! + this.share(); + }); + this.normal_mode_button.addEventListener("click", () => { this.settings.vimMode = false; this.view.dispatch({ effects: this.vimModeCompartment.reconfigure([]) }); @@ -564,6 +590,22 @@ export class Editor { ]; } + emptyUrl = () => { + window.history.replaceState({}, document.title, "/"); + }; + + share() { + const hashed_table = JSON.stringify( + this.settings.universes[this.selected_universe] + ); + const url = new URL(window.location.href); + url.searchParams.set( + "universe", + this.selected_universe + "-" + hashed_table + ); + window.history.replaceState({}, "", url.toString()); + } + showDocumentation() { if (document.getElementById("app")?.classList.contains("hidden")) { document.getElementById("app")?.classList.remove("hidden");