Skip to content

Commit 12c5d16

Browse files
committed
fix(ai): make sure ShowSelection works
1 parent bc38a3b commit 12c5d16

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

packages/core/src/extensions/ShowSelection/ShowSelection.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const PLUGIN_KEY = new PluginKey(`blocknote-show-selection`);
1414
*/
1515
export const ShowSelectionExtension = createExtension(({ editor }) => {
1616
const store = createStore(
17-
{ enabled: false },
17+
{ enabledSet: new Set<string>() },
1818
{
1919
onUpdate() {
2020
editor.transact((tr) => tr.setMeta(PLUGIN_KEY, {}));
@@ -30,7 +30,7 @@ export const ShowSelectionExtension = createExtension(({ editor }) => {
3030
props: {
3131
decorations: (state) => {
3232
const { doc, selection } = state;
33-
if (!store.state.enabled) {
33+
if (store.state.enabledSet.size === 0) {
3434
return DecorationSet.empty;
3535
}
3636
const dec = Decoration.inline(selection.from, selection.to, {
@@ -43,9 +43,19 @@ export const ShowSelectionExtension = createExtension(({ editor }) => {
4343
],
4444
/**
4545
* Show or hide the selection decoration
46+
*
47+
* @param shouldShow - Whether to show the selection decoration
48+
* @param key - The key of the selection to show or hide,
49+
* this is necessary to prevent disabling ShowSelection from one place
50+
* will interfere with other parts of the code that need to show the selection decoration
51+
* (e.g.: CreateLinkButton and AIExtension)
4652
*/
47-
showSelection(shouldShow: boolean) {
48-
store.setState({ enabled: shouldShow });
53+
showSelection(shouldShow: boolean, key: string) {
54+
store.setState({
55+
enabledSet: shouldShow
56+
? new Set([...store.state.enabledSet, key])
57+
: new Set([...store.state.enabledSet].filter((k) => k !== key)),
58+
});
4959
},
5060
} as const;
5161
});

packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export const CreateLinkButton = () => {
4949

5050
const [showPopover, setShowPopover] = useState(false);
5151
useEffect(() => {
52-
showSelection(showPopover);
53-
return () => showSelection(false);
52+
showSelection(showPopover, "createLinkButton");
53+
return () => showSelection(false, "createLinkButton");
5454
}, [showPopover, showSelection]);
5555

5656
const state = useEditorState({

packages/xl-ai/src/AIExtension.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ export const AIExtension = createExtension(
143143
* Open the AI menu at a specific block
144144
*/
145145
openAIMenuAtBlock(blockID: string) {
146-
editor.getExtension(ShowSelectionExtension)?.showSelection(true);
146+
editor
147+
.getExtension(ShowSelectionExtension)
148+
?.showSelection(true, "aiMenu");
147149
editor.isEditable = false;
148150
store.setState({
149151
aiMenuState: {
@@ -167,7 +169,9 @@ export const AIExtension = createExtension(
167169
aiMenuState: "closed",
168170
});
169171
chatSession = undefined;
170-
editor.getExtension(ShowSelectionExtension)?.showSelection(false);
172+
editor
173+
.getExtension(ShowSelectionExtension)
174+
?.showSelection(false, "aiMenu");
171175
editor.isEditable = true;
172176
editor.focus();
173177
},
@@ -337,7 +341,9 @@ export const AIExtension = createExtension(
337341
}
338342

339343
if (status === "ai-writing") {
340-
editor.getExtension(ShowSelectionExtension)?.showSelection(false);
344+
editor
345+
.getExtension(ShowSelectionExtension)
346+
?.showSelection(false, "aiMenu");
341347
}
342348

343349
if (typeof status === "object") {

0 commit comments

Comments
 (0)