-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
102 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Sharable link | ||
|
||
One of the main features of the application is the ability to share a diagram with other users via a sharable link. | ||
|
||
The link is generated by setting a query parameter in the URL to the content of the code editor. | ||
|
||
The content of the query parameter is read into the code editor when the application is loaded. | ||
|
||
Here is a snippet for generating the sharable link: | ||
|
||
```ts | ||
const url = new URL(window.location.href); | ||
url.searchParams.set("preview", "true"); | ||
url.searchParams.set("content", editorContent); | ||
navigator.clipboard.writeText(url.href); | ||
enqueueSnackbar("Copied link to clipboard", { | ||
variant: "info", | ||
}); | ||
``` |
32 changes: 32 additions & 0 deletions
32
docs/technical-documentation/frontend/02-exporting-diagrams.md
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,32 @@ | ||
# Exporting diagrams | ||
|
||
The diagram itself is an HTML element, but the main issue is that browsers do not provide a way to export an HTML element as an image. | ||
|
||
The current solution is to capture the diagram element, convert to a data URL, then download it as an image. | ||
|
||
The conversion step is handled by [html-to-image](https://github.com/bubkoo/html-to-image). | ||
It provides simple APIs to capture an HTML element and convert it to a data URL. | ||
In the project, I used `toPng`, `toSvg` and `toJpeg` functions. | ||
|
||
The download step is handled by [FileSaver.js](https://github.com/eligrey/FileSaver.js) | ||
|
||
Here is a snippet of the code: | ||
|
||
```ts | ||
const HTMLNode = document.getElementById( | ||
"structogram-preview", | ||
); | ||
if (HTMLNode === null) { | ||
return; | ||
} | ||
toSvg(HTMLNode).then((blob) => { | ||
if (blob === null) { | ||
return; | ||
} | ||
if (window.saveAs) { | ||
window.saveAs(blob, "diagram"); | ||
} else { | ||
saveAs(blob, "diagram"); | ||
} | ||
}); | ||
``` |
6 changes: 6 additions & 0 deletions
6
docs/technical-documentation/frontend/03-snackbar-notification.md
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,6 @@ | ||
# Snackbar notification | ||
|
||
When copying a shareable link or exporting a diagram, a snackbar notification will appear to provide feedback to the user. | ||
This is handled by [Notistack](https://iamhosseindhv.com/notistack). | ||
|
||
There is no practical reason why I chose Notistack over other libraries, apart from the fact that it is built on top of Material UI, so it is very easy to integrate it with Material UI. |
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,5 @@ | ||
# UI library | ||
|
||
The UI library is built with [Material UI](https://material-ui.com/). | ||
|
||
I was tempted to use other icon libraries like [Font Awesome](https://fontawesome.com/) or [Feather Icons](https://feathericons.com/), but I decided to stick with [MUI Icon](https://material-ui.com/components/material-icons/) for style consistency. |
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,5 @@ | ||
# Code editor | ||
|
||
The code editor is the most important part of the project, so I want to a few functionalities of text editors such auto-pairing brackets, syntax highlighting, etc. | ||
|
||
The code editor is built with [CodeMirror](https://codemirror.net/), which is a very popular text editor library. |
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,35 @@ | ||
# Auto-saving | ||
|
||
The save the progress of the user, the application automatically saves the content of the code editor to the browser's local storage when a change has been made to the code editor. | ||
|
||
```ts | ||
useEffect(() => { | ||
localStorage.setItem( | ||
"editorContent", | ||
editorContent, | ||
); | ||
}, [editorContent]); | ||
``` | ||
|
||
This is a very simple solution, but it is not perfect. | ||
|
||
There will be cases where the user opened a sharable link while having some previous progress saved in the local storage. | ||
In this case, the content of the code editor will be overwritten by the content of the sharable link. | ||
|
||
I have decided that the sharable link should take precedence over the local storage, so the content of the sharable link will be loaded into the code editor if it is available. | ||
|
||
```ts | ||
const url = new URL(window.location.href); | ||
const content = url.searchParams.get("content"); | ||
if (content !== null) { | ||
window.localStorage.setItem("content", content); | ||
return content; | ||
} | ||
|
||
const savedContent = | ||
window.localStorage.getItem("content"); | ||
if (savedContent !== null) { | ||
return savedContent; | ||
} | ||
return ""; | ||
``` |
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 was deleted.
Oops, something went wrong.