Skip to content

Commit

Permalink
feat: added diagram preview and edit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
boris0301 committed Aug 6, 2024
1 parent 55d6a59 commit 55e6451
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 111 deletions.
17 changes: 0 additions & 17 deletions appsscript.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,5 @@
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/documents"
],
"addOns": {
"common": {
"name": "Mermaid Chart",
"logoUrl": "https://www.mermaidchart.com/img/mermaid-chart-48.png",
"useLocaleFromApp": true,
"universalActions": [
{
"label": "Learn more about Mermaid Chart",
"openLink": "www.mermaidchart.com"
}
],
"layoutProperties": {
"primaryColor": "#424242",
"secondaryColor": "#ff3670"
}
}
},
"runtimeVersion": "V8"
}
36 changes: 22 additions & 14 deletions src/client/edit-diagram-dialog/components/edit-diagram-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { serverFunctions } from '../../utils/serverFunctions';
import { buildUrl, handleDialogClose } from '../../utils/helpers';
import useAuth from '../../hooks/useAuth';

const editUrl = localStorage.getItem('editUrl');

const EditDiagramDialog = () => {
const { authState, authStatus } = useAuth();
const [diagramsUrl, setDiagramsUrl] = useState('');
Expand All @@ -12,28 +14,34 @@ const EditDiagramDialog = () => {

const getMetadata = async () => {
try {
if (editUrl) {
const iframeUrl = buildUrl(editUrl, authState.token);
setDiagramsUrl(iframeUrl);
localStorage.removeItem('editUrl');
return;
}
const metadata = await serverFunctions.readSelectedImageMetadata();
if (typeof metadata === 'string') {
const params = new URLSearchParams(metadata);
const projectID = params.get('projectID');
const documentID = params.get('documentID');
const major = params.get('major');
const minor = params.get('minor');
if (projectID && documentID && major && minor) {
const iframeUrl = buildUrl(
`/app/projects/${projectID}/diagrams/${documentID}/version/v.${major}.${minor}/edit`,
authState.token
);
setDiagramsUrl(iframeUrl);
}
if (typeof metadata !== 'string') return;

const params = new URLSearchParams(metadata);
const projectID = params.get('projectID');
const documentID = params.get('documentID');
const major = params.get('major');
const minor = params.get('minor');
if (projectID && documentID && major && minor) {
const iframeUrl = buildUrl(
`/app/projects/${projectID}/diagrams/${documentID}/version/v.${major}.${minor}/edit`,
authState.token
);
setDiagramsUrl(iframeUrl);
}
} catch (error) {
console.log(error);
}
};

getMetadata();
}, [authState]);
}, [authState, editUrl]);

Check warning on line 44 in src/client/edit-diagram-dialog/components/edit-diagram-dialog.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has an unnecessary dependency: 'editUrl'. Either exclude it or remove the dependency array. Outer scope values like 'editUrl' aren't valid dependencies because mutating them doesn't re-render the component

useEffect(() => {
const handleMessage = async (e: MessageEvent) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useEffect, useState } from 'react';
import { buildUrl } from '../../utils/helpers';
import useAuth from '../../hooks/useAuth';
import { CircularProgress, Container, Typography } from '@mui/material';

const previewUrl = localStorage.getItem('previewUrl');

const PreviewDiagramDialog = () => {
const { authState, authStatus } = useAuth();
const [diagramsUrl, setDiagramsUrl] = useState('');

useEffect(() => {
if (!authState?.authorized || !previewUrl) return;
const url = buildUrl(previewUrl, authState.token);
setDiagramsUrl(url);
}, [authState, previewUrl]);

Check warning on line 16 in src/client/preview-diagram-dialog/components/preview-diagram-dialog.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has an unnecessary dependency: 'previewUrl'. Either exclude it or remove the dependency array. Outer scope values like 'previewUrl' aren't valid dependencies because mutating them doesn't re-render the component

if (authStatus === 'idle' || authStatus === 'loading') {
return (
<Container
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '96.5vh',
}}
>
<CircularProgress />
</Container>
);
}

if (authStatus === 'error') {
return (
<Container
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '96.5vh',
}}
>
<Typography variant="h5" gutterBottom my={2} textAlign="center">
Error
</Typography>
<Typography paragraph textAlign="center">
Something went wrong. Please try again later.
</Typography>
</Container>
);
}

return (
<div style={{ padding: '3px', overflowX: 'hidden', height: '100%' }}>
<iframe
src={diagramsUrl}
title="diagrams"
style={{
border: 'none',
width: '100%',
height: '96.5vh',
}}
/>
</div>
);
};

export default PreviewDiagramDialog;
33 changes: 33 additions & 0 deletions src/client/preview-diagram-dialog/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<base target="_top" />
<!-- Add any external scripts and stylesheets here -->
<script
crossorigin
src="https://unpkg.com/[email protected]/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/@mui/[email protected]/umd/material-ui.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/[email protected]/dist/index.js"
></script>
<script
crossorigin
src="https://unpkg.com/@types/[email protected]/index.d.ts"
></script>
</head>
<body>
<section id="index">
<script type="module" src="./index.jsx"></script>
<!-- bundled js and css will get inlined here during build-->
</section>
</body>
</html>
7 changes: 7 additions & 0 deletions src/client/preview-diagram-dialog/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ReactDOM from 'react-dom';
import PreviewDiagramDialog from './components/preview-diagram-dialog';
import './styles.css';

const container = document.getElementById('index');
const root = ReactDOM.createRoot(container);
root.render(<PreviewDiagramDialog />);
4 changes: 4 additions & 0 deletions src/client/preview-diagram-dialog/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* needed to make consistent test snapshots across OSs */
body {
font-family: Arial !important;
}
Loading

0 comments on commit 55e6451

Please sign in to comment.