Skip to content

Commit 1ac46dd

Browse files
committed
add support for embed retain placeholder string
1 parent b8e5d2c commit 1ac46dd

4 files changed

Lines changed: 120 additions & 3 deletions

File tree

lib/delta/op.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,19 @@ defmodule Delta.Op do
242242
@spec get_embed_data!(map, map) :: {any, any, any}
243243
def get_embed_data!(a, b) do
244244
cond do
245+
a == "\n" and is_map(b) and map_size(b) == 1 ->
246+
[type] = Map.keys(b)
247+
248+
empty =
249+
case b[type] do
250+
%{} -> %{}
251+
value when is_list(value) -> []
252+
"" <> _ -> ""
253+
_ -> nil
254+
end
255+
256+
{type, empty, b[type]}
257+
245258
not is_map(a) ->
246259
raise("cannot retain #{inspect(a)}")
247260

test/delta/delta/compose_test.exs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ defmodule Tests.Delta.Compose do
298298
end
299299
end
300300

301-
describe ".compose/2 (custom embeds)" do
301+
describe ".compose/2 (custom delta embed)" do
302302
@describetag custom_embeds: [TestEmbed]
303303

304304
test "retain an embed with a number" do
@@ -329,6 +329,22 @@ defmodule Tests.Delta.Compose do
329329
assert Delta.compose(a, b) == expected
330330
end
331331

332+
test "retain newline with an embed" do
333+
a = [Op.insert("\n")]
334+
b = [Op.retain(%{"delta" => [Op.insert("b")]})]
335+
expected = [Op.insert(%{"delta" => [Op.insert("b")]})]
336+
337+
assert Delta.compose(a, b) == expected
338+
end
339+
340+
test "retain string with an embed" do
341+
a = [Op.insert("1\n2")]
342+
b = [Op.retain(1), Op.retain(%{"delta" => [Op.insert("b")]})]
343+
expected = [Op.insert("1"), Op.insert(%{"delta" => [Op.insert("b")]}), Op.insert("2")]
344+
345+
assert Delta.compose(a, b) == expected
346+
end
347+
332348
test "delete a retain" do
333349
a = [Op.retain(%{"delta" => [Op.insert("a")]})]
334350
b = [Op.delete(1)]
@@ -337,4 +353,58 @@ defmodule Tests.Delta.Compose do
337353
assert Delta.compose(a, b) == expected
338354
end
339355
end
356+
357+
describe ".compose/2 (custom map embed)" do
358+
@describetag custom_embeds: [MapEmbed]
359+
360+
test "retain an embed with a number" do
361+
a = [Op.insert(%{"map" => "image.png"})]
362+
b = [Op.retain(1, %{"bold" => true})]
363+
expected = [Op.insert(%{"map" => "image.png"}, %{"bold" => true})]
364+
365+
assert Delta.compose(a, b) == expected
366+
end
367+
368+
test "retain a number with an embed" do
369+
a = [Op.retain(10, %{"bold" => true})]
370+
b = [Op.retain(%{"map" => "image.png"})]
371+
372+
expected = [
373+
Op.retain(%{"map" => "image.png"}, %{"bold" => true}),
374+
Op.retain(9, %{"bold" => true})
375+
]
376+
377+
assert Delta.compose(a, b) == expected
378+
end
379+
380+
test "retain an embed with an embed" do
381+
a = [Op.retain(%{"map" => "image1.png"})]
382+
b = [Op.retain(%{"map" => "image2.png"})]
383+
384+
assert Delta.compose(a, b) == b
385+
end
386+
387+
test "retain newline with an embed" do
388+
a = [Op.insert("\n")]
389+
b = [Op.retain(%{"map" => "image.png"})]
390+
391+
assert Delta.compose(a, b) == [Op.insert(%{"map" => "image.png"})]
392+
end
393+
394+
test "retain string with an embed" do
395+
a = [Op.insert("1\n2")]
396+
b = [Op.retain(1), Op.retain(%{"map" => "image.png"})]
397+
expected = [Op.insert("1"), Op.insert(%{"map" => "image.png"}), Op.insert("2")]
398+
399+
assert Delta.compose(a, b) == expected
400+
end
401+
402+
test "delete a retain" do
403+
a = [Op.retain(%{"map" => "image.png"})]
404+
b = [Op.delete(1)]
405+
expected = [Op.delete(1)]
406+
407+
assert Delta.compose(a, b) == expected
408+
end
409+
end
340410
end

test/support/case.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ defmodule Delta.Support.Case do
55
using do
66
quote do
77
alias Delta
8-
alias Delta.{Op, Attr}
9-
alias Delta.Support.{TestEmbed, QuoteEmbed}
8+
alias Delta.Attr
9+
alias Delta.Op
10+
alias Delta.Support.MapEmbed
11+
alias Delta.Support.QuoteEmbed
12+
alias Delta.Support.TestEmbed
1013
end
1114
end
1215

test/support/map_embed.ex

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
defmodule Delta.Support.MapEmbed do
2+
@moduledoc false
3+
@behaviour Delta.EmbedHandler
4+
5+
@impl true
6+
def name, do: "map"
7+
8+
@impl true
9+
def compose(_a, b, _keep_nil), do: b
10+
11+
@impl true
12+
def transform(a, b, priority?) do
13+
if priority?, do: b, else: a
14+
end
15+
16+
@impl true
17+
def invert(_change, base), do: base
18+
19+
@impl true
20+
def diff(base, other) do
21+
attr_diff = Delta.Attr.diff(base["attributes"], other["attributes"])
22+
23+
diff =
24+
case {base["insert"], other["insert"]} do
25+
{img, img} -> 1
26+
{_base, other_img} -> other_img
27+
end
28+
29+
[Delta.Op.retain(diff, attr_diff)]
30+
end
31+
end

0 commit comments

Comments
 (0)