diff --git a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx index 6553b650a9..237556b5e7 100644 --- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx +++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx @@ -8,7 +8,7 @@ import { DEFAULT_GUTTER_SIZE } from "../../../services/resizer"; import utils, { isMobile } from "../../../services/utils"; import ActionButton, { ActionButtonProps } from "../../react/ActionButton"; import Admonition from "../../react/Admonition"; -import { useNoteLabelBoolean, useTriliumOption } from "../../react/hooks"; +import { useNoteBlob, useNoteLabelBoolean, useTriliumOption } from "../../react/hooks"; import { EditableCode, EditableCodeProps } from "../code/Code"; export interface SplitEditorProps extends EditableCodeProps { @@ -30,12 +30,22 @@ export interface SplitEditorProps extends EditableCodeProps { * - Can display errors to the user via {@link setError}. * - Horizontal or vertical orientation for the editor/preview split, adjustable via the switch split orientation button floating button. */ -export default function SplitEditor({ note, error, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, ...editorProps }: SplitEditorProps) { - const splitEditorOrientation = useSplitOrientation(forceOrientation); - const [ readOnly ] = useNoteLabelBoolean(note, "readOnly"); - const containerRef = useRef(null); +export default function SplitEditor(props: SplitEditorProps) { + const [ readOnly ] = useNoteLabelBoolean(props.note, "readOnly"); - const editor = (!readOnly && + if (readOnly) { + return ; + } + + return ; + +} + +function EditorWithSplit({ note, error, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, ...editorProps }: SplitEditorProps) { + const containerRef = useRef(null); + const splitEditorOrientation = useSplitOrientation(forceOrientation); + + const editor = (
{editorBefore}
@@ -53,19 +63,14 @@ export default function SplitEditor({ note, error, splitOptions, previewContent,
); - const preview = ( -
-
- {previewContent} -
-
- {previewButtons} -
-
- ); + const preview = ; useEffect(() => { - if (!utils.isDesktop() || !containerRef.current || readOnly) return; + if (!utils.isDesktop() || !containerRef.current) return; const elements = Array.from(containerRef.current?.children) as HTMLElement[]; const splitInstance = Split(elements, { rtl: glob.isRtl, @@ -76,10 +81,10 @@ export default function SplitEditor({ note, error, splitOptions, previewContent, }); return () => splitInstance.destroy(); - }, [ readOnly, splitEditorOrientation ]); + }, [ splitEditorOrientation ]); return ( -
+
{splitEditorOrientation === "horizontal" ? <>{editor}{preview} : <>{preview}{editor}} @@ -87,6 +92,43 @@ export default function SplitEditor({ note, error, splitOptions, previewContent, ); } +function ReadOnlyView({ ...props }: SplitEditorProps) { + const { note, onContentChanged } = props; + const content = useNoteBlob(note); + const onContentChangedRef = useRef(onContentChanged); + + useEffect(() => { + onContentChangedRef.current = onContentChanged; + }); + + useEffect(() => { + onContentChangedRef.current?.(content?.content ?? ""); + }, [ content ]); + + return ( +
+ +
+ ); +} + +function PreviewContainer({ error, previewContent, previewButtons }: { + error?: string | null; + previewContent: ComponentChildren; + previewButtons?: ComponentChildren; +}) { + return ( +
+
+ {previewContent} +
+
+ {previewButtons} +
+
+ ); +} + export function PreviewButton(props: Omit) { return { /** @@ -144,7 +145,7 @@ export default function SvgSplitEditor({ ntxId, note, attachmentName, renderSvg, } {...props} /> - ) + ); } function useResizer(containerRef: RefObject, noteId: string, svg: string | undefined) { @@ -181,7 +182,7 @@ function useResizer(containerRef: RefObject, noteId: string, svg lastPanZoom.current = { pan: zoomInstance.getPan(), zoom: zoomInstance.getZoom() - } + }; zoomRef.current = undefined; zoomInstance.destroy(); };