Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
AttributionExtension,
getReferenceClientRects,
getReferenceRect,
} from "@blocknote/core/y";
import type { AttributionExtension } from "@blocknote/core/y";
import { flip, offset, shift, inline } from "@floating-ui/react";
import { FC, useMemo } from "react";

Expand Down Expand Up @@ -39,22 +35,33 @@ export const AttributionTooltipController = (props: {
*/
portalElement?: HTMLElement | null;
}) => {
const state = useExtensionState(AttributionExtension, {
const state = useExtensionState<typeof AttributionExtension>("attribution", {
selector: (state) => state,
});

// Position off the hovered mark. Block-level marks are `display: contents`, so
// a live `getBoundingClientRect` (via `getReferenceRect`) is provided rather
// than relying on the wrapper's own (zero-sized) box.
const reference = useMemo<GenericPopoverReference | undefined>(
() =>
state
? {
element: state.anchor,
getBoundingClientRect: () => getReferenceRect(state.anchor),
// Required by the `inline()` middleware — a virtual reference has no
// default `getClientRects`, and `inline()` reads per-line rects.
getClientRects: () => getReferenceClientRects(state.anchor),
getBoundingClientRect: () => {
const content = state.anchor.firstElementChild ?? state.anchor;
const rect = content.getBoundingClientRect();
const el =
rect.width || rect.height
? content
: (content.firstElementChild ?? content);
return el.getBoundingClientRect();
},
getClientRects: () => {
const content = state.anchor.firstElementChild ?? state.anchor;
const rect = content.getBoundingClientRect();
const el =
rect.width || rect.height
? content
: (content.firstElementChild ?? content);
return el.getClientRects();
},
}
: undefined,
[state],
Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/editor/BlockNoteDefaultUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
SuggestionMenu,
TableHandlesExtension,
} from "@blocknote/core/extensions";
import { AttributionExtension } from "@blocknote/core/y";
import { lazy, Suspense } from "react";

import { FilePanelController } from "../components/FilePanel/FilePanelController.js";
Expand Down Expand Up @@ -162,7 +161,7 @@ export function BlockNoteDefaultUI(props: BlockNoteDefaultUIProps) {
<FloatingThreadController portalElement={commentsPortal} />
</Suspense>
)}
{editor.getExtension(AttributionExtension) &&
{editor.getExtension("attribution") &&
props.attributionTooltip !== false && (
<AttributionTooltipController
portalElement={attributionTooltipPortal}
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/hooks/useExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ export function useExtensionState<
: never,
TSelected = NoInfer<ExtractStore<TStore>>,
>(
plugin: T,
plugin: T | string,
ctx?: {
editor?: BlockNoteEditor<any, any, any>;
selector?: (state: NoInfer<ExtractStore<TStore>>) => TSelected;
},
): TSelected {
const { store } = useExtension(plugin, ctx);
const extension = useExtension(
plugin as ExtensionFactory | Extension | string,
ctx,
);
const { store } = extension;
if (!store) {
throw new Error("Store not found on plugin", { cause: { plugin } });
}
Expand Down
Loading