···11+// ok so. we need plain text to parse markup from, but
22+// this is a "rich text" editor with paragraphs and other unimaginable things
33+// so, we have to convert the contents to plain text, but
44+// also need to convert indexes into the plaintext string, BACK
55+// into indexes into the rich text, for applying the decorations
66+export class EditorPlaintext {
77+ constructor(node) {
88+ let text = ""
99+ let decor_index = 0
1010+ // indexes converts an index in our raw string -> index for inline decor
1111+ let indexes = [[0,0]]
1212+ function recurse(node, islast=false) {
1313+ if (node.type.name=='doc') {
1414+ let content = node.content.content
1515+ for (let i=0; i<content.length; i++)
1616+ recurse(content[i], i==content.length-1)
1717+ } else if (node.type.name=='paragraph') {
1818+ let content = node.content.content
1919+ for (let i=0; i<content.length; i++)
2020+ recurse(content[i])
2121+ if (!islast) {
2222+ text += '\n'
2323+ decor_index += 2 // why 2? no idea.
2424+ indexes.push([text.length,decor_index])
2525+ }
2626+ } else if (node.type.name=='hardBreak') {
2727+ text += "\n"
2828+ decor_index += 1 // i assume ?
2929+ indexes.push([text.length,decor_index])
3030+ } else if (node.type.name=='text') {
3131+ text += node.text
3232+ decor_index += node.text.length
3333+ } else if (node.type.name=='mention') {
3434+ text += `@${node.attrs?.id || ''}`
3535+ decor_index += 1 // ?
3636+ indexes.push([text.length,decor_index])
3737+ }
3838+ }
3939+ recurse(node)
4040+ this.text = text
4141+ this.indexes = indexes
4242+ }
4343+ index(index) {
4444+ let i
4545+ for (i=0; i<this.indexes.length; i++) {
4646+ if (this.indexes[i][0]>index)
4747+ break
4848+ }
4949+ let [real_index, decor_index] = this.indexes[i-1]
5050+ let offset = index - real_index
5151+ return decor_index + offset + 1 // egh
5252+ }
5353+}