Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date/time JSON and files management #94

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/lib/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class JSONReader {
const { "qboard-version": version, pages } = object;
switch (version) {
case 1:
case 2:
return pages;
default:
return pages;
Expand All @@ -46,13 +47,15 @@ export class JSONWriter {
private readonly sourceJSON: {
"qboard-version": number;
pages: PageJSON[];
"exported-date": Date;
};
private stringified: string;

constructor(pagesJSON: PageJSON[]) {
this.sourceJSON = {
"qboard-version": 1,
"qboard-version": 2,
pages: pagesJSON,
"exported-date": new Date(), // this date is only parsed once per new JSONWriter()
};
}

Expand All @@ -70,6 +73,41 @@ export class JSONWriter {
const revoke = () => window.URL.revokeObjectURL(url);
return [url, revoke];
};

download = (): void => {
const [fileURL, revokeURL] = this.toURL();

new FileUI().download(
`qboard-${FileUI.timeStringNow()}.json`,
fileURL,
revokeURL
);
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should actually return an object of a new type FileUIDownloader with the data

{
  name: `qboard-${FileUI.timeStringNow()}.json`,
  fileURL,
  revokeURL,
}

Then FileUI::download should accept an object of this type, and any function calling JSONWriter::download should take the result and call its own FileUI::download. I'd probably rename JSONWriter::download to JSONWriter::getDownloader.

(I use the :: syntax to denote instance methods.)

}

export class FileUI {
static timeString = (date): string => {
return [
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours(),
date.getMinutes(),
].join("-");
};
static timeStringNow = (): string => FileUI.timeString(new Date());

download = (name: string, fileURL: string, revokeURL = (): void => {}) => {
const elt = document.createElement("a");
elt.style.display = "none";
elt.href = fileURL;
elt.download = name;
document.body.appendChild(elt);
elt.click();
elt.parentElement.removeChild(elt);

revokeURL();
};
}

export type FileHandlerResponse = {
Expand Down
26 changes: 5 additions & 21 deletions src/lib/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import pdfMake from "pdfmake/build/pdfmake.min";
import { fabric } from "fabric";

import Page from "./page";
import { JSONWriter } from "./files";
import { FileUI, JSONWriter } from "./files";

export type PageJSON = {
version: string;
Expand All @@ -16,14 +16,6 @@ const defaultPageJSON: PageJSON = {
background: "white",
};

const timeString = (): string => {
const offset = new Date().getTimezoneOffset() * 60000;
return new Date(Date.now() - offset)
.toISOString()
.slice(0, -8)
.replace(/\D/g, "-");
};

export default class Pages {
pagesJSON: PageJSON[] = [defaultPageJSON];
currentIndex = 0;
Expand Down Expand Up @@ -94,24 +86,16 @@ export default class Pages {
content,
};

pdfMake.createPdf(docDefinition).download(`qboard-${timeString()}.pdf`);
pdfMake
.createPdf(docDefinition)
.download(`qboard-${FileUI.timeStringNow()}.pdf`);

await this.canvas.loadFromJSONAsync(this.pagesJSON[currentIndexCopy]);
};

saveFile = (): void => {
this.savePage();
const [fileURL, revokeURL] = new JSONWriter(this.pagesJSON).toURL();

const elt = document.createElement("a");
elt.style.display = "none";
elt.href = fileURL;
elt.download = `qboard-${timeString()}.json`;
document.body.appendChild(elt);
elt.click();
elt.parentElement.removeChild(elt);

revokeURL();
new JSONWriter(this.pagesJSON).download();
this.canvas.modified = false;
};

Expand Down