Skip to content

Commit

Permalink
feat: add "new" button to clear the canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewinci committed Mar 16, 2023
1 parent 84126cd commit b420067
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ const App = () => {
if ((drawingName?.length ?? 0) == 0) return;

// retrieve the drawing from content-script
const drawing = await getDrawing();
await createDrawing({
const rawDrawing = await getDrawing();
const drawing = await createDrawing({
id: Date.now().toString(), //todo: use an uuid
name: drawingName!,
lastUpdate: new Date().toDateString(),
data: drawing
})
data: rawDrawing
});
await setDrawing(drawing);
}

const SaveNewDrawing = <>
Expand All @@ -84,6 +85,7 @@ const App = () => {
<Button
onClick={onSaveButtonClick}
color='green'>Save</Button>
<Button onClick={() => setDrawing({ data: [], name: null })} color='red'>Clear</Button>
</Group>
</>;

Expand All @@ -92,7 +94,7 @@ const App = () => {
<Text size='s'>{activeDrawingName}</Text>
</Group>
<Group margin="5px 0 20px 0">
<Button color='red'>New</Button>
<Button onClick={() => setDrawing({ data: [], name: null })} color='red'>New</Button>
<Button color='orange'>Update</Button>
<Button color='green'>Save as</Button>
</Group>
Expand Down
2 changes: 1 addition & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type GetDrawingResponse = { action: "drawing"; data: any };
* Set the current drawing in the local store
* with the data content
*/
type SetDrawingRequest = { action: "set-drawing"; name: string; data: any };
type SetDrawingRequest = { action: "set-drawing"; name: string | null; data: any };

type SetDrawingResponse = { action: "drawing-set"; success: boolean };

Expand Down
4 changes: 3 additions & 1 deletion src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ chrome.runtime.onMessage.addListener(function (
break;
case "set-drawing":
localStorage.setItem(ACTIVE_DRAWING_DATA_KEY, request.data);
localStorage.setItem(ACTIVE_DRAWING_NAME_KEY, request.name);
if (request.name)
localStorage.setItem(ACTIVE_DRAWING_NAME_KEY, request.name);
else localStorage.removeItem(ACTIVE_DRAWING_NAME_KEY);
reply({
action: "drawing-set",
success: true,
Expand Down
2 changes: 1 addition & 1 deletion src/use-content-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const useContentScript = () => {
}
};

const setDrawing = async (drawing: Drawing) => {
const setDrawing = async (drawing: {data: any, name: string | null}) => {
const response = await sendReceiveMessage({
action: "set-drawing",
data: drawing.data,
Expand Down
3 changes: 2 additions & 1 deletion src/use-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const useStorage = () => {
readDrawings();
}, []);

const createDrawing = async (newDrawing: Drawing): Promise<void> => {
const createDrawing = async (newDrawing: Drawing): Promise<Drawing> => {
await readDrawings();
if (drawings.find((d) => d.id === newDrawing.id)) {
throw Error("[createDrawing] Duplicated id found");
Expand All @@ -45,6 +45,7 @@ export const useStorage = () => {
await storage.set(DATA_KEY, result);
// update the state with read drawings
await readDrawings();
return newDrawing
};

const readDrawings = async (): Promise<void> => {
Expand Down

0 comments on commit b420067

Please sign in to comment.