oficina / mapa Public

Columns (WIP): the schema's columns and column nodes, and fromBlocks builds them; L4's toDOM parity holds on every layout. Serialiser and fidelity next (L1-L3 fail on layouts until then)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 8c693dfcdf9ae276de34762e6e487e94cdb04e3b parent 48ba0f9
2 files changed, +87 −17
  • editor/fromMarkdown.mjs +50 −10
  • editor/schema.mjs +37 −7
diff --git a/editor/fromMarkdown.mjs b/editor/fromMarkdown.mjs
index a94c7c6..6c979e7 100644
--- a/editor/fromMarkdown.mjs
+++ b/editor/fromMarkdown.mjs
@@ -13,7 +13,7 @@ import { Mark } from 'prosemirror-model'
import {
parse, plain, spans, words, task, itemSyntax, isDate, isFenceLine,
fenceOf, closesFence,
- Paragraph, Heading, Item, Code, Quote, Rule,
+ Paragraph, Heading, Item, Code, Quote, Rule, Columns, Column, ColumnEnd,
InCode, WikiLink, WebLink, Embed, Image
} from './markup.mjs'
@@ -23,23 +23,63 @@ export function fromMarkdown (schema, body, options) {
export function fromBlocks (schema, blocks, options = {}) {
const origin = options.origin ?? (() => null)
+ const content = contentFrom(schema, blocks, 0, blocks.length, origin)
+ const last = content[content.length - 1]
+ if (options.trailing !== false &&
+ (!last || last.type !== schema.nodes.paragraph)) {
+ content.push(schema.nodes.paragraph.create())
+ }
+ return schema.nodes.doc.create(null, content)
+}
+
+// contentFrom is the nodes for blocks[from:to]: lists nested as
+// renderList nests them, and a column layout as its columns (columns
+// and icons spec §1.7).
+function contentFrom (schema, blocks, from, to, origin) {
const content = []
- for (let index = 0; index < blocks.length;) {
+ for (let index = from; index < to;) {
if (blocks[index].kind === Item) {
- const [lists, next] = listsFrom(schema, blocks, index, origin)
+ const [lists, next] = listsFrom(schema, blocks, index, origin, to)
content.push(...lists)
index = next
continue
}
+ if (blocks[index].kind === Columns) {
+ const [layout, next] = layoutFrom(schema, blocks, index, origin)
+ content.push(layout)
+ index = next
+ continue
+ }
content.push(blockNode(schema, blocks[index], origin(index)))
index++
}
- const last = content[content.length - 1]
- if (options.trailing !== false &&
- (!last || last.type !== schema.nodes.paragraph)) {
- content.push(schema.nodes.paragraph.create())
+ return content
+}
+
+const colons = source => /^[ \t]*(:+)/u.exec(source)[1].length
+
+// layoutFrom is the layout opening at start, and the index after it.
+// An empty column holds the editor's empty paragraph, as Go draws it.
+function layoutFrom (schema, blocks, start, origin) {
+ const columns = []
+ let index = start + 1
+ while (index < blocks.length && blocks[index].kind === Column) {
+ let end = index + 1
+ while (end < blocks.length && blocks[end].kind !== ColumnEnd) end++
+ const inside = contentFrom(schema, blocks, index + 1, end, origin)
+ columns.push(schema.nodes.column.create({
+ fence: colons(blocks[index].source),
+ openOrigin: origin(index),
+ closeOrigin: origin(end)
+ }, inside.length ? inside : [schema.nodes.paragraph.create()]))
+ index = end + 1
}
- return schema.nodes.doc.create(null, content)
+ const layout = schema.nodes.columns.create({
+ fence: colons(blocks[start].source),
+ openOrigin: origin(start),
+ closeOrigin: origin(index)
+ }, columns)
+ return [layout, index + 1]
}
function blockNode (schema, block, origin) {
@@ -88,11 +128,11 @@ function quotePrefixes (source) {
// listsFrom is renderList's nesting: depth is min(level, lists open), a
// level skip nests one deep, and an item of the other kind starts a new
// list at its depth.
-function listsFrom (schema, blocks, start, origin) {
+function listsFrom (schema, blocks, start, origin, to = blocks.length) {
const top = []
const open = []
let index = start
- for (; index < blocks.length && blocks[index].kind === Item; index++) {
+ for (; index < to && blocks[index].kind === Item; index++) {
const block = blocks[index]
const depth = Math.min(block.level, open.length)
open.length = Math.min(open.length, depth + 1)
diff --git a/editor/schema.mjs b/editor/schema.mjs
index e095480..d1d861e 100644
--- a/editor/schema.mjs
+++ b/editor/schema.mjs
@@ -28,7 +28,7 @@ const nodes = {
doc: { content: 'block+' },
paragraph: {
- group: 'block',
+ group: 'block flow',
content: 'inline*',
attrs: { ...textblock },
parseDOM: [{ tag: 'p' }],
@@ -38,7 +38,7 @@ const nodes = {
// heading: level 1 to 6, drawn h2 to h6, because the page title is
// the H1. source is the line as typed (# count and spacing).
heading: {
- group: 'block',
+ group: 'block flow',
content: 'inline*',
defining: true,
attrs: { ...textblock, level: { default: 1 }, source: { default: null } },
@@ -55,14 +55,14 @@ const nodes = {
// kind starts another. An item's content is its one paragraph, then
// the lists nested under it: more than one when the kind changes there.
bullet_list: {
- group: 'block',
+ group: 'block flow',
content: 'list_item+',
parseDOM: [{ tag: 'ul' }],
toDOM: () => ['ul', 0]
},
ordered_list: {
- group: 'block',
+ group: 'block flow',
content: 'list_item+',
parseDOM: [{ tag: 'ol' }],
toDOM: () => ['ol', 0]
@@ -103,7 +103,7 @@ const nodes = {
// blockquote: one Mapa quote block. prefix is each line's quote mark
// as typed (">" or "> ", after any spaces).
blockquote: {
- group: 'block',
+ group: 'block flow',
content: 'paragraph',
defining: true,
attrs: { marker: { default: null }, prefix: { default: null } },
@@ -117,7 +117,7 @@ const nodes = {
// the text does not hold it (markup.codeText drops a last line that
// looks like a fence, closing or not), or null. A new block is closed.
code_block: {
- group: 'block',
+ group: 'block flow',
content: 'text*',
marks: '',
code: true,
@@ -133,8 +133,38 @@ const nodes = {
toDOM: () => ['pre', ['code', 0]]
},
- horizontal_rule: {
+ // A column layout (columns and icons spec §1.7): two to four columns,
+ // each holding any block but another layout. fence is the colon count
+ // of the fences as typed; openOrigin and closeOrigin are the side
+ // table's keys for the fence lines, never serialised or drawn.
+ columns: {
group: 'block',
+ content: 'column{2,4}',
+ defining: true,
+ attrs: {
+ fence: { default: 4 },
+ openOrigin: { default: null },
+ closeOrigin: { default: null }
+ },
+ parseDOM: [{ tag: 'div[mapa-columns]' }],
+ toDOM: node => ['div', { 'mapa-columns': String(node.childCount) }, 0]
+ },
+
+ column: {
+ content: 'flow+',
+ isolating: true,
+ defining: true,
+ attrs: {
+ fence: { default: 3 },
+ openOrigin: { default: null },
+ closeOrigin: { default: null }
+ },
+ parseDOM: [{ tag: 'div[mapa-column]' }],
+ toDOM: () => ['div', { 'mapa-column': '' }, 0]
+ },
+
+ horizontal_rule: {
+ group: 'block flow',
attrs: { origin: { default: null }, source: { default: '---' } },
parseDOM: [{ tag: 'hr' }],
toDOM: () => ['hr']