-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
201 additions
and
25 deletions.
There are no files selected for viewing
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
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,96 @@ | ||
import React, { memo } from 'react' | ||
import { | ||
Box, | ||
Button, | ||
LightMode, | ||
Menu, | ||
MenuButton, | ||
MenuList, | ||
MenuItem, | ||
MenuDivider, | ||
LinkProps, | ||
MenuItemProps, | ||
MenuButtonProps, | ||
ButtonProps, | ||
} from '@chakra-ui/core' | ||
import useDispatch from '../hooks/useDispatch' | ||
import { loadFromJSON, saveAsJSON } from '../utils/import' | ||
import { useSelector } from 'react-redux' | ||
import { getComponents } from '../core/selectors/components' | ||
import { FaBomb, FaSave } from 'react-icons/fa' | ||
import { GoRepo } from 'react-icons/go' | ||
import { FiUpload } from 'react-icons/fi' | ||
|
||
type MenuItemLinkProps = MenuItemProps | LinkProps | ||
|
||
// Ignore because of AS typing issues | ||
// @ts-ignore | ||
const MenuItemLink: React.FC<MenuItemLinkProps> = React.forwardRef( | ||
(props, ref: React.Ref<HTMLLinkElement>) => { | ||
// @ts-ignore | ||
return <MenuItem ref={ref} as="a" {...props} /> | ||
}, | ||
) | ||
|
||
// @ts-ignore | ||
const CustomMenuButton: React.FC< | ||
MenuButtonProps | ButtonProps | ||
> = React.forwardRef((props, ref: React.Ref<HTMLLinkElement>) => { | ||
// @ts-ignore | ||
return <MenuButton as={Button} {...props} /> | ||
}) | ||
|
||
const ExportMenuItem = () => { | ||
const components = useSelector(getComponents) | ||
|
||
return ( | ||
<MenuItem onClick={() => saveAsJSON(components)}> | ||
<Box mr={2} as={FaSave} /> | ||
Save components | ||
</MenuItem> | ||
) | ||
} | ||
const HeaderMenu = () => { | ||
const dispatch = useDispatch() | ||
|
||
return ( | ||
<Menu> | ||
<CustomMenuButton | ||
rightIcon="chevron-down" | ||
as={Button} | ||
size="xs" | ||
variant="ghost" | ||
variantColor="gray" | ||
> | ||
Editor | ||
</CustomMenuButton> | ||
<LightMode> | ||
<MenuList zIndex={100}> | ||
<ExportMenuItem /> | ||
<MenuItem | ||
onClick={async () => { | ||
const components = await loadFromJSON() | ||
dispatch.components.reset(components) | ||
}} | ||
> | ||
<Box mr={2} as={FiUpload} /> | ||
Import components | ||
</MenuItem> | ||
|
||
<MenuDivider /> | ||
|
||
<MenuItemLink isExternal href="https://chakra-ui.com/getting-started"> | ||
<Box mr={2} as={GoRepo} /> | ||
Chakra UI Docs | ||
</MenuItemLink> | ||
<MenuItemLink href="https://github.com/premieroctet/openchakra/issues"> | ||
<Box mr={2} as={FaBomb} /> | ||
Report issue | ||
</MenuItemLink> | ||
</MenuList> | ||
</LightMode> | ||
</Menu> | ||
) | ||
} | ||
|
||
export default memo(HeaderMenu) |
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
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,39 @@ | ||
import { fileOpen, fileSave } from 'browser-nativefs' | ||
import { INITIAL_COMPONENTS } from '../core/models/components' | ||
|
||
export async function loadFromJSON() { | ||
const blob = await fileOpen({ | ||
extensions: ['json'], | ||
mimeTypes: ['application/json'], | ||
}) | ||
|
||
const contents: string = await new Promise(resolve => { | ||
const reader = new FileReader() | ||
reader.readAsText(blob, 'utf8') | ||
reader.onloadend = () => { | ||
if (reader.readyState === FileReader.DONE) { | ||
resolve(reader.result as string) | ||
} | ||
} | ||
}) | ||
|
||
try { | ||
return JSON.parse(contents) | ||
} catch (error) {} | ||
|
||
return INITIAL_COMPONENTS | ||
} | ||
|
||
export async function saveAsJSON(components: IComponents) { | ||
const serialized = JSON.stringify(components) | ||
const name = `components.json` | ||
|
||
await fileSave( | ||
new Blob([serialized], { type: 'application/json' }), | ||
{ | ||
fileName: name, | ||
description: 'Excalidraw file', | ||
}, | ||
(window as any).handle, | ||
) | ||
} |
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