mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	Compare commits
	
		
			10 Commits
		
	
	
		
			v0.9.0-bet
			...
			v0.9.2
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					840af15dae | ||
| 
						 | 
					0fdb6af98a | ||
| 
						 | 
					f6c7f6a0f2 | ||
| 
						 | 
					354999f37a | ||
| 
						 | 
					348c622845 | ||
| 
						 | 
					44bcdedaba | ||
| 
						 | 
					755c0f3ce2 | ||
| 
						 | 
					895bda41b5 | ||
| 
						 | 
					b2df622cb6 | ||
| 
						 | 
					9ba6e6d0f5 | 
@@ -1,7 +1,7 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "trilium",
 | 
			
		||||
  "description": "Trilium Notes",
 | 
			
		||||
  "version": "0.9.0-beta",
 | 
			
		||||
  "version": "0.9.2",
 | 
			
		||||
  "license": "AGPL-3.0-only",
 | 
			
		||||
  "main": "electron.js",
 | 
			
		||||
  "repository": {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,22 @@
 | 
			
		||||
function ScriptContext(startNote, allNotes) {
 | 
			
		||||
    const modules = {};
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
        modules: {},
 | 
			
		||||
        modules: modules,
 | 
			
		||||
        notes: toObject(allNotes, note => [note.noteId, note]),
 | 
			
		||||
        apis: toObject(allNotes, note => [note.noteId, ScriptApi(startNote, note)]),
 | 
			
		||||
        require: moduleNoteIds => {
 | 
			
		||||
            return moduleName => {
 | 
			
		||||
                const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
 | 
			
		||||
                const note = candidates.find(c => c.title === moduleName);
 | 
			
		||||
 | 
			
		||||
                if (!note) {
 | 
			
		||||
                    throw new Error("Could not find module note " + moduleName);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return modules[note.noteId].exports;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,7 @@ async function exportNote(noteTreeId, directory, pack, repo) {
 | 
			
		||||
 | 
			
		||||
    const metadata = await getMetadata(note);
 | 
			
		||||
 | 
			
		||||
    if ('exclude_from_export' in metadata.attributes) {
 | 
			
		||||
    if (metadata.attributes.find(attr => attr.name === 'exclude_from_export')) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -36,8 +36,10 @@ router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
 | 
			
		||||
    for (const note of notes) {
 | 
			
		||||
        const bundle = await script.getScriptBundle(note);
 | 
			
		||||
 | 
			
		||||
        if (bundle) {
 | 
			
		||||
            scripts.push(bundle);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    res.send(scripts);
 | 
			
		||||
}));
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/scripts/Today.tar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/scripts/Today.tar
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/scripts/Weight Tracker.tar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/scripts/Weight Tracker.tar
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							@@ -1,13 +0,0 @@
 | 
			
		||||
api.addButtonToToolbar('go-today', $('<button class="btn btn-xs" onclick="goToday();"><span class="ui-icon ui-icon-calendar"></span> Today</button>'));
 | 
			
		||||
 | 
			
		||||
window.goToday = async function() {
 | 
			
		||||
    const todayDateStr = formatDateISO(new Date());
 | 
			
		||||
 | 
			
		||||
    const todayNoteId = await server.exec([todayDateStr], async todayDateStr => {
 | 
			
		||||
        return await this.getDateNoteId(todayDateStr);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    api.activateNote(todayNoteId);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
$(document).bind('keydown', "alt+t", window.goToday);
 | 
			
		||||
@@ -1,146 +0,0 @@
 | 
			
		||||
<form id="weight-form" style="display: flex; width: 700px; justify-content: space-around; align-items: flex-end;">
 | 
			
		||||
    <div>
 | 
			
		||||
        <label for="weight-date">Date</label>
 | 
			
		||||
        <input type="text" id="weight-date" class="form-control" style="width: 150px; text-align: center;" />
 | 
			
		||||
    </div>
 | 
			
		||||
    <div>
 | 
			
		||||
        <label for="weight">Weight</label>
 | 
			
		||||
        <input type="number" id="weight" value="80.0" step="0.1" class="form-control" style="text-align: center; width: 100px;" />
 | 
			
		||||
    </div>
 | 
			
		||||
    <div>
 | 
			
		||||
        <label for="comment">Comment</label>
 | 
			
		||||
        <input type="text" id="comment" class="form-control" style="width: 200px;" />
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <button type="submit" class="btn btn-primary">Add</button>
 | 
			
		||||
</form>
 | 
			
		||||
 | 
			
		||||
<br/><br/>
 | 
			
		||||
 | 
			
		||||
<canvas id="canvas"></canvas>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
    (async function() {
 | 
			
		||||
        const $form = $("#weight-form");
 | 
			
		||||
        const $date = $("#weight-date");
 | 
			
		||||
        const $weight = $("#weight");
 | 
			
		||||
        const $comment = $("#comment");
 | 
			
		||||
        let chart;
 | 
			
		||||
 | 
			
		||||
        $date.datepicker();
 | 
			
		||||
        $date.datepicker('option', 'dateFormat', 'yy-mm-dd');
 | 
			
		||||
        $date.datepicker('setDate', new Date());
 | 
			
		||||
 | 
			
		||||
        async function saveWeight() {
 | 
			
		||||
            await server.exec([$date.val(), parseFloat($weight.val()), $comment.val()], async (date, weight, comment) => {
 | 
			
		||||
                const dataNote = await this.getNoteWithAttribute('date_data', date);
 | 
			
		||||
 | 
			
		||||
                if (dataNote) {
 | 
			
		||||
                    dataNote.jsonContent.weight = weight;
 | 
			
		||||
 | 
			
		||||
                    if (comment) {
 | 
			
		||||
                        dataNote.jsonContent.weight_comment = comment;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    await this.updateEntity(dataNote);
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    const parentNoteId = await this.getDateNoteId(date);
 | 
			
		||||
                    const jsonContent = { weight: weight };
 | 
			
		||||
 | 
			
		||||
                    if (comment) {
 | 
			
		||||
                        jsonContent.weight_comment = comment;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    await this.createNote(parentNoteId, 'data', jsonContent, {
 | 
			
		||||
                        json: true,
 | 
			
		||||
                        attributes: {
 | 
			
		||||
                            date_data: date,
 | 
			
		||||
                            hide_in_autocomplete: null
 | 
			
		||||
                        }
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            showMessage("Weight has been saved");
 | 
			
		||||
 | 
			
		||||
            chart.data = await getData();
 | 
			
		||||
            chart.update();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        async function drawChart() {
 | 
			
		||||
            const data = await getData();
 | 
			
		||||
 | 
			
		||||
            const ctx = $("#canvas")[0].getContext("2d");
 | 
			
		||||
 | 
			
		||||
            chart = new Chart(ctx, {
 | 
			
		||||
                type: 'line',
 | 
			
		||||
                data: data,
 | 
			
		||||
                options: {
 | 
			
		||||
                    tooltips: {
 | 
			
		||||
                        enabled: true,
 | 
			
		||||
                        mode: 'single',
 | 
			
		||||
                        callbacks: {
 | 
			
		||||
                            label: function (tooltipItem, data) {
 | 
			
		||||
                                const multistringText = [tooltipItem.yLabel];
 | 
			
		||||
                                const comment = data.comments[tooltipItem['index']];
 | 
			
		||||
 | 
			
		||||
                                if (comment) {
 | 
			
		||||
                                    multistringText.push(comment);
 | 
			
		||||
                                }
 | 
			
		||||
 | 
			
		||||
                                return multistringText;
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    },
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        async function getData() {
 | 
			
		||||
            const data = await server.exec([], async () => {
 | 
			
		||||
                const notes = await this.getNotesWithAttribute('date_data');
 | 
			
		||||
                const data = [];
 | 
			
		||||
 | 
			
		||||
                for (const note of notes) {
 | 
			
		||||
                    const dateAttr = await note.getAttribute('date_data');
 | 
			
		||||
 | 
			
		||||
                    data.push({
 | 
			
		||||
                        date: dateAttr.value,
 | 
			
		||||
                        weight: note.jsonContent.weight,
 | 
			
		||||
                        comment: note.jsonContent.weight_comment
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                data.sort((a, b) => a.date < b.date ? -1 : +1);
 | 
			
		||||
 | 
			
		||||
                return data;
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            const datasets = [{
 | 
			
		||||
                label: "Weight",
 | 
			
		||||
                backgroundColor: 'red',
 | 
			
		||||
                borderColor: 'red',
 | 
			
		||||
                data: data.map(row => row.weight),
 | 
			
		||||
                fill: false
 | 
			
		||||
            }];
 | 
			
		||||
 | 
			
		||||
            const labels = data.map(row => row.date);
 | 
			
		||||
            const comments = data.map(row => row.comment);
 | 
			
		||||
 | 
			
		||||
            return {
 | 
			
		||||
                labels: labels,
 | 
			
		||||
                datasets: datasets,
 | 
			
		||||
                comments: comments
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $form.submit(event => {
 | 
			
		||||
            saveWeight();
 | 
			
		||||
 | 
			
		||||
            event.preventDefault();
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        drawChart();
 | 
			
		||||
    })();
 | 
			
		||||
</script>
 | 
			
		||||
@@ -73,7 +73,7 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNote
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (await note.hasAttribute('disable_inclusion')) {
 | 
			
		||||
    if (!root && await note.hasAttribute('disable_inclusion')) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -111,13 +111,15 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNote
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const moduleNoteIds = modules.map(mod => mod.noteId);
 | 
			
		||||
 | 
			
		||||
    if (note.isJavaScript()) {
 | 
			
		||||
        bundle.script += `
 | 
			
		||||
apiContext.modules['${note.noteId}'] = {};
 | 
			
		||||
${root ? 'return ' : ''}await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') +
 | 
			
		||||
${root ? 'return ' : ''}await (async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
 | 
			
		||||
            modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
 | 
			
		||||
${note.content}
 | 
			
		||||
})({}, apiContext.modules['${note.noteId}'], apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
 | 
			
		||||
})({}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
 | 
			
		||||
            modules.map(mod => `apiContext.modules['${mod.noteId}'].exports`).join(', ') + `);
 | 
			
		||||
`;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,18 @@ function ScriptContext(dataKey, startNote, allNotes) {
 | 
			
		||||
    this.modules = {};
 | 
			
		||||
    this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
 | 
			
		||||
    this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(dataKey, startNote, note)]);
 | 
			
		||||
    this.require = moduleNoteIds => {
 | 
			
		||||
        return moduleName => {
 | 
			
		||||
            const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
 | 
			
		||||
            const note = candidates.find(c => c.title === moduleName);
 | 
			
		||||
 | 
			
		||||
            if (!note) {
 | 
			
		||||
                throw new Error("Could not find module note " + moduleName);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return this.modules[note.noteId].exports;
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function ScriptApi(dataKey, startNote, currentNote) {
 | 
			
		||||
@@ -26,7 +38,8 @@ function ScriptApi(dataKey, startNote, currentNote) {
 | 
			
		||||
 | 
			
		||||
    this.utils = {
 | 
			
		||||
        unescapeHtml: utils.unescapeHtml,
 | 
			
		||||
        isoDateTimeStr: utils.dateStr
 | 
			
		||||
        isoDateTimeStr: utils.dateStr,
 | 
			
		||||
        isoDateStr: date => utils.dateStr(date).substr(0, 10)
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    this.getInstanceName = () => config.General ? config.General.instanceName : null;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user