Skip to content

Commit

Permalink
feat: add editor
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-at-airwalk committed May 27, 2024
1 parent a5baaa7 commit cdfb993
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 31 deletions.
95 changes: 73 additions & 22 deletions src/_components/Layouts/MenuWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import { Box } from '@mui/material';
import Container from '@mui/material/Container';
import React, { Suspense, useState } from 'react';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import React, { Suspense, useEffect, useState } from 'react';

import TopBar from '@/components/Layouts/TopBar';
import { HeaderMinimalMenu } from '@/components/Menus';
import EditorWrapper from '@/features/Mdx/EditorWrapper';
import { getLogger } from '@/lib/Logger';
import type { MenuStructure } from '@/lib/Types';
import type { ContentItem, MenuStructure } from '@/lib/Types';

const logger = getLogger();

Expand All @@ -16,7 +18,7 @@ const logger = getLogger();
interface MenuWrapperProps {
menuStructure?: MenuStructure[];
menuComponent: string;
// initialContext?: keyof typeof siteConfig.content;
context: ContentItem;
loading: boolean;
children: React.ReactElement<any, string | React.JSXElementConstructor<any>>;
}
Expand All @@ -26,55 +28,104 @@ logger.info('Loading MenuWrapper');
export default function MenuWrapper({
menuStructure,
menuComponent,
// initialContext,
context,
loading,
children,
}: MenuWrapperProps): React.ReactElement {
const navDrawerWidth = menuStructure ? 300 : 0;
const topBarHeight = 65;
const searchParams = useSearchParams();
// const isEditing = Boolean(searchParams.get('edit'));
// logger.debug({ isEditing });

const [menuOpen, setMenuOpen] = useState(true);
const [isEditing, setIsEditing] = useState(false);

const handleOnNavButtonClick = () => setMenuOpen((prevState) => !prevState);
const router = useRouter();
const pathname = usePathname();

const MenuComponent =
menuComponent === 'HeaderMinimalMenu'
? HeaderMinimalMenu
: HeaderMinimalMenu;

const handleEdit = () => {
// const isEditing = Boolean(searchParams.get('edit'));
const params = new URLSearchParams(searchParams);
if (isEditing) {
params.delete('edit');
setMenuOpen(true);
setIsEditing(false);
router.push(
pathname + (params.toString() ? `?${params.toString()}` : ''),
);
} else {
params.set('edit', 'true');
setMenuOpen(false);
setIsEditing(true);
router.push(
pathname + (params.toString() ? `?${params.toString()}` : ''),
);
}
};

useEffect(() => {
const editParam = searchParams.get('edit');
if (editParam === 'true') {
setIsEditing(true);
setMenuOpen(false);
} else {
setIsEditing(false);
}
}, [searchParams]);

return (
<>
<TopBar
onNavButtonClick={handleOnNavButtonClick}
navOpen={menuOpen}
navOpen={isEditing ? false : menuOpen}
menu
logo
edit
handleEdit={() => handleEdit()}
// topBarHeight={topBarHeight}
/>
{menuStructure && (

{menuStructure && menuOpen && (
<MenuComponent
menu={menuStructure}
open={menuOpen}
top={topBarHeight}
drawerWidth={navDrawerWidth}
/>
)}

<div
style={{
marginTop: topBarHeight,
paddingLeft: !menuOpen || loading ? 0 : navDrawerWidth,
}}
>
<Box sx={{ px: '2%' }}>
<Container sx={{ maxHeight: '100vh', pt: '2%' }}>
<Suspense fallback={<p>Loading...</p>}>
{children && children}
</Suspense>
{/* probably should fix this to display a 404 or something if nothing is nested */}
</Container>
</Box>
</div>
<Suspense fallback={<p>Loading...</p>}>
{isEditing && (
<div
style={{
marginTop: topBarHeight,
paddingLeft: !menuOpen || loading ? 0 : navDrawerWidth,
}}
>
<EditorWrapper context={context} />
</div>
)}
{!isEditing && (
<div
style={{
marginTop: topBarHeight,
paddingLeft: !menuOpen || loading ? 0 : navDrawerWidth,
}}
>
<Box sx={{ px: '2%' }}>
<Container sx={{ maxHeight: '100vh', pt: '2%' }}>
{children && children}
</Container>
</Box>
</div>
)}
</Suspense>
</>
);
}
4 changes: 4 additions & 0 deletions src/_components/Layouts/TopBar.stories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import TopBar from '@/components/Layouts/TopBar';
import { fn } from '@storybook/test';

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
export default {
Expand All @@ -15,6 +16,9 @@ export default {
color: { control: 'color' },
edit: { control: 'boolean' },
},
args: {
handleEdit: fn(),
},
};

// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
Expand Down
14 changes: 7 additions & 7 deletions src/_components/Layouts/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface ComponentProps {
edit?: boolean;
// handlePrint?: () => void;
// handlePresentation?: () => void;
handleMore?: () => void;
handleEdit?: () => void;
}

export default function TopBar({
Expand All @@ -52,7 +52,7 @@ export default function TopBar({
edit,
// handlePrint,
// handlePresentation,
handleMore,
handleEdit,
}: ComponentProps): React.ReactElement {
// Function body

Expand All @@ -72,11 +72,11 @@ export default function TopBar({
setActiveMenu('');
};

const handleMoreClick = () => {
if (typeof handleMore === 'function') {
handleMore();
const handleEditClick = () => {
if (typeof handleEdit === 'function') {
handleEdit();
} else {
// console.error('TopBar: Error: handleMore is not a function');
// console.error('TopBar: Error: handleEdit is not a function');
}
};

Expand Down Expand Up @@ -217,7 +217,7 @@ export default function TopBar({
{edit && (
<Button
size="large"
onClick={() => handleMoreClick()}
onClick={() => handleEditClick()}
color="inherit"
startIcon={<AutoAwesomeIcon />}
>
Expand Down
34 changes: 34 additions & 0 deletions src/_features/Mdx/EditorWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use client';

import { type MDXEditorMethods } from '@mdxeditor/editor';
import { Box } from '@mui/material';
import Container from '@mui/material/Container';
import React, { useRef } from 'react';

import { Editor } from '@/components/Editor';
import type { ContentItem } from '@/lib/Types';

interface EditorWrapperProps {
context: ContentItem;
}

export default function EditorWrapper({ context }: EditorWrapperProps) {
const editorRef = useRef<MDXEditorMethods | null>(null);

return (
<Box sx={{ px: '2%' }}>
<Container sx={{ maxHeight: '100vh', pt: '2%' }}>
<Editor
context={context}
editorRef={editorRef}
editorSaveHandler={() => Promise.resolve('')}
enabled
imagePreviewHandler={() => Promise.resolve('')}
imageUploadHandler={() => Promise.resolve('')}
markdown="# test"
top={0}
/>
</Container>
</Box>
);
}
6 changes: 4 additions & 2 deletions src/app/docs/[[...path]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export default async function Page({
// title={`${siteConfig.title} | ${contentConfig?.path?.charAt(0).toUpperCase()}${contentConfig?.path?.slice(1)}`}
menuComponent='HeaderMinimalMenu'
menuStructure={menuStructure}
loading={loading}>
loading={loading}
context={contentConfig}>
{ contentConfig ? <IndexTiles initialContext={{ ...contentConfig, source: '' }} /> : <></> }
</MenuWrapper>
</main>
Expand Down Expand Up @@ -107,7 +108,8 @@ export default async function Page({
// title={`${siteConfig.title} | ${contentConfig?.path?.charAt(0).toUpperCase()}${contentConfig?.path?.slice(1)}`}
menuComponent='HeaderMinimalMenu'
menuStructure={menuStructure}
loading={loading}>
loading={loading}
context={contentConfig}>
<ContentViewer pageContent={pageContentText} contributors={pageContent.contributors} context={ contentConfig } loading={loading} />
</MenuWrapper>
</main>
Expand Down

0 comments on commit cdfb993

Please sign in to comment.