From f0558927578d7089989efbf26caa5ae9ee0fb693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Wed, 6 Nov 2024 13:03:59 +0100 Subject: [PATCH] feat(editor): expandable json editor (#6446) * Update spacing * Fix pointer events css * Add expand button * Add whitespace to gutters * Adjust expand button offset --- .../src/components/json-editor.tsx | 13 +++++-- packages/compass-editor/src/action-button.tsx | 13 +++++-- .../compass-editor/src/actions-container.tsx | 34 +++++++++++++++++-- packages/compass-editor/src/editor.tsx | 16 ++++++++- 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/packages/compass-crud/src/components/json-editor.tsx b/packages/compass-crud/src/components/json-editor.tsx index 2dda284dba4..3c86cff5c68 100644 --- a/packages/compass-crud/src/components/json-editor.tsx +++ b/packages/compass-crud/src/components/json-editor.tsx @@ -45,8 +45,7 @@ const editorDarkModeStyles = css({ }); const actionsGroupStyles = css({ - paddingTop: spacing[2], - paddingRight: spacing[2], + padding: spacing[200], }); export type JSONEditorProps = { @@ -258,6 +257,14 @@ const JSONEditor: React.FunctionComponent = ({ onDeletionFinished, ]); + const toggleExpandCollapse = useCallback(() => { + if (doc.expanded) { + doc.collapse(); + } else { + doc.expand(); + } + }, [doc]); + // Trying to change CodeMirror editor state when an update "effect" is in // progress results in an error which is why we timeout the code mirror update // itself. @@ -297,6 +304,8 @@ const JSONEditor: React.FunctionComponent = ({ className={cx(editorStyles, darkMode && editorDarkModeStyles)} actionsClassName={actionsGroupStyles} completer={completer} + onExpand={editing ? undefined : toggleExpandCollapse} + expanded={expanded} /> div:has(svg)': { + paddingLeft: 3, + paddingRight: 3, + }, }); const actionButtonContentStyle = css({ @@ -52,7 +60,8 @@ export const ActionButton: React.FunctionComponent<{ ...args: Parameters> ) => boolean | void; 'data-testid'?: string; -}> = ({ label, icon, onClick, ...props }) => { + compact?: boolean; +}> = ({ label, icon, onClick, compact, ...props }) => { const [clickResult, setClickResult] = useState<'success' | 'error'>( 'success' ); @@ -89,7 +98,7 @@ export const ActionButton: React.FunctionComponent<{ aria-label={label} title={label} onClick={onButtonClick} - className={actionButtonStyle} + className={cx(actionButtonStyle, { [actionCompactButtonStyle]: compact })} data-testid={props['data-testid'] ?? `editor-action-${label}`} >
diff --git a/packages/compass-editor/src/actions-container.tsx b/packages/compass-editor/src/actions-container.tsx index f309f1de438..dc9e439924b 100644 --- a/packages/compass-editor/src/actions-container.tsx +++ b/packages/compass-editor/src/actions-container.tsx @@ -10,14 +10,29 @@ type ActionsContainerProps = { customActions?: Action[]; className?: string; editorRef: RefObject; + onExpand?: () => void; + expanded?: boolean; }; const actionsContainerStyle = css({ position: 'absolute', - top: spacing[1], - right: spacing[2], + top: spacing[100], + right: spacing[100], + left: spacing[100], display: 'none', - gap: spacing[2], + gap: spacing[200], + pointerEvents: 'none', +}); + +const expandContainerStyle = css({ + position: 'relative', + top: -spacing[100], + left: -spacing[100], +}); + +const actionsGroupItemSeparator = css({ + flex: '1 0 auto', + pointerEvents: 'none', }); export const ActionsContainer = ({ @@ -26,6 +41,8 @@ export const ActionsContainer = ({ customActions, className, editorRef, + onExpand, + expanded, }: ActionsContainerProps) => { return (
+ {onExpand && ( +
+ +
+ )} + {copyable && ( void; + expanded?: boolean; }; const MultilineEditor = React.forwardRef( @@ -1411,8 +1420,10 @@ const MultilineEditor = React.forwardRef( editorClassName, actionsClassName, darkMode: _darkMode, + onExpand, + expanded, ...props - }, + }: MultilineEditorProps, ref ) { const darkMode = useDarkMode(_darkMode); @@ -1469,6 +1480,7 @@ const MultilineEditor = React.forwardRef( multilineEditorContainerStyle, darkMode && multilineEditorContainerDarkModeStyle, hasActions && multilineEditorContainerWithActionsStyle, + onExpand && multilineEditorContainerWithExpandStyle, className )} // We want folks to be able to click into the container element @@ -1492,6 +1504,8 @@ const MultilineEditor = React.forwardRef( > {hasActions && (