Add utility functions for adding notes

This commit is contained in:
Joey Payne 2016-03-11 15:09:47 -07:00
commit 4204d56a6f

View file

@ -26,6 +26,58 @@ export function getNotebookPath(notebook){
return path.join(noteBookPath, notebook.uuid+'.qvnotebook') return path.join(noteBookPath, notebook.uuid+'.qvnotebook')
} }
export function createNotebookDir(notebookNameOrUUID){
var dataPath = getAppDataPath()
var notebookPath = path.join(dataPath, notebookNameOrUUID+'.qvnotebook')
mkdirp.sync(notebookPath)
}
export function loadNote(notePath){
var metaPath = path.join(notePath, 'meta.json')
var contentPath = path.join(notePath, 'content.json')
var meta = jsfile.readFileSync(metaPath)
var content = jsfile.readFileSync(contentPath)
var note = meta
note.summary = ''
note.path = notePath
if(content.cells.length > 0){
note.summary = content.cells[0].data
}
return note
}
export function loadNotes(notebook){
var notebookPath = getNotebookPath(notebook)
var dataPath = getAppDataPath()
var noteGlob = ''
if(notebook.glob){
noteGlob = path.join(dataPath, notebook.glob)
}
else{
noteGlob = path.join(notebookPath, '*.qvnote')
}
var notePaths = glob.sync(noteGlob)
var notes = []
for(var i=0; i<notePaths.length; i++){
var notePath = notePaths[i]
var note = loadNote(notePath)
note.notebook = notebook
notes.push(note)
}
return notes
}
export function loadNotebookByName(nameOrUUID){ export function loadNotebookByName(nameOrUUID){
var dataPath = getAppDataPath() var dataPath = getAppDataPath()
var notebookPath = path.join(dataPath, nameOrUUID+'.qvnotebook') var notebookPath = path.join(dataPath, nameOrUUID+'.qvnotebook')
@ -48,6 +100,17 @@ export function loadNotebookByName(nameOrUUID){
return nb return nb
} }
export function updateObject(old, newObj){
for (var key in newObj) {
old[key] = newObj[key];
}
return old
}
export function getNotePathFromUUID(notebook, uuid){
return getNotePath(notebook, {uuid: uuid})
}
export function getNotebookPathFromUUID(uuid){ export function getNotebookPathFromUUID(uuid){
return getNotebookPath({uuid: uuid}) return getNotebookPath({uuid: uuid})
} }
@ -56,3 +119,18 @@ export function isFunction(functionToCheck) {
var getType = {} var getType = {}
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]' return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'
} }
export function trunc(str, n){
return (str.length > n) ? str.substr(0, n-1)+'...' : str;
};
export function compareNotebooks(a, b) {
let atitle = a.title.toLowerCase()
let btitle = b.title.toLowerCase()
if(atitle > btitle)
return 1
if(atitle < btitle)
return -1
return 0
}