import { BlockNoteEditor, createStore, Extension, ExtensionFactory, } from "@blocknote/core"; import { useStore } from "@tanstack/react-store"; import { useBlockNoteEditor } from "./useBlockNoteEditor.js"; type Store = ReturnType>; /** * Use an extension instance */ export function useExtension< const T extends ExtensionFactory | Extension | string, >( plugin: T, ctx?: { editor?: BlockNoteEditor }, ): T extends ExtensionFactory ? NonNullable>> : T extends string ? Extension : T extends Extension ? T : never { // eslint-disable-next-line react-hooks/rules-of-hooks const editor = ctx?.editor ?? useBlockNoteEditor(); const instance = editor.getExtension(plugin as any); if (!instance) { throw new Error("Extension not found", { cause: { plugin } }); } return instance; } type ExtractStore = T extends Store ? U : never; /** * Use the state of an extension */ export function useExtensionState< T extends ExtensionFactory | Extension, TExtension = T extends ExtensionFactory ? ReturnType> : T, TStore = TExtension extends { store: Store } ? TExtension["store"] : never, TSelected = NoInfer>, >( plugin: T, ctx?: { editor?: BlockNoteEditor; selector?: (state: NoInfer>) => TSelected; }, ): TSelected { const { store } = useExtension(plugin, ctx); if (!store) { throw new Error("Store not found on plugin", { cause: { plugin } }); } return useStore, TSelected>(store, ctx?.selector as any); }