|
1 | | -import { useCallback, useEffect, useRef, useState, type FC } from 'react'; |
2 | | -import { Parser } from '../../renderer'; |
3 | | -import { Bridge, getClassNameFactory, GuestEventType, HostEventType } from '../../utils'; |
| 1 | +import { useState, type FC } from 'react'; |
| 2 | +import { CloudEditorControl } from '../CloudEditorControl'; |
| 3 | +import { CloudEditorEventHandler } from '../CloudEditorEventHandler'; |
| 4 | +import { CloudEditorInitializer } from '../CloudEditorInitializer'; |
4 | 5 | import { type EditingRef } from '../EditingContext'; |
5 | 6 | import { Editor } from '../Editor'; |
6 | 7 | import { type VisualEditorProps } from '../VisualEditor'; |
7 | | -import styles from './CloudEditor.module.css'; |
8 | | - |
9 | | -const getClassName = getClassNameFactory('CloudEditor', styles); |
10 | 8 |
|
11 | 9 | type Props = VisualEditorProps; |
12 | 10 |
|
13 | 11 | export const CloudEditor: FC<Props> = ({ viewports }) => { |
14 | | - const bridgeRef = useRef<Bridge | null>(null); |
15 | | - |
16 | 12 | const [editingRef, setEditingRef] = useState<EditingRef | null>(null); |
17 | 13 | const [title, setTitle] = useState('Untitled'); |
18 | | - const [initialized, setInitialized] = useState(false); |
19 | | - |
20 | | - const handleClickSettings = useCallback(() => { |
21 | | - if (!bridgeRef.current) { |
22 | | - return; |
23 | | - } |
24 | | - |
25 | | - bridgeRef.current.emit({ type: GuestEventType.SettingsClicked }); |
26 | | - }, []); |
27 | | - |
28 | | - const handleClickSave = useCallback(() => { |
29 | | - if (!bridgeRef.current || !editingRef) { |
30 | | - return; |
31 | | - } |
32 | | - |
33 | | - bridgeRef.current.emit({ |
34 | | - type: GuestEventType.SaveClicked, |
35 | | - content: editingRef.getSource(), |
36 | | - }); |
37 | | - }, [editingRef]); |
38 | | - |
39 | | - useEffect(() => { |
40 | | - if (!editingRef) { |
41 | | - return; |
42 | | - } |
43 | | - |
44 | | - bridgeRef.current = new Bridge(window.parent); |
45 | | - |
46 | | - const { replaceRoot, getSource } = editingRef; |
47 | | - const { on, emit, dispose } = bridgeRef.current; |
48 | | - |
49 | | - on(HostEventType.Initialize, data => { |
50 | | - setTitle(data.title); |
51 | | - replaceRoot(Parser.parse(data.content)); |
52 | | - }); |
53 | | - |
54 | | - on(HostEventType.ContentRequested, () => { |
55 | | - emit({ |
56 | | - type: GuestEventType.ContentProvided, |
57 | | - content: getSource(), |
58 | | - }); |
59 | | - }); |
60 | | - |
61 | | - requestAnimationFrame(() => { |
62 | | - if (!initialized) { |
63 | | - emit({ type: GuestEventType.Ready }); |
64 | | - setInitialized(true); |
65 | | - } |
66 | | - }); |
67 | | - |
68 | | - return dispose; |
69 | | - }, [editingRef, initialized]); |
70 | 14 |
|
71 | 15 | return ( |
72 | | - <Editor |
73 | | - ref={setEditingRef} |
74 | | - title={title} |
75 | | - source="<></>" |
76 | | - viewports={viewports} |
77 | | - renderControl={() => ( |
78 | | - <div className={getClassName('Controls')}> |
79 | | - <button type="button" className={getClassName('SettingsButton')} onClick={handleClickSettings}> |
80 | | - Settings |
81 | | - </button> |
82 | | - <button type="button" className={getClassName('SaveButton')} onClick={handleClickSave}> |
83 | | - Save |
84 | | - </button> |
85 | | - </div> |
| 16 | + <> |
| 17 | + <Editor |
| 18 | + ref={setEditingRef} |
| 19 | + title={title} |
| 20 | + source="<></>" |
| 21 | + viewports={viewports} |
| 22 | + renderControl={() => (editingRef ? <CloudEditorControl getSource={editingRef.getSource} /> : null)} |
| 23 | + /> |
| 24 | + <CloudEditorInitializer /> |
| 25 | + {editingRef && ( |
| 26 | + <CloudEditorEventHandler |
| 27 | + setTitle={setTitle} |
| 28 | + replaceRoot={editingRef.replaceRoot} |
| 29 | + getSource={editingRef.getSource} |
| 30 | + /> |
86 | 31 | )} |
87 | | - /> |
| 32 | + </> |
88 | 33 | ); |
89 | 34 | }; |
0 commit comments