11import {
2- App ,
3- Plugin ,
4- PluginSettingTab ,
5- Setting ,
6- TFile ,
7- WorkspaceLeaf ,
8- FileView ,
9- Notice ,
2+ App ,
3+ Plugin ,
4+ PluginSettingTab ,
5+ Setting ,
6+ TFile ,
7+ WorkspaceLeaf ,
8+ FileView ,
9+ Notice ,
1010} from "obsidian" ;
1111// Remember to rename these classes and interfaces!
1212
1313interface AnotherNameSettings {
14- propertyName : string ;
14+ propertyName : string ;
1515}
1616
1717// type Lang = "zh-CN" | "en";
@@ -21,163 +21,170 @@ interface AnotherNameSettings {
2121// }
2222
2323const DEFAULT_SETTINGS : AnotherNameSettings = {
24- propertyName : "another-name" ,
24+ propertyName : "another-name" ,
2525} ;
2626
2727export default class AnotherNamePlugin extends Plugin {
28- settings : AnotherNameSettings ;
29- noticeTime : number ;
30- // i18n: AnotherNameI18N;
31- reloadLeaf : ( leaf : WorkspaceLeaf ) => void ;
32- reloadAllLeaves : ( ) => void ;
33- // translate: (key: string, lang: Lang, params?: object) => string;
34-
35- onload ( ) {
36- this . loadSettings ( ) ;
37- this . noticeTime = 5000 ;
38-
39- // This adds a settings tab so the user can configure various aspects of the plugin
40- this . addSettingTab ( new AnotherNameSettingTab ( this . app , this ) ) ;
41-
42- // this.translate = (key: string, lang: string, params: object) => {
43- // let translation = this.i18n[lang][key];
44-
45- // for (const placeholder in params) {
46- // translation = translation.replace(`{{${placeholder}}}`, params[placeholder]);
47- // }
48-
49- // return translation;
50- // };
51-
52- this . reloadLeaf = ( leaf : WorkspaceLeaf ) => {
53- const viewState = leaf . getViewState ( ) ;
54- if ( viewState . type === "markdown" ) {
55- const view = leaf . view as FileView ;
56- const containerEl = view . containerEl ;
57-
58- const oldNames = containerEl . querySelectorAll ( ".another-name" ) ;
59-
60- oldNames . forEach ( ( v ) => {
61- v . remove ( ) ;
28+ settings : AnotherNameSettings ;
29+ noticeTime : number ;
30+ // i18n: AnotherNameI18N;
31+ reloadLeaf : ( leaf : WorkspaceLeaf ) => void ;
32+ reloadAllLeaves : ( ) => void ;
33+ // translate: (key: string, lang: Lang, params?: object) => string;
34+
35+ onload ( ) {
36+ this . loadSettings ( ) ;
37+ this . noticeTime = 5000 ;
38+
39+ // This adds a settings tab so the user can configure various aspects of the plugin
40+ this . addSettingTab ( new AnotherNameSettingTab ( this . app , this ) ) ;
41+
42+ // this.translate = (key: string, lang: string, params: object) => {
43+ // let translation = this.i18n[lang][key];
44+
45+ // for (const placeholder in params) {
46+ // translation = translation.replace(`{{${placeholder}}}`, params[placeholder]);
47+ // }
48+
49+ // return translation;
50+ // };
51+
52+ this . reloadLeaf = ( leaf : WorkspaceLeaf ) => {
53+ const viewState = leaf . getViewState ( ) ;
54+ if ( viewState . type === "markdown" ) {
55+ const view = leaf . view as FileView ;
56+ const containerEl = view . containerEl ;
57+
58+ const oldNames = containerEl . querySelectorAll ( ".another-name" ) ;
59+
60+ oldNames . forEach ( ( v ) => {
61+ v . remove ( ) ;
62+ } ) ;
63+
64+ const inlineTitle = containerEl . querySelector ( ".inline-title" ) ;
65+ if ( ! inlineTitle ) {
66+ return ;
67+ }
68+
69+ const file = view . file ;
70+ if ( ! file ) {
71+ return ;
72+ }
73+ const cache = this . app . metadataCache . getFileCache ( file ) ;
74+ if ( ! cache || ! cache . frontmatter ) {
75+ return ;
76+ }
77+
78+ const anotherNameProperty = this . settings . propertyName ;
79+
80+ const anotherName = cache . frontmatter [ anotherNameProperty ] ;
81+
82+ if ( ! anotherName ) {
83+ return ;
84+ } else if ( typeof anotherName !== "string" ) {
85+ new Notice (
86+ `Another-name: File ${ file . basename } have invalid property type.` ,
87+ this . noticeTime
88+ ) ;
89+ // new Notice(this.translate("notice_1", "en"), this.noticeTime);
90+ return ;
91+ }
92+ const anotherNameEl = document . createElement ( "div" ) ;
93+ anotherNameEl . classList . add ( "another-name" ) ;
94+ anotherNameEl . innerText = anotherName ;
95+
96+ inlineTitle . insertAdjacentElement ( "afterend" , anotherNameEl ) ;
97+ }
98+ } ;
99+
100+ this . reloadAllLeaves = ( ) => {
101+ this . app . workspace . iterateAllLeaves ( this . reloadLeaf ) ;
102+ } ;
103+
104+ this . registerEvent (
105+ this . app . metadataCache . on ( "changed" , ( file : TFile ) => {
106+ if ( file ) {
107+ this . app . workspace . iterateAllLeaves (
108+ ( leaf : WorkspaceLeaf ) => {
109+ const view = leaf . view as FileView ;
110+ if ( view . file === file ) {
111+ this . reloadLeaf ( leaf ) ;
112+ }
113+ }
114+ ) ;
115+ }
116+ } )
117+ ) ;
118+
119+ this . registerEvent (
120+ this . app . workspace . on ( "layout-change" , ( ) => {
121+ this . app . workspace . iterateAllLeaves ( this . reloadLeaf ) ;
122+ } )
123+ ) ;
124+
125+ // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
126+ // Using this function will automatically remove the event listener when this plugin is disabled.
127+ // this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
128+ // // console.log('click', evt);
129+ // });
130+
131+ // When registering intervals, this function will automatically clear the interval when the plugin is disabled.
132+ // this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
133+ }
134+
135+ onunload ( ) { }
136+
137+ async loadSettings ( ) {
138+ this . settings = Object . assign (
139+ { } ,
140+ DEFAULT_SETTINGS ,
141+ await this . loadData ( )
142+ ) ;
143+ }
144+
145+ async saveSettings ( ) {
146+ await this . saveData ( this . settings ) . catch ( ( err ) => {
147+ new Notice (
148+ `Another-Name: Error: Something goes wrong when saving settings. \n ${ err } ` ,
149+ this . noticeTime
150+ ) ;
151+ // new Notice(this.translate("error_1", "en"), this.noticeTime);
152+ console . error ( err ) ;
62153 } ) ;
154+ }
63155
64- const inlineTitle = containerEl . querySelector ( ".inline-title" ) ;
65- if ( ! inlineTitle ) {
66- return ;
67- }
68-
69- const file = view . file ;
70- if ( ! file ) {
71- return ;
72- }
73- const cache = this . app . metadataCache . getFileCache ( file ) ;
74- if ( ! cache || ! cache . frontmatter ) {
75- return ;
76- }
77-
78- const anotherNameProperty = this . settings . propertyName ;
79-
80- const anotherName = cache . frontmatter [ anotherNameProperty ] ;
81-
82- if ( ! anotherName ) {
83- return ;
84- } else if ( typeof anotherName !== "string" ) {
85- new Notice ( `Another-name: File ${ file . basename } have invalid property type.` , this . noticeTime ) ;
86- // new Notice(this.translate("notice_1", "en"), this.noticeTime);
87- return ;
88- }
89- const anotherNameEl = document . createElement ( "div" ) ;
90- anotherNameEl . classList . add ( "another-name" ) ;
91- anotherNameEl . innerText = anotherName ;
92-
93- inlineTitle . insertAdjacentElement ( "afterend" , anotherNameEl ) ;
94- }
95- } ;
96-
97- this . reloadAllLeaves = ( ) => {
98- this . app . workspace . iterateRootLeaves ( this . reloadLeaf ) ;
99- } ;
100-
101- this . registerEvent (
102- this . app . metadataCache . on ( "changed" , ( file : TFile ) => {
103- if ( file ) {
104- this . app . workspace . iterateRootLeaves ( ( leaf : WorkspaceLeaf ) => {
105- const view = leaf . view as FileView ;
106- if ( view . file === file ) {
107- this . reloadLeaf ( leaf ) ;
108- }
109- } ) ;
110- }
111- } )
112- ) ;
113-
114- this . registerEvent (
115- this . app . workspace . on ( "layout-change" , ( ) => {
116- this . app . workspace . iterateRootLeaves ( this . reloadLeaf ) ;
117- } )
118- ) ;
119-
120- this . registerEvent (
121- this . app . workspace . on ( "codemirror" , ( ) => {
122- console . log ( "triggered" ) ;
123- } )
124- ) ;
125- // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
126- // Using this function will automatically remove the event listener when this plugin is disabled.
127- // this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
128- // // console.log('click', evt);
129- // });
130-
131- // When registering intervals, this function will automatically clear the interval when the plugin is disabled.
132- // this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
133- }
134-
135- onunload ( ) { }
136-
137- async loadSettings ( ) {
138- this . settings = Object . assign ( { } , DEFAULT_SETTINGS , await this . loadData ( ) ) ;
139- }
140-
141- async saveSettings ( ) {
142- await this . saveData ( this . settings ) . catch ( ( err ) => {
143- new Notice ( `Another-Name: Error: Something goes wrong when saving settings. \n ${ err } ` , this . noticeTime ) ;
144- // new Notice(this.translate("error_1", "en"), this.noticeTime);
145- console . error ( err ) ;
146- } ) ;
147- }
148-
149- updateSettings ( ) {
150- this . loadSettings ( ) ;
151- }
156+ updateSettings ( ) {
157+ this . loadSettings ( ) ;
158+ }
152159}
153160
154161// Settings tab
155162class AnotherNameSettingTab extends PluginSettingTab {
156- plugin : AnotherNamePlugin ;
157-
158- constructor ( app : App , plugin : AnotherNamePlugin ) {
159- super ( app , plugin ) ;
160- this . plugin = plugin ;
161- }
162-
163- display ( ) : void {
164- const { containerEl } = this ;
165-
166- containerEl . empty ( ) ;
167-
168- new Setting ( containerEl )
169- . setName ( "Property name" )
170- . setDesc ( "Change the property name in metadata." )
171- . addText ( ( text ) =>
172- text
173- . setPlaceholder ( "another name" )
174- . setValue ( this . plugin . settings . propertyName )
175- . onChange ( async ( value ) => {
176- this . plugin . settings . propertyName = value ;
177- await this . plugin . saveSettings ( ) ;
178- await this . plugin . loadSettings ( ) ;
179- this . plugin . reloadAllLeaves ( ) ;
180- } )
181- ) ;
182- }
163+ plugin : AnotherNamePlugin ;
164+
165+ constructor ( app : App , plugin : AnotherNamePlugin ) {
166+ super ( app , plugin ) ;
167+ this . plugin = plugin ;
168+ }
169+
170+ display ( ) : void {
171+ const { containerEl } = this ;
172+
173+ containerEl . empty ( ) ;
174+
175+ new Setting ( containerEl )
176+ . setName ( "Property name" )
177+ . setDesc ( "Change the property name in metadata." )
178+ . addText ( ( text ) =>
179+ text
180+ . setPlaceholder ( "another name" )
181+ . setValue ( this . plugin . settings . propertyName )
182+ . onChange ( async ( value ) => {
183+ this . plugin . settings . propertyName = value ;
184+ await this . plugin . saveSettings ( ) ;
185+ await this . plugin . loadSettings ( ) ;
186+ this . plugin . reloadAllLeaves ( ) ;
187+ } )
188+ ) ;
189+ }
183190}
0 commit comments