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: add "Copy code to clipboard" action (#421)

Myles Scolnick718d74d632c6
diff --git a/frontend/src/components/editor/CommandPallette.tsx b/frontend/src/components/editor/CommandPallette.tsx
index 38e1b9ee5f..020e3cc167 100644
--- a/frontend/src/components/editor/CommandPallette.tsx
+++ b/frontend/src/components/editor/CommandPallette.tsx
@@ -24,7 +24,7 @@ export const commandPalletteAtom = atom(false);
export const CommandPallette = () => {
const [open, setOpen] = useAtom(commandPalletteAtom);
const registeredActions = useRegisteredActions();
- const notebookActions = useNotebookActions({});
+ const notebookActions = useNotebookActions();
const notebookActionsWithoutHotkeys = notebookActions.filter(
(action) => !action.hotkey
);
diff --git a/frontend/src/components/editor/Controls.tsx b/frontend/src/components/editor/Controls.tsx
index 9ed6c58e4a..bdd01301c7 100644
--- a/frontend/src/components/editor/Controls.tsx
+++ b/frontend/src/components/editor/Controls.tsx
@@ -88,7 +88,7 @@ export const Controls = ({
{!closed && (
<div className={topRightControls}>
{presenting && <LayoutSelect />}
- <NotebookMenuDropdown filename={filename} />
+ <NotebookMenuDropdown />
<AppConfigButton />
<ShutdownButton onShutdown={onShutdown} />
</div>
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index a7d200ec17..3b35f9e02c 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -9,6 +9,7 @@ import {
ZapOffIcon,
BookMarkedIcon,
FolderDownIcon,
+ ClipboardCopyIcon,
} from "lucide-react";
import { commandPalletteAtom } from "../CommandPallette";
import {
@@ -17,14 +18,15 @@ import {
useCellActions,
useNotebook,
} from "@/core/cells/cells";
-import { saveCellConfig } from "@/core/network/requests";
+import { readCode, saveCellConfig } from "@/core/network/requests";
import { Objects } from "@/utils/objects";
import { ActionButton } from "./types";
import { downloadAsHTML } from "@/core/static/download-html";
import { toast } from "@/components/ui/use-toast";
+import { useFilename } from "@/core/saving/filename";
-export function useNotebookActions(opts: { filename?: string | null }) {
- const { filename } = opts;
+export function useNotebookActions() {
+ const [filename] = useFilename();
const notebook = useNotebook();
const { updateCellConfig } = useCellActions();
@@ -97,6 +99,20 @@ export function useNotebookActions(opts: { filename?: string | null }) {
},
},
+ {
+ icon: <ClipboardCopyIcon size={14} strokeWidth={1.5} />,
+ label: "Copy code to clipboard",
+ hidden: !filename,
+ handle: async () => {
+ const code = await readCode();
+ navigator.clipboard.writeText(code.contents);
+ toast({
+ title: "Copied",
+ description: "Code copied to clipboard.",
+ });
+ },
+ },
+
{
icon: <CommandIcon size={14} strokeWidth={1.5} />,
label: "Command palette",
diff --git a/frontend/src/components/editor/notebook-menu-dropdown.tsx b/frontend/src/components/editor/notebook-menu-dropdown.tsx
index 0ae417bc45..5d721fc68e 100644
--- a/frontend/src/components/editor/notebook-menu-dropdown.tsx
+++ b/frontend/src/components/editor/notebook-menu-dropdown.tsx
@@ -11,12 +11,8 @@ import {
import { renderMinimalShortcut } from "../shortcuts/renderShortcut";
import { useNotebookActions } from "./actions/useNotebookActions";
-interface Props {
- filename: string | null;
-}
-
-export const NotebookMenuDropdown: React.FC<Props> = ({ filename }) => {
- const actions = useNotebookActions({ filename });
+export const NotebookMenuDropdown: React.FC = () => {
+ const actions = useNotebookActions();
const button = (
<Button
diff --git a/frontend/src/core/App.tsx b/frontend/src/core/App.tsx
index f43ddd8bae..433bf9b562 100644
--- a/frontend/src/core/App.tsx
+++ b/frontend/src/core/App.tsx
@@ -46,7 +46,6 @@ import { toast } from "../components/ui/use-toast";
import { SortableCellsProvider } from "../components/sort/SortableCellsProvider";
import { CellId, HTMLCellId } from "./cells/ids";
import { CellConfig } from "./cells/types";
-import { getFilenameFromDOM } from "./dom/htmlUtils";
import { CellArray } from "../components/editor/renderers/CellArray";
import { RuntimeState } from "./RuntimeState";
import { CellsRenderer } from "../components/editor/renderers/cells-renderer";
@@ -56,6 +55,7 @@ import { useRunStaleCells } from "../components/editor/cell/useRunCells";
import { formatAll } from "./codemirror/format";
import { cn } from "@/utils/cn";
import { isStaticNotebook } from "./static/static-state";
+import { useFilename } from "./saving/filename";
interface AppProps {
userConfig: UserConfig;
@@ -66,7 +66,7 @@ export const App: React.FC<AppProps> = ({ userConfig, appConfig }) => {
const notebook = useNotebook();
const { setCells, updateCellCode } = useCellActions();
const [viewState, setViewState] = useAtom(viewStateAtom);
- const [filename, setFilename] = useState(getFilenameFromDOM());
+ const [filename, setFilename] = useFilename();
const [savedCodes, setSavedCodes] = useState<string[]>([""]);
const [savedConfigs, setSavedConfigs] = useState<CellConfig[]>([]);
const { openModal, closeModal, openAlert } = useImperativeModal();
diff --git a/frontend/src/core/saving/filename.ts b/frontend/src/core/saving/filename.ts
new file mode 100644
index 0000000000..0ecd5fedb2
--- /dev/null
+++ b/frontend/src/core/saving/filename.ts
@@ -0,0 +1,10 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+
+import { atom, useAtom } from "jotai";
+import { getFilenameFromDOM } from "../dom/htmlUtils";
+
+const filenameAtom = atom<string | null>(getFilenameFromDOM());
+
+export function useFilename() {
+ return useAtom(filenameAtom);
+}