1
1
import SwitchWidget from "./switch.js" ;
2
2
import attributeService from "../services/attributes.js" ;
3
3
import { t } from "../services/i18n.js" ;
4
+ import type FNote from "../entities/fnote.js" ;
5
+ import type { EventData } from "../components/app_context.js" ;
4
6
5
7
/**
6
8
* Switch for the basic properties widget which allows the user to select whether the note is a template or not, which toggles the `#template` attribute.
7
9
*/
8
10
export default class TemplateSwitchWidget extends SwitchWidget {
9
11
isEnabled ( ) {
10
- return super . isEnabled ( ) && ! this . noteId . startsWith ( "_options" ) ;
12
+ return super . isEnabled ( ) && ! this . noteId ? .startsWith ( "_options" ) ;
11
13
}
12
14
13
15
doRender ( ) {
@@ -23,22 +25,28 @@ export default class TemplateSwitchWidget extends SwitchWidget {
23
25
}
24
26
25
27
async switchOn ( ) {
26
- await attributeService . setLabel ( this . noteId , "template" ) ;
28
+ if ( this . noteId ) {
29
+ await attributeService . setLabel ( this . noteId , "template" ) ;
30
+ }
27
31
}
28
32
29
33
async switchOff ( ) {
34
+ if ( ! this . note || ! this . noteId ) {
35
+ return ;
36
+ }
37
+
30
38
for ( const templateAttr of this . note . getOwnedLabels ( "template" ) ) {
31
39
await attributeService . removeAttributeById ( this . noteId , templateAttr . attributeId ) ;
32
40
}
33
41
}
34
42
35
- async refreshWithNote ( note ) {
43
+ async refreshWithNote ( note : FNote ) {
36
44
const isTemplate = note . hasLabel ( "template" ) ;
37
45
this . $switchOn . toggle ( ! isTemplate ) ;
38
46
this . $switchOff . toggle ( ! ! isTemplate ) ;
39
47
}
40
48
41
- entitiesReloadedEvent ( { loadResults } ) {
49
+ entitiesReloadedEvent ( { loadResults } : EventData < "entitiesReloaded" > ) {
42
50
if ( loadResults . getAttributeRows ( ) . find ( ( attr ) => attr . type === "label" && attr . name === "template" && attr . noteId === this . noteId ) ) {
43
51
this . refresh ( ) ;
44
52
}
0 commit comments