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,
saveDebounce: null, //Prevent save from being called numerous times quickly
updated: 'Never',
lastInteractionTimestamp:null, //Tracks when note was loaded and last saved/refreshed
editDebounce: null,
textChangedDebounce: null,
keyPressesCounter: 0, //Determine keys pressed between saves
@ -747,35 +748,8 @@
return
}
//Set up local data
this.currentNoteId = this.noteid
this.rawTextId = response.data.rawTextId
this.shareUsername = response.data.shareUsername
this.created = response.data.created
this.updated = response.data.updated
this.noteTitle = ''
if(response.data.title){
this.noteTitle = response.data.title
}
this.noteText = response.data.text
this.lastNoteHash = this.hashString( response.data.text )
// this.diffNoteText = response.data.text
//Setup note tags
this.allTags = response.data.tags ? response.data.tags.split(','):[]
//Set up note colors
if(response.data.color){
this.styleObject = JSON.parse(response.data.color)
}
if(response.data.pinned != null){
this.pinned = response.data.pinned
}
this.archived = response.data.archived
this.attachmentCount = response.data.attachment_count
//Setup all responsive vue data
this.setupLoadedNoteData(response)
this.loading = false
@ -792,6 +766,43 @@
console.log('Could not fetch note')
}
},
setupLoadedNoteData(response){
//All the data returned by the server, setup locally in vue component
//Set up local data
this.currentNoteId = this.noteid
this.rawTextId = response.data.rawTextId
this.shareUsername = response.data.shareUsername
this.created = response.data.created
this.updated = response.data.updated
this.lastInteractionTimestamp = +new Date
this.noteTitle = ''
if(response.data.title){
this.noteTitle = response.data.title
}
this.noteText = response.data.text
this.lastNoteHash = this.hashString( response.data.text )
// this.diffNoteText = response.data.text
//Setup note tags
this.allTags = response.data.tags ? response.data.tags.split(','):[]
//Set up note colors
if(response.data.color){
this.styleObject = JSON.parse(response.data.color)
}
if(response.data.pinned != null){
this.pinned = response.data.pinned
}
this.archived = response.data.archived
this.attachmentCount = response.data.attachment_count
return true
},
//Called on squire event for keyup
diffText(event){
@ -915,6 +926,7 @@
axios.post('/api/note/update', postData).then( response => {
this.statusText = 'saved'
this.updated = +new Date
this.lastInteractionTimestamp = +new Date
this.modified = true
this.diffsApplied = 0
@ -927,23 +939,35 @@
},
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(this.lastVisibilityState == 'hidden' && document.visibilityState == 'visible'){
// console.log('Checking for note updates after visibility change.')
if(this.lastVisibilityState == 'hidden' && document.visibilityState == 'visible' && checkForUpdateTimeout){
//Focus Regained on Note, check for update
axios.post('/api/note/get', { 'noteId': this.noteid })
.then(response => {
const postData = {
noteId:this.currentNoteId,
text:this.getText(),
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.lastInteractionTimestamp = +new Date
},
hashString(inText){