Skip to content

Feat/draggable inline content; Fix/dragging content within the editor caused duplication #1818

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions docs/pages/docs/custom-schemas/custom-inline-content.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type CustomInlineContentConfig = {
type: string;
content: "styled" | "none";
readonly propSchema: PropSchema;
draggable?: boolean;
};
```

Expand Down Expand Up @@ -84,6 +85,12 @@ type PropSchema<

_In the mentions demo, we add a `user` prop for the user that's being mentioned._

**`draggable`**

Specifies whether the inline content can be dragged within the editor. If set to `true`, the inline content will be draggable. Defaults to `false` if not specified.

If this is true, you should add `data-drag-handle` to the DOM element that should function as the drag handle.

#### Inline Content Implementation (`ReactCustomInlineContentImplementation`)

The Inline Content Implementation defines how the inline content should be rendered to HTML.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"playground": true,
"docs": false,
"author": "hectorzhuang",
"tags": []
}
80 changes: 80 additions & 0 deletions examples/06-custom-schema/inline-content-draggable/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { BlockNoteSchema, defaultInlineContentSpecs } from "@blocknote/core";
import "@blocknote/core/fonts/inter.css";
import {
createReactInlineContentSpec,
useCreateBlockNote,
} from "@blocknote/react";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";

const draggableButton = createReactInlineContentSpec(
{
type: "draggableButton",
propSchema: {
title: {
default: "",
},
},
draggable: true,
content: "none"
},
{
render: (props) => {
return (
<span
style={{
border: "none",
background: "blue",
color: "white",
padding: "5px 10px",
borderRadius: "4px",
cursor: "move",
}}
data-drag-handle>
{props.inlineContent.props.title}
</span>
);
},
}
);

const schema = BlockNoteSchema.create({
inlineContentSpecs: {
draggableButton,
...defaultInlineContentSpecs,
},
});

export default function ReactInlineContent() {
const editor = useCreateBlockNote({
schema,
initialContent: [
{
type: "paragraph",
content: [
"I enjoy working with ",
{
type: "draggableButton",
props: {
title: "Hector",
},
},
],
},
{
type: "paragraph",
content: [
"I love ",
{
type: "draggableButton",
props: {
title: "BlockNote",
},
},
],
},
],
});

return <BlockNoteView editor={editor} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Inline Content Draggable
14 changes: 14 additions & 0 deletions examples/06-custom-schema/inline-content-draggable/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html lang="en">
<head>
<script>
<!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY -->
</script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Draggable Inline Content</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="./main.tsx"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/06-custom-schema/inline-content-draggable/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

const root = createRoot(document.getElementById("root")!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
37 changes: 37 additions & 0 deletions examples/06-custom-schema/inline-content-draggable/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@blocknote/example-inline-content-draggable",
"description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
"private": true,
"version": "0.12.4",
"scripts": {
"start": "vite",
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"lint": "eslint . --max-warnings 0"
},
"dependencies": {
"@blocknote/core": "latest",
"@blocknote/react": "latest",
"@blocknote/ariakit": "latest",
"@blocknote/mantine": "latest",
"@blocknote/shadcn": "latest",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^8.10.0",
"vite": "^5.3.4"
},
"eslintConfig": {
"extends": [
"../../../.eslintrc.js"
]
},
"eslintIgnore": [
"dist"
]
}
36 changes: 36 additions & 0 deletions examples/06-custom-schema/inline-content-draggable/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"__comment": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": [
"DOM",
"DOM.Iterable",
"ESNext"
],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"composite": true
},
"include": [
"."
],
"__ADD_FOR_LOCAL_DEV_references": [
{
"path": "../../../packages/core/"
},
{
"path": "../../../packages/react/"
}
]
}
32 changes: 32 additions & 0 deletions examples/06-custom-schema/inline-content-draggable/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
import react from "@vitejs/plugin-react";
import * as fs from "fs";
import * as path from "path";
import { defineConfig } from "vite";
// import eslintPlugin from "vite-plugin-eslint";
// https://vitejs.dev/config/
export default defineConfig((conf) => ({
plugins: [react()],
optimizeDeps: {},
build: {
sourcemap: true,
},
resolve: {
alias:
conf.command === "build" ||
!fs.existsSync(path.resolve(__dirname, "../../packages/core/src"))
? {}
: ({
// Comment out the lines below to load a built version of blocknote
// or, keep as is to load live from sources with live reload working
"@blocknote/core": path.resolve(
__dirname,
"../../packages/core/src/",
),
"@blocknote/react": path.resolve(
__dirname,
"../../packages/react/src/",
),
} as any),
},
}));
16 changes: 8 additions & 8 deletions packages/core/src/extensions/SideMenu/SideMenuPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ export class SideMenuView<
return;
}

// if (
// this.sideMenuDetection === "editor" ||
// (event as any).synthetic ||
// !event.dataTransfer?.types.includes("blocknote/html")
// ) {
// return;
// }

this.editor._tiptapEditor.commands.blur();

// Finds the BlockNote editor element that the drop event occurred in (if
Expand Down Expand Up @@ -335,14 +343,6 @@ export class SideMenuView<
}
}

if (
this.sideMenuDetection === "editor" ||
(event as any).synthetic ||
!event.dataTransfer?.types.includes("blocknote/html")
) {
return;
}

const pos = this.pmView.posAtCoords({
left: event.clientX,
top: event.clientY,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/schema/inlineContent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { StyleSchema, Styles } from "../styles/types.js";
export type CustomInlineContentConfig = {
type: string;
content: "styled" | "none"; // | "plain"
draggable?: boolean;
readonly propSchema: PropSchema;
// content: "inline" | "none" | "table";
};
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/schema/ReactInlineContentSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function createReactInlineContentSpec<
group: "inline",
selectable: inlineContentConfig.content === "styled",
atom: inlineContentConfig.content === "none",
draggable: inlineContentConfig.draggable,
content: (inlineContentConfig.content === "styled"
? "inline*"
: "") as T["content"] extends "styled" ? "inline*" : "",
Expand Down
21 changes: 21 additions & 0 deletions playground/src/examples.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,27 @@
"pathFromRoot": "examples/06-custom-schema",
"slug": "custom-schema"
}
},
{
"projectSlug": "inline-content-draggable",
"fullSlug": "custom-schema/inline-content-draggable",
"pathFromRoot": "examples/06-custom-schema/inline-content-draggable",
"config": {
"playground": true,
"docs": true,
"author": "hectorchong",
"tags": [
"Intermediate",
"Inline Content",
"Custom Schemas",
"Draggable"
]
},
"title": "Draggable Inline Content",
"group": {
"pathFromRoot": "examples/06-custom-schema",
"slug": "custom-schema"
}
}
]
},
Expand Down