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];
}
const options = {
transform(text, element, id, editor) {
if (text === undefined) {
return;
}
let converted = text
if (converted === '{{table}}') {
editor.treeForId(id).then(tree => {
let [header, rows] = tree[0].children
let rowData = _.map(rows.children, row => {
let page = row.text.substring(2).substring(0, row.text.length - 4)
return fetch('/' + page + '?format=metakv')
.then(res => res.json())
.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
})
])
function transformTable(editor, id, element) {
editor.treeForId(id).then(tree => {
let [header, rows] = tree[0].children
let rowData = _.map(rows.children, row => {
let page = row.text.substring(2).substring(0, row.text.length - 4)
return fetch('/' + page + '?format=metakv')
.then(res => res.json())
.then(res => res.meta)
.then(rowData => {
return el("tr", [
el("td", [renderInline(row.text)]),
..._.map(header.children, col => {
let td = el("td")
transform(rowData[_.snakeCase(_.trim(col.text))], $(td), id, editor)
return td
})
])
})
})
Promise.all(rowData)
.then(trs => {
return el("table", [
el("thead", [
el("tr", [
el("th", [
document.createTextNode("Title")
]),
..._.map(header.children, col => {
return el("th", [
document.createTextNode(col.text)
])
})
]
),
]),
el("tbody", trs)
])
}).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
}
Promise.all(rowData)
.then(trs => {
return el("table", [
el("thead", [
el("tr", [
el("th", [
document.createTextNode("Title")
]),
..._.map(header.children, col => {
return el("th", [
document.createTextNode(col.text)
])
})
]
),
]),
el("tbody", trs)
])
}).then(table => element.html(table))
})
}
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)

View File

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

View File

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