enhance: undo redo use patches instead of full text

This commit is contained in:
Tienson Qin
2021-01-07 10:31:14 +08:00
parent 14e4af2957
commit 249bf680c1
10 changed files with 168 additions and 122 deletions

View File

@@ -1,15 +1,34 @@
(ns frontend.diff
(:require [clojure.string :as string]
["diff" :as jsdiff]
["diff-match-patch" :as diff-match-patch]
[goog.object :as gobj]
[lambdaisland.glogi :as log]
[cljs-bean.core :as bean]))
;; TODO: replace with diff-match-patch
(defn diff
[s1 s2]
(-> ((gobj/get jsdiff "diffLines") s1 s2)
bean/->clj))
(defonce dmp (diff-match-patch.))
(defn diffs
[s1 s2]
(.diff_main dmp s1 s2 true))
(defn get-patches
[s1 s2 diffs]
(.patch_make dmp s1 s2 diffs))
(defn apply-patches!
[text patches]
(if (seq patches)
(let [result (.patch_apply dmp patches text)]
(nth result 0))
text))
;; (find-position "** hello _w_" "hello w")
(defn find-position
[markup text]
@@ -32,6 +51,6 @@
:else
(recur r1 t2 (inc i1) i2))))
(catch js/Error e
(log/error :diff/find-position {:error e})
(count markup))))
(catch js/Error e
(log/error :diff/find-position {:error e})
(count markup))))