@@ -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