Table formatting WIP

This commit is contained in:
Magnus Åhall 2026-06-01 17:50:17 +02:00
parent 88304b90a4
commit b59c6c8b58
2 changed files with 91 additions and 2 deletions

View file

@ -9,7 +9,7 @@ export class N2PageNodeUI extends CustomHTMLElement {
<style>
.el-functions {
display: grid;
grid-template-columns: 1fr min-content;
grid-template-columns: 1fr min-content min-content;
grid-gap: 8px;
align-items: center;
justify-items: end;
@ -27,6 +27,7 @@ export class N2PageNodeUI extends CustomHTMLElement {
<div data-el="functions">
<img data-el="icon-markdown">
<img data-el="icon-save" src="/images/${_VERSION}/icon_save_disabled.svg">
<img data-el="icon-table-format" src="/images/${_VERSION}/icon_table.svg">
</div>
`
}// }}}
@ -74,6 +75,14 @@ export class N2PageNodeUI extends CustomHTMLElement {
this.elNodeContent.addEventListener('input', event => this.contentChanged(event))
this.elNodeContent.addEventListener('paste', async (event) => this.pasteHandler(event))
this.elIconMarkdown.addEventListener('click', () => this.showMarkdown(!this.showMarkdown()))
this.elIconTableFormat.addEventListener('click', () => {
const from = this.elNodeContent.selectionStart
const to = this.elNodeContent.selectionEnd
const sel = this.elNodeContent.value.slice(from, to)
this.formatTable(sel)
})
this.showMarkdown(true)
}// }}}
@ -134,7 +143,7 @@ export class N2PageNodeUI extends CustomHTMLElement {
if (!file)
throw new Error("Couldn't convert image to file object.")
const uuid = uuidv7()
await globalThis.nodeStore.files.add({ data: { UUID: uuid, file: file }})
await globalThis.nodeStore.files.add({ data: { UUID: uuid, file: file } })
const [start, end] = [this.elNodeContent.selectionStart, this.elNodeContent.selectionEnd]
this.elNodeContent.setRangeText(`![${file.name}](db://${uuid})`, start, end, 'select');
@ -155,6 +164,37 @@ export class N2PageNodeUI extends CustomHTMLElement {
this.elNodeContent.selectionEnd = data.position.end
this.elNodeContent.focus()
}// }}}
formatTable(t) {
const lines = t.split(/\r?\n/)
if (lines < 1)
return
let first = -1
let last = -1
let numColumns = 0
let colwidth = []
for (let i = 0; i < lines.length; i++) {
// -1 for split, -1 because number of columns are one less than number of pipes.
const columns = lines[i].split('|')
const linecols = columns.length - 1 - 1
numColumns = Math.max(numColumns, linecols)
if (linecols >= 1) {
if (first == -1)
first = i
last = i
continue
}
if (linecols < 1 && last > -1)
break
// Keep count of column width
}
console.log(first, last, columns)
}
}
customElements.define('n2-nodeui', N2PageNodeUI)