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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: add copy button in console, and fix ctrl-a to copy all (#5765)
Myles Scolnick025900a2dfae
diff --git a/frontend/src/components/editor/output/ConsoleOutput.tsx b/frontend/src/components/editor/output/ConsoleOutput.tsxindex 819c44c95f..a5a7d48b45 100644--- a/frontend/src/components/editor/output/ConsoleOutput.tsx+++ b/frontend/src/components/editor/output/ConsoleOutput.tsx@@ -1,18 +1,21 @@/* Copyright 2024 Marimo. All rights reserved. */import { AnsiUp } from "ansi_up";-import { ChevronRightIcon, WrapTextIcon } from "lucide-react";+import { ChevronRightIcon, CopyIcon, WrapTextIcon } from "lucide-react";import React, { useLayoutEffect } from "react";-import { ToggleButton, Tooltip, TooltipTrigger } from "react-aria-components";+import { ToggleButton } from "react-aria-components";import { DebuggerControls } from "@/components/debugger/debugger-code";import { Input } from "@/components/ui/input";+import { Tooltip } from "@/components/ui/tooltip";import type { CellId } from "@/core/cells/ids";import { isInternalCellName } from "@/core/cells/names";import type { WithResponse } from "@/core/cells/types";import type { OutputMessage } from "@/core/kernel/messages";import { useSelectAllContent } from "@/hooks/useSelectAllContent";import { cn } from "@/utils/cn";+import { copyToClipboard } from "@/utils/copy";import { invariant } from "@/utils/invariant";+import { Strings } from "@/utils/strings";import { NameCellContentEditable } from "../actions/name-cell-input";import { ErrorBoundary } from "../boundary/ErrorBoundary";import { OutputRenderer } from "../Output";@@ -68,7 +71,7 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {const hasOutputs = consoleOutputs.length > 0;// Enable Ctrl/Cmd-A to select all content within the console output- useSelectAllContent(ref, hasOutputs);+ const selectAllProps = useSelectAllContent(hasOutputs);// Keep scroll at the bottom if it is within 120px of the bottom,// so when we add new content, it will lock to the bottom@@ -110,23 +113,43 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {return (<div className="relative group">- <TooltipTrigger>- <ToggleButton- aria-label="Toggle text wrapping"- className="absolute top-1 right-1 h-6 w-6 z-10 rounded flex items-center justify-center opacity-0 group-hover:opacity-100 bg-transparent text-muted-foreground data-[hovered]:text-foreground data-[selected]:text-foreground"- isSelected={wrapText}- onChange={setWrapText}- >- <WrapTextIcon className="h-4 w-4" />- </ToggleButton>- <Tooltip className="z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-xs data-[side=bottom]:slide-in-from-top-1 data-[side=left]:slide-in-from-right-1 data-[side=right]:slide-in-from-left-1 data-[side=top]:slide-in-from-bottom-1">- {wrapText ? "Disable wrap text" : "Wrap text"}+ <div className="absolute top-1 right-5 z-10 opacity-0 group-hover:opacity-100 flex gap-1">+ <Tooltip content="Copy all">+ <span>+ <button+ aria-label="Copy all console output"+ className="p-1 rounded bg-transparent text-muted-foreground hover:text-foreground"+ type="button"+ onClick={() => {+ const text = reversedOutputs+ .filter((output) => output.channel !== "pdb")+ .map((output) => Strings.asString(output.data))+ .join("\n");+ void copyToClipboard(text);+ }}+ >+ <CopyIcon className="h-4 w-4" />+ </button>+ </span></Tooltip>- </TooltipTrigger>+ <Tooltip content={wrapText ? "Disable wrap text" : "Wrap text"}>+ <span>+ <ToggleButton+ aria-label="Toggle text wrapping"+ className="p-1 rounded bg-transparent text-muted-foreground data-[hovered]:text-foreground data-[selected]:text-foreground"+ isSelected={wrapText}+ onChange={setWrapText}+ >+ <WrapTextIcon className="h-4 w-4" />+ </ToggleButton>+ </span>+ </Tooltip>+ </div><divtitle={stale ? "This console output is stale" : undefined}data-testid="console-output-area"ref={ref}+ {...selectAllProps}// biome-ignore lint/a11y/noNoninteractiveTabindex: Needed to capture keypress eventstabIndex={0}className={cn(diff --git a/frontend/src/hooks/useHotkey.ts b/frontend/src/hooks/useHotkey.tsindex d73b92702a..1ca3848ed5 100644--- a/frontend/src/hooks/useHotkey.ts+++ b/frontend/src/hooks/useHotkey.ts@@ -54,33 +54,6 @@ export function useHotkey(shortcut: HotkeyAction, callback: HotkeyHandler) {}, [memoizeCallback, shortcut, isNOOP, registerAction, unregisterAction]);}-/**- * Registers a hotkey listener on a given element or ref to an element.- */-export function useHotkeysOnElement<T extends HotkeyAction>(- element: RefObject<HTMLElement | null> | null,- handlers: Record<T, HotkeyHandler | undefined>,-) {- const hotkeys = useAtomValue(hotkeysAtom);- useEventListener(element, "keydown", (e) => {- for (const [shortcut, callback] of Objects.entries(handlers)) {- if (callback === undefined) {- continue;- }- const key = hotkeys.getHotkey(shortcut).key;- if (parseShortcut(key)(e)) {- Logger.debug("Satisfied", key, e);- const response = callback(e);- // Prevent default if the callback does not return false- if (response !== false) {- e.preventDefault();- e.stopPropagation();- }- }- }- });-}-/*** Registers a hotkey listener on a given element or ref to an element.*/diff --git a/frontend/src/hooks/useSelectAllContent.ts b/frontend/src/hooks/useSelectAllContent.tsindex 5dd01c05a5..01ae40b164 100644--- a/frontend/src/hooks/useSelectAllContent.ts+++ b/frontend/src/hooks/useSelectAllContent.ts@@ -1,25 +1,17 @@/* Copyright 2024 Marimo. All rights reserved. */-import type { RefObject } from "react";-import { useEventListener } from "./useEventListener";/*** Hook that enables Ctrl/Cmd-A to select all text content within a specific element* @param ref - React ref to the element whose content should be selectable* @param enabled - Whether the hook should be active (defaults to true)*/-export function useSelectAllContent<T extends HTMLElement>(- ref: RefObject<T | null>,- enabled = true,-) {- const handleKeyDown = (event: KeyboardEvent) => {+export function useSelectAllContent<T extends HTMLElement>(enabled = true) {+ const handleKeyDown = (event: React.KeyboardEvent<T>) => {if (!enabled) {return;}- const element = ref.current;- if (!element) {- return;- }+ const element = event.currentTarget;// Check for Ctrl-A (Windows/Linux) or Cmd-A (Mac)const isSelectAll =@@ -46,5 +38,5 @@ export function useSelectAllContent<T extends HTMLElement>(selection.addRange(range);};- useEventListener(ref, "keydown", handleKeyDown);+ return { onKeyDown: handleKeyDown };}diff --git a/frontend/src/utils/strings.ts b/frontend/src/utils/strings.tsindex 9b69159c2d..10b7f40712 100644--- a/frontend/src/utils/strings.ts+++ b/frontend/src/utils/strings.ts@@ -46,6 +46,16 @@ export const Strings = {withoutLeadingSlash(url: string): string {return url.startsWith("/") ? url.slice(1) : url;},+ asString: (value: unknown): string => {+ if (typeof value === "string") {+ return value;+ }+ try {+ return JSON.stringify(value);+ } catch {+ return String(value);+ }+ },};export const decodeUtf8 = (array: Uint8Array): string => {