// -- The main (so far only) Vue instance --
var app = new Vue({
// In the html file, need to enclose in an element with the class specified
// here, if we want Vue to know where to put it.
el: '.app19',
data: {
appTitle: 'Tiddler text paragraphs are displayed as separate `
`,
methods: {
updateTiddlerText(ev) {
this.currentTiddler.text = ev.target.innerText
},
getParagraphs(text) {
var chunks = text.split("\n")
console.log(chunks[0])
var paras = ""
for (var i = 0 ; i < chunks.length ; i++) {
paras += "
" + chunks[i] + "
"
}
return paras
},
addNewTag(ev) {
this.currentTiddler.tags.push(ev.target.innerText)
this.currentTiddler.tags = [...new Set(this.currentTiddler.tags)]
ev.target.innerText = ''
},
removeTag(tag) {
this.currentTiddler.tags.splice(this.currentTiddler.tags.indexOf(tag), 1)
},
selectTiddler(tiddler) {
this.currentTiddler = JSON.parse(JSON.stringify(tiddler))
this.currentCreated = tiddler.created
this.editFlag = true
},
submitTiddler(tiddler) {
if (this.editFlag == true) {
this.updateTiddler()
} else {
this.addTiddler()
}
this.resetTiddler()
},
isTheTiddler(tiddler) {
return tiddler.created === this.currentCreated
},
updateTiddler() {
console.log("updateTiddler:")
console.log(" currentCreated was " + this.currentCreated)
var tiddler = this.tiddlers.find(this.isTheTiddler)
var idx = this.tiddlers.indexOf(tiddler)
console.log(" replacing tiddler created at " + tiddler.created)
this.tiddlers.splice(idx, 1, this.currentTiddler)
},
addTiddler() {
this.currentTiddler.created = moment().utc().format('YYYYMMDDHHmmssSSS') // In TW, times are stored in UTC
this.currentTiddler.utcOffset = moment().utcOffset()
this.tiddlers.push(this.currentTiddler)
},
resetTiddler() {
this.currentTiddler = JSON.parse(JSON.stringify(this.emptyTiddler))
this.currentCreated = undefined
this.editFlag = false
},
removeTiddler() {
if (this.currentCreated != undefined && confirm("Delete this tiddler?")) {
var tiddler = this.tiddlers.find(this.isTheTiddler)
var idx = this.tiddlers.indexOf(tiddler)
this.tiddlers.splice(idx, 1)
this.resetTiddler()
}
},
localFromUTC(tiddler) {
var timeInt = moment.utc(tiddler.created, 'YYYYMMDDHHmmssSSS')
var localInt = timeInt.add(tiddler.utcOffset, 'minute')
var localTime = localInt.format('YYYY-MM-DD, hh:mm a')
return localTime
},
saveToLocalStorage() {
localStorage.setItem('stored_tiddlers', JSON.stringify(this.tiddlers))
},
loadFromLocalStorage() {
if (localStorage.getItem('stored_tiddlers')) {
this.tiddlers = JSON.parse(localStorage.getItem('stored_tiddlers'))
}
},
saveFile() {
// var blob = new Blob([JSON.stringify(this.tiddlers)], {type: "text/plain;charset=utf-8"})
var blob = new Blob([JSON.stringify(this.tiddlers)], {type: "application/json"})
var createdTime = moment().format('YYYYMMDDHHmmssZZ')
var fileName = 'tiddlers_' + createdTime + '.json'
console.log(fileName)
saveAs(blob, fileName)
},
fileSelect() {
document.getElementById("load-tiddlers").click()
},
handleFile(ev) {
console.log(ev.target.files[0])
var reader = new FileReader()
reader.onload = e => {this.tiddlers = JSON.parse(reader.result)}
// var self = this
// reader.onload = function(e) {
// self.tiddlers = JSON.parse(reader.result)
// }
reader.readAsText(ev.target.files[0])
}
}
})