Skip to content

Commit c4b382c

Browse files
Add import button
1 parent 0103b66 commit c4b382c

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

src/view-model/ratings-view-model.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class RatingsViewModel {
124124
let scanKeyPath: string = "";
125125
let scan: Scan;
126126
let imgRatingProperty: RatingProperty;
127-
for (const [i, img] of this.model.imgsArray.entries()) {
127+
for (const img of this.model.imgsArray.values()) {
128128
if (!this.model.ratingPropertiesByHash.has(img.hash)) {
129129
throw new Error(`RatingProperty not found for img '${img.hash}'`);
130130
}
@@ -180,10 +180,6 @@ export class RatingsViewModel {
180180
}
181181
}
182182

183-
// get img(): Img {
184-
// return
185-
// }
186-
187183
set(hash: string, rating: Rating): void {
188184
if (!this.model.ratingPropertiesByHash.has(hash)) {
189185
throw new Error(`Unknown hash '${hash}'`);

src/view/render.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export class Attribute {
99
}
1010

1111
// inspired by hyperscript
12-
export function h(tag: string, attrs: Attribute[], children: Node[]): HTMLElement {
13-
let element: HTMLElement = document.createElement(tag);
12+
export function h<K extends keyof HTMLElementTagNameMap>(tag: K, attrs: Attribute[], children: Node[]): HTMLElementTagNameMap[K] {
13+
let element = document.createElement(tag);
1414

1515
for (let attr of attrs) {
1616
element.setAttribute(attr.key, attr.value);

src/view/sidebar.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,20 @@ export class Sidebar extends HTMLElement {
3434
this.viewModel = viewModel;
3535

3636
const importButton = h(
37-
"button",
37+
"a",
3838
[new Attribute("class", "dropdown-item")],
3939
[t("Import...")]
4040
);
41+
const fileInput = h(
42+
"input",
43+
[
44+
new Attribute("type", "file"),
45+
new Attribute("hidden", ""),
46+
new Attribute("accept", ".json"),
47+
],
48+
[]
49+
);
50+
4151
const exportButton = h(
4252
"a",
4353
[
@@ -47,6 +57,7 @@ export class Sidebar extends HTMLElement {
4757
],
4858
[t("Export")]
4959
);
60+
5061
const viewTypeButton: { [key in ViewType]?: HTMLElement } = {};
5162
for (const viewType of viewTypes) {
5263
viewTypeButton[viewType] = h(
@@ -91,7 +102,7 @@ export class Sidebar extends HTMLElement {
91102
"ul",
92103
[new Attribute("class", "dropdown-menu")],
93104
[
94-
// h("li", [], [importButton]),
105+
h("li", [], [importButton, fileInput]),
95106
h("li", [], [exportButton]),
96107
h("li", [], [this.viewTypeButtonGroup]),
97108
h("li", [], [this.sortKeyButtonGroup]),
@@ -107,9 +118,35 @@ export class Sidebar extends HTMLElement {
107118
menuButton.classList.toggle("active");
108119
});
109120

110-
// importButton.addEventListener("click", () => {
111-
// //
112-
// });
121+
importButton.addEventListener("click", (e) => {
122+
e.preventDefault();
123+
fileInput.click();
124+
});
125+
fileInput.addEventListener("change", () => {
126+
const [ file ] = fileInput.files;
127+
const reader = new FileReader();
128+
129+
reader.addEventListener("load", () => {
130+
const database = viewModel.model.database;
131+
132+
const objs = JSON.parse(reader.result as string);
133+
for (const obj of objs) {
134+
const { rating } = obj;
135+
delete obj["rating"];
136+
137+
if (rating == "none") {
138+
continue;
139+
}
140+
141+
for (const img of database.findAll(obj)) {
142+
viewModel.ratingsViewModel.set(img.hash, rating);
143+
}
144+
}
145+
});
146+
147+
reader.readAsText(file);
148+
});
149+
113150
exportButton.addEventListener("click", () => {
114151
const objs = new Array();
115152
for (const [hash, ratingProperty] of viewModel.model.ratingPropertiesByHash) {

0 commit comments

Comments
 (0)