/**
* The workspaces of the editor beside the canvas: the design with a live preview, the settings
* of the whole form, what happens after sending, and the figures of the form.
*/
import React, { useEffect, useMemo, useRef, useState } from 'react';
import type { FormDocument } from '@funnelforms/form-schema';
import { ApiError, type FormsApi } from '../api';
import type { Device } from '../store';
import { styleClasses, styleCss } from '../styleCss';
import { ActionsSection } from './Actions';
import { CalculationsSection } from './Calculations';
import { problemTexts } from './catalog';
import { useBuilder, useEnv } from './context';
import { DesignInspector } from './Design';
import { ExperimentsSection } from './Experiments';
import { __ } from './i18n';
import { FormSettings } from './Inspector';
import { LanguagesSection } from './Languages';
import { DesignModeBar, DesignPartPanel, useLiveDesign } from './LiveDesign';
import { DEVICE_WIDTH, Stage } from './Preview';
import { useShownStyle } from './previewStyle';
import { QuizSection } from './Quiz';
import { AbandonSection, AccessSection, DoubleOptInSection, OwnPageSection, PaymentSection, SpamSection } from './Settings';
import { Button, icons, Notice, ProBadge, ProFeatures, Spinner, type ProFeature } from './ui';
/* ------------------------------------------------------------------ design */
export function DesignWorkspace( { api, formId }: { api: FormsApi; formId: number } ) {
const doc = useBuilder( ( s ) => s.doc );
if ( ! doc ) {
return null;
}
return (
);
}
type Live = { state: 'loading' } | { state: 'ready'; html: string } | { state: 'failed'; message: string; problems: string[] };
/**
* The saved draft as visitors see it. Changes of the design show at once (style message); fonts,
* the background image, custom CSS and the progress settings after a moment, when the page is
* loaded again. In the design
* mode a click in the preview picks a part of the form and shows its tokens (LiveDesign.tsx).
*/
function LivePreview( { api, formId }: { api: FormsApi; formId: number } ) {
const { store, ctx, lang } = useEnv();
const device = useBuilder( ( s ) => s.device );
const style = useBuilder( ( s ) => s.doc?.style );
// The progress bar and the height of the steps are built by the runtime, not by the style.
const progress = useBuilder( ( s ) => JSON.stringify( [ s.doc?.settings.progressBar, s.doc?.settings.autoHeight ] ) );
const locale = lang !== ctx.locale ? lang : undefined;
// The look shows at once: a theme the pointer rests on, otherwise the form's own tokens before
// they are saved; the page follows after a moment with fonts and the background image.
const shown = useShownStyle();
const message = useMemo( () => {
const tokens = shown ?? style?.tokens ?? {};
return { type: 'ffx-preview-style', css: styleCss( tokens ), classes: styleClasses( tokens ) };
}, [ shown, style ] );
// The page is loaded again only for what the message cannot show: fonts of the site, the
// background image, the form's own CSS (styleCss.ts) and the progress settings.
const reload = useMemo( () => {
const t = style?.tokens ?? {};
return JSON.stringify( [ t[ 'font.family' ], t[ 'font.heading' ], t[ 'font.field' ], t[ 'background.image' ], style?.css, progress ] );
}, [ style, progress ] );
const [ live, setLive ] = useState( { state: 'loading' } );
const [ busy, setBusy ] = useState( true );
const [ run, setRun ] = useState( 0 );
const first = useRef( true );
const frame = useRef( null );
const chooser = useRef( null );
const doc = useBuilder( ( s ) => s.doc );
// The preview opens to try the form; a click on Edit design makes its parts pickable.
const [ designing, setDesigning ] = useState( false );
const design = useLiveDesign( frame, designing && 'ready' === live.state );
const closePart = (): void => {
design.choose( null );
chooser.current?.focus();
};
useEffect( () => {
let cancelled = false;
// The first load right away; changes wait until the user pauses.
const timer = window.setTimeout(
async () => {
setBusy( true );
try {
await store.getState().saveNow();
const { html } = locale ? await api.preview( formId, locale ) : await api.preview( formId );
if ( ! cancelled ) {
setLive( { state: 'ready', html } );
}
} catch ( e ) {
if ( ! cancelled ) {
const problems = e instanceof ApiError && Array.isArray( e.data.problems ) ? ( e.data.problems as string[] ) : [];
setLive( { state: 'failed', message: e instanceof Error ? e.message : String( e ), problems } );
}
} finally {
if ( ! cancelled ) {
setBusy( false );
}
}
},
first.current ? 0 : 800
);
first.current = false;
return () => {
cancelled = true;
window.clearTimeout( timer );
};
}, [ api, formId, store, locale, reload, run ] );
const devices: Array<[ Device, string ]> = [
[ 'desktop', __( 'Desktop', 'funnelforms-pro' ) ],
[ 'tablet', __( 'Tablet', 'funnelforms-pro' ) ],
[ 'mobile', __( 'Mobile', 'funnelforms-pro' ) ],
];
return (