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 2 つのテキストにこれがある (同じ)
  • -1 最初のテキストのみ (削除)
  • 2番目のテキストのみに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 に答える