Skip to content

feat: File insertion QoL #1459

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 2 commits into from
Feb 25, 2025
Merged
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,12 +1,12 @@
import { PartialBlock } from "../../../blocks/defaultBlocks.js";
import { Block, PartialBlock } from "../../../blocks/defaultBlocks.js";
import type { BlockNoteEditor } from "../../../editor/BlockNoteEditor";
import {
BlockSchema,
FileBlockConfig,
InlineContentSchema,
StyleSchema,
} from "../../../schema/index.js";
import { getBlockInfo, getNearestBlockPos } from "../../getBlockInfoFromPos.js";
import { getNearestBlockPos } from "../../getBlockInfoFromPos.js";
import { acceptedMIMETypes } from "./acceptedMIMETypes.js";

function checkFileExtensionsMatch(
@@ -41,6 +41,33 @@ function checkMIMETypesMatch(mimeType1: string, mimeType2: string) {
return types1[0] === types2[0] && types1[1] === types2[1];
}

function insertOrUpdateBlock<
BSchema extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema
>(
editor: BlockNoteEditor<BSchema, I, S>,
referenceBlock: Block<BSchema, I, S>,
newBlock: PartialBlock<BSchema, I, S>
) {
let insertedBlockId: string | undefined;

if (
Array.isArray(referenceBlock.content) &&
referenceBlock.content.length === 0
) {
insertedBlockId = editor.updateBlock(referenceBlock, newBlock).id;
} else {
insertedBlockId = editor.insertBlocks(
[newBlock],
referenceBlock,
"after"
)[0].id;
}

return insertedBlockId;
}

export async function handleFileInsertion<
BSchema extends BlockSchema,
I extends InlineContentSchema,
@@ -120,11 +147,8 @@ export async function handleFileInsertion<
let insertedBlockId: string | undefined = undefined;

if (event.type === "paste") {
insertedBlockId = editor.insertBlocks(
[fileBlock],
editor.getTextCursorPosition().block,
"after"
)[0].id;
const currentBlock = editor.getTextCursorPosition().block;
insertedBlockId = insertOrUpdateBlock(editor, currentBlock, fileBlock);
} else if (event.type === "drop") {
const coords = {
left: (event as DragEvent).clientX,
@@ -141,13 +165,11 @@ export async function handleFileInsertion<
pos.pos
);

const blockInfo = getBlockInfo(posInfo);

insertedBlockId = editor.insertBlocks(
[fileBlock],
blockInfo.bnBlock.node.attrs.id,
"after"
)[0].id;
insertedBlockId = insertOrUpdateBlock(
editor,
editor.getBlock(posInfo.node.attrs.id)!,
fileBlock
);
} else {
return;
}