Move transformTable function

This commit is contained in:
Peter Stuifzand 2020-10-25 19:49:22 +01:00
parent 5859046169
commit 9e295eee25
3 changed files with 76 additions and 70 deletions

View File

@ -163,79 +163,85 @@ function Editor(holder, input) {
return span[0]; return span[0];
} }
const options = { function transformTable(editor, id, element) {
transform(text, element, id, editor) { editor.treeForId(id).then(tree => {
if (text === undefined) { let [header, rows] = tree[0].children
return; let rowData = _.map(rows.children, row => {
} let page = row.text.substring(2).substring(0, row.text.length - 4)
return fetch('/' + page + '?format=metakv')
let converted = text .then(res => res.json())
.then(res => res.meta)
if (converted === '{{table}}') { .then(rowData => {
editor.treeForId(id).then(tree => { return el("tr", [
let [header, rows] = tree[0].children el("td", [renderInline(row.text)]),
let rowData = _.map(rows.children, row => { ..._.map(header.children, col => {
let page = row.text.substring(2).substring(0, row.text.length - 4) let td = el("td")
return fetch('/' + page + '?format=metakv') transform(rowData[_.snakeCase(_.trim(col.text))], $(td), id, editor)
.then(res => res.json()) return td
.then(res => res.meta)
.then(rowData => {
return el("tr", [
el("td", [renderInline(row.text)]),
..._.map(header.children, col => {
let td = el("td")
this.transform(rowData[_.snakeCase(_.trim(col.text))], $(td), id, editor)
return td
})
])
}) })
])
}) })
})
Promise.all(rowData) Promise.all(rowData)
.then(trs => { .then(trs => {
return el("table", [ return el("table", [
el("thead", [ el("thead", [
el("tr", [ el("tr", [
el("th", [ el("th", [
document.createTextNode("Title") document.createTextNode("Title")
]), ]),
..._.map(header.children, col => { ..._.map(header.children, col => {
return el("th", [ return el("th", [
document.createTextNode(col.text) document.createTextNode(col.text)
]) ])
}) })
] ]
), ),
]), ]),
el("tbody", trs) el("tbody", trs)
]) ])
}).then(table => element.html(table)) }).then(table => element.html(table))
}) })
return }
} else if (converted.startsWith("```", 0) || converted.startsWith("$$", 0)) {
converted = MD.render(converted)
} else if (converted.startsWith("=", 0)) {
try {
converted = math.evaluate(converted.substring(1), scope).toString()
} catch (e) {
converted = converted + ' <span style="background: red; color: white;">' + e.message + '</span>';
}
} else {
let re = /^([A-Z0-9 ]+):: (.+)$/i;
if (text.match(re)) {
converted = converted.replace(re, '**[[$1]]**: $2')
} else if (text.match(/#\[\[TODO]]/)) {
converted = converted.replace('#[[TODO]]', '<input class="checkbox" type="checkbox" />')
} else if (text.match(/#\[\[DONE]]/)) {
converted = converted.replace('#[[DONE]]', '<input class="checkbox" type="checkbox" checked />')
}
MD.options.html = true
converted = MD.renderInline(converted)
MD.options.html = false
}
element.html(converted) function transform(text, element, id, editor) {
if (text === undefined) {
return;
} }
let converted = text
if (converted === '{{table}}') {
transformTable.call(this, editor, id, element);
return
} else if (converted.startsWith("```", 0) || converted.startsWith("$$", 0)) {
converted = MD.render(converted)
} else if (converted.startsWith("=", 0)) {
try {
converted = math.evaluate(converted.substring(1), scope).toString()
} catch (e) {
converted = converted + ' <span style="background: red; color: white;">' + e.message + '</span>';
}
} else {
let re = /^([A-Z0-9 ]+):: (.+)$/i;
if (text.match(re)) {
converted = converted.replace(re, '**[[$1]]**: $2')
} else if (text.match(/#\[\[TODO]]/)) {
converted = converted.replace('#[[TODO]]', '<input class="checkbox" type="checkbox" />')
} else if (text.match(/#\[\[DONE]]/)) {
converted = converted.replace('#[[DONE]]', '<input class="checkbox" type="checkbox" checked />')
}
MD.options.html = true
converted = MD.renderInline(converted)
MD.options.html = false
}
element.html(converted)
}
const options = {
transform
} }
let inputData = input ? input : JSON.parse(holder.dataset.input) let inputData = input ? input : JSON.parse(holder.dataset.input)

View File

@ -1,8 +1,8 @@
import _ from 'lodash' import _ from 'lodash'
import $ from 'jquery' import $ from 'jquery'
import he from 'he' import he from 'he'
import textareaAutosizeInit from "./textarea.autosize"
import dragula from 'dragula' import dragula from 'dragula'
import textareaAutosizeInit from "./textarea.autosize"
import createCursor from './cursor' import createCursor from './cursor'
import createSelection from './selection' import createSelection from './selection'
import Store from './store' import Store from './store'

View File

@ -1,6 +1,6 @@
{ {
"name": "wiki-list-editor", "name": "wiki-list-editor",
"version": "0.8.12", "version": "0.8.13",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {