1
- import { App , Plugin , PluginSettingTab , Setting } from "obsidian" ;
1
+ import {
2
+ App ,
3
+ Notice ,
4
+ Plugin ,
5
+ PluginSettingTab ,
6
+ Setting ,
7
+ requestUrl ,
8
+ } from "obsidian" ;
9
+ import { v4 as uuidv4 } from "uuid" ;
2
10
3
11
interface Divider {
4
12
id : string ;
@@ -30,6 +38,8 @@ export default class DividerPlugin extends Plugin {
30
38
async onload ( ) {
31
39
await this . loadSettings ( ) ;
32
40
41
+ this . versionCheck ( ) ;
42
+
33
43
// Render the dividers based on what is already in settings
34
44
Object . keys ( this . settings . dividers ) . forEach ( ( dividerId ) => {
35
45
const divider = this . settings . dividers [ dividerId ] ;
@@ -39,6 +49,20 @@ export default class DividerPlugin extends Plugin {
39
49
40
50
// This adds a settings tab so the user can configure various aspects of the plugin
41
51
this . addSettingTab ( new DividerSettingTab ( this . app , this ) ) ;
52
+
53
+ if ( process . env . NODE_ENV === "development" ) {
54
+ // @ts -ignore
55
+ if ( process . env . EMULATE_MOBILE && ! this . app . isMobile ) {
56
+ // @ts -ignore
57
+ this . app . emulateMobile ( true ) ;
58
+ }
59
+
60
+ // @ts -ignore
61
+ if ( ! process . env . EMULATE_MOBILE && this . app . isMobile ) {
62
+ // @ts -ignore
63
+ this . app . emulateMobile ( false ) ;
64
+ }
65
+ }
42
66
}
43
67
44
68
onunload ( ) { }
@@ -62,6 +86,43 @@ export default class DividerPlugin extends Plugin {
62
86
await this . saveData ( this . settings ) ;
63
87
}
64
88
89
+ /**
90
+ * Check the local plugin version against github. If there is a new version, notify the user.
91
+ */
92
+ async versionCheck ( ) {
93
+ const localVersion = process . env . PLUGIN_VERSION ;
94
+ const stableVersion = await requestUrl (
95
+ "https://raw.githubusercontent.com/andrewmcgivery/obsidian-ribbon-divider/main/package.json"
96
+ ) . then ( async ( res ) => {
97
+ if ( res . status === 200 ) {
98
+ const response = await res . json ;
99
+ return response . version ;
100
+ }
101
+ } ) ;
102
+ const betaVersion = await requestUrl (
103
+ "https://raw.githubusercontent.com/andrewmcgivery/obsidian-ribbon-divider/beta/package.json"
104
+ ) . then ( async ( res ) => {
105
+ if ( res . status === 200 ) {
106
+ const response = await res . json ;
107
+ return response . version ;
108
+ }
109
+ } ) ;
110
+
111
+ if ( localVersion ?. indexOf ( "beta" ) !== - 1 ) {
112
+ if ( localVersion !== betaVersion ) {
113
+ new Notice (
114
+ "There is a beta update available for the Ribbon Divider plugin. Please update to to the latest version to get the latest features!" ,
115
+ 0
116
+ ) ;
117
+ }
118
+ } else if ( localVersion !== stableVersion ) {
119
+ new Notice (
120
+ "There is an update available for the Ribbon Divider plugin. Please update to to the latest version to get the latest features!" ,
121
+ 0
122
+ ) ;
123
+ }
124
+ }
125
+
65
126
/**
66
127
* Renders a divider on the ribbon. The HTMLElement is saved to this.dividerElemenets so we can remove it if the
67
128
* user deletes it from the settings screen.
@@ -70,7 +131,7 @@ export default class DividerPlugin extends Plugin {
70
131
async renderDivider ( divider : Divider ) {
71
132
const dividerIconEl = this . addRibbonIcon (
72
133
"" ,
73
- `Divider: ${ divider . id } ` ,
134
+ `- ` ,
74
135
( evt : MouseEvent ) => { }
75
136
) ;
76
137
dividerIconEl . addClass ( "ribbon-divider" ) ;
@@ -97,8 +158,10 @@ export default class DividerPlugin extends Plugin {
97
158
async removeDivider ( dividerId : string ) {
98
159
delete this . settings . dividers [ dividerId ] ;
99
160
this . saveSettings ( ) ;
100
- this . dividerElements [ dividerId ] . remove ( ) ;
101
- delete this . dividerElements [ dividerId ] ;
161
+ if ( this . dividerElements [ dividerId ] ) {
162
+ this . dividerElements [ dividerId ] . remove ( ) ;
163
+ delete this . dividerElements [ dividerId ] ;
164
+ }
102
165
}
103
166
}
104
167
@@ -127,20 +190,14 @@ class DividerSettingTab extends PluginSettingTab {
127
190
128
191
Object . keys ( this . plugin . settings . dividers ) . forEach ( ( dividerId ) => {
129
192
const divider = this . plugin . settings . dividers [ dividerId ] ;
130
- const dividerEl = dividersContainerEl . createEl ( "div" , {
131
- attr : {
132
- "data-gate-id" : divider . id ,
133
- class : "ribbondividers-settings-divider" ,
134
- } ,
135
- } ) ;
136
193
137
- new Setting ( dividerEl )
194
+ new Setting ( dividersContainerEl )
138
195
. setName ( "Divider" )
139
196
. setDesc ( `Id: ${ divider . id } ` )
140
197
. addButton ( ( button ) => {
141
198
button . setButtonText ( "Delete" ) . onClick ( async ( ) => {
142
199
await this . plugin . removeDivider ( divider . id ) ;
143
- dividerEl . remove ( ) ;
200
+ this . display ( ) ;
144
201
} ) ;
145
202
} ) ;
146
203
} ) ;
@@ -149,7 +206,7 @@ class DividerSettingTab extends PluginSettingTab {
149
206
. createEl ( "button" , { text : "New divider" , cls : "mod-cta" } )
150
207
. addEventListener ( "click" , ( ) => {
151
208
this . plugin . addDivider ( {
152
- id : crypto . randomUUID ( ) ,
209
+ id : uuidv4 ( ) ,
153
210
} ) ;
154
211
this . display ( ) ;
155
212
} ) ;
0 commit comments