11"use client" ;
22
3- import { useEffect , type ComponentPropsWithoutRef } from "react" ;
3+ import { useEffect , useRef , type ComponentPropsWithoutRef } from "react" ;
44import Loading from "@/components/Loading" ;
55import { vsURL } from "@/lib/editor/cdn" ;
66import { EditorWrapper , type Kind } from "@/lib/editor/editor" ;
@@ -10,6 +10,7 @@ import { loader, Editor as MonacoEditor } from "@monaco-editor/react";
1010import { useTranslations } from "next-intl" ;
1111import { useShallow } from "zustand/shallow" ;
1212import { example } from "./data" ;
13+ import { getInitialJSONFromSearch } from "./url" ;
1314
1415loader . config ( { paths : { vs : vsURL } } ) ;
1516
@@ -105,10 +106,21 @@ export function useEditTree(kind: Kind) {
105106function 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}
0 commit comments