Added focus and interaction to refresh notes that have been changed while user was looking away

This commit is contained in:
Max G 2022-02-25 04:26:12 +00:00
parent bc44b3db9a
commit 0c4f6e94c1

View File

@ -399,6 +399,7 @@
lastNoteHash: null, lastNoteHash: null,
saveDebounce: null, //Prevent save from being called numerous times quickly saveDebounce: null, //Prevent save from being called numerous times quickly
updated: 'Never', updated: 'Never',
lastInteractionTimestamp:null, //Tracks when note was loaded and last saved/refreshed
editDebounce: null, editDebounce: null,
textChangedDebounce: null, textChangedDebounce: null,
keyPressesCounter: 0, //Determine keys pressed between saves keyPressesCounter: 0, //Determine keys pressed between saves
@ -747,6 +748,27 @@
return return
} }
//Setup all responsive vue data
this.setupLoadedNoteData(response)
this.loading = false
this.$nextTick(() => {
//Adjust note title size after load
this.titleResize()
this.initSquire()
})
})
.catch(error => { this.$bus.$emit('notification', 'Failed to Open Note') })
} else {
console.log('Could not fetch note')
}
},
setupLoadedNoteData(response){
//All the data returned by the server, setup locally in vue component
//Set up local data //Set up local data
this.currentNoteId = this.noteid this.currentNoteId = this.noteid
this.rawTextId = response.data.rawTextId this.rawTextId = response.data.rawTextId
@ -754,6 +776,7 @@
this.created = response.data.created this.created = response.data.created
this.updated = response.data.updated this.updated = response.data.updated
this.lastInteractionTimestamp = +new Date
this.noteTitle = '' this.noteTitle = ''
if(response.data.title){ if(response.data.title){
this.noteTitle = response.data.title this.noteTitle = response.data.title
@ -777,20 +800,8 @@
this.archived = response.data.archived this.archived = response.data.archived
this.attachmentCount = response.data.attachment_count this.attachmentCount = response.data.attachment_count
this.loading = false return true
this.$nextTick(() => {
//Adjust note title size after load
this.titleResize()
this.initSquire()
})
})
.catch(error => { this.$bus.$emit('notification', 'Failed to Open Note') })
} else {
console.log('Could not fetch note')
}
}, },
//Called on squire event for keyup //Called on squire event for keyup
diffText(event){ diffText(event){
@ -915,6 +926,7 @@
axios.post('/api/note/update', postData).then( response => { axios.post('/api/note/update', postData).then( response => {
this.statusText = 'saved' this.statusText = 'saved'
this.updated = +new Date this.updated = +new Date
this.lastInteractionTimestamp = +new Date
this.modified = true this.modified = true
this.diffsApplied = 0 this.diffsApplied = 0
@ -927,23 +939,35 @@
}, },
checkForUpdatedNote(){ checkForUpdatedNote(){
const now = +new Date
//Only check every 3 seconds
const checkForUpdateTimeout = now - this.lastInteractionTimestamp > (2 * 1000)
//If user leaves page then returns to page, reload the first batch //If user leaves page then returns to page, reload the first batch
if(this.lastVisibilityState == 'hidden' && document.visibilityState == 'visible'){ if(this.lastVisibilityState == 'hidden' && document.visibilityState == 'visible' && checkForUpdateTimeout){
// console.log('Checking for note updates after visibility change.')
const postData = { //Focus Regained on Note, check for update
noteId:this.currentNoteId, axios.post('/api/note/get', { 'noteId': this.noteid })
text:this.getText(), .then(response => {
updated: this.updated
const serverTextHash = this.hashString( response.data.text )
if(this.lastNoteHash != serverTextHash){
// console.log('note was changed UPDATE THAT BITCH!!!!')
this.setupLoadedNoteData(response)
//Manually set squire text to show
this.setText(this.noteText)
} }
console.log('Focus regained with note open.') })
console.log('Attempting to fix diff text. fix this. Search spleen')
// return
} }
//Track visibility state //Keep track of visibility change and last interaction time
this.lastVisibilityState = document.visibilityState this.lastVisibilityState = document.visibilityState
this.lastInteractionTimestamp = +new Date
}, },
hashString(inText){ hashString(inText){