@@ -34,10 +34,20 @@ export class Sidebar extends HTMLElement {
34
34
this . viewModel = viewModel ;
35
35
36
36
const importButton = h (
37
- "button " ,
37
+ "a " ,
38
38
[ new Attribute ( "class" , "dropdown-item" ) ] ,
39
39
[ t ( "Import..." ) ]
40
40
) ;
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
+
41
51
const exportButton = h (
42
52
"a" ,
43
53
[
@@ -47,6 +57,7 @@ export class Sidebar extends HTMLElement {
47
57
] ,
48
58
[ t ( "Export" ) ]
49
59
) ;
60
+
50
61
const viewTypeButton : { [ key in ViewType ] ?: HTMLElement } = { } ;
51
62
for ( const viewType of viewTypes ) {
52
63
viewTypeButton [ viewType ] = h (
@@ -91,7 +102,7 @@ export class Sidebar extends HTMLElement {
91
102
"ul" ,
92
103
[ new Attribute ( "class" , "dropdown-menu" ) ] ,
93
104
[
94
- // h("li", [], [importButton]),
105
+ h ( "li" , [ ] , [ importButton , fileInput ] ) ,
95
106
h ( "li" , [ ] , [ exportButton ] ) ,
96
107
h ( "li" , [ ] , [ this . viewTypeButtonGroup ] ) ,
97
108
h ( "li" , [ ] , [ this . sortKeyButtonGroup ] ) ,
@@ -107,9 +118,35 @@ export class Sidebar extends HTMLElement {
107
118
menuButton . classList . toggle ( "active" ) ;
108
119
} ) ;
109
120
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
+
113
150
exportButton . addEventListener ( "click" , ( ) => {
114
151
const objs = new Array ( ) ;
115
152
for ( const [ hash , ratingProperty ] of viewModel . model . ratingPropertiesByHash ) {
0 commit comments