Skip to content

Commit

Permalink
Add support for getUrl FragmentAction
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreedcmu committed Mar 21, 2024
1 parent 28a42c2 commit 4b09213
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ export async function encode(text: string): Promise<string> {
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<FragmentAction> {
Expand Down
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -78,12 +78,19 @@ function initEditor(): EditorView {

async function initTwelf(editor: EditorView) {

function resolveFragmentAction(action: FragmentAction): void {
async function resolveFragmentAction(action: FragmentAction): Promise<void> {
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)}`);
}
}
}

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 4b09213

Please sign in to comment.