livebook-dev / kino

Client-driven interactive widgets for Livebook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I update the Collaborative Editor state in a Kino.SmartCell

BrooklinJazz opened this issue · comments

Please let me know if this isn't the preferred channel for "how to" questions. Thank you for all the hard work on Kino! SmartCells have been a game changer!

I have a Kino.SmartCell using the code editor. I'd like to manually set the content of the code editor.
As I understand it, the editor state is being stored in the "code" key in attrs.

@impl true
def init(attrs, ctx) do
  {:ok, ctx,
   editor: [
     attribute: "code",
     language: "elixir",
     default_source: default_source()
   ]}
end

However, I can't seem to update the "code" attr.

@impl true
def to_attrs(ctx) do
  %{"code" => "hello", "test" => "hello"}
end

The "test" attr is set, but not the "code" attr. "code" is still the value set by default_source/0.

def to_source(attrs) do
  IO.inspect(attrs)

  ""
end

The above prints the following, where "" is the string returned by default_source/0.

%{
  "code" => "",
  "test" => "hello"
}

I may fundamentally misunderstand how this all works! I'm still very new to Smart Cells. I would appreciate a pointer in the correct direction, or perhaps I'm doing something very unintended! For context, my goal is to create a "reset" button that resets the contents of the code editor.

Hi @BrooklinJazz! Using the discussions tab in the Livebook GitHub is likely the best place for questions! :)

for this question in particular, I believe it is actually not possible to update the code contents. That’s because doing server code updates is actually incompatible with collaborative editing, as it will cause all clients to go out of sync. You can add some default code snippet though.