Skip to content

Commit 3d3f679

Browse files
mondseonloggerhead
authored andcommitted
feat: load editor json from URL params
1 parent fa7c2ea commit 3d3f679

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

__tests__/editorUrl.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getInitialJSONFromSearch } from "@/containers/editor/editor/url";
2+
3+
describe("editor URL params", () => {
4+
test("reads encoded json param", () => {
5+
const json = '{"hello":"world","count":1}';
6+
7+
expect(getInitialJSONFromSearch(`?json=${encodeURIComponent(json)}`)).toBe(json);
8+
});
9+
10+
test("returns undefined when json param is absent", () => {
11+
expect(getInitialJSONFromSearch("?foo=bar")).toBeUndefined();
12+
});
13+
});

e2e/tests/editor.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect } from "@playwright/test";
2-
import { clearEditor, getEditor, selectAllInEditor, writeToClipboard } from "../helpers/utils";
2+
import { clearEditor, getEditor, getEditorText, selectAllInEditor, writeToClipboard } from "../helpers/utils";
33

44
test.describe("edit in the editor", () => {
55
test.beforeEach(async ({ page }) => {
@@ -37,4 +37,15 @@ test.describe("edit in the editor", () => {
3737
await page.keyboard.type("{");
3838
await expect(page.getByRole("treeitem").getByText("hello")).toBeVisible();
3939
});
40+
41+
test("loads json from URL params", async ({ page }) => {
42+
const json = JSON.stringify({ hello: "world", count: 1 });
43+
44+
await page.goto(`/editor?json=${encodeURIComponent(json)}`);
45+
await getEditor(page);
46+
47+
await expect(page.getByRole("treeitem").getByText("hello")).toBeVisible();
48+
await expect(page.getByRole("treeitem").getByText("world")).toBeVisible();
49+
await expect.poll(() => getEditorText(page)).toContain('"count": 1');
50+
});
4051
});

src/containers/editor/editor/Editor.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { useEffect, type ComponentPropsWithoutRef } from "react";
3+
import { useEffect, useRef, type ComponentPropsWithoutRef } from "react";
44
import Loading from "@/components/Loading";
55
import { vsURL } from "@/lib/editor/cdn";
66
import { EditorWrapper, type Kind } from "@/lib/editor/editor";
@@ -10,6 +10,7 @@ import { loader, Editor as MonacoEditor } from "@monaco-editor/react";
1010
import { useTranslations } from "next-intl";
1111
import { useShallow } from "zustand/shallow";
1212
import { example } from "./data";
13+
import { getInitialJSONFromSearch } from "./url";
1314

1415
loader.config({ paths: { vs: vsURL } });
1516

@@ -105,10 +106,21 @@ export function useEditTree(kind: Kind) {
105106
function useDisplayExample(kind: Kind) {
106107
const editor = useEditor("main");
107108
const incrEditorInitCount = useStatusStore((state) => state.incrEditorInitCount);
109+
const didSetInitialText = useRef(false);
108110

109111
useEffect(() => {
110-
if (kind === "main" && editor && incrEditorInitCount() <= 1) {
111-
editor.parseAndSet(example);
112+
if (kind !== "main" || !editor || didSetInitialText.current) {
113+
return;
112114
}
115+
116+
const initialJSON = getInitialJSONFromSearch(window.location.search);
117+
const initialText = initialJSON ?? (incrEditorInitCount() <= 1 ? example : undefined);
118+
119+
if (initialText === undefined) {
120+
return;
121+
}
122+
123+
didSetInitialText.current = true;
124+
editor.parseAndSet(initialText);
113125
}, [editor]);
114126
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const initialJSONParamName = "json";
2+
3+
export function getInitialJSONFromSearch(search: string) {
4+
const searchParams = new URLSearchParams(search);
5+
return searchParams.has(initialJSONParamName) ? searchParams.get(initialJSONParamName)! : undefined;
6+
}

0 commit comments

Comments
 (0)