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.

improvement: hide all markdown action (dropdown and command palette) (#3777)

Myles Scolnick625b10ba1fe2
diff --git a/frontend/src/components/editor/actions/useHideAllMarkdownCode.ts b/frontend/src/components/editor/actions/useHideAllMarkdownCode.ts
new file mode 100644
index 0000000000..d0594b0c8c
--- /dev/null
+++ b/frontend/src/components/editor/actions/useHideAllMarkdownCode.ts
@@ -0,0 +1,52 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { useCellActions } from "@/core/cells/cells";
+import { getNotebook } from "@/core/cells/cells";
+import { saveCellConfig } from "@/core/network/requests";
+import { useCallback } from "react";
+import { MarkdownLanguageAdapter } from "@/core/codemirror/language/markdown";
+import type { CellId } from "@/core/cells/ids";
+import type { CellConfig } from "@/core/network/types";
+import { Objects } from "@/utils/objects";
+
+/**
+ * Hook to hide all markdown code cells
+ */
+export const useHideAllMarkdownCode = () => {
+ const { updateCellConfig } = useCellActions();
+
+ return useCallback(async () => {
+ const markdownAdapter = new MarkdownLanguageAdapter();
+ const notebook = getNotebook();
+ const cellIds = notebook.cellIds.inOrderIds;
+
+ const newConfigs: Record<CellId, Partial<CellConfig>> = {};
+
+ // Find all markdown cells that aren't already hidden
+ for (const cellId of cellIds) {
+ if (notebook.cellData[cellId] === undefined) {
+ continue;
+ }
+ const { code, config } = notebook.cellData[cellId];
+ if (config.hide_code) {
+ continue;
+ }
+ if (markdownAdapter.isSupported(code)) {
+ newConfigs[cellId] = { hide_code: true };
+ }
+ }
+
+ const entries = Objects.entries(newConfigs);
+
+ if (entries.length === 0) {
+ return;
+ }
+
+ // Save to backend
+ await saveCellConfig({ configs: newConfigs });
+
+ // Update on frontend
+ for (const [cellId, config] of entries) {
+ updateCellConfig({ cellId, config });
+ }
+ }, [updateCellConfig]);
+};
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index 10788d66bc..0459871662 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -31,6 +31,7 @@ import {
FilePlus2Icon,
FastForwardIcon,
DatabaseIcon,
+ EyeOffIcon,
} from "lucide-react";
import { commandPaletteAtom } from "../controls/command-palette";
import {
@@ -73,6 +74,7 @@ import { newNotebookURL } from "@/utils/urls";
import { useRunAllCells } from "../cell/useRunCells";
import { settingDialogAtom } from "@/components/app-config/state";
import { AddDatabaseDialogContent } from "../database/add-database-form";
+import { useHideAllMarkdownCode } from "./useHideAllMarkdownCode";
const NOOP_HANDLER = (event?: Event) => {
event?.preventDefault();
@@ -86,6 +88,7 @@ export function useNotebookActions() {
const { selectedPanel } = useChromeState();
const [viewState] = useAtom(viewStateAtom);
const kioskMode = useAtomValue(kioskModeAtom);
+ const hideAllMarkdownCode = useHideAllMarkdownCode();
const { updateCellConfig, undoDeleteCell, clearAllCellOutputs } =
useCellActions();
@@ -355,6 +358,11 @@ export function useNotebookActions() {
}
},
},
+ {
+ icon: <EyeOffIcon size={14} strokeWidth={1.5} />,
+ label: "Hide all markdown code",
+ handle: hideAllMarkdownCode,
+ },
{
icon: <XCircleIcon size={14} strokeWidth={1.5} />,
label: "Clear all outputs",