import type { IconGlyph } from '@mongodb-js/compass-components'; import { cx } from '@mongodb-js/compass-components'; import { css, spacing } from '@mongodb-js/compass-components'; import { Button, Icon } from '@mongodb-js/compass-components'; import type { Ace } from 'ace-builds'; import React, { useCallback, useEffect, useMemo, useRef, useState, } from 'react'; import type { EditorProps as BaseEditorProps } from './base-editor'; import { BaseEditor } from './base-editor'; const actionButtonStyle = css({ flex: 'none', }); const actionButtonContentStyle = css({ position: 'relative', }); const actionButtonIconContainerStyle = css({ opacity: 1, // leafygreen icon small size width: 14, height: 14, transition: 'opacity .2s linear', }); const actionButtonIconContainerHiddenStyle = css({ opacity: 0, }); const actionButtonClickResultIconStyle = css({ position: 'absolute', // leafygreen icon small size width: 14, height: 14, top: 0, pointerEvents: 'none', opacity: 1, transition: 'opacity .2s linear', }); const actionButtonClickResultIconHiddenStyle = css({ opacity: 0, }); const ActionButton: React.FunctionComponent<{ icon: string | React.ReactNode; label: string; onClick: ( ...args: Parameters> ) => boolean | void; }> = ({ label, icon, onClick }) => { const [clickResult, setClickResult] = useState<'success' | 'error'>( 'success' ); const [clickResultVisible, setClickResultVisible] = useState(false); const onButtonClick = useCallback( (...args: Parameters>) => { const result = onClick(...args); if (typeof result === 'boolean') { setClickResult(result ? 'success' : 'error'); setClickResultVisible(true); } }, [onClick] ); useEffect(() => { if (!clickResultVisible) { return; } const timeoutId = setTimeout(() => { setClickResultVisible(false); }, 1000); return () => { clearTimeout(timeoutId); }; }, [clickResultVisible]); return ( ); }; type CustomActionItem = { icon: IconGlyph; action: string; label: string; }; type EditorProps = Omit & { copyable?: boolean; formattable?: boolean; customActions?: CustomActionItem[]; onAction?: (editor: Ace.Editor | undefined, action: string) => void; }; const FormatIcon = ({ size = 16, ...props }: React.SVGProps & { size?: number }) => ( ); const multilineEditorContainerStyle = css({ position: 'relative', height: '100%', [`&:focus-within > .multiline-editor-actions, &:hover > .multiline-editor-actions`]: { display: 'flex', }, }); const editorContainerStyle = css({ overflow: 'auto', // To account for the leafygreen button shadow on hover we add padding to the // top of the container that wraps the editor paddingTop: spacing[1], height: `calc(100% - ${spacing[1]}px)`, // We want folks to be able to click into the container element // they're using for the editor to focus the editor. minHeight: 'inherit', }); const actionsContainerStyle = css({ position: 'absolute', top: spacing[1], right: spacing[2], display: 'none', gap: spacing[2], }); const MultilineEditor: React.FunctionComponent = ({ copyable = true, formattable = true, customActions = [], onAction, onLoad, className, ...props }) => { const editorRef = useRef(); const actions = useMemo(() => { return [ copyable && ( { editorRef.current?.execCommand('copy-all'); return true; }} > ), formattable && ( } onClick={() => { editorRef.current?.execCommand('prettify'); return true; }} > ), ...customActions.map((action) => { return ( { onAction?.(editorRef.current, action.action); }} > ); }), ]; }, [copyable, formattable, customActions, onAction]); return (
{/* Separate scrollable container for editor so that action buttons can */} {/* stay in one place when scrolling */}
{ editorRef.current = editor; onLoad?.(editor); }} {...props} >
{actions.length > 0 && (
{actions}
)}
); }; export { MultilineEditor as Editor };