summaryrefslogtreecommitdiff
path: root/reader.js
blob: 112d430b4e117efcb729cede04e71277ff1d3eb1 (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const TCRSYNC_SERVER = "https://tt-rss.org/tcrsync/";

function Model() {
	const self = this;

	self.fileName = ko.observable("");
	self._zip = null;
	self._zipEntries = ko.observableArray();
	self.syncAccount = ko.observable("");

	self.cacheKey = function(suffix) {
		const sha1 = require('js-sha1');

		return sha1(self.fileName()) + ":" + suffix;
	};

	self.syncClient = {
		_sha1_compat: function(str) {
			const sha1 = require('js-sha1');
			const hash = sha1.create();
			hash.update(str);

			const chars = hash.array('');
			let rv = "";

			for (let i = 0; i < chars.length; i++) {
				rv += Number(chars[i]).toString(16);
			}

			return rv;
		},
		set: function(page) {
			if (!self.syncAccount() || !self.fileName())
				return;

			const owner = this._sha1_compat(self.syncAccount());
			const hash = this._sha1_compat(self.fileName());

			console.log('syncClient, set', owner, hash, page)
		},
		get: function() {
			if (!self.syncAccount() || !self.fileName())
				return;

			const owner = this._sha1_compat(self.syncAccount());
			const hash = this._sha1_compat(self.fileName());

			console.log('syncClient, get', owner, hash);

			return new Promise((resolve, reject) => {
				$.post(TCRSYNC_SERVER, {op: "get", version: 2, owner: owner, hash: hash}).then((resp) => {
					console.log('tcrsync_get', resp);

					const page = isNaN(resp) ? -1 : parseInt(resp);

					resolve(page);
				});
			});
		},
	};

	self.openFile = function (file) {
		console.log('openFile', file);

		self._zipEntries.removeAll();
		self.currentPage(-2);

		const AdmZip = require('adm-zip');
		self._zip = new AdmZip(file);

		const zipEntries = self._zip.getEntries();

		for (let i = 0; i < zipEntries.length; i++) {
			const ze = zipEntries[i];

			if (ze.entryName.match(/\.(jpe?g|gif|bmp|png|webp)$/i)) {
				self._zipEntries.push(ze);
			}
		}

		self._zipEntries.sort(function(a, b) {
			return a.entryName.localeCompare(b.entryName);
		});

		self.fileName(file.split(/[\\/]/).pop());


		localforage.getItem(self.cacheKey("POSITION")).then((page) => {
			if (page)
				self.currentPage(page);
			else
				self.currentPage(0);
		});

	};

	self.documentTitle = ko.computed(function () {
		if (self.fileName())
			return "Pow! Comics Reader: " + self.fileName();
		else
			return "Pow! Comics Reader";
	});

	self.totalPages = ko.computed(function () {
		return self._zipEntries().length;
	});

	self._currentPage = ko.observable(-2);

	self.currentPage = ko.computed({
		read: function() {
			return self._currentPage();
		},
		write: function(page) {
			self._currentPage(page);

			localforage.getItem(model.cacheKey("POSITION")).then((stored) => {
				if (stored < page) {
					localforage.setItem(model.cacheKey("POSITION"), page).then(() => {
						console.log('saved position', page);

						if (page % 10 == 0) {
							model.syncClient.set(page);
						}
					});
				}
			});
		},
	});

	self.currentPageDisp = ko.computed(function () {
		const page = self.currentPage();

		if (page < 0)
			return 0;
		else
			return page + 1;
	});

	self.progressPct = ko.computed(function () {
		if (self.totalPages() > 0) {
			return parseInt(self.currentPage() / self.totalPages() * 100) + "%";
		} else {
			return "";
		}
	});

	self.getLeftPage = ko.computed(function () {
		const page = self.currentPage();

		if (self._zip && page >= 0 && page < self._zipEntries().length) {
			const ze = self._zipEntries()[page];

			if (ze) {
				return 'data:image/jpeg;base64,' + ze.getData().toString("base64");
			}
		}
	});

	self.getRightPage = ko.computed(function () {
		const page = self.currentPage() + 1;

		if (self._zip && page >= 0 && page < self._zipEntries().length) {
			const ze = self._zipEntries()[page];

			if (ze) {
				return 'data:image/jpeg;base64,' + ze.getData().toString("base64");
			}
		}
	});
}

const model = new Model();

$(document).ready(function () {
	const {remote, ipcRenderer} = require('electron');
	const {BrowserWindow} = remote;

	localforage.getItem("TTC:SYNC-ACCOUNT").then((acct) => {
		if (acct)
			model.syncAccount(acct);
	});

	ko.applyBindings(model, document.querySelector('html'));

	ipcRenderer.on("open-settings", () => {
		$('#settings-modal').modal('show');
	});

	ipcRenderer.on("open-file", (event, args) => {
		model.openFile(args[0]);
	});

	ipcRenderer.on("sync-to-last", (event, args) => {
		model.syncClient.get().then((page) => {
			if (confirm("You are currently on page %p. Furthest read page stored on the server is %r. Open it instead?"
							.replace("%p", model.currentPageDisp())
							.replace("%r", page + 1))) {

				model.currentPage(page);

			}
		});
	});

	$(".location").click(function() {
		const current = model.currentPageDisp();
		const total = model.totalPages();

		const page = prompt("Jump to location [1-" + total + "]", parseInt(current));

		if (page) {
			$("#reader img").attr("data-page", parseInt(page - 1));
		}
	});

	$(".sync-account").on("change", function() {
		const acct = $(this).val();

		console.log('sa', acct);

		localforage.setItem("TTC:SYNC-ACCOUNT", acct).then(() => {
			model.syncAccount(acct);
		});
	});

	$("#left").on("click", (e) => {
		e.stopPropagation();

		let page = model.currentPage();

		if ($(".right-page").is(":visible") && page > 1)
			page = page - 1;

		if (page > 0)
			model.currentPage(page - 1);

	});

	$("#right").on("click", (e) => {
		e.stopPropagation();

		let page = model.currentPage();

		if ($(".right-page").is(":visible") && page < total_pages - 2)
			page = page + 1;

		if (page < model.totalPages()) {
			model.currentPage(page + 1);
		}
	});

	$(document).on("keydown", (e) => {
		if ($(".modal").is(":visible"))
			return;

		switch (e.which) {
			case 27:
				$(".header,.footer").fadeIn();
				break;
			case 37:
				$("#left").click();
				break;
			case 32:
			case 39:
				$("#right").click();
				break;
		}
	});

});