Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add onInit prop with editor instance #194

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/core/src/ui/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function Editor({
defaultValue = defaultEditorContent,
extensions = [],
editorProps = {},
onInit = () => {},
onUpdate = () => {},
onDebouncedUpdate = () => {},
debounceDuration = 750,
Expand Down Expand Up @@ -54,6 +55,12 @@ export default function Editor({
* Defaults to {}.
*/
editorProps?: EditorProps;
/**
* A callback function that is called whenever the editor is initialized.
* Defaults to () => {}.
*/
// eslint-disable-next-line no-unused-vars
onInit?: (editor?: EditorClass) => void | Promise<void>;
/**
* A callback function that is called whenever the editor is updated.
* Defaults to () => {}.
Expand Down Expand Up @@ -85,6 +92,7 @@ export default function Editor({
const [content, setContent] = useLocalStorage(storageKey, defaultValue);

const [hydrated, setHydrated] = useState(false);
const [initialized, setInitialized] = useState(false);

const debouncedUpdates = useDebouncedCallback(async ({ editor }) => {
const json = editor.getJSON();
Expand Down Expand Up @@ -126,6 +134,13 @@ export default function Editor({
autofocus: "end",
});

useEffect(() => {
if (!editor || initialized) return;

onInit(editor);
setInitialized(true);
}, [editor, initialized, onInit]);

const { complete, completion, isLoading, stop } = useCompletion({
id: "novel",
api: completionApi,
Expand Down