@@ -31,8 +31,9 @@ export class EncryptedFileContentView extends TextFileView {
31
31
//console.debug('EncryptedFileContentView.constructor', {leaf});
32
32
33
33
this . elActionIconLockNote = this . addAction ( 'lock' , 'Lock' , ( ) => this . actionLockFile ( ) ) ;
34
-
35
34
this . elActionChangePassword = this . addAction ( 'key' , 'Change Password' , ( ) => this . actionChangePassword ( ) ) ;
35
+ this . elActionIconLockNote . hide ( ) ;
36
+ this . elActionChangePassword . hide ( ) ;
36
37
37
38
this . contentEl . style . display = 'flex' ;
38
39
this . contentEl . style . flexDirection = 'column' ;
@@ -42,6 +43,7 @@ export class EncryptedFileContentView extends TextFileView {
42
43
43
44
private actionLockFile ( ) {
44
45
this . encryptionPassword = '' ;
46
+ SessionPasswordService . clearForFile ( this . file ) ;
45
47
this . refreshView ( EncryptedFileContentViewStateEnum . decryptNote ) ;
46
48
}
47
49
@@ -72,7 +74,7 @@ export class EncryptedFileContentView extends TextFileView {
72
74
super . onPaneMenu ( menu , source ) ;
73
75
}
74
76
75
- private createTitle ( title :string ) : HTMLElement {
77
+ private addTitle ( title :string ) : HTMLElement {
76
78
return this . contentEl . createDiv ( {
77
79
text : `🔐 ${ title } 🔐` ,
78
80
attr : {
@@ -93,9 +95,12 @@ export class EncryptedFileContentView extends TextFileView {
93
95
return passwordMatch ? '' :'Password doesn\'t match' ;
94
96
}
95
97
96
- private createNewNoteView ( ) : HTMLElement {
98
+ private addNewNoteView ( ) {
99
+
100
+ this . addTitle ( 'This note will be encrypted' ) ;
101
+
97
102
//console.debug('createDecryptNoteView', { "hint": this.hint} );
98
- const container = this . createInputContainer ( ) ;
103
+ const container = this . addInputContainer ( ) ;
99
104
100
105
new Setting ( container )
101
106
. setDesc ( 'Please provide a password and hint to start editing this note.' )
@@ -123,7 +128,7 @@ export class EncryptedFileContentView extends TextFileView {
123
128
}
124
129
}
125
130
126
- const bestGuessPassAndHint = SessionPasswordService . getBestGuess ( this . file ) ;
131
+ const bestGuessPassAndHint = SessionPasswordService . get ( this . file ) ;
127
132
let password = bestGuessPassAndHint . password ;
128
133
let confirm = '' ;
129
134
let hint = bestGuessPassAndHint . hint ;
@@ -191,24 +196,21 @@ export class EncryptedFileContentView extends TextFileView {
191
196
} )
192
197
;
193
198
194
- return container ;
195
199
}
196
200
201
+ private addDecryptNoteView ( ) {
202
+
203
+ this . addTitle ( 'This note is encrypted' ) ;
197
204
198
- private createDecryptNoteView ( ) : HTMLElement {
199
- const container = this . createInputContainer ( ) ;
200
-
205
+ const container = this . addInputContainer ( ) ;
206
+
201
207
new Setting ( container )
202
208
. setDesc ( 'Please provide a password to unlock this note.' )
203
209
;
204
210
205
- const bestGuessPassAndHint = SessionPasswordService . getBestGuess ( this . file ) ;
206
- this . encryptionPassword = bestGuessPassAndHint . password ;
207
-
208
211
UiHelper . buildPasswordSetting ( {
209
212
container,
210
213
name :'Password:' ,
211
- initialValue : this . encryptionPassword ,
212
214
autoFocus : true ,
213
215
placeholder : this . formatHint ( this . hint ) ,
214
216
onChangeCallback : ( value ) => {
@@ -228,7 +230,21 @@ export class EncryptedFileContentView extends TextFileView {
228
230
} )
229
231
;
230
232
231
- return container ;
233
+ // try to decode and go to edit mode if password is known
234
+ const bestGuessPassAndHint = SessionPasswordService . get ( this . file ) ;
235
+ this . encryptionPassword = bestGuessPassAndHint . password ;
236
+
237
+ this . decryptWithPassword ( bestGuessPassAndHint . password )
238
+ . then ( decryptedText => {
239
+ if ( decryptedText != null ) {
240
+ this . currentEditorText = decryptedText ;
241
+ this . refreshView ( EncryptedFileContentViewStateEnum . editNote ) ;
242
+ new Notice ( 'Decrypted using remembered password' , 2000 ) ;
243
+ }
244
+ } )
245
+ ;
246
+
247
+
232
248
}
233
249
234
250
private async encodeAndSave ( ) {
@@ -251,36 +267,40 @@ export class EncryptedFileContentView extends TextFileView {
251
267
}
252
268
}
253
269
254
- private createEditorView ( ) : HTMLElement {
255
- //const container = this.contentEl.createEl('textarea');
270
+ private addEditorView ( ) {
271
+
272
+ this . elActionIconLockNote . show ( ) ;
273
+ this . elActionChangePassword . show ( ) ;
274
+
275
+ this . addTitle ( 'This note is encrypted' ) ;
276
+
256
277
const container = this . contentEl . createDiv ( ) ;
257
278
container . contentEditable = 'true' ;
258
279
container . style . flexGrow = '1' ;
259
280
container . style . alignSelf = 'stretch' ;
260
281
261
- //container.value = this.currentEditorText
262
282
container . innerText = this . currentEditorText ;
263
283
container . focus ( ) ;
264
284
265
285
container . on ( 'input' , '*' , async ( ev , target ) => {
266
- //console.debug('editor input',{ev, target});
267
- //this.currentEditorText = container.value;
268
286
this . currentEditorText = container . innerText ;
269
287
await this . encodeAndSave ( ) ;
270
288
} ) ;
271
- return container ;
272
289
}
273
290
274
- private createInputContainer ( ) : HTMLElement {
291
+ private addInputContainer ( ) : HTMLElement {
275
292
return this . contentEl . createDiv ( {
276
293
'attr' : {
277
294
'style' : 'width:100%; max-width:400px;'
278
295
}
279
296
} ) ;
280
297
}
281
298
282
- private createChangePasswordView ( ) : HTMLElement {
283
- const container = this . createInputContainer ( ) ;
299
+ private addChangePasswordView ( ) {
300
+
301
+ this . addTitle ( 'Change encrypted note password' ) ;
302
+
303
+ const container = this . addInputContainer ( ) ;
284
304
285
305
let newPassword = '' ;
286
306
let confirm = '' ;
@@ -300,6 +320,9 @@ export class EncryptedFileContentView extends TextFileView {
300
320
301
321
this . encodeAndSave ( ) ;
302
322
this . refreshView ( EncryptedFileContentViewStateEnum . editNote ) ;
323
+
324
+ SessionPasswordService . put ( { password : newPassword , hint : newHint } , this . file ) ;
325
+
303
326
new Notice ( 'Password and Hint were changed' ) ;
304
327
}
305
328
}
@@ -378,8 +401,6 @@ export class EncryptedFileContentView extends TextFileView {
378
401
;
379
402
} )
380
403
;
381
-
382
- return container ;
383
404
}
384
405
385
406
private formatHint ( hint :string ) : string {
@@ -406,39 +427,35 @@ export class EncryptedFileContentView extends TextFileView {
406
427
407
428
switch ( this . currentView ) {
408
429
case EncryptedFileContentViewStateEnum . newNote :
409
- this . createTitle ( 'This note will be encrypted' ) ;
410
- this . createNewNoteView ( ) ;
430
+ this . addNewNoteView ( ) ;
411
431
break ;
412
432
413
433
case EncryptedFileContentViewStateEnum . decryptNote :
414
- this . createTitle ( 'This note is encrypted' ) ;
415
- this . createDecryptNoteView ( ) ;
434
+ this . addDecryptNoteView ( ) ;
416
435
break ;
417
436
418
437
case EncryptedFileContentViewStateEnum . editNote :
419
- this . elActionIconLockNote . show ( ) ;
420
- this . elActionChangePassword . show ( ) ;
421
- this . createTitle ( 'This note is encrypted' ) ;
422
- this . createEditorView ( ) ;
438
+ this . addEditorView ( ) ;
423
439
break ;
424
440
425
441
case EncryptedFileContentViewStateEnum . changePassword :
426
- this . createTitle ( 'Change encrypted note password' ) ;
427
- this . createChangePasswordView ( ) ;
442
+ this . addChangePasswordView ( ) ;
428
443
break ;
429
444
}
430
445
431
446
}
432
447
448
+ async decryptWithPassword ( pw : string ) : Promise < string | null > {
449
+ if ( pw . length == 0 ) {
450
+ return null ;
451
+ }
452
+ const fileData = JsonFileEncoding . decode ( this . data ) ;
453
+ const decryptedText = await FileDataHelper . decrypt ( fileData , pw ) ;
454
+ return decryptedText ;
455
+ }
456
+
433
457
async handleDecryptButtonClick ( ) {
434
- const fileData = JsonFileEncoding . decode ( this . data )
435
-
436
- //console.debug('Decrypt button', fileData);
437
-
438
- const decryptedText = await FileDataHelper . decrypt (
439
- fileData ,
440
- this . encryptionPassword
441
- ) ;
458
+ const decryptedText = await this . decryptWithPassword ( this . encryptionPassword ) ;
442
459
443
460
if ( decryptedText === null ) {
444
461
new Notice ( 'Decryption failed' ) ;
0 commit comments