Browse Source

Move transformTable function

single-block-api
Peter Stuifzand 11 months ago
parent
commit
9e295eee25
  1. 140
      editor/src/editor.js
  2. 2
      list-editor/index.js
  3. 2
      list-editor/package-lock.json

140
editor/src/editor.js

@ -163,79 +163,85 @@ function Editor(holder, input) {
return span[0];
}
const options = {
transform(text, element, id, editor) {
if (text === undefined) {
return;
}
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
})
])
})
})
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)]),
Promise.all(rowData)
.then(trs => {
return el("table", [
el("thead", [
el("tr", [
el("th", [
document.createTextNode("Title")
]),
..._.map(header.children, col => {
let td = el("td")
this.transform(rowData[_.snakeCase(_.trim(col.text))], $(td), id, editor)
return td
return el("th", [
document.createTextNode(col.text)
])
})
])
})
})
]
),
]),
el("tbody", trs)
])
}).then(table => element.html(table))
})
}
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
}
function transform(text, element, id, editor) {
if (text === undefined) {
return;
}
element.html(converted)
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)

2
list-editor/index.js

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

2
list-editor/package-lock.json

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

Loading…
Cancel
Save