1+ 'use strict' ;
2+
3+ var obsidian = require ( 'obsidian' ) ;
4+
5+ /*! *****************************************************************************
6+ Copyright (c) Microsoft Corporation.
7+
8+ Permission to use, copy, modify, and/or distribute this software for any
9+ purpose with or without fee is hereby granted.
10+
11+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17+ PERFORMANCE OF THIS SOFTWARE.
18+ ***************************************************************************** */
19+ /* global Reflect, Promise */
20+
21+ var extendStatics = function ( d , b ) {
22+ extendStatics = Object . setPrototypeOf ||
23+ ( { __proto__ : [ ] } instanceof Array && function ( d , b ) { d . __proto__ = b ; } ) ||
24+ function ( d , b ) { for ( var p in b ) if ( Object . prototype . hasOwnProperty . call ( b , p ) ) d [ p ] = b [ p ] ; } ;
25+ return extendStatics ( d , b ) ;
26+ } ;
27+
28+ function __extends ( d , b ) {
29+ extendStatics ( d , b ) ;
30+ function __ ( ) { this . constructor = d ; }
31+ d . prototype = b === null ? Object . create ( b ) : ( __ . prototype = b . prototype , new __ ( ) ) ;
32+ }
33+
34+ function __awaiter ( thisArg , _arguments , P , generator ) {
35+ function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
36+ return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
37+ function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
38+ function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
39+ function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
40+ step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
41+ } ) ;
42+ }
43+
44+ function __generator ( thisArg , body ) {
45+ var _ = { label : 0 , sent : function ( ) { if ( t [ 0 ] & 1 ) throw t [ 1 ] ; return t [ 1 ] ; } , trys : [ ] , ops : [ ] } , f , y , t , g ;
46+ return g = { next : verb ( 0 ) , "throw" : verb ( 1 ) , "return" : verb ( 2 ) } , typeof Symbol === "function" && ( g [ Symbol . iterator ] = function ( ) { return this ; } ) , g ;
47+ function verb ( n ) { return function ( v ) { return step ( [ n , v ] ) ; } ; }
48+ function step ( op ) {
49+ if ( f ) throw new TypeError ( "Generator is already executing." ) ;
50+ while ( _ ) try {
51+ if ( f = 1 , y && ( t = op [ 0 ] & 2 ? y [ "return" ] : op [ 0 ] ? y [ "throw" ] || ( ( t = y [ "return" ] ) && t . call ( y ) , 0 ) : y . next ) && ! ( t = t . call ( y , op [ 1 ] ) ) . done ) return t ;
52+ if ( y = 0 , t ) op = [ op [ 0 ] & 2 , t . value ] ;
53+ switch ( op [ 0 ] ) {
54+ case 0 : case 1 : t = op ; break ;
55+ case 4 : _ . label ++ ; return { value : op [ 1 ] , done : false } ;
56+ case 5 : _ . label ++ ; y = op [ 1 ] ; op = [ 0 ] ; continue ;
57+ case 7 : op = _ . ops . pop ( ) ; _ . trys . pop ( ) ; continue ;
58+ default :
59+ if ( ! ( t = _ . trys , t = t . length > 0 && t [ t . length - 1 ] ) && ( op [ 0 ] === 6 || op [ 0 ] === 2 ) ) { _ = 0 ; continue ; }
60+ if ( op [ 0 ] === 3 && ( ! t || ( op [ 1 ] > t [ 0 ] && op [ 1 ] < t [ 3 ] ) ) ) { _ . label = op [ 1 ] ; break ; }
61+ if ( op [ 0 ] === 6 && _ . label < t [ 1 ] ) { _ . label = t [ 1 ] ; t = op ; break ; }
62+ if ( t && _ . label < t [ 2 ] ) { _ . label = t [ 2 ] ; _ . ops . push ( op ) ; break ; }
63+ if ( t [ 2 ] ) _ . ops . pop ( ) ;
64+ _ . trys . pop ( ) ; continue ;
65+ }
66+ op = body . call ( thisArg , _ ) ;
67+ } catch ( e ) { op = [ 6 , e ] ; y = 0 ; } finally { f = t = 0 ; }
68+ if ( op [ 0 ] & 5 ) throw op [ 1 ] ; return { value : op [ 0 ] ? op [ 1 ] : void 0 , done : true } ;
69+ }
70+ }
71+
72+ var SETTINGS = {
73+ totalUptime : 0 ,
74+ lastLoad : Date . now ( ) ,
75+ currentSessionDuration : 0 ,
76+ showTotalUptimeInStatusBar : false ,
77+ displayStaringTimeAsHumanString : true ,
78+ staringText : 'You have been staring at your vault for ' ,
79+ totalStaringText : 'Your total staring time in this vault is ' ,
80+ pausedText : 'Your staring counter is paused'
81+ } ;
82+ var YouHaveBeenStaring = /** @class */ ( function ( _super ) {
83+ __extends ( YouHaveBeenStaring , _super ) ;
84+ function YouHaveBeenStaring ( ) {
85+ return _super !== null && _super . apply ( this , arguments ) || this ;
86+ }
87+ YouHaveBeenStaring . prototype . onload = function ( ) {
88+ return __awaiter ( this , void 0 , void 0 , function ( ) {
89+ var _this = this ;
90+ return __generator ( this , function ( _a ) {
91+ switch ( _a . label ) {
92+ case 0 : return [ 4 /*yield*/ , this . loadSettings ( ) ] ;
93+ case 1 :
94+ _a . sent ( ) ;
95+ this . settings . lastLoad = Date . now ( ) ;
96+ this . settings . currentSessionDuration = 0 ;
97+ this . counterActive = true ;
98+ this . staringTimeStatusBar = this . addStatusBarItem ( ) ;
99+ this . totalStaringTimeStatusBar = this . addStatusBarItem ( ) ;
100+ this . registerInterval ( window . setInterval ( function ( ) {
101+ _this . showTimeSinceLoad ( ) ,
102+ _this . showTotalStaringTime ( ) ,
103+ _this . settings . totalUptime += _this . counterActive ? 1000 : 0 ,
104+ _this . settings . currentSessionDuration += _this . counterActive ? 1000 : 0 ;
105+ } , 1000 ) ) ;
106+ // Save every 10 minutes
107+ this . registerInterval ( window . setInterval ( function ( ) {
108+ _this . saveSettings ( ) ;
109+ } , 600000 ) ) ;
110+ this . addSettingTab ( new YouHaveBeenStaringSettingsTab ( this . app , this ) ) ;
111+ this . addRibbonIcon ( 'any-key' , 'Start/stop staring counter' , function ( ) {
112+ _this . counterActive = ! _this . counterActive ;
113+ new obsidian . Notice ( 'Turning staring counter ' + ( _this . counterActive ? 'on' : 'off' ) ) ;
114+ _this . saveSettings ( ) ;
115+ } ) ;
116+ return [ 2 /*return*/ ] ;
117+ }
118+ } ) ;
119+ } ) ;
120+ } ;
121+ YouHaveBeenStaring . prototype . onunload = function ( ) {
122+ this . saveSettings ( ) ;
123+ } ;
124+ YouHaveBeenStaring . prototype . showTimeSinceLoad = function ( ) {
125+ if ( this . settings . lastLoad && this . counterActive ) {
126+ var moment = window . moment ;
127+ var staringTime = moment . duration ( this . settings . currentSessionDuration , 'milliseconds' ) . humanize ( ) ;
128+ this . staringTimeStatusBar . setText ( this . settings . staringText + ( "" + staringTime ) ) ;
129+ }
130+ if ( ! this . counterActive ) {
131+ this . staringTimeStatusBar . setText ( this . settings . pausedText ) ;
132+ }
133+ } ;
134+ YouHaveBeenStaring . prototype . showTotalStaringTime = function ( ) {
135+ if ( this . settings . showTotalUptimeInStatusBar && this . settings . totalUptime > 0 ) {
136+ var moment = window . moment ;
137+ var totalStaringTime = this . settings . displayStaringTimeAsHumanString
138+ ? moment . duration ( this . settings . totalUptime , 'milliseconds' ) . humanize ( )
139+ : moment . duration ( this . settings . totalUptime , 'milliseconds' ) . hours ( ) + ':' + moment . duration ( this . settings . totalUptime , 'milliseconds' ) . minutes ( ) ;
140+ this . totalStaringTimeStatusBar . setText ( this . settings . totalStaringText + ( "" + totalStaringTime ) ) ;
141+ }
142+ } ;
143+ YouHaveBeenStaring . prototype . loadSettings = function ( ) {
144+ return __awaiter ( this , void 0 , void 0 , function ( ) {
145+ var _a , _b , _c , _d ;
146+ return __generator ( this , function ( _e ) {
147+ switch ( _e . label ) {
148+ case 0 :
149+ _a = this ;
150+ _c = ( _b = Object ) . assign ;
151+ _d = [ SETTINGS ] ;
152+ return [ 4 /*yield*/ , this . loadData ( ) ] ;
153+ case 1 :
154+ _a . settings = _c . apply ( _b , _d . concat ( [ _e . sent ( ) ] ) ) ;
155+ return [ 2 /*return*/ ] ;
156+ }
157+ } ) ;
158+ } ) ;
159+ } ;
160+ YouHaveBeenStaring . prototype . saveSettings = function ( ) {
161+ return __awaiter ( this , void 0 , void 0 , function ( ) {
162+ return __generator ( this , function ( _a ) {
163+ switch ( _a . label ) {
164+ case 0 : return [ 4 /*yield*/ , this . saveData ( this . settings ) ] ;
165+ case 1 :
166+ _a . sent ( ) ;
167+ return [ 2 /*return*/ ] ;
168+ }
169+ } ) ;
170+ } ) ;
171+ } ;
172+ return YouHaveBeenStaring ;
173+ } ( obsidian . Plugin ) ) ;
174+ var YouHaveBeenStaringSettingsTab = /** @class */ ( function ( _super ) {
175+ __extends ( YouHaveBeenStaringSettingsTab , _super ) ;
176+ function YouHaveBeenStaringSettingsTab ( app , plugin ) {
177+ var _this = _super . call ( this , app , plugin ) || this ;
178+ _this . plugin = plugin ;
179+ return _this ;
180+ }
181+ YouHaveBeenStaringSettingsTab . prototype . display = function ( ) {
182+ var _this = this ;
183+ var containerEl = this . containerEl ;
184+ containerEl . empty ( ) ;
185+ containerEl . createEl ( 'h3' , { text : 'YouHaveBeenStaring settings' } ) ;
186+ new obsidian . Setting ( containerEl )
187+ . setName ( 'Show total staring duration' )
188+ . setDesc ( 'Displays the total duration you have been staring on your vault (well at least when this plugin was able to measure it).' )
189+ . addToggle ( function ( toggle ) {
190+ return toggle
191+ . setValue ( _this . plugin . settings . showTotalUptimeInStatusBar )
192+ . onChange ( function ( value ) {
193+ _this . plugin . settings . showTotalUptimeInStatusBar = value ;
194+ if ( ! value ) {
195+ _this . plugin . totalStaringTimeStatusBar . empty ( ) ;
196+ }
197+ _this . plugin . saveSettings ( ) ;
198+ } ) ;
199+ } ) ;
200+ new obsidian . Setting ( containerEl )
201+ . setName ( 'Display total recorded staring time as human readable' )
202+ . setDesc ( 'The recorded staring time is shown as text, such as \'10 hours\'. Turning this off will show the total recorded time in HH:mm fashion.' )
203+ . addToggle ( function ( toggle ) {
204+ return toggle
205+ . setValue ( _this . plugin . settings . displayStaringTimeAsHumanString )
206+ . onChange ( function ( value ) {
207+ _this . plugin . settings . displayStaringTimeAsHumanString = value ;
208+ _this . plugin . showTotalStaringTime ( ) ;
209+ _this . plugin . saveSettings ( ) ;
210+ } ) ;
211+ } ) ;
212+ new obsidian . Setting ( containerEl )
213+ . setName ( 'Status bar text of staring time' )
214+ . setDesc ( 'Overrides the status bar text displaying your staring time.' )
215+ . addTextArea ( function ( text ) {
216+ return text
217+ . setValue ( _this . plugin . settings . staringText )
218+ . setPlaceholder ( 'You have been staring at your vault for ' )
219+ . onChange ( function ( value ) {
220+ _this . plugin . settings . staringText = value ;
221+ _this . plugin . saveSettings ( ) ;
222+ } ) ;
223+ } ) ;
224+ new obsidian . Setting ( containerEl )
225+ . setName ( 'Status bar text of total staring time' )
226+ . setDesc ( 'Overrides the status bar text displaying your total staring time for this vault.' )
227+ . addTextArea ( function ( text ) {
228+ return text
229+ . setValue ( _this . plugin . settings . totalStaringText )
230+ . setPlaceholder ( 'Your total staring time in this vault is ' )
231+ . onChange ( function ( value ) {
232+ _this . plugin . settings . totalStaringText = value ;
233+ _this . plugin . saveSettings ( ) ;
234+ } ) ;
235+ } ) ;
236+ new obsidian . Setting ( containerEl )
237+ . setName ( 'Status bar text when staring counter is paused' )
238+ . setDesc ( 'Overrides the status bar text shown when you disabled the staring time counting.' )
239+ . addTextArea ( function ( text ) {
240+ return text
241+ . setValue ( _this . plugin . settings . pausedText )
242+ . setPlaceholder ( 'Your staring counter is paused' )
243+ . onChange ( function ( value ) {
244+ _this . plugin . settings . pausedText = value ;
245+ _this . plugin . saveSettings ( ) ;
246+ } ) ;
247+ } ) ;
248+ } ;
249+ return YouHaveBeenStaringSettingsTab ;
250+ } ( obsidian . PluginSettingTab ) ) ;
251+
252+ module . exports = YouHaveBeenStaring ;
253+
254+
255+ /* nosourcemap */
0 commit comments