@@ -5,16 +5,19 @@ import { YouTubePlayer } from 'react-youtube';
55interface YoutubeTimestampPluginSettings {
66 mySetting : string ;
77 player : YouTubePlayer ;
8+ urlStartTimeMap : Map < string , number > ;
9+ currURL : string ;
810}
911
1012const DEFAULT_SETTINGS : YoutubeTimestampPluginSettings = {
1113 mySetting : "" ,
12- player : undefined
14+ player : undefined ,
15+ urlStartTimeMap : new Map < string , number > ( ) ,
16+ currURL : undefined ,
1317}
1418
1519export default class YoutubeTimestampPlugin extends Plugin {
1620 settings : YoutubeTimestampPluginSettings ;
17-
1821 // Helper function to validate url and activate view
1922 validateURL = ( url : string ) => {
2023 const regExp = / ^ .* ( ( y o u t u .b e \/ ) | ( v \/ ) | ( \/ u \/ \w \/ ) | ( e m b e d \/ ) | ( w a t c h \? ) ) \? ? v ? = ? ( [ ^ # & ? ] * ) .* / ;
@@ -75,7 +78,7 @@ export default class YoutubeTimestampPlugin extends Plugin {
7578 id : 'trigger-youtube-player' ,
7679 name : 'Open Youtube Player (copy youtube url and use hotkey)' ,
7780 editorCallback : ( editor : Editor , view : MarkdownView ) => {
78- // Get selected text and match against youtube url to convert link to youtube video id
81+ // Get selected text and match against youtube url to convert link to youtube video id => also triggers activateView in validateURL
7982 const url = editor . getSelection ( ) . trim ( ) ;
8083 editor . replaceSelection ( editor . getSelection ( ) + "\n" + this . validateURL ( url ) ) ;
8184 editor . setCursor ( editor . getCursor ( ) . line + 1 )
@@ -126,8 +129,15 @@ export default class YoutubeTimestampPlugin extends Plugin {
126129 } ) ;
127130 }
128131
129- onunload ( ) {
132+ async onunload ( ) {
133+ if ( this . settings . player ) {
134+ this . settings . player . destroy ( ) ;
135+ }
136+
137+ this . settings . player = null ;
138+ this . settings . currURL = null ;
130139 this . app . workspace . detachLeavesOfType ( YOUTUBE_VIEW ) ;
140+ await this . saveSettings ( ) ;
131141 }
132142
133143 // This is called when a valid url is found => it activates the View which loads the React view
@@ -144,21 +154,36 @@ export default class YoutubeTimestampPlugin extends Plugin {
144154 ) ;
145155
146156
157+ this . settings . currURL = url ;
147158 // This triggers the React component to be loaded
148159 this . app . workspace . getLeavesOfType ( YOUTUBE_VIEW ) . forEach ( async ( leaf ) => {
149160 if ( leaf . view instanceof YoutubeView ) {
150161
151162 const setupPlayer = ( yt : YouTubePlayer ) => {
152163 this . settings . player = yt ;
153164 }
154- leaf . setEphemeralState ( { url, setupPlayer } ) ;
165+
166+ const saveTimeOnUnload = async ( ) => {
167+ if ( this . settings . player ) {
168+ this . settings . urlStartTimeMap . set ( this . settings . currURL , this . settings . player . getCurrentTime ( ) . toFixed ( 0 ) ) ;
169+ }
170+ await this . saveSettings ( ) ;
171+ }
172+
173+ // create a new YoutubeView instance, sets up state/unload functionality, and passes in a start time if available else 0
174+ leaf . setEphemeralState ( { url, setupPlayer, saveTimeOnUnload, start : ~ ~ this . settings . urlStartTimeMap . get ( url ) } ) ;
175+
155176 await this . saveSettings ( ) ;
156177 }
157178 } ) ;
158179 }
159180
160181 async loadSettings ( ) {
161- this . settings = Object . assign ( { } , DEFAULT_SETTINGS , await this . loadData ( ) ) ;
182+ // Fix for a weird bug that turns default map into a normal object when loaded
183+ const data = await this . loadData ( )
184+ const map = new Map ( Object . keys ( data . urlStartTimeMap ) . map ( k => [ k , data . urlStartTimeMap [ k ] ] ) )
185+
186+ this . settings = { ...DEFAULT_SETTINGS , ...data , urlStartTimeMap : map } ;
162187 }
163188
164189
0 commit comments