Skip to content

Commit 06078bb

Browse files
authored
fix: handle string keys attrs (#1)
1 parent fad4c36 commit 06078bb

File tree

8 files changed

+63
-18
lines changed

8 files changed

+63
-18
lines changed

assets/shared/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export function deserializeData(data) {
77
try {
88
const json = JSON.parse(data);
99
return restore(json);
10-
} catch (_e) {
10+
} catch (e) {
11+
console.log("Failed to deserialize data", e);
1112
return {};
1213
}
1314
} else {

lib/assets/smart_cell/build/main.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/assets/static/build/main.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/kino/excalidraw/embedded.ex

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
defmodule Kino.Excalidraw.Embedded do
2+
@moduledoc """
3+
An Excalidraw component that embeds the data directly into the livebook page.
4+
5+
See options in `Kino.Excalidraw.Options`.
6+
"""
7+
28
use Kino.JS, assets_path: "lib/assets/static/build"
39

410
use TypedStructor
511

6-
alias KinoExcalidraw.Options
12+
alias Kino.Excalidraw.Options
713

814
typed_structor do
915
field :data, binary(), enforce: true
1016
field :options, Options.t(), default: %{}
1117
end
1218

19+
@spec new(attrs :: Enumerable.t()) :: Kino.JS.t()
1320
def new(attrs \\ []) do
1421
cell =
1522
__MODULE__

lib/kino_excalidraw/options.ex lib/kino/excalidraw/options.ex

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
defmodule KinoExcalidraw.Options do
1+
defmodule Kino.Excalidraw.Options do
2+
@moduledoc """
3+
The options for configuring the Excalidraw editor.
4+
5+
6+
| Option | Description | Default |
7+
|--------|-------------| ------- |
8+
| `:height` | The height of the editor in pixels. | `600` |
9+
| `:scroll_to_content` | Whether to scroll to the content when the editor is loaded. | `true` |
10+
| `:view_mode_enabled` | Whether the view mode is enabled. | `false` |
11+
| `:zen_mode_enabled` | Whether the zen mode is enabled. | `false` |
12+
| `:grid_mode_enabled` | Whether the grid mode is enabled. | `false` |
13+
"""
14+
215
@type t() :: %{
316
optional(:height) => pos_integer(),
417
optional(:scroll_to_content) => boolean(),

lib/kino/excalidraw/remote.ex

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
defmodule Kino.Excalidraw.Remote do
2+
@moduledoc """
3+
An Excalidraw component that fetches the data from a remote URL.
4+
5+
See options in `Kino.Excalidraw.Options`.
6+
"""
7+
28
use Kino.JS, assets_path: "lib/assets/static/build"
39

410
use TypedStructor
511

6-
alias KinoExcalidraw.Options
12+
alias Kino.Excalidraw.Options
713

814
typed_structor do
915
field :url, URI.t(), enforce: true
1016
field :options, Options.t(), default: %{}
1117
end
1218

19+
@spec new(attrs :: Enumerable.t()) :: Kino.JS.t()
1320
def new(attrs \\ []) do
1421
cell =
1522
__MODULE__

lib/kino_excalidraw/smart_cell.ex

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,53 @@
11
defmodule KinoExcalidraw.SmartCell do
2+
@moduledoc """
3+
A SmartCell for embedding and editing Excalidraw diagrams in Livebook notebooks.
4+
This SmartCell allows you to create and modify Excalidraw graphs, and configure various editor options.
5+
6+
See options in `Kino.Excalidraw.Options`.
7+
"""
8+
29
use Kino.JS, assets_path: "lib/assets/smart_cell/build"
310
use Kino.JS.Live
411
use Kino.SmartCell, name: "Excalidraw"
512

613
use TypedStructor
714

8-
alias KinoExcalidraw.Options
15+
alias Kino.Excalidraw.Options
916

1017
typed_structor do
1118
field :variable, binary(), default: "excalidraw"
1219
field :data, binary()
1320
field :options, Options.t(), default: %{}
1421
end
1522

23+
@spec new(attrs :: Enumerable.t()) :: __MODULE__.t()
1624
def new(attrs \\ []) do
1725
__MODULE__
18-
|> struct(attrs)
26+
|> struct(atomize_attrs(attrs))
1927
|> Map.update!(:options, &Options.build/1)
2028
|> Map.update!(
2129
:variable,
2230
&Kino.SmartCell.prefixed_var_name("excalidraw", &1)
2331
)
2432
end
2533

34+
defp atomize_attrs(attrs) do
35+
Enum.map(attrs, fn
36+
{key, value} when is_atom(key) -> {key, value}
37+
{"variable", variable} -> {:variable, variable}
38+
{"data", data} -> {:data, data}
39+
{"options", options} -> {:options, options}
40+
end)
41+
end
42+
2643
@impl true
2744
def init(attrs, ctx) do
2845
{:ok, assign(ctx, cell: new(attrs))}
2946
end
3047

3148
@impl true
3249
def handle_connect(ctx) do
33-
{:ok, to_json(ctx.assigns.cell), ctx}
50+
{:ok, Map.take(ctx.assigns.cell, [:variable, :data, :options]), ctx}
3451
end
3552

3653
@impl true
@@ -69,14 +86,18 @@ defmodule KinoExcalidraw.SmartCell do
6986

7087
@impl true
7188
def to_attrs(ctx) do
72-
to_json(ctx.assigns.cell)
89+
%{
90+
"variable" => ctx.assigns.cell.variable,
91+
"data" => ctx.assigns.cell.data,
92+
"options" => ctx.assigns.cell.options
93+
}
7394
end
7495

7596
@impl true
7697
def to_source(cell) do
77-
variable = quoted_var(cell.variable)
78-
data = Macro.escape(cell.data)
79-
options = Macro.escape(cell.options)
98+
variable = quoted_var(cell["variable"])
99+
data = Macro.escape(cell["data"])
100+
options = Macro.escape(cell["options"])
80101

81102
quote do
82103
unquote(variable) =
@@ -90,10 +111,6 @@ defmodule KinoExcalidraw.SmartCell do
90111

91112
defp quoted_var(string), do: {String.to_atom(string), [], nil}
92113

93-
defp to_json(%__MODULE__{} = cell) do
94-
Map.take(cell, [:variable, :data, :options])
95-
end
96-
97114
defimpl Kino.Render do
98115
def to_livebook(_cell) do
99116
%{type: :ignored}

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule KinoExcalidraw.MixProject do
22
use Mix.Project
33

4-
@version "0.1.0"
4+
@version "0.2.0"
55
@description "Excalidraw integration with Livebook"
66

77
def project do

0 commit comments

Comments
 (0)