-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor JSON editor action container into its own component
- Loading branch information
1 parent
3e48138
commit 216642b
Showing
5 changed files
with
125 additions
and
95 deletions.
There are no files selected for viewing
9 changes: 8 additions & 1 deletion
9
packages/compass-editor/src/actions.tsx → ...ages/compass-editor/src/action-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React, { type RefObject, useMemo } from 'react'; | ||
|
||
import { type Action, ActionButton, FormatIcon } from './action-button'; | ||
import type { EditorRef } from './types'; | ||
import { css, cx, spacing } from '@mongodb-js/compass-components'; | ||
|
||
type ActionsContainerProps = { | ||
copyable: boolean; | ||
formattable: boolean; | ||
customActions?: Action[]; | ||
className?: string; | ||
editorRef: RefObject<EditorRef>; | ||
}; | ||
|
||
const actionsContainerStyle = css({ | ||
position: 'absolute', | ||
top: spacing[100], | ||
right: spacing[100], | ||
// left: spacing[100], | ||
display: 'none', | ||
gap: spacing[200], | ||
}); | ||
|
||
export const ActionsContainer = ({ | ||
copyable, | ||
formattable, | ||
customActions, | ||
className, | ||
editorRef, | ||
}: ActionsContainerProps) => { | ||
const actions = useMemo(() => { | ||
return [ | ||
copyable && ( | ||
<ActionButton | ||
key="Copy" | ||
label="Copy" | ||
icon="Copy" | ||
onClick={() => { | ||
return editorRef.current?.copyAll() ?? false; | ||
}} | ||
></ActionButton> | ||
), | ||
formattable && ( | ||
<ActionButton | ||
key="Format" | ||
label="Format" | ||
icon={ | ||
<FormatIcon | ||
size={/* leafygreen small */ 14} | ||
role="presentation" | ||
></FormatIcon> | ||
} | ||
onClick={() => { | ||
return editorRef.current?.prettify() ?? false; | ||
}} | ||
></ActionButton> | ||
), | ||
...(customActions ?? []).map((action) => { | ||
return ( | ||
<ActionButton | ||
key={action.label} | ||
icon={action.icon} | ||
label={action.label} | ||
onClick={() => { | ||
if (!editorRef.current?.editor) { | ||
return false; | ||
} | ||
return action.action(editorRef.current.editor); | ||
}} | ||
></ActionButton> | ||
); | ||
}), | ||
]; | ||
}, [copyable, formattable, customActions, editorRef]); | ||
|
||
return ( | ||
<div | ||
className={cx( | ||
'multiline-editor-actions', | ||
actionsContainerStyle, | ||
className | ||
)} | ||
> | ||
{actions} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters