(delta: InsertDelta)
| 3 | import { deepEquals } from './object' |
| 4 | |
| 5 | export function normalizeInsertDelta(delta: InsertDelta): InsertDelta { |
| 6 | const normalized: InsertDelta = [] |
| 7 | |
| 8 | for (const element of delta) { |
| 9 | if (typeof element.insert === 'string' && element.insert.length === 0) { |
| 10 | continue |
| 11 | } |
| 12 | |
| 13 | const prev = normalized[normalized.length - 1] |
| 14 | if (!prev || typeof prev.insert !== 'string' || typeof element.insert !== 'string') { |
| 15 | normalized.push(element) |
| 16 | continue |
| 17 | } |
| 18 | |
| 19 | const merge = |
| 20 | prev.attributes === element.attributes || |
| 21 | (!prev.attributes === !element.attributes && |
| 22 | deepEquals(prev.attributes ?? {}, element.attributes ?? {})) |
| 23 | |
| 24 | if (merge) { |
| 25 | prev.insert += element.insert |
| 26 | continue |
| 27 | } |
| 28 | |
| 29 | normalized.push(element) |
| 30 | } |
| 31 | |
| 32 | return normalized |
| 33 | } |
| 34 | |
| 35 | export function yTextToInsertDelta(yText: Y.XmlText): InsertDelta { |
| 36 | return normalizeInsertDelta(yText.toDelta()) as InsertDelta |
no test coverage detected
searching dependent graphs…