Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import { CrdtId, LwwValue } from './tagged-block-common.js';import { CrdtSequence, CrdtSequenceItem } from './crdt-sequence.js';import * as si from './scene-items.js';
export function* expandTextItem (item) { if (item.deletedLength > 0) { const chars = Array(item.deletedLength).fill(''); let itemId = item.itemId; let leftId = item.leftId; for (let i = 0; i < chars.length - 1; i++) { const rightId = new CrdtId(itemId.part1, itemId.part2 + 1); yield new CrdtSequenceItem(itemId, leftId, rightId, 1, ''); leftId = itemId; itemId = rightId; } yield new CrdtSequenceItem(itemId, leftId, item.rightId, 1, ''); return; }
if (typeof item.value === 'number') { yield item; return; }
const chars = item.value; if (!chars) { process.stderr.write(`Warning: unexpected empty text item\n`); return; }
let itemId = item.itemId; let leftId = item.leftId; for (let i = 0; i < chars.length - 1; i++) { const rightId = new CrdtId(itemId.part1, itemId.part2 + 1); yield new CrdtSequenceItem(itemId, leftId, rightId, 0, chars[i]); leftId = itemId; itemId = rightId; } yield new CrdtSequenceItem(itemId, leftId, item.rightId, 0, chars[chars.length - 1]);}
export function* expandTextItems (items) { for (const item of items) yield* expandTextItem(item);}
export class CrdtStr { constructor (s = '', i = [], properties = {}) { this.s = s; this.i = i; this.properties = properties; } toString () { return this.s; }}
export class Paragraph { constructor (contents, startId, style = null) { this.contents = contents; this.startId = startId; this.style = style ?? new LwwValue(new CrdtId(0, 0), si.ParagraphStyle.PLAIN); } toString () { return this.contents.map(s => s.s).join(''); }}
export class TextDocument { constructor (contents) { this.contents = contents; }
static fromSceneItem (text) { const charFormats = new Map(); for (const [key, lww] of text.styles) charFormats.set(key, lww.value); const endKey = new CrdtId(0, 0).toKey(); if (!charFormats.has(endKey)) charFormats.set(endKey, si.ParagraphStyle.PLAIN);
const charItems = new CrdtSequence([...expandTextItems(text.items.sequenceItems())]); const keys = [...charItems]; let keyIdx = 0;
const properties = { 'font-weight': 'normal', 'font-style': 'normal' };
function handleFormattingCode (code) { if (code === 1) properties['font-weight'] = 'bold'; else if (code === 2) properties['font-weight'] = 'normal'; if (code === 3) properties['font-style'] = 'italic'; else if (code === 4) properties['font-style'] = 'normal'; }
function parseParagraphContents () { let startId; if (keyIdx < keys.length && charItems.get(keys[keyIdx]) === '\n') { startId = keys[keyIdx++]; } else { startId = new CrdtId(0, 0); } const contents = []; while (keyIdx < keys.length) { const char = charItems.get(keys[keyIdx]); if (typeof char === 'number') { handleFormattingCode(char); } else if (char === '\n') { break; } else { if (!contents.length || JSON.stringify(contents[contents.length - 1].properties) !== JSON.stringify(properties)) { contents.push(new CrdtStr('', [], { ...properties })); } contents[contents.length - 1].s += char; contents[contents.length - 1].i.push(keys[keyIdx]); } keyIdx++; } return [startId, contents]; }
const paragraphs = []; while (keyIdx < keys.length) { const [startId, contents] = parseParagraphContents(); const styleKey = startId.toKey(); const style = text.styles.get(styleKey) ?? new LwwValue(new CrdtId(0, 0), si.ParagraphStyle.PLAIN); paragraphs.push(new Paragraph(contents, startId, style)); }
return new TextDocument(paragraphs); }}