Skip to content

Commit 0a29f53

Browse files
fix: Updated resizable file wrappers (#1914)
* Updated resizable file wrappers * Updated unit tests * Revert "Updated unit tests" This reverts commit ef1e44e. * Revert "Updated resizable file wrappers" This reverts commit 571af73. * Re-added `eventCaptureElement` * Added `position: "relative"` to wrapper element * Updated unit test snaps
1 parent 2068f4b commit 0a29f53

File tree

15 files changed

+55
-16
lines changed

15 files changed

+55
-16
lines changed

packages/core/src/blocks/FileBlockContent/helpers/render/createResizableFileBlockWrapper.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const createResizableFileBlockWrapper = (
1818
buttonIcon,
1919
);
2020
const wrapper = dom;
21+
wrapper.style.position = "relative";
2122
if (block.props.url && block.props.showPreview) {
2223
if (block.props.previewWidth) {
2324
wrapper.style.width = `${block.props.previewWidth}px`;
@@ -33,6 +34,15 @@ export const createResizableFileBlockWrapper = (
3334
rightResizeHandle.className = "bn-resize-handle";
3435
rightResizeHandle.style.right = "4px";
3536

37+
// This element ensures `mousemove` and `mouseup` events are captured while
38+
// resizing when the cursor is over the wrapper content. This is because
39+
// embeds are treated as separate HTML documents, so if the content is an
40+
// embed, the events will only fire within that document.
41+
const eventCaptureElement = document.createElement("div");
42+
eventCaptureElement.style.position = "absolute";
43+
eventCaptureElement.style.height = "100%";
44+
eventCaptureElement.style.width = "100%";
45+
3646
// Temporary parameters set when the user begins resizing the element, used to
3747
// calculate the new width of the element.
3848
let resizeParams:
@@ -118,6 +128,10 @@ export const createResizableFileBlockWrapper = (
118128

119129
resizeParams = undefined;
120130

131+
if (wrapper.contains(eventCaptureElement)) {
132+
wrapper.removeChild(eventCaptureElement);
133+
}
134+
121135
editor.updateBlock(block, {
122136
props: {
123137
previewWidth: width,
@@ -161,6 +175,10 @@ export const createResizableFileBlockWrapper = (
161175
const leftResizeHandleMouseDownHandler = (event: MouseEvent) => {
162176
event.preventDefault();
163177

178+
if (!wrapper.contains(eventCaptureElement)) {
179+
wrapper.appendChild(eventCaptureElement);
180+
}
181+
164182
resizeParams = {
165183
handleUsed: "left",
166184
initialWidth: wrapper.clientWidth,
@@ -170,6 +188,10 @@ export const createResizableFileBlockWrapper = (
170188
const rightResizeHandleMouseDownHandler = (event: MouseEvent) => {
171189
event.preventDefault();
172190

191+
if (!wrapper.contains(eventCaptureElement)) {
192+
wrapper.appendChild(eventCaptureElement);
193+
}
194+
173195
resizeParams = {
174196
handleUsed: "right",
175197
initialWidth: wrapper.clientWidth,

packages/react/src/blocks/FileBlockContent/helpers/render/ResizableFileBlockWrapper.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ export const ResizableFileBlockWrapper = (
154154
: undefined
155155
}
156156
>
157-
<div className={"bn-visual-media-wrapper"} ref={ref}>
157+
<div
158+
className={"bn-visual-media-wrapper"}
159+
style={{ position: "relative" }}
160+
ref={ref}
161+
>
158162
{props.children}
159163
{(hovered || resizeParams) && (
160164
<>
@@ -170,6 +174,19 @@ export const ResizableFileBlockWrapper = (
170174
/>
171175
</>
172176
)}
177+
{/* This element ensures `mousemove` and `mouseup` events are captured
178+
while resizing when the cursor is over the wrapper content. This is
179+
because embeds are treated as separate HTML documents, so if the
180+
content is an embed, the events will only fire within that document. */}
181+
{resizeParams && (
182+
<div
183+
style={{
184+
position: "absolute",
185+
height: "100%",
186+
width: "100%",
187+
}}
188+
/>
189+
)}
173190
</div>
174191
</FileBlockWrapper>
175192
);

packages/server-util/src/context/__snapshots__/ServerBlockNoteEditor.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`Test ServerBlockNoteEditor > converts to HTML (blocksToFullHTML) 1`] = `"<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1" data-text-color="yellow" data-background-color="blue"><div class="bn-block" data-node-type="blockContainer" data-id="1" data-text-color="yellow" data-background-color="blue"><div class="bn-block-content" data-content-type="heading" data-text-alignment="right" data-level="2"><h2 class="bn-inline-content"><strong><u>Heading </u></strong><em><s>2</s></em></h2></div><div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="2" data-background-color="red"><div class="bn-block" data-node-type="blockContainer" data-id="2" data-background-color="red"><div class="bn-block-content" data-content-type="paragraph"><p class="bn-inline-content">Paragraph</p></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="3"><div class="bn-block" data-node-type="blockContainer" data-id="3"><div class="bn-block-content" data-content-type="bulletListItem"><p class="bn-inline-content">list item</p></div></div></div></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="4"><div class="bn-block" data-node-type="blockContainer" data-id="4"><div class="bn-block-content" data-content-type="image" data-name="Example" data-url="exampleURL" data-caption="Caption" data-preview-width="256" data-file-block=""><div class="bn-file-block-content-wrapper" style="width: 256px;"><div class="bn-visual-media-wrapper"><img class="bn-visual-media" src="exampleURL" alt="Example" draggable="false"></div><p class="bn-file-caption">Caption</p></div></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="5"><div class="bn-block" data-node-type="blockContainer" data-id="5"><div class="bn-block-content" data-content-type="image" data-name="Example" data-url="exampleURL" data-caption="Caption" data-show-preview="false" data-preview-width="256" data-file-block=""><div class="bn-file-block-content-wrapper"><div class="bn-file-name-with-icon"><div class="bn-file-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M3 8L9.00319 2H19.9978C20.5513 2 21 2.45531 21 2.9918V21.0082C21 21.556 20.5551 22 20.0066 22H3.9934C3.44476 22 3 21.5501 3 20.9932V8ZM10 4V9H5V20H19V4H10Z"></path></svg></div><p class="bn-file-name">Example</p></div><p class="bn-file-caption">Caption</p></div></div></div></div></div>"`;
3+
exports[`Test ServerBlockNoteEditor > converts to HTML (blocksToFullHTML) 1`] = `"<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1" data-text-color="yellow" data-background-color="blue"><div class="bn-block" data-node-type="blockContainer" data-id="1" data-text-color="yellow" data-background-color="blue"><div class="bn-block-content" data-content-type="heading" data-text-alignment="right" data-level="2"><h2 class="bn-inline-content"><strong><u>Heading </u></strong><em><s>2</s></em></h2></div><div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="2" data-background-color="red"><div class="bn-block" data-node-type="blockContainer" data-id="2" data-background-color="red"><div class="bn-block-content" data-content-type="paragraph"><p class="bn-inline-content">Paragraph</p></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="3"><div class="bn-block" data-node-type="blockContainer" data-id="3"><div class="bn-block-content" data-content-type="bulletListItem"><p class="bn-inline-content">list item</p></div></div></div></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="4"><div class="bn-block" data-node-type="blockContainer" data-id="4"><div class="bn-block-content" data-content-type="image" data-name="Example" data-url="exampleURL" data-caption="Caption" data-preview-width="256" data-file-block=""><div class="bn-file-block-content-wrapper" style="position: relative; width: 256px;"><div class="bn-visual-media-wrapper"><img class="bn-visual-media" src="exampleURL" alt="Example" draggable="false"></div><p class="bn-file-caption">Caption</p></div></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="5"><div class="bn-block" data-node-type="blockContainer" data-id="5"><div class="bn-block-content" data-content-type="image" data-name="Example" data-url="exampleURL" data-caption="Caption" data-show-preview="false" data-preview-width="256" data-file-block=""><div class="bn-file-block-content-wrapper" style="position: relative;"><div class="bn-file-name-with-icon"><div class="bn-file-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M3 8L9.00319 2H19.9978C20.5513 2 21 2.45531 21 2.9918V21.0082C21 21.556 20.5551 22 20.0066 22H3.9934C3.44476 22 3 21.5501 3 20.9932V8ZM10 4V9H5V20H19V4H10Z"></path></svg></div><p class="bn-file-name">Example</p></div><p class="bn-file-caption">Caption</p></div></div></div></div></div>"`;
44

55
exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLossy) 1`] = `"<h2 data-text-color="yellow" data-background-color="blue" data-text-alignment="right" data-level="2"><strong><u>Heading </u></strong><em><s>2</s></em></h2><p data-background-color="red">Paragraph</p><ul><li><p>list item</p></li></ul><figure data-name="Example" data-url="exampleURL" data-caption="Caption" data-preview-width="256"><img src="exampleURL" alt="Example" width="256"><figcaption>Caption</figcaption></figure><div data-name="Example" data-url="exampleURL" data-caption="Caption" data-show-preview="false" data-preview-width="256"><a href="exampleURL">Example</a><p>Caption</p></div>"`;
66

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/image/basic.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
data-preview-width="256"
1111
data-file-block=""
1212
>
13-
<div class="bn-file-block-content-wrapper">
13+
<div class="bn-file-block-content-wrapper" style="position: relative;">
1414
<div class="bn-visual-media-wrapper">
1515
<img class="bn-visual-media" src="exampleURL" alt="example" draggable="false" />
1616
</div>

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/image/button.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
33
<div class="bn-block" data-node-type="blockContainer" data-id="1">
44
<div class="bn-block-content" data-content-type="image" data-file-block="">
5-
<div class="bn-file-block-content-wrapper">
5+
<div class="bn-file-block-content-wrapper" style="position: relative;">
66
<div class="bn-add-file-button">
77
<div class="bn-add-file-button-icon">
88
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/image/nested.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
data-preview-width="256"
1010
data-file-block=""
1111
>
12-
<div class="bn-file-block-content-wrapper">
12+
<div class="bn-file-block-content-wrapper" style="position: relative;">
1313
<div class="bn-visual-media-wrapper">
1414
<img class="bn-visual-media" src="exampleURL" alt="Caption" draggable="false" />
1515
</div>
@@ -27,7 +27,7 @@
2727
data-preview-width="256"
2828
data-file-block=""
2929
>
30-
<div class="bn-file-block-content-wrapper">
30+
<div class="bn-file-block-content-wrapper" style="position: relative;">
3131
<div class="bn-visual-media-wrapper">
3232
<img class="bn-visual-media" src="exampleURL" alt="Caption" draggable="false" />
3333
</div>

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/image/noCaption.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
data-preview-width="256"
1010
data-file-block=""
1111
>
12-
<div class="bn-file-block-content-wrapper">
12+
<div class="bn-file-block-content-wrapper" style="position: relative;">
1313
<div class="bn-visual-media-wrapper">
1414
<img class="bn-visual-media" src="exampleURL" alt="example" draggable="false" />
1515
</div>

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/image/noName.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
data-preview-width="256"
1010
data-file-block=""
1111
>
12-
<div class="bn-file-block-content-wrapper">
12+
<div class="bn-file-block-content-wrapper" style="position: relative;">
1313
<div class="bn-visual-media-wrapper">
1414
<img class="bn-visual-media" src="exampleURL" alt="Caption" draggable="false" />
1515
</div>

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/image/noPreview.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
data-preview-width="256"
1212
data-file-block=""
1313
>
14-
<div class="bn-file-block-content-wrapper">
14+
<div class="bn-file-block-content-wrapper" style="position: relative;">
1515
<div class="bn-file-name-with-icon">
1616
<div class="bn-file-icon">
1717
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">

tests/src/unit/react/formatConversion/export/__snapshots__/blocknoteHTML/reactImage/basic.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
data-preview-width="256"
1111
data-file-block=""
1212
>
13-
<div class="bn-file-block-content-wrapper">
13+
<div class="bn-file-block-content-wrapper" style="position: relative;">
1414
<div class="bn-visual-media-wrapper">
1515
<img class="bn-visual-media" src="exampleURL" alt="example" draggable="false" />
1616
</div>

0 commit comments

Comments
 (0)