11"use client" ;
22
3- import type { Node } from "prosemirror-model" ;
4-
5- import { useMemo , useState } from "react" ;
3+ import dynamic from "next/dynamic" ;
64import { Value } from "@sinclair/typebox/value" ;
7- import { docHasChanged } from "context-editor/utils" ;
85import { useFormContext } from "react-hook-form" ;
96import { richTextInputConfigSchema } from "schemas" ;
107
118import { InputComponent } from "db/public" ;
12- import { FormControl , FormDescription , FormField , FormItem , FormLabel , FormMessage } from "ui/form" ;
9+ import { FormField } from "ui/form" ;
10+ import { Skeleton } from "ui/skeleton" ;
1311
1412import type { ElementProps } from "../types" ;
15- import { fromHTMLToNode , renderNodeToHTML } from "~/lib/editor/serialization/client" ;
16- import { ContextEditorClient } from "../../ContextEditor/ContextEditorClient" ;
17- import { useContextEditorContext } from "../../ContextEditor/ContextEditorContext" ;
13+ import { renderNodeToHTML } from "~/lib/editor/serialization/client" ;
1814import { useFormElementToggleContext } from "../FormElementToggleContext" ;
1915
20- const EMPTY_DOC = {
21- type : "doc" ,
22- attrs : {
23- meta : { } ,
24- } ,
25- content : [
26- {
27- type : "paragraph" ,
28- attrs : {
29- id : null ,
30- class : null ,
31- } ,
32- } ,
33- ] ,
34- } ;
35-
36- const EditorFormElement = ( {
37- label,
38- help,
39- onChange,
40- initialValue,
41- disabled,
42- } : {
43- label : string ;
44- help ?: string ;
45- onChange : ( state : any ) => void ;
46- initialValue ?: string ;
47- disabled ?: boolean ;
48- } ) => {
49- const { pubs, pubTypes, pubId, pubTypeId } = useContextEditorContext ( ) ;
50- const [ initialHTML ] = useState ( initialValue ) ;
51- const initialDoc = useMemo (
52- ( ) => ( initialHTML ? fromHTMLToNode ( initialHTML ) : undefined ) ,
53- [ initialHTML ]
54- ) ;
55- console . log ( initialDoc ) ;
56-
57- if ( ! pubId || ! pubTypeId ) {
58- return null ;
16+ const LazyContextEditorElement = dynamic (
17+ ( ) => import ( "./ContextEditorLazyElement" ) . then ( ( mod ) => mod . EditorFormElementLazy ) ,
18+ {
19+ ssr : false ,
20+ loading : ( ) => < Skeleton className = "h-16 w-full" /> ,
5921 }
60-
61- return (
62- < FormItem >
63- < FormLabel className = "flex" > { label } </ FormLabel >
64- < div className = "w-full" >
65- < FormControl >
66- < ContextEditorClient
67- pubId = { pubId }
68- pubs = { pubs }
69- pubTypes = { pubTypes }
70- pubTypeId = { pubTypeId }
71- onChange = { ( state ) => {
72- // Control changing the state more granularly or else the dirty field will trigger on load
73- // Since we can't control the dirty state directly, even this workaround does not handle the case of
74- // if someone changes the doc but then reverts it--that will still count as dirty since react-hook-form is tracking that
75- const hasChanged = docHasChanged ( initialDoc ?? EMPTY_DOC , state ) ;
76- if ( hasChanged ) {
77- onChange ( state ) ;
78- }
79- } }
80- initialDoc = { initialDoc }
81- disabled = { disabled }
82- className = "max-h-96 overflow-scroll"
83- />
84- </ FormControl >
85- </ div >
86- < FormDescription > { help } </ FormDescription >
87- < FormMessage />
88- </ FormItem >
89- ) ;
90- } ;
22+ ) ;
9123
9224export const ContextEditorElement = ( {
9325 slug,
@@ -109,7 +41,7 @@ export const ContextEditorElement = ({
10941 name = { slug }
11042 render = { ( { field } ) => {
11143 return (
112- < EditorFormElement
44+ < LazyContextEditorElement
11345 label = { label }
11446 help = { config . help }
11547 onChange = { ( state ) => {
0 commit comments