mirror of
https://github.com/logseq/logseq.git
synced 2026-04-29 08:26:40 +00:00
enhance: undo redo use patches instead of full text
This commit is contained in:
@@ -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))))
|
||||
|
||||
Reference in New Issue
Block a user