From 4b0921342bdea4056b9780f73b1095379ad36b54 Mon Sep 17 00:00:00 2001 From: Jason Reed Date: Thu, 21 Mar 2024 15:18:32 -0400 Subject: [PATCH] Add support for getUrl FragmentAction --- src/encoding.ts | 8 +++++++- src/index.ts | 13 ++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/encoding.ts b/src/encoding.ts index 914f79d..5bb6f26 100644 --- a/src/encoding.ts +++ b/src/encoding.ts @@ -49,8 +49,14 @@ export async function encode(text: string): Promise { return encodeWithJsonz({ t: 'setTextAndExec', text }); } -export type FragmentAction = +export type UrlAction = | { t: 'setTextAndExec', text: string } + +export type FragmentAction = + | UrlAction + // fetch url, expected to be json-encoded UrlAction, + // and then we will do that. + | { t: 'getUrl', url: string } ; export async function decode(fragment: string): Promise { diff --git a/src/index.ts b/src/index.ts index ec4bb46..93c2aa1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { syntaxHighlighting } from '@codemirror/language'; import { Diagnostic, lintGutter, setDiagnostics } from '@codemirror/lint'; import { EditorView, basicSetup } from "codemirror"; -import { FragmentAction, decode, encode } from "./encoding"; +import { FragmentAction, UrlAction, decode, encode } from "./encoding"; import { twelfHighlightStyle, twelfLanguage } from './twelf-mode'; import { TwelfStatus, TwelfError, TwelfExecStatus } from './twelf-worker-types'; import { TwelfWorker, mkTwelfWorker } from './twelf-worker'; @@ -78,12 +78,19 @@ function initEditor(): EditorView { async function initTwelf(editor: EditorView) { - function resolveFragmentAction(action: FragmentAction): void { + async function resolveFragmentAction(action: FragmentAction): Promise { switch (action.t) { case 'setTextAndExec': { setText(action.text); execCurrentBuffer(); } break; + case 'getUrl': { + const urlAction = await (await fetch(action.url)).json() as UrlAction; + resolveFragmentAction(urlAction); + } break; + default: { + throw new Error(`Unknown FragmentAction: ${JSON.stringify(action)}`); + } } } @@ -165,7 +172,7 @@ async function initTwelf(editor: EditorView) { }; if (window.location.hash) { - resolveFragmentAction(await decode(window.location.hash.substring(1))); + await resolveFragmentAction(await decode(window.location.hash.substring(1))); } else { const savedElf = localStorage.getItem('savedElf');