chore(react/type_widget): basic editable code

This commit is contained in:
Elian Doran
2025-09-20 09:44:36 +03:00
parent 6517dd1190
commit 9eae6620d0
9 changed files with 118 additions and 85 deletions

View File

@@ -19,6 +19,8 @@ import Mark from "mark.js";
import { DragData } from "../note_tree";
import Component from "../../components/component";
import toast, { ToastOptions } from "../../services/toast";
import protected_session_holder from "../../services/protected_session_holder";
import server from "../../services/server";
export function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void) {
const parentComponent = useContext(ParentComponent);
@@ -73,6 +75,29 @@ export function useSpacedUpdate(callback: () => void | Promise<void>, interval =
return spacedUpdateRef.current;
}
export function useEditorSpacedUpdate({ note, getData, dataSaved }: {
note: FNote,
getData: () => Promise<object | undefined> | object | undefined,
dataSaved?: () => void
}) {
const parentComponent = useContext(ParentComponent);
const callback = useMemo(() => {
return async () => {
const data = await getData();
// for read only notes
if (data === undefined) return;
protected_session_holder.touchProtectedSessionIfNecessary(note);
await server.put(`notes/${note.noteId}/data`, data, parentComponent?.componentId);
dataSaved?.();
}
}, [ note, getData, dataSaved ])
const spacedUpdate = useSpacedUpdate(callback);
return spacedUpdate;
}
/**
* Allows a React component to read and write a Trilium option, while also watching for external changes.
*