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
Expand Up @@ -170,9 +170,14 @@ export async function handleFileInsertion<

const blockRect = blockElement?.getBoundingClientRect();

const existingBlock = editor.getBlock(id);
if (!existingBlock) {
return;
}

return insertOrUpdateBlock(
editor,
editor.getBlock(id)!,
existingBlock,
fileBlock,
blockRect && (blockRect.top + blockRect.bottom) / 2 > coords.top
? "before"
Expand All @@ -183,6 +188,10 @@ export async function handleFileInsertion<
return;
}

if (!insertedBlockId) {
return;
}

const updateData = await editor.uploadFile(file, insertedBlockId);

const updatedFileBlock =
Expand Down
21 changes: 16 additions & 5 deletions packages/core/src/blocks/ToggleWrapper/createToggleWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,25 @@ export const createToggleWrapper = (
const toggleButtonOnClick = () => {
// Toggles visibility of child blocks. Also adds/removes the "add block"
// button if there are no child blocks.
const currentBlock = editor.getBlock(block);
if (!currentBlock) {
return;
}

if (toggleWrapper.getAttribute("data-show-children") === "true") {
toggleWrapper.setAttribute("data-show-children", "false");
toggledState.set(editor.getBlock(block)!, false);
toggledState.set(currentBlock, false);

if (dom.contains(toggleAddBlockButton)) {
dom.removeChild(toggleAddBlockButton);
}
} else {
toggleWrapper.setAttribute("data-show-children", "true");
toggledState.set(editor.getBlock(block)!, true);
toggledState.set(currentBlock, true);

if (
editor.isEditable &&
editor.getBlock(block)?.children.length === 0 &&
currentBlock.children.length === 0 &&
!dom.contains(toggleAddBlockButton)
) {
dom.appendChild(toggleAddBlockButton);
Expand Down Expand Up @@ -111,7 +116,10 @@ export const createToggleWrapper = (
// If a child block is added while children are hidden, show children.
if (toggleWrapper.getAttribute("data-show-children") === "false") {
toggleWrapper.setAttribute("data-show-children", "true");
toggledState.set(editor.getBlock(block)!, true);
const currentBlock = editor.getBlock(block);
if (currentBlock) {
toggledState.set(currentBlock, true);
}
}

// Remove the "add block" button as we want to show child blocks and
Expand All @@ -124,7 +132,10 @@ export const createToggleWrapper = (
// children.
if (toggleWrapper.getAttribute("data-show-children") === "true") {
toggleWrapper.setAttribute("data-show-children", "false");
toggledState.set(editor.getBlock(block)!, false);
const currentBlock = editor.getBlock(block);
if (currentBlock) {
toggledState.set(currentBlock, false);
}
}

// Remove the "add block" button as we want to hide child blocks,
Expand Down
15 changes: 12 additions & 3 deletions packages/core/src/extensions/SideMenu/SideMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ export class SideMenuView<
if (this.editor.isEditable) {
const blockContentBoundingBox = block.node.getBoundingClientRect();
const column = block.node.closest("[data-node-type=column]");
const sideMenuBlock = this.editor.getBlock(
this.hoveredBlock!.getAttribute("data-id")!,
);
if (!sideMenuBlock) {
if (this.state?.show) {
this.state.show = false;
this.hoveredBlock = undefined;
this.emitUpdate(this.state);
}
return;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
this.state = {
show: true,
referencePos: new DOMRect(
Expand All @@ -257,9 +268,7 @@ export class SideMenuView<
blockContentBoundingBox.width,
blockContentBoundingBox.height,
),
block: this.editor.getBlock(
this.hoveredBlock!.getAttribute("data-id")!,
)!,
block: sideMenuBlock,
};
this.updateState(this.state);
}
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/extensions/TableHandles/TableHandles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,10 @@ export class TableHandlesView implements PluginView {
}

// Hide handles if the table block has been removed.
this.state.block = this.editor.getBlock(this.state.block.id)!;
const refreshedBlock = this.editor.getBlock(this.state.block.id);
if (
!this.state.block ||
this.state.block.type !== "table" ||
!refreshedBlock ||
refreshedBlock.type !== "table" ||
// when collaborating, the table element might be replaced and out of date
// because yjs replaces the element when for example you change the color via the side menu
!this.tableElement?.isConnected
Expand All @@ -548,6 +548,7 @@ export class TableHandlesView implements PluginView {

return;
}
this.state.block = refreshedBlock as typeof this.state.block;

const { height: rowCount, width: colCount } = getDimensionsOfTable(
this.state.block,
Expand Down
16 changes: 10 additions & 6 deletions packages/react/src/blocks/ToggleWrapper/ToggleWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ export const ToggleWrapper = (
);

const handleToggle = (block: Block<any, any, any>) => {
(toggledState || defaultToggledState).set(
editor.getBlock(block)!,
!showChildren,
);
const currentBlock = editor.getBlock(block);
if (!currentBlock) {
return;
}
(toggledState || defaultToggledState).set(currentBlock, !showChildren);
dispatch({
type: "toggled",
});
Expand Down Expand Up @@ -91,7 +92,10 @@ export const ToggleWrapper = (
return 0;
}

const newBlock = editor.getBlock(block)!;
const newBlock = editor.getBlock(block);
if (!newBlock) {
return 0;
}
const newChildCount = newBlock.children.length || 0;

if (newChildCount > childCount) {
Expand Down Expand Up @@ -122,7 +126,7 @@ export const ToggleWrapper = (
className="bn-toggle-button"
type="button"
onMouseDown={(event) => event.preventDefault()}
onClick={() => handleToggle(editor.getBlock(block)!)}
onClick={() => handleToggle(block)}
>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
20 changes: 15 additions & 5 deletions packages/react/src/components/FilePanel/DefaultTabs/EmbedTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const EmbedTab = <

const editor = useBlockNoteEditor<B, I, S>();

const block = editor.getBlock(props.blockId)!;
const block = editor.getBlock(props.blockId);

const [currentURL, setCurrentURL] = useState<string>("");

Expand All @@ -41,25 +41,35 @@ export const EmbedTab = <
(event: KeyboardEvent) => {
if (event.key === "Enter" && !event.nativeEvent.isComposing) {
event.preventDefault();
editor.updateBlock(block.id, {
if (!editor.getBlock(props.blockId)) {
return;
}
editor.updateBlock(props.blockId, {
props: {
name: filenameFromURL(currentURL),
url: currentURL,
} as any,
});
}
},
[editor, block.id, currentURL],
[editor, props.blockId, currentURL],
);

const handleURLClick = useCallback(() => {
editor.updateBlock(block.id, {
if (!editor.getBlock(props.blockId)) {
return;
}
editor.updateBlock(props.blockId, {
props: {
name: filenameFromURL(currentURL),
url: currentURL,
} as any,
});
}, [editor, block.id, currentURL]);
}, [editor, props.blockId, currentURL]);

if (!block) {
return null;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return (
<Components.FilePanel.TabPanel className={"bn-tab-panel"}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const UploadTab = <

const editor = useBlockNoteEditor<B, I, S>();

const block = editor.getBlock(props.blockId)!;
const block = editor.getBlock(props.blockId);

const [uploadFailed, setUploadFailed] = useState<boolean>(false);

Expand All @@ -53,6 +53,9 @@ export const UploadTab = <
if (editor.uploadFile !== undefined) {
try {
let updateData = await editor.uploadFile(file, props.blockId);
if (!editor.getBlock(props.blockId)) {
return;
}
if (typeof updateData === "string") {
// received a url
updateData = {
Expand All @@ -76,6 +79,10 @@ export const UploadTab = <
[props.blockId, editor, setLoading],
);

if (!block) {
return null;
}

Comment thread
coderabbitai[bot] marked this conversation as resolved.
const spec = editor.schema.blockSpecs[block.type];
const accept = spec.implementation.meta?.fileBlockAccept?.length
? spec.implementation.meta.fileBlockAccept.join(",")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export function createMDRebaseTool(
editor: BlockNoteEditor<any, any, any>,
) {
const tr = getApplySuggestionsTr(editor);
const md = editor.blocksToMarkdownLossy([getBlock(tr.doc, id)!]);
const block = getBlock(tr.doc, id);
if (!block) {
throw new Error("block not found");
}
const md = editor.blocksToMarkdownLossy([block]);
const blocks = editor.tryParseMarkdownToBlocks(md);

const steps = updateToReplaceSteps(
Expand Down
Loading