atproto explorer pdsls.dev
atproto tool

dynamically import editor

juli.ee 7950db0e a5fa2339

verified
Changed files
+32 -21
src
components
+27 -17
src/components/create.tsx
···
import { getSession, OAuthUserAgent } from "@atcute/oauth-browser-client";
import { remove } from "@mary/exif-rm";
import { useNavigate, useParams } from "@solidjs/router";
-
import { createEffect, createSignal, For, onCleanup, Show } from "solid-js";
-
import { Editor, editorView } from "../components/editor.jsx";
import { agent } from "../components/login.jsx";
import { sessions } from "./account.jsx";
import { Button } from "./button.jsx";
···
import { TextInput } from "./text-input.jsx";
import Tooltip from "./tooltip.jsx";
export const [placeholder, setPlaceholder] = createSignal<any>();
export const RecordEditor = (props: { create: boolean; record?: any; refetch?: any }) => {
···
const rkey = formData.get("rkey");
let record: any;
try {
-
record = JSON.parse(editorView.state.doc.toString());
} catch (e: any) {
setNotice(e.message);
return;
···
};
const editRecord = async (recreate?: boolean) => {
-
const record = editorView.state.doc.toString();
if (!record) return;
const rpc = new Client({ handler: agent()! });
try {
···
const insertTimestamp = () => {
const timestamp = new Date().toISOString();
-
editorView.dispatch({
changes: {
-
from: editorView.state.selection.main.head,
insert: `"${timestamp}"`,
},
});
···
setError(res.data.error);
return;
}
-
editorView.dispatch({
changes: {
-
from: editorView.state.selection.main.head,
insert: JSON.stringify(res.data.blob, null, 2),
},
});
···
</Show>
</Show>
<div class="min-h-0 flex-1">
-
<Editor
-
content={JSON.stringify(
-
!props.create ? props.record
-
: params.rkey ? placeholder()
-
: defaultPlaceholder(),
-
null,
-
2,
-
)}
-
/>
</div>
<div class="flex flex-col gap-2">
<Show when={notice()}>
···
import { getSession, OAuthUserAgent } from "@atcute/oauth-browser-client";
import { remove } from "@mary/exif-rm";
import { useNavigate, useParams } from "@solidjs/router";
+
import { createEffect, createSignal, For, lazy, onCleanup, Show, Suspense } from "solid-js";
import { agent } from "../components/login.jsx";
import { sessions } from "./account.jsx";
import { Button } from "./button.jsx";
···
import { TextInput } from "./text-input.jsx";
import Tooltip from "./tooltip.jsx";
+
const Editor = lazy(() => import("../components/editor.jsx").then((m) => ({ default: m.Editor })));
+
+
export const editorInstance = { view: null as any };
export const [placeholder, setPlaceholder] = createSignal<any>();
export const RecordEditor = (props: { create: boolean; record?: any; refetch?: any }) => {
···
const rkey = formData.get("rkey");
let record: any;
try {
+
record = JSON.parse(editorInstance.view.state.doc.toString());
} catch (e: any) {
setNotice(e.message);
return;
···
};
const editRecord = async (recreate?: boolean) => {
+
const record = editorInstance.view.state.doc.toString();
if (!record) return;
const rpc = new Client({ handler: agent()! });
try {
···
const insertTimestamp = () => {
const timestamp = new Date().toISOString();
+
editorInstance.view.dispatch({
changes: {
+
from: editorInstance.view.state.selection.main.head,
insert: `"${timestamp}"`,
},
});
···
setError(res.data.error);
return;
}
+
editorInstance.view.dispatch({
changes: {
+
from: editorInstance.view.state.selection.main.head,
insert: JSON.stringify(res.data.blob, null, 2),
},
});
···
</Show>
</Show>
<div class="min-h-0 flex-1">
+
<Suspense
+
fallback={
+
<div class="flex h-full items-center justify-center">
+
<span class="iconify lucide--loader-circle animate-spin text-xl"></span>
+
</div>
+
}
+
>
+
<Editor
+
content={JSON.stringify(
+
!props.create ? props.record
+
: params.rkey ? placeholder()
+
: defaultPlaceholder(),
+
null,
+
2,
+
)}
+
/>
+
</Suspense>
</div>
<div class="flex flex-col gap-2">
<Show when={notice()}>
+5 -4
src/components/editor.tsx
···
import { basicLight } from "@fsegurai/codemirror-theme-basic-light";
import { basicSetup, EditorView } from "codemirror";
import { onCleanup, onMount } from "solid-js";
-
-
export let editorView: EditorView;
const Editor = (props: { content: string }) => {
let editorDiv!: HTMLDivElement;
let themeColor = new Compartment();
const themeEvent = () => {
-
editorView.dispatch({
effects: themeColor.reconfigure(
window.matchMedia("(prefers-color-scheme: dark)").matches ? basicDark : basicLight,
),
···
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", themeEvent);
-
editorView = new EditorView({
doc: props.content,
parent: editorDiv,
extensions: [
···
themeColor.of(document.documentElement.classList.contains("dark") ? basicDark : basicLight),
],
});
});
onCleanup(() =>
···
import { basicLight } from "@fsegurai/codemirror-theme-basic-light";
import { basicSetup, EditorView } from "codemirror";
import { onCleanup, onMount } from "solid-js";
+
import { editorInstance } from "./create";
const Editor = (props: { content: string }) => {
let editorDiv!: HTMLDivElement;
let themeColor = new Compartment();
+
let view: EditorView;
const themeEvent = () => {
+
view.dispatch({
effects: themeColor.reconfigure(
window.matchMedia("(prefers-color-scheme: dark)").matches ? basicDark : basicLight,
),
···
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", themeEvent);
+
view = new EditorView({
doc: props.content,
parent: editorDiv,
extensions: [
···
themeColor.of(document.documentElement.classList.contains("dark") ? basicDark : basicLight),
],
});
+
editorInstance.view = view;
});
onCleanup(() =>