mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 10:55:55 +01:00
refactor(react/type_widget): simplify handling of new notes
This commit is contained in:
@@ -75,12 +75,15 @@ export function useSpacedUpdate(callback: () => void | Promise<void>, interval =
|
|||||||
return spacedUpdateRef.current;
|
return spacedUpdateRef.current;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useEditorSpacedUpdate({ note, getData, dataSaved }: {
|
export function useEditorSpacedUpdate({ note, getData, onContentChange, dataSaved }: {
|
||||||
note: FNote,
|
note: FNote,
|
||||||
getData: () => Promise<object | undefined> | object | undefined,
|
getData: () => Promise<object | undefined> | object | undefined,
|
||||||
|
onContentChange: (newContent: string) => void,
|
||||||
dataSaved?: () => void
|
dataSaved?: () => void
|
||||||
}) {
|
}) {
|
||||||
const parentComponent = useContext(ParentComponent);
|
const parentComponent = useContext(ParentComponent);
|
||||||
|
const blob = useNoteBlob(note);
|
||||||
|
|
||||||
const callback = useMemo(() => {
|
const callback = useMemo(() => {
|
||||||
return async () => {
|
return async () => {
|
||||||
const data = await getData();
|
const data = await getData();
|
||||||
@@ -95,6 +98,12 @@ export function useEditorSpacedUpdate({ note, getData, dataSaved }: {
|
|||||||
}
|
}
|
||||||
}, [ note, getData, dataSaved ])
|
}, [ note, getData, dataSaved ])
|
||||||
const spacedUpdate = useSpacedUpdate(callback);
|
const spacedUpdate = useSpacedUpdate(callback);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!blob) return;
|
||||||
|
spacedUpdate.allowUpdateWithoutChange(() => onContentChange(blob.content));
|
||||||
|
}, [ blob ]);
|
||||||
|
|
||||||
return spacedUpdate;
|
return spacedUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,21 +31,17 @@ export function ReadOnlyCode({ note, viewScope, ntxId }: TypeWidgetProps) {
|
|||||||
|
|
||||||
export function EditableCode({ note, ntxId }: TypeWidgetProps) {
|
export function EditableCode({ note, ntxId }: TypeWidgetProps) {
|
||||||
const editorRef = useRef<VanillaCodeMirror>(null);
|
const editorRef = useRef<VanillaCodeMirror>(null);
|
||||||
const blob = useNoteBlob(note);
|
|
||||||
const spacedUpdate = useEditorSpacedUpdate({
|
const spacedUpdate = useEditorSpacedUpdate({
|
||||||
note,
|
note,
|
||||||
getData: () => ({ content: editorRef.current?.getText() })
|
getData: () => ({ content: editorRef.current?.getText() }),
|
||||||
});
|
onContentChange: (content) => {
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
spacedUpdate.allowUpdateWithoutChange(() => {
|
|
||||||
const codeEditor = editorRef.current;
|
const codeEditor = editorRef.current;
|
||||||
if (!codeEditor) return;
|
if (!codeEditor) return;
|
||||||
codeEditor.setText(blob?.content ?? "");
|
codeEditor.setText(content ?? "");
|
||||||
codeEditor.setMimeType(note.mime);
|
codeEditor.setMimeType(note.mime);
|
||||||
codeEditor.clearHistory();
|
codeEditor.clearHistory();
|
||||||
});
|
}
|
||||||
}, [ blob ]);
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="note-detail-code note-detail-printable">
|
<div className="note-detail-code note-detail-printable">
|
||||||
|
|||||||
Reference in New Issue
Block a user