Skip to content

Commit c048296

Browse files
committed
Added OK button to popup & fixed overflow behavior
1 parent 0517c90 commit c048296

5 files changed

Lines changed: 133 additions & 16 deletions

File tree

packages/core/src/blocks/Code/helpers/render/createSourceBlockWithPreview.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ export const createSourceBlockWithPreview = (
103103
sourceBlockPopup.className = "bn-source-block-popup";
104104
sourceBlockPopup.appendChild(sourceBlock.dom);
105105

106+
// Wrapper with an "OK" button that closes the popup.
107+
const okButtonWrapper = document.createElement("div");
108+
okButtonWrapper.className = "bn-code-block-source-popup-ok-button-wrapper";
109+
okButtonWrapper.contentEditable = "false";
110+
111+
const okButton = document.createElement("button");
112+
okButton.className = "bn-code-block-source-popup-ok-button";
113+
okButton.textContent = "OK";
114+
okButtonWrapper.appendChild(okButton);
115+
116+
// Lays the source code and the "OK" button out side by side, with the button
117+
// to the right of the `pre` element.
118+
const pre = sourceBlock.contentDOM.parentElement!;
119+
const sourcePopupBody = document.createElement("div");
120+
sourcePopupBody.className = "bn-code-block-source-popup-body";
121+
sourceBlockPopup.insertBefore(sourcePopupBody, pre);
122+
sourcePopupBody.appendChild(pre);
123+
sourcePopupBody.appendChild(okButtonWrapper);
124+
106125
const errorMessage = "error" in preview && preview.error ? preview.error : "";
107126

108127
const sourceError = document.createElement("div");
@@ -129,7 +148,7 @@ export const createSourceBlockWithPreview = (
129148
const unsubscribeFromStore = store?.subscribe(updateFromStore);
130149

131150
// Opens the popup when clicking the preview.
132-
const handleMouseDown = (event: MouseEvent) => {
151+
const handlePreviewMouseDown = (event: MouseEvent) => {
133152
if (!editor.isEditable) {
134153
return;
135154
}
@@ -142,7 +161,16 @@ export const createSourceBlockWithPreview = (
142161
editor.setTextCursorPosition(block.id, "end");
143162
editor.focus();
144163
};
145-
previewContainer.addEventListener("mousedown", handleMouseDown);
164+
previewContainer.addEventListener("mousedown", handlePreviewMouseDown);
165+
166+
// Closes the popup when clicking the "OK" button.
167+
const handleOkButtonMouseDown = (event: MouseEvent) => {
168+
event.preventDefault();
169+
event.stopPropagation();
170+
171+
store?.setState((state) => ({ ...state, popupOpen: undefined }));
172+
};
173+
okButton.addEventListener("mousedown", handleOkButtonMouseDown);
146174

147175
return {
148176
dom: previewWithSourcePopup,
@@ -194,7 +222,8 @@ export const createSourceBlockWithPreview = (
194222
destroy: () => {
195223
sourceBlock.destroy();
196224
unsubscribeFromStore?.();
197-
previewContainer.removeEventListener("mousedown", handleMouseDown);
225+
previewContainer.removeEventListener("mousedown", handlePreviewMouseDown);
226+
okButton.removeEventListener("mousedown", handleOkButtonMouseDown);
198227
},
199228
};
200229
};

packages/core/src/editor/Block.css

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ NESTED BLOCKS
485485
padding: 12px;
486486
min-height: 1.5em;
487487
cursor: text;
488+
overflow: auto;
488489
}
489490

490491
.bn-source-block-popup {
@@ -498,7 +499,9 @@ NESTED BLOCKS
498499
border: var(--bn-border);
499500
border-radius: var(--bn-border-radius-medium);
500501
box-shadow: var(--bn-shadow-medium);
502+
max-height: 200px;
501503
opacity: 0;
504+
overflow: auto;
502505
pointer-events: none;
503506
}
504507

@@ -532,11 +535,17 @@ NESTED BLOCKS
532535
color: black;
533536
}
534537

535-
.bn-source-block-popup > pre {
538+
.bn-code-block-source-popup-body {
539+
display: flex;
540+
align-items: flex-end;
541+
}
542+
543+
.bn-code-block-source-popup-body > pre {
536544
white-space: pre;
537545
overflow-x: auto;
538546
margin: 0;
539-
width: 100%;
547+
flex: 1;
548+
min-width: 0;
540549
tab-size: 2;
541550
padding: 16px;
542551
}
@@ -549,6 +558,29 @@ NESTED BLOCKS
549558
white-space: pre-wrap;
550559
}
551560

561+
.bn-code-block-source-popup-ok-button-wrapper {
562+
align-items: flex-end;
563+
display: flex;
564+
justify-content: flex-end;
565+
padding: 15px 16px;
566+
}
567+
568+
.bn-code-block-source-popup-ok-button {
569+
appearance: none;
570+
border: none;
571+
cursor: pointer;
572+
background-color: rgb(37, 99, 235);
573+
color: white;
574+
font-size: 0.8em;
575+
font-weight: 500;
576+
border-radius: var(--bn-border-radius-small);
577+
padding: 4px 8px;
578+
}
579+
580+
.bn-editor[contenteditable="true"] .bn-code-block-source-popup-ok-button:hover {
581+
background-color: rgb(29, 78, 216);
582+
}
583+
552584
.bn-add-source-code-button {
553585
align-items: center;
554586
background-color: rgb(242, 241, 238);
@@ -587,6 +619,10 @@ NESTED BLOCKS
587619
/* INLINE MATH PREVIEW */
588620
/* Shares the block-preview markup/classes; these rules override the
589621
block-preview defaults that don't suit inline use. */
622+
.bn-inline-content-section .bn-preview-with-source-popup {
623+
display: inline-block;
624+
}
625+
590626
.bn-inline-content-section .bn-preview-container {
591627
padding: 0;
592628
min-height: 0;
@@ -598,7 +634,7 @@ NESTED BLOCKS
598634
}
599635

600636
/* Keeps the popup from collapsing to a sliver while the source is empty. */
601-
.bn-inline-content-section .bn-source-block-popup > pre {
637+
.bn-inline-content-section .bn-code-block-source-popup-body > pre {
602638
min-height: 1.5em;
603639
box-sizing: content-box;
604640
}

packages/math-block/src/block/react/render/MathBlockPreviewWithPopup.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const MathBlockPreviewWithPopup = (
3131
const { mathMLString, error } = useLatexToMathMLString(source);
3232

3333
// Opens the popup when clicking the preview.
34-
const handleMouseDown = (event: MouseEvent) => {
34+
const handlePreviewMouseDown = (event: MouseEvent) => {
3535
if (!editor.isEditable) {
3636
return;
3737
}
@@ -45,6 +45,14 @@ export const MathBlockPreviewWithPopup = (
4545
editor.focus();
4646
};
4747

48+
// Closes the popup when clicking the "OK" button.
49+
const handleOkButtonMouseDown = (event: MouseEvent) => {
50+
event.preventDefault();
51+
event.stopPropagation();
52+
53+
store.setState((state) => ({ ...state, popupOpen: undefined }));
54+
};
55+
4856
return (
4957
<div
5058
className={
@@ -56,7 +64,7 @@ export const MathBlockPreviewWithPopup = (
5664
<div
5765
className="bn-preview-container"
5866
contentEditable={false}
59-
onMouseDown={handleMouseDown}
67+
onMouseDown={handlePreviewMouseDown}
6068
>
6169
{source.length > 0 ? (
6270
<span dangerouslySetInnerHTML={{ __html: mathMLString }} />
@@ -67,9 +75,22 @@ export const MathBlockPreviewWithPopup = (
6775
)}
6876
</div>
6977
<div className="bn-source-block-popup">
70-
<pre>
71-
<code ref={contentRef} />
72-
</pre>
78+
<div className="bn-code-block-source-popup-body">
79+
<pre>
80+
<code ref={contentRef} />
81+
</pre>
82+
<div
83+
className="bn-code-block-source-popup-ok-button-wrapper"
84+
contentEditable={false}
85+
>
86+
<button
87+
className="bn-code-block-source-popup-ok-button"
88+
onMouseDown={handleOkButtonMouseDown}
89+
>
90+
OK
91+
</button>
92+
</div>
93+
</div>
7394
<div
7495
className="bn-code-block-source-error"
7596
contentEditable={false}

packages/math-block/src/block/vanilla/render/createMathBlockPreviewWithPopup.tsx renamed to packages/math-block/src/block/vanilla/render/createMathBlockPreviewWithPopup.ts

File renamed without changes.

packages/math-block/src/inlineContent/react/render/MathInlinePreviewWithPopup.tsx

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const MathInlinePreviewWithPopup = (
3737
const { mathMLString, error } = useLatexToMathMLString(source, true);
3838

3939
// Opens the popup when clicking the preview.
40-
const handleMouseDown = (event: MouseEvent) => {
40+
const handlePreviewMouseDown = (event: MouseEvent) => {
4141
if (!editor.isEditable || !pos) {
4242
return;
4343
}
@@ -56,6 +56,24 @@ export const MathInlinePreviewWithPopup = (
5656
editor.focus();
5757
};
5858

59+
// Closes the popup by moving the selection to just after the inline content.
60+
const handleOkButtonMouseDown = (event: MouseEvent) => {
61+
if (!editor.isEditable || !pos) {
62+
return;
63+
}
64+
65+
event.preventDefault();
66+
event.stopPropagation();
67+
68+
const view = editor.prosemirrorView!;
69+
view.dispatch(
70+
view.state.tr.setSelection(
71+
TextSelection.create(view.state.tr.doc, pos + node.nodeSize),
72+
),
73+
);
74+
editor.focus();
75+
};
76+
5977
return (
6078
<span
6179
// The source is hidden, so highlight the whole inline content while the
@@ -69,7 +87,7 @@ export const MathInlinePreviewWithPopup = (
6987
<span
7088
className="bn-preview-container"
7189
contentEditable={false}
72-
onMouseDown={handleMouseDown}
90+
onMouseDown={handlePreviewMouseDown}
7391
>
7492
{source.length > 0 ? (
7593
<span dangerouslySetInnerHTML={{ __html: mathMLString }} />
@@ -80,9 +98,22 @@ export const MathInlinePreviewWithPopup = (
8098
)}
8199
</span>
82100
<div className="bn-source-block-popup">
83-
<pre>
84-
<code ref={contentRef} />
85-
</pre>
101+
<div className="bn-code-block-source-popup-body">
102+
<pre>
103+
<code ref={contentRef} />
104+
</pre>
105+
<div
106+
className="bn-code-block-source-popup-ok-button-wrapper"
107+
contentEditable={false}
108+
>
109+
<button
110+
className="bn-code-block-source-popup-ok-button"
111+
onMouseDown={handleOkButtonMouseDown}
112+
>
113+
OK
114+
</button>
115+
</div>
116+
</div>
86117
<div
87118
className="bn-code-block-source-error"
88119
contentEditable={false}

0 commit comments

Comments
 (0)