e87e8513bc
* Cleaned up unused things * Updated squire which had a comment typo update...thats it * Background color picker has matching colors and styles to text color picker * Added new black theme * Moved search to main page, show it on mobile and added options to push things to notes from search with experimental tag searching * Added active note menu buttons based on cursor location in text * Added more instant updating if app is open in two locations for the same user Scratch Pad and home page update with new notes and new text in real time
112 lines
3.0 KiB
JavaScript
112 lines
3.0 KiB
JavaScript
let db = require('@config/database')
|
|
|
|
let Note = require('@models/Note')
|
|
|
|
let QuickNote = module.exports = {}
|
|
|
|
|
|
QuickNote.get = (userId, masterKey) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
|
.query(`
|
|
SELECT note.id FROM note WHERE quick_note = 1 AND user_id = ? LIMIT 1
|
|
`, [userId])
|
|
.then((rows, fields) => {
|
|
|
|
//Quick Note is set, return note text
|
|
if(rows[0][0] != undefined){
|
|
let noteId = rows[0][0].id
|
|
Note.get(userId, noteId, masterKey)
|
|
.then( noteObject => {
|
|
return resolve(noteObject)
|
|
})
|
|
} else {
|
|
return resolve(null)
|
|
}
|
|
|
|
|
|
})
|
|
.catch(console.log)
|
|
})
|
|
}
|
|
|
|
QuickNote.newNote = (userId) => {
|
|
return new Promise((resolve, reject) => {
|
|
db.promise().query('UPDATE note SET quick_note = 0 WHERE quick_note = 1 AND user_id = ?',[userId])
|
|
.then((rows, fields) => {
|
|
resolve(true)
|
|
})
|
|
})
|
|
}
|
|
|
|
QuickNote.makeUrlLink = (inputText) => {
|
|
var replacedText, replacePattern1, replacePattern2, replacePattern3;
|
|
|
|
//URLs starting with http://, https://, or ftp://
|
|
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
|
|
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
|
|
|
|
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
|
|
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
|
|
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
|
|
|
|
//Change email addresses to mailto:: links.
|
|
replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
|
|
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
|
|
|
|
return replacedText;
|
|
}
|
|
|
|
QuickNote.update = (io, userId, pushText, masterKey) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let finalId = null
|
|
let finalText = ''
|
|
|
|
//Process pushText, split at \n (new lines), put <p> tags around each new line
|
|
let broken = '<p>' +
|
|
pushText.split(/\r?\n/).map( (line, index) => {
|
|
|
|
let clean = line
|
|
.replace(/&[#A-Za-z0-9]+;/g,'') //Rip out all HTML entities
|
|
.replace(/<[^>]+>/g, '') //Rip out all HTML tags
|
|
|
|
//Turn links into actual linx
|
|
clean = QuickNote.makeUrlLink(clean)
|
|
|
|
if(clean == ''){ clean = ' ' }
|
|
let newLine = ''
|
|
if(index > 0){ newLine = '<br>' }
|
|
|
|
//Return line wrapped in p tags
|
|
return `${newLine}<span>${clean}</span>`
|
|
|
|
}).join('') + '</p><p><br></p>'
|
|
|
|
QuickNote.get(userId, masterKey)
|
|
.then(noteObject => {
|
|
|
|
if(noteObject == null){
|
|
|
|
finalText += broken
|
|
|
|
return Note.create(io, userId, 'Quick Note', finalText, masterKey)
|
|
.then(insertedId => {
|
|
finalId = insertedId
|
|
return db.promise().query('UPDATE note SET quick_note = 1 WHERE id = ? AND user_id = ?',[insertedId, userId])
|
|
})
|
|
|
|
} else {
|
|
|
|
finalText += (broken + noteObject.text)
|
|
finalId = noteObject.id
|
|
return Note.update(io, userId, noteObject.id, finalText, noteObject.title, noteObject.color, noteObject.pinned, noteObject.archived, null, masterKey)
|
|
}
|
|
})
|
|
.then( saveResults => {
|
|
return resolve(true)
|
|
})
|
|
})
|
|
|
|
} |