(function(root) { // Conservative three-way merge: each side is treated as one changed range. // Multiple scattered changes may conflict, but are never silently discarded. function merge(base, local, remote) { if (local === remote || remote === base) return local if (local === base) return remote const change = text => { let from = 0, end = 0 while (from < base.length && from < text.length && base[from] === text[from]) from++ while (end < base.length-from && end < text.length-from && base[base.length-1-end] === text[text.length-1-end]) end++ return {from, to:base.length-end, insert:text.slice(from,text.length-end)} } const a = change(local), b = change(remote) // Adjacent/identical insertion points require a decision too. if (!(a.to < b.from || b.to < a.from)) return null const [first,last] = [a,b].sort((x,y) => x.from-y.from) return base.slice(0,first.from)+first.insert+base.slice(first.to,last.from)+last.insert+base.slice(last.to) } function validDraft(draft, now = Date.now()) { return draft?.version === 1 && typeof draft.text === 'string' && typeof draft.baseBody === 'string' && typeof draft.baseRevision === 'string' && ['html','markdown','code','plain'].includes(draft.format) && Number.isFinite(draft.savedAt) && now-draft.savedAt < 7*86400000 && draft.savedAt <= now+60000 } root.PagesEditorCore = {merge,validDraft} })(typeof window === 'undefined' ? globalThis : window)