0

我正在开发一个用户向服务器发送文本的应用程序。然后服务器更改文本,并以原始文本和修改后的文本进行响应。

如何表示在 JSON 中删除的文本。(删除线)

例如像这样:

在此处输入图像描述

我想过这样的事情:

包含所有文本的数组,拆分到包含笔划的位置和不包含笔划的位置:

 data={
   textBefore:'this is the first world. this is the second world',
   textAfter:'this is first world. this is second world,
  changes:[
    {'nostroke':'this is th'},
    {'stroe':'efirst world'},
    {'nostroke':'in the third world'}
  ]
}

你有更好的主意吗?

或者像这样:

data={
  textBefore:'this is the first world. this is the second world',
  textAfter:'this is first world. this is second world,
  changes:[
    {from:2,to:22,text:'repace from char 2 to char 22 with this text'},
    {from:2,to:4} // This will only delete 2 chars
    {from:2,to:2,text:'This will only append this text without replae},
  ]
4

4 回答 4

1

我检查了如何google-diff-match-patch在数组中表示它,这就是结果。original text和之间的区别original txet

var x=new diff_match_patch().diff_main('original text','original txet')
JSON.stringify(x)
//Result
"[[0,"original t"],[-1,"e"],[0,"x"],[1,"e"],[0,"t"]]"

解释:

  • 0 两个文本有这个(相同)
  • -1 仅在第一个文本中(已删除)
  • 1 仅在第二个文本中(添加)

源代码:

https://code.google.com/archive/p/google-diff-match-patch/wikis/API.wiki

演示:

https://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html

感谢@Andriy 的链接

于 2016-02-15T13:55:37.010 回答
0

您可以根据需要构建自己的sheme...

可能:

message: {
    text: 'Then the server change the text',
    strikeAt: 16,
    strikeLen: 6,
    otherFunProperty: 'yNot'
}

甚至更好:

message: {
    text: 'Then the server change the text',
    strikes: [ [ 16, 6 ] ],
    bolds: [],
    otherFunProperty: 'yNot'
}

但这只是一种方式...

也许不是更适合您的具体情况。

如果您必须存储某种历史记录,您可以查看diff -ushell 命令(用于patch命令):

--- stringBefore    2016-02-15 11:49:04.493941099 +0100
+++ stringAfter
@@ -16,0 +17,3 @@
+<s>
@@ -22,0 +26,4 @@
+</s>
于 2016-02-15T10:04:19.493 回答
0

您可以<strike></strike>在服务器的响应中使用 html 标记(假设您将单词替换为duperawesome

{
    "old":"My super duper text",
    "new":"My super <strike>duper</strike> awesome text"
}

然后你必须解释它在你的客户端代码中有html。

于 2016-02-15T09:44:28.213 回答
0

您必须在AJAX 成功处理程序的前端(我假设您发送 XMLHttpRequests)或后端(在形成新的和旧的文本字符串之后,但在创建 JSON 响应之前)通过将删除的单词包装到罢工标签中来找到删除的文本。

start发现和刺痛之间的差异end可能非常复杂。但是,我建议您检查存在的 SO 问题:

此外,如果您能够对后端如何处理提交的字符串有所了解,那么服务器执行此类逻辑可能会更容易......

于 2016-02-15T09:56:15.870 回答