-
Notifications
You must be signed in to change notification settings - Fork 9
/
pages.ts
205 lines (186 loc) · 6.62 KB
/
pages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import pdfMake from "pdfmake/build/pdfmake";
import { TDocumentDefinitions } from "pdfmake/interfaces";
import { fabric } from "fabric";
import Page from "./page";
import { JSONWriter } from "./files";
export type PageJSON = {
version: string;
objects: fabric.Object[];
background: string;
};
const defaultPageJSON: PageJSON = {
version: "4.2.0",
objects: [],
background: "white",
};
/**
* @return The current local time in the form `YYYY-MM-DD-HH-mm`
*/
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 {
currentIndex = 0;
constructor(
public canvas: Page,
public canvasWidth: number,
public canvasHeight: number,
public updateState: () => void,
public pagesJSON: PageJSON[] = [defaultPageJSON]
) {}
savePage = (): void => {
this.pagesJSON[this.currentIndex] = this.canvas.toObject([
"data",
"strokeUniform",
]);
};
/**
* Safe method to load "move to"/"switch to" a specific page in UI.
* Will not do anything if `{@param index} === this.currentIndex && {@param saveExisting}`.
*
* @param index The 0-based index of the page to load
* @param saveExisting
* Whether to save the contents on the current page to memory before switching pages.
* Forces an unconditional re-render when set to `false`,
* as opposed to not doing anything if the current page is {@param index}.
* May need to set to `false` if directly manipulating the internal array to prevent an override.
* @return
* The index of the loaded page.
* This equals {@param index}.
*/
// TODO: Should saveExisting be renamed and negated to force?
loadPage = async (index: number, saveExisting = true): Promise<number> => {
if (saveExisting) this.savePage();
if (index === this.currentIndex && saveExisting) return index;
await this.canvas.loadFromJSONAsync(this.pagesJSON[index]);
this.currentIndex = index;
this.updateState();
return index;
};
/**
* Safely move back one page, creating a blank page if necessary.
* @return The index of the loaded page.
*/
previousOrNewPage = (): Promise<number> => {
if (this.currentIndex === 0) {
return this.insertPagesBefore([defaultPageJSON], true);
}
return this.loadPage(this.currentIndex - 1);
};
/**
* Safely move forward one page, creating a blank page if necessary.
* @return The index of the loaded page.
*/
nextOrNewPage = (): Promise<number> => {
if (this.currentIndex === this.pagesJSON.length - 1) {
return this.insertPagesAfter([defaultPageJSON], true);
}
return this.loadPage(this.currentIndex + 1);
};
/**
* Safely convert the existing pages to PDF, and initiate a download.
* The filename is of the form `qboard-YYYY-MM-DD-HH-mm.pdf` in local time.
*/
export = async (): Promise<void> => {
this.savePage();
const ratio = 2;
const content: { svg: string; width: number }[] = [];
const currentIndexCopy = this.currentIndex;
// Load each page and then record it as svg
for (const page of this.pagesJSON) {
// As of now, each page needs to be individually loaded, so we await each load
// eslint-disable-next-line no-await-in-loop
await this.canvas.loadFromJSONAsync(page);
content.push({
svg: this.canvas.toSVG(),
width: this.canvasWidth / ratio,
});
}
const docDefinition: TDocumentDefinitions = {
pageSize: {
width: this.canvasWidth / ratio,
height: this.canvasHeight / ratio,
},
pageMargins: [0, 0],
content,
};
pdfMake.createPdf(docDefinition).download(`qboard-${timeString()}.pdf`);
await this.canvas.loadFromJSONAsync(this.pagesJSON[currentIndexCopy]);
};
/**
* Safely convert the existing pages to qboard JSON, and initiate a download.
* The filename is of the form `qboard-YYYY-MM-DD-HH-mm.json` in local time.
*/
saveFile = (): void => {
this.savePage();
new JSONWriter(this.pagesJSON).download(`qboard-${timeString()}.json`);
this.canvas.modified = false;
};
/**
* Replace the entire board with different data,
* after prompting (window.confirm) the user for confirmation.
* @param pages an array of page data in the internal format
* @return Whether the pages were successfully overwritten.
* This is the same as whether the user accepted the prompt.
*/
overwritePages = async (
pages: PageJSON[] = [defaultPageJSON]
): Promise<boolean> => {
const response =
!this.canvas.modified ||
this.pagesJSON.every((page) => page.objects.length === 0) ||
window.confirm(
"Your work will be overwritten. Are you sure you wish to continue?"
);
if (!response) return false;
this.pagesJSON = pages;
await this.loadPage(0, false);
this.canvas.modified = false;
return true;
};
/**
* Add the content of {@param pages} to the pages list before the current page.
* @param isNonModifying `true` if you don't want to mark the board as modified due to this change.
* Generally set when inserting blank pages, which don't contain any objects.
*/
insertPagesBefore = async (
pages: PageJSON[] = [defaultPageJSON],
isNonModifying = false
): Promise<number> => {
this.savePage();
this.pagesJSON.splice(this.currentIndex, 0, ...pages);
// make sure not to do
// this.canvas.modified = !isNonModifying
// because that marks an already modified board as unmodified
if (!isNonModifying) {
this.canvas.modified = true;
}
// you have already saved the pages via splice;
// if you saveExisting then you will be saving to the wrong index
return this.loadPage(this.currentIndex, false);
};
/**
* Add the content of {@param pages} to the pages list after the current page.
* @param isNonModifying `true` if you don't want to mark the board as modified due to this change.
* Generally set when inserting blank pages, which don't contain any objects.
*/
insertPagesAfter = async (
pages: PageJSON[] = [defaultPageJSON],
isNonModifying = false
): Promise<number> => {
this.pagesJSON.splice(this.currentIndex + 1, 0, ...pages);
// make sure not to do
// this.canvas.modified = !isNonModifying
// because that marks an already modified board as unmodified
if (!isNonModifying) {
this.canvas.modified = true;
}
// you can saveExisting because you're only updating indices _after_ the current page.
// you can also saveExisting before the splice
return this.loadPage(this.currentIndex + 1, true);
};
}