Skip to content

Commit 223f755

Browse files
committed
fix(editor): stabilize deletion and undo history
1 parent 02b004e commit 223f755

3 files changed

Lines changed: 92 additions & 54 deletions

File tree

‎clj-e2e/test/logseq/e2e/editor_basic_test.clj‎

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@
938938
(defn- worker-op-logs
939939
[logs op-names]
940940
(->> logs
941-
(filter #(and (string/includes? % "[frontend.db.transact]")
941+
(filter #(and (string/includes? % ":db-worker/outliner-op-perf")
942942
(string/includes? % (str ":op-names " op-names))))))
943943

944944
(defn- editor-input-state
@@ -1058,6 +1058,15 @@
10581058
"Invalid renderer resource UUID"])
10591059
logs)))))
10601060

1061+
(deftest backspace-at-start-removes-pending-block-dom-test
1062+
(b/new-blocks ["source"])
1063+
(b/new-block "pending block")
1064+
(let [block-uuid (.getAttribute (util/get-edit-block-container) "blockid")]
1065+
(util/move-cursor-to-start)
1066+
(k/backspace)
1067+
(is (nil? (ls-api-call! :editor.getBlock block-uuid)))
1068+
(assert/assert-have-count (str "#ls-block-" block-uuid) 0)))
1069+
10611070
(deftest today-queries-render-without-resource-errors
10621071
(let [page (ls-api-call! :editor.createJournalPage
10631072
(str (java.time.Instant/now)))]
@@ -1242,6 +1251,12 @@
12421251
return `${editor.selectionStart}:${editor.selectionEnd}`;
12431252
})()"))
12441253

1254+
(defn- assert-editor-value!
1255+
[value]
1256+
(-> (w/-query util/editor-q)
1257+
assert/assert-that
1258+
(.hasValue value)))
1259+
12451260
(defn- clipboard-text!
12461261
[text]
12471262
(w/eval-js "text => navigator.clipboard.writeText(text)" text))
@@ -1311,37 +1326,37 @@
13111326

13121327
(deftest cursor-boundaries-word-motion-and-kill-test
13131328
(testing "cursor motion follows boundaries and range deletion is one undoable edit"
1314-
(b/new-blocks ["first cursor line" "second cursor line"])
1315-
(k/shift+enter)
1316-
(util/press-seq "continued")
1317-
(let [multiline "second cursor line\ncontinued"
1318-
second-line-start (inc (count "second cursor line"))]
1319-
(is (= multiline (util/get-edit-content)))
1329+
(let [word-modifier (if util/mac? "Alt" "Control")]
1330+
(b/new-blocks ["first cursor line" "second cursor line"])
1331+
(k/shift+enter)
1332+
(util/press-seq "continued")
1333+
(let [multiline "second cursor line\ncontinued"
1334+
second-line-start (inc (count "second cursor line"))]
1335+
(is (= multiline (util/get-edit-content)))
1336+
(k/press "Home")
1337+
(is (= (str second-line-start ":" second-line-start)
1338+
(selection-range)))
1339+
(k/arrow-up)
1340+
(is (= multiline (util/get-edit-content))))
13201341
(k/press "Home")
1321-
(is (= (str second-line-start ":" second-line-start)
1322-
(selection-range)))
1342+
(is (= "0:0" (selection-range)))
13231343
(k/arrow-up)
1324-
(is (= multiline (util/get-edit-content))))
1325-
(k/press "Home")
1326-
(is (= "0:0" (selection-range)))
1327-
(k/arrow-up)
1328-
(b/wait-editor-text "first cursor line")
1329-
(is (= "first cursor line" (util/get-edit-content)))
1330-
(util/move-cursor-to-end)
1331-
(let [before (util/get-edit-content)]
1332-
(k/press "ControlOrMeta+ArrowLeft")
1333-
(is (not= (str (count before) ":" (count before))
1334-
(selection-range))))
1335-
(k/press "ControlOrMeta+Backspace")
1336-
(let [after-kill (util/get-edit-content)]
1337-
(is (not= "first cursor line" after-kill))
1344+
(b/wait-editor-text "first cursor line")
1345+
(is (= "first cursor line" (util/get-edit-content)))
1346+
(util/move-cursor-to-end)
1347+
(let [before (util/get-edit-content)]
1348+
(k/press (str word-modifier "+ArrowLeft"))
1349+
(is (not= (str (count before) ":" (count before))
1350+
(selection-range))))
1351+
(k/press (str word-modifier "+Backspace"))
1352+
(assert-editor-value! "first line")
13381353
(b/undo)
1339-
(is (= "first cursor line" (util/get-edit-content))))
1340-
(k/press "ControlOrMeta+a")
1341-
(k/backspace)
1342-
(is (= "" (util/get-edit-content)))
1343-
(b/undo)
1344-
(is (= "first cursor line" (util/get-edit-content)))))
1354+
(assert-editor-value! "first cursor line")
1355+
(k/press "ControlOrMeta+a")
1356+
(k/backspace)
1357+
(assert-editor-value! "")
1358+
(b/undo)
1359+
(assert-editor-value! "first cursor line"))))
13451360

13461361
(deftest text-format-shortcuts-and-source-roundtrip-test
13471362
(testing "format shortcuts wrap only the selection and rendered text round-trips"

‎deps/outliner/src/logseq/outliner/op/construct.cljc‎

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,18 @@
835835
:always
836836
seq)))
837837

838+
(defn- reverse-inverse-entry-groups
839+
[inverse-entries forward-op-group-sizes]
840+
(loop [entries inverse-entries
841+
sizes forward-op-group-sizes
842+
groups []]
843+
(if-let [group-size (first sizes)]
844+
(let [[group remaining] (split-at group-size entries)]
845+
(recur remaining (next sizes) (conj groups group)))
846+
(mapcat identity (reverse groups)))))
847+
838848
(defn- ^:large-vars/cleanup-todo build-strict-inverse-outliner-ops
839-
[db-before db-after tx-data forward-ops]
849+
[db-before db-after tx-data forward-ops forward-op-group-sizes]
840850
(when (seq forward-ops)
841851
(let [inverse-entries
842852
(mapv (fn [[op args]]
@@ -902,7 +912,7 @@
902912
;; Any missing inverse entry means the whole semantic inverse is incomplete.
903913
;; Use raw reversed tx instead of partially replaying.
904914
(when (every? some? inverse-entries)
905-
(some->> (reverse inverse-entries)
915+
(some->> (reverse-inverse-entry-groups inverse-entries forward-op-group-sizes)
906916
(mapcat #(if (and (sequential? %)
907917
(sequential? (first %)))
908918
%
@@ -1060,27 +1070,33 @@
10601070
(normalize-property-op-entry-ids id ids property-id op args))]
10611071
(or normalized op-entry)))
10621072

1063-
(defn- canonicalize-explicit-outliner-ops
1073+
(defn- canonicalize-explicit-outliner-op-groups
10641074
[db tx-data ops]
10651075
(let [ops' (normalize-op-entries ops)]
10661076
(cond
10671077
(nil? ops')
1068-
nil
1078+
nil
10691079

10701080
(seq ops')
10711081
(->> ops'
1072-
(mapcat (fn [op]
1073-
(let [canonicalized-op (canonicalize-semantic-outliner-op db tx-data op)]
1074-
(if (and (sequential? canonicalized-op)
1075-
(sequential? (first canonicalized-op))
1076-
(keyword? (ffirst canonicalized-op)))
1077-
canonicalized-op
1078-
[canonicalized-op]))))
1082+
(mapv (fn [op]
1083+
(let [canonicalized-op (canonicalize-semantic-outliner-op db tx-data op)]
1084+
(if (and (sequential? canonicalized-op)
1085+
(sequential? (first canonicalized-op))
1086+
(keyword? (ffirst canonicalized-op)))
1087+
(vec canonicalized-op)
1088+
[canonicalized-op]))))
10791089
vec)
10801090

10811091
:else
10821092
nil)))
10831093

1094+
(defn- canonicalize-explicit-outliner-ops
1095+
[db tx-data ops]
1096+
(some->> (canonicalize-explicit-outliner-op-groups db tx-data ops)
1097+
(mapcat identity)
1098+
vec))
1099+
10841100
(defn- patch-inverse-delete-block-ops
10851101
[inverse-outliner-ops forward-outliner-ops]
10861102
(let [forward-insert-ops* (atom (->> forward-outliner-ops
@@ -1123,16 +1139,16 @@
11231139
seq
11241140
vec))
11251141

1126-
(defn- canonicalize-outliner-ops
1142+
(defn- canonicalize-outliner-op-groups
11271143
[db tx-meta tx-data]
11281144
(let [explicit-forward-ops (normalize-op-entries (:db-sync/forward-outliner-ops tx-meta))
11291145
outliner-ops (normalize-op-entries (:outliner-ops tx-meta))]
11301146
(cond
11311147
(seq explicit-forward-ops)
1132-
(canonicalize-explicit-outliner-ops db tx-data explicit-forward-ops)
1148+
(canonicalize-explicit-outliner-op-groups db tx-data explicit-forward-ops)
11331149

11341150
(seq outliner-ops)
1135-
(canonicalize-explicit-outliner-ops db tx-data outliner-ops)
1151+
(canonicalize-explicit-outliner-op-groups db tx-data outliner-ops)
11361152

11371153
:else
11381154
nil)))
@@ -1240,16 +1256,23 @@
12401256

12411257
(defn derive-history-outliner-ops
12421258
[db-before db-after tx-data tx-meta]
1243-
(let [canonical-forward-outliner-ops (patch-forward-delete-block-op-ids
1244-
db-before
1245-
(canonicalize-outliner-ops db-after tx-meta tx-data))
1246-
canonical-forward-outliner-ops (some-> canonical-forward-outliner-ops
1247-
seq
1248-
vec
1249-
(->> (mapv #(normalize-op-entry-ids db-after %))))
1259+
(let [canonical-forward-outliner-op-groups
1260+
(some->> (canonicalize-outliner-op-groups db-after tx-meta tx-data)
1261+
(mapv (fn [group]
1262+
(->> (patch-forward-delete-block-op-ids db-before group)
1263+
(mapv #(normalize-op-entry-ids db-after %))))))
1264+
canonical-forward-outliner-ops (some->> canonical-forward-outliner-op-groups
1265+
(mapcat identity)
1266+
seq
1267+
vec)
12501268
_ (assert-no-stale-numeric-ids! db-after canonical-forward-outliner-ops :forward-outliner-ops)
12511269
forward-outliner-ops canonical-forward-outliner-ops
1252-
built-inverse-outliner-ops (some-> (build-strict-inverse-outliner-ops db-before db-after tx-data forward-outliner-ops)
1270+
forward-op-group-sizes (mapv count canonical-forward-outliner-op-groups)
1271+
built-inverse-outliner-ops (some-> (build-strict-inverse-outliner-ops db-before
1272+
db-after
1273+
tx-data
1274+
forward-outliner-ops
1275+
forward-op-group-sizes)
12531276
seq
12541277
vec
12551278
(->> (mapv #(normalize-op-entry-ids db-before %))))

‎src/main/frontend/handler/editor.cljs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,12 @@
458458
#(assoc % :tab-indent? indent?))))
459459

460460
(defn- backspace-pending-new-block!
461-
[]
461+
[delete-block?]
462462
(when (pending-new-block?)
463463
(state/update-state!
464464
:editor/pending-new-block
465465
(fn [{:keys [typed-text] :as pending}]
466-
(if (seq typed-text)
466+
(if (and (seq typed-text) (not delete-block?))
467467
(assoc pending :typed-text (subs typed-text 0 (dec (count typed-text))))
468468
(assoc pending :delete? true))))))
469469

@@ -3217,7 +3217,7 @@
32173217
(pending-new-block?)
32183218
(do
32193219
(util/stop e)
3220-
(backspace-pending-new-block!))
3220+
(backspace-pending-new-block! (zero? (util/get-selection-start (.-target e)))))
32213221

32223222
(= input element)
32233223
(let [id (state/get-edit-input-id)

0 commit comments

Comments
 (0)