Compare commits

...

10 Commits

Author SHA1 Message Date
azivner
840af15dae release 0.9.2 2018-03-13 08:22:25 -04:00
azivner
0fdb6af98a Merge branch 'master' into stable 2018-03-10 20:33:58 -05:00
azivner
f6c7f6a0f2 added require() method for commonJS compliancy 2018-03-10 11:53:51 -05:00
azivner
354999f37a fix weight tracker script 2018-03-09 19:28:38 -05:00
azivner
348c622845 release 0.9.1-beta 2018-03-09 00:12:22 -05:00
azivner
44bcdedaba Updated all scripts to current versions working with current script API 2018-03-09 00:10:43 -05:00
azivner
755c0f3ce2 fix exclude from export 2018-03-09 00:10:02 -05:00
azivner
895bda41b5 return only startup bundles for executale notes 2018-03-08 23:36:08 -05:00
azivner
b2df622cb6 Merge branch 'stable' 2018-03-08 23:35:17 -05:00
azivner
9ba6e6d0f5 disable inclusion should work only on non-root notes 2018-03-08 23:35:08 -05:00
11 changed files with 39 additions and 167 deletions

View File

@@ -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": {

View File

@@ -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;
}
}
};
}

View File

@@ -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;
}

View File

@@ -36,7 +36,9 @@ router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
for (const note of notes) {
const bundle = await script.getScriptBundle(note);
scripts.push(bundle);
if (bundle) {
scripts.push(bundle);
}
}
res.send(scripts);

Binary file not shown.

BIN
src/scripts/Today.tar Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -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);

View File

@@ -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>

View File

@@ -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(', ') + `);
`;
}

View File

@@ -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;