Skip to content

feat: Non-selectable custom blocks #1090

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

Merged
merged 4 commits into from
Sep 20, 2024
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
33 changes: 31 additions & 2 deletions packages/core/src/schema/blocks/createSpec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Editor } from "@tiptap/core";
import { TagParseRule } from "@tiptap/pm/model";
import { NodeView } from "@tiptap/pm/view";
import type { BlockNoteEditor } from "../../editor/BlockNoteEditor";
import { InlineContentSchema } from "../inlineContent/types";
import { StyleSchema } from "../styles/types";
Expand Down Expand Up @@ -61,6 +63,27 @@ export type CustomBlockImplementation<
) => PartialBlockFromConfig<T, I, S>["props"] | undefined;
};

// Function that enables copying of selected content within non-selectable
// blocks.
export function applyNonSelectableBlockFix(nodeView: NodeView, editor: Editor) {
nodeView.stopEvent = (event) => {
// Ensures copy events are handled by the browser and not by ProseMirror.
if (event.type === "copy" || event.type === "cut") {
return true;
}
// Blurs the editor on mouse down as the block is non-selectable. This is
// mainly done to prevent UI elements like the formatting toolbar from being
// visible while content within a non-selectable block is selected.
if (event.type === "mousedown") {
setTimeout(() => {
editor.view.dom.blur();
}, 10);
return true;
}
return false;
};
}

// Function that uses the 'parse' function of a blockConfig to create a
// TipTap node's `parseHTML` property. This is only used for parsing content
// from the clipboard.
Expand Down Expand Up @@ -125,7 +148,7 @@ export function createBlockSpec<
? "inline*"
: "") as T["content"] extends "inline" ? "inline*" : "",
group: "blockContent",
selectable: true,
selectable: blockConfig.isSelectable ?? true,

addAttributes() {
return propsToAttributes(blockConfig.propSchema);
Expand Down Expand Up @@ -163,13 +186,19 @@ export function createBlockSpec<

const output = blockImplementation.render(block as any, editor);

return wrapInBlockStructure(
const nodeView: NodeView = wrapInBlockStructure(
output,
block.type,
block.props,
blockConfig.propSchema,
blockContentDOMAttributes
);

if (blockConfig.isSelectable === false) {
applyNonSelectableBlockFix(nodeView, this.editor);
}

return nodeView;
};
},
});
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/schema/blocks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type FileBlockConfig = {
};
};
content: "none";
isSelectable?: boolean;
isFileBlock: true;
fileBlockAccept?: string[];
};
Expand All @@ -60,6 +61,7 @@ export type BlockConfig =
type: string;
readonly propSchema: PropSchema;
content: "inline" | "none" | "table";
isSelectable?: boolean;
isFileBlock?: false;
}
| FileBlockConfig;
Expand Down
17 changes: 13 additions & 4 deletions packages/react/src/schema/ReactBlockSpec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
applyNonSelectableBlockFix,
BlockFromConfig,
BlockNoteEditor,
BlockSchemaWithBlock,
Expand All @@ -18,6 +19,7 @@ import {
StyleSchema,
} from "@blocknote/core";
import {
NodeView,
NodeViewContent,
NodeViewProps,
NodeViewWrapper,
Expand Down Expand Up @@ -118,7 +120,7 @@ export function createReactBlockSpec<
? "inline*"
: "") as T["content"] extends "inline" ? "inline*" : "",
group: "blockContent",
selectable: true,
selectable: blockConfig.isSelectable ?? true,

addAttributes() {
return propsToAttributes(blockConfig.propSchema);
Expand All @@ -140,8 +142,8 @@ export function createReactBlockSpec<
},

addNodeView() {
return (props) =>
ReactNodeViewRenderer(
return (props) => {
const nodeView = ReactNodeViewRenderer(
(props: NodeViewProps) => {
// Gets the BlockNote editor instance
const editor = this.options.editor! as BlockNoteEditor<any>;
Expand Down Expand Up @@ -178,7 +180,14 @@ export function createReactBlockSpec<
{
className: "bn-react-node-view-renderer",
}
)(props);
)(props) as NodeView<any>;

if (blockConfig.isSelectable === false) {
applyNonSelectableBlockFix(nodeView, this.editor);
}

return nodeView;
};
},
});

Expand Down
Loading