Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

ux: double click to reveal hidden markdown code (#7495)

Akshay Agrawal664b78bab668
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 7d1d950501..ccdd150d20 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -37,7 +37,6 @@ import { cn } from "@/utils/cn";
import { invariant } from "@/utils/invariant";
import { mergeRefs } from "@/utils/mergeRefs";
import { AiCompletionEditor } from "../../ai/ai-completion-editor";
-import { HideCodeButton } from "../../code/readonly-python-code";
import {
closeSignatureHelp,
useCellEditorNavigationProps,
@@ -401,10 +400,6 @@ const CellEditorInternal = ({
const navigationProps = useCellEditorNavigationProps(cellId, editorViewRef);
- // Completely hide the editor & icons if it's markdown and hidden. If there is output, we show.
- const showHideButton =
- (hidden && !isMarkdown) || (hidden && isMarkdown && !hasOutput);
-
let editorClassName = "";
if (isMarkdown && hidden && hasOutput) {
editorClassName = "h-0 overflow-hidden";
@@ -448,17 +443,12 @@ const CellEditorInternal = ({
outputArea={outputArea}
>
<div className="relative w-full" {...navigationProps}>
- {showHideButton && (
- <HideCodeButton
- tooltip="Edit code"
- className="absolute inset-0 z-10"
- onClick={showHiddenCode}
- />
- )}
<CellCodeMirrorEditor
className={editorClassName}
editorView={editorViewRef.current}
ref={editorViewParentRef}
+ hidden={hidden}
+ showHiddenCode={showHiddenCode}
/>
{!hidden && showLanguageToggles && (
<div className="absolute top-1 right-5">
@@ -480,10 +470,12 @@ const CellCodeMirrorEditor = React.forwardRef(
props: {
className?: string;
editorView: EditorView | null;
+ hidden?: boolean;
+ showHiddenCode?: (opts?: { focus?: boolean }) => void;
},
ref?: React.Ref<HTMLDivElement>,
) => {
- const { className, editorView } = props;
+ const { className, editorView, hidden, showHiddenCode } = props;
const internalRef = useRef<HTMLDivElement>(null);
// If this gets unmounted/remounted, we need to re-append the editorView
@@ -503,6 +495,12 @@ const CellCodeMirrorEditor = React.forwardRef(
return (
<div
className={cn("cm mathjax_ignore", className)}
+ onDoubleClick={(e) => {
+ if (hidden && showHiddenCode) {
+ e.stopPropagation();
+ showHiddenCode({ focus: true });
+ }
+ }}
ref={(r) => {
if (ref) {
mergeRefs(ref, internalRef)(r);
diff --git a/frontend/src/components/editor/notebook-cell.tsx b/frontend/src/components/editor/notebook-cell.tsx
index 95408fa8c2..c89a647da5 100644
--- a/frontend/src/components/editor/notebook-cell.tsx
+++ b/frontend/src/components/editor/notebook-cell.tsx
@@ -78,7 +78,6 @@ import {
} from "./cell/StagedAICell";
import { useDeleteCellCallback } from "./cell/useDeleteCell";
import { useRunCell } from "./cell/useRunCells";
-import { HideCodeButton } from "./code/readonly-python-code";
import { cellDomProps } from "./common";
import { SqlValidationErrorBanner } from "./errors/sql-validation-errors";
import { useCellNavigationProps } from "./navigation/navigation";
@@ -456,13 +455,17 @@ const EditableCellComponent = ({
const cellOutput = userConfig.display.cell_output;
const hasOutputAbove = hasOutput && cellOutput === "above";
- const hasOutputBelow = hasOutput && cellOutput === "below";
// If the cell is too short, we need to position some icons inline to prevent overlaps.
// This can only happen to markdown cells when the code is hidden completely
const [isCellStatusInline, setIsCellStatusInline] = useState(false);
const [isCellButtonsInline, setIsCellButtonsInline] = useState(false);
+ // For markdown cells, get the inner content directly from the editor
+ // (editorView.state.doc contains the transformed markdown, not the mo.md(...) wrapper)
+ const isEmptyMarkdownContent =
+ isMarkdown && editorView.current?.state.doc.toString().trim() === "";
+
useResizeObserver({
ref: cellContainerRef,
skip: !isMarkdown,
@@ -480,15 +483,28 @@ const EditableCellComponent = ({
},
});
- const renderHideCodeButton = (className: string) => (
- <HideCodeButton
- tooltip="Edit markdown"
- className={cn("z-20 relative", className)}
- onClick={showHiddenCode}
- />
- );
+ const emptyMarkdownPlaceholder = isMarkdownCodeHidden &&
+ isEmptyMarkdownContent &&
+ !needsRun && (
+ <div
+ role="button"
+ aria-label="Double-click to edit markdown"
+ className="relative cursor-pointer px-3 py-2"
+ onDoubleClick={showHiddenCodeIfMarkdown}
+ onKeyDown={(e) => {
+ if (e.key === "Enter") {
+ showHiddenCodeIfMarkdown();
+ }
+ }}
+ tabIndex={0}
+ >
+ <span className="text-[var(--slate-8)] text-sm">
+ Double-click (or enter) to edit
+ </span>
+ </div>
+ );
- const outputArea = hasOutput && (
+ const outputArea = hasOutput && !isEmptyMarkdownContent && (
<div className="relative" onDoubleClick={showHiddenCodeIfMarkdown}>
<div className="absolute top-5 -left-7 z-20 print:hidden">
<CollapseToggle
@@ -503,7 +519,6 @@ const EditableCellComponent = ({
canCollapse={canCollapse}
/>
</div>
- {isMarkdownCodeHidden && hasOutputBelow && renderHideCodeButton("top-3")}
<OutputArea
// Only allow expanding in edit mode
allowExpand={true}
@@ -515,9 +530,6 @@ const EditableCellComponent = ({
stale={isStaleCell}
loading={outputIsLoading(cellRuntime.status)}
/>
- {isMarkdownCodeHidden &&
- hasOutputAbove &&
- renderHideCodeButton("bottom-3")}
</div>
);
@@ -582,7 +594,7 @@ const EditableCellComponent = ({
{...cellDomProps(cellId, cellData.name)}
>
<CellLeftSideActions cellId={cellId} actions={actions} />
- {cellOutput === "above" && outputArea}
+ {cellOutput === "above" && (outputArea || emptyMarkdownPlaceholder)}
<div
className={cn("tray")}
data-has-output-above={hasOutputAbove}
@@ -660,7 +672,7 @@ const EditableCellComponent = ({
cellId={cellId}
hide={cellRuntime.errored && !isStaleCell}
/>
- {cellOutput === "below" && outputArea}
+ {cellOutput === "below" && (outputArea || emptyMarkdownPlaceholder)}
{cellRuntime.serialization && (
<div className="py-1 px-2 flex items-center justify-end gap-2 last:rounded-b">
{isToplevel && (