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
14 changes: 14 additions & 0 deletions frontend/src/components/editor/actions/useNotebookActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {
MessagesSquareIcon,
YoutubeIcon,
DiamondPlusIcon,
ChevronDownCircleIcon,
ChevronRightCircleIcon,
} from "lucide-react";
import { commandPaletteAtom } from "../controls/command-palette";
import {
Expand Down Expand Up @@ -81,6 +83,7 @@ import { useRunAllCells } from "../cell/useRunCells";
import { settingDialogAtom } from "@/components/app-config/state";
import { AddDatabaseDialogContent } from "../database/add-database-form";
import { useHideAllMarkdownCode } from "./useHideAllMarkdownCode";
import { useSectionCollapse } from "./useSectionCollapse";
import { Constants } from "@/core/constants";
import { getFeatureFlag } from "@/core/config/feature-flag";

Expand All @@ -97,6 +100,7 @@ export function useNotebookActions() {
const [viewState] = useAtom(viewStateAtom);
const kioskMode = useAtomValue(kioskModeAtom);
const hideAllMarkdownCode = useHideAllMarkdownCode();
const { collapseAllSections, expandAllSections } = useSectionCollapse();

const {
updateCellConfig,
Expand Down Expand Up @@ -375,6 +379,16 @@ export function useNotebookActions() {
label: "Hide all markdown code",
handle: hideAllMarkdownCode,
},
{
icon: <ChevronRightCircleIcon size={14} strokeWidth={1.5} />,
label: "Collapse all sections",
handle: collapseAllSections,
},
{
icon: <ChevronDownCircleIcon size={14} strokeWidth={1.5} />,
label: "Expand all sections",
handle: expandAllSections,
},
{
icon: <DiamondPlusIcon size={14} strokeWidth={1.5} />,
label: "Add setup cell",
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/components/editor/actions/useSectionCollapse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { useCellActions } from "@/core/cells/cells";
import { getNotebook } from "@/core/cells/cells";
import { canCollapseOutline } from "@/core/dom/outline";
import useEvent from "react-use-event-hook";

/**
* Hooks to collapse and expand all sections in the notebook.
*/

export const useSectionCollapse = () => {
const { collapseCell, expandCell } = useCellActions();

const processAllSections = async (action: "collapse" | "expand") => {
const notebook = getNotebook();
const cellIds = notebook.cellIds.inOrderIds;

// Find all markdown cells that aren't already hidden
for (const cellId of cellIds) {
const outline = notebook.cellRuntime[cellId].outline;
// Check if the cell is a markdown cell with a TOC outline
if (!outline) {
continue;
}
// Check if the cell is a collapsible header
if (!canCollapseOutline(outline)) {
continue;
}
// Collapse or expand the cell based on the action
action === "collapse" ? collapseCell({ cellId }) : expandCell({ cellId });
}
};

return {
collapseAllSections: useEvent(() => processAllSections("collapse")),
expandAllSections: useEvent(() => processAllSections("expand")),
};
};
Loading