From 24327aeda384ca666533429a30ab0a8a348bf875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?E=CC=81ric=20D?= <15271+edas@users.noreply.github.com> Date: Wed, 5 Feb 2020 19:14:45 +0100 Subject: [PATCH] fix: Allows catchup from empty documents Using the jsonTransformer directly introduce a bug for empty documents with only one paragraph. The transformed document has no content (not even the paragraph) and makes the Editor to always when think it is as version=0. This would break the catchup logic. This patch fixes this unique case, creating a correct empty document with an empty paragraph --- src/lib/collab/provider.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib/collab/provider.js b/src/lib/collab/provider.js index 083cf47d..914d39f4 100644 --- a/src/lib/collab/provider.js +++ b/src/lib/collab/provider.js @@ -7,6 +7,24 @@ import get from 'lodash/get' import debounce from 'lodash/debounce' const jsonTransformer = new JSONTransformer() +// Using the jsonTransformer directly introduce a bug for empty documents +// with only one paragraph. The transformed document has no content +// (not even the paragraph) and makes the Editor to always when think +// it is as version=0. This would break the catchup logic. +// This patch fixes this unique case, creating a correct empty document with +// an empty paragraph +const oldEncode = jsonTransformer.encode.bind(jsonTransformer) +jsonTransformer.encode = function(doc) { + const transformed = oldEncode(doc) + if (transformed.content.length === 0 && doc.content.length != 0) { + return { + content: [{ type: 'paragraph', content: [] }], + type: 'doc' + } + } else { + return transformed + } +} /** * The CollabProvider is called directly by the