From db5603c5cd83ec9e9ce76bab32094116bb62aea6 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Mon, 26 Oct 2020 21:28:10 +0100 Subject: [PATCH] Add date functions to calculator --- editor/src/editor.js | 31 +++++++++++---- editor/src/formula.js | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 editor/src/formula.js diff --git a/editor/src/editor.js b/editor/src/editor.js index fa669fc..cc37ca8 100644 --- a/editor/src/editor.js +++ b/editor/src/editor.js @@ -22,9 +22,14 @@ import 'prismjs/components/prism-jq' import MD from './markdown' import he from 'he' import {all, create} from 'mathjs' +import formulaFunctions from './formula' + +moment.locale('nl') const math = create(all) +math.import(formulaFunctions) + function isMultiline(input) { return input.value.startsWith("```", 0) || input.value.startsWith("$$", 0) @@ -151,8 +156,6 @@ function el(tag, children = []) { } function Editor(holder, input) { - let scope = {} - function renderInline(cellText) { if (!cellText) return document.createTextNode('') @@ -164,8 +167,11 @@ function Editor(holder, input) { } function transformTable(editor, id, element) { + editor.treeForId(id).then(tree => { - let [header, rows] = tree[0].children + let header = _.find(tree[0].children, c => c.text === 'headers') || [] + let rows = _.find(tree[0].children, c => c.text === 'rows') || [] + let rowData = _.map(rows.children, row => { let page = row.text.substring(2).substring(0, row.text.length - 4) return fetch('/' + page + '?format=metakv') @@ -176,7 +182,11 @@ function Editor(holder, input) { el("td", [renderInline(row.text)]), ..._.map(header.children, col => { let td = el("td") - transform(rowData[_.snakeCase(_.trim(col.text))], $(td), id, editor) + let value = rowData[_.snakeCase(_.trim(col.text))]; + if (col.children && col.children.length > 0) { + value = col.children[0].text + } + transform(value ? value.replace(/^:/, '=') : '', $(td), id, editor, rowData) return td }) ]) @@ -205,10 +215,13 @@ function Editor(holder, input) { }) } - function transform(text, element, id, editor) { + function transform(text, element, id, editor, scope) { if (text === undefined) { return; } + if (!scope) { + scope = editor.scope || {} + } let converted = text @@ -224,9 +237,10 @@ function Editor(holder, input) { converted = converted + ' ' + e.message + ''; } } else { - let re = /^([A-Z0-9 ]+):: (.+)$/i; - if (text.match(re)) { - converted = converted.replace(re, '**[[$1]]**: $2') + let re = /^([A-Z0-9 ]+)::\s*(.+)$/i; + let res = text.match(re) + if (res) { + converted = '**[[' + res[1] + ']]**: ' + res[2] } else if (text.match(/#\[\[TODO]]/)) { converted = converted.replace('#[[TODO]]', '') } else if (text.match(/#\[\[DONE]]/)) { @@ -248,6 +262,7 @@ function Editor(holder, input) { let editor = listEditor(holder, inputData, options); holder.$listEditor = editor + editor.scope = {} $(holder).find('.content input[type="checkbox"]').on('click', function (event) { let that = this diff --git a/editor/src/formula.js b/editor/src/formula.js new file mode 100644 index 0000000..1880f50 --- /dev/null +++ b/editor/src/formula.js @@ -0,0 +1,90 @@ +import moment from "moment"; + +/** + * @param {string|moment.Moment} x + * @returns {moment.Moment} + */ +function date(x) { + if (!x) { + return moment() + } + if (_.isString(x)) { + let r = x.match(/^\[\[(.+)\]\]$/); + return moment(r[1], "DD MMM YYYY") + } + return moment(x); +} + +/** + * + * @param {moment.Moment} d + * @param {int} n + * @param {string} s + * @returns {moment.Moment} + */ +function dateAdd(d, n, s) { + return date(d).add(n, s) +} + +function dateFormat(d, fmt) { + return date(d).format(fmt) +} + +function dateStartOf(d, period) { + return date(d).startOf(period) +} + +function dateEndOf(d, period) { + return date(d).endOf(period) +} + +function dateIsBefore(a, b) { + return date(a).isBefore(date(b)) +} + +function dateIsAfter(a, b) { + return date(a).isBefore(date(b)) +} + +function dateIsSame(a, b, g) { + return date(a).isSame(date(b), g); +} + +function dateCalendar(d) { + return date(d).calendar(moment()) +} + +function dateDiff(a, b, g) { + return date(a).diff(date(b), g) +} + +export default { + date, + dateAdd, + dateFormat, + dateStartOf, + dateEndOf, + dateIsBefore, + dateIsAfter, + dateIsSame, + dateCalendar, + dateDiff, + empty(x) { + return !x; + }, + if: function (x, a, b) { + return x ? a : b + }, + gt: function (a, b) { + return a > b + }, + lt: function (a, b) { + return a < b + }, + gteq: function (a, b) { + return a >= b + }, + lteq: function (a, b) { + return a <= b + } +}