这是(5年后我知道的)一个关于为什么会发生这种情况的图形解释。事实上,这就是 @osma 所描述的,但以图形方式解释:
当您在 GDocs 中加粗字符串时,您正在将字符串包装到容器中,大概是这样<strong></strong>
但他们可以使用任何其他语法。为了简单起见,我们假设将字符串加粗只需要在单词开头加一个“+”。因此,为了简单起见,文本“lorem ipsum”将变为lorem +ipsum
并不是lorem <strong>ipsum<strong>
1
Both Alice and Bob start with the text "Lorem ipsum"
2
Bob then deletes "ipsum". Notice that he sends the changeset retain(6), delete(5)
to the server. A changeset is essentially a patch, Google probably used this library https://github.com/GerHobbelt/google-diff-match-patch.
3
Now Alice bolds "ipsum" (adding "+"). She sends is the changeset retain(6), insert(+), retain(5)
4
Both changesets are traveling to the server. The server knows nothing about these sets yet.
5
Assuming the worst scenario: Bob's package arrives first and then the word will be deleted. The other scenario is obvious.
6
When Alice's package arrives, it will only add a "+" to the text because what she sent is only a single changeset.
7
Both texts are then broadcasted to the clients. This is the first one.
8
And this is the second one.
9
将这些变更集修补到原始文本中后,您最终会得到“Lorem +”。服务器和所有客户端现在具有相同的文本。这+
符号稍后将被常见的 HTML 清理过程删除,该过程消除了空标签,例如<tag></tag>
,
要测试此演示,请访问:http://operational-transformation.github.io/visualization.html http://operational-transformation.github.io/visualization.html。在那里,您可以在发送/接收文本和包裹时进行操作。