Skip to content

Commit

Permalink
Migrate inputs to Kino (#714)
Browse files Browse the repository at this point in the history
* Migrate inputs to Kino

* Update lib/livebook/session/data.ex

Co-authored-by: José Valim <jose.valim@dashbit.co>

* Try parsing numbers as integers

* Garbage collect input values

* Adjust tests

* Remove unused variable

* Fix frame rendering

* Wrap inputs in border depending on its type

* Add textarea

* Reorder

* Update tests

Co-authored-by: José Valim <jose.valim@dashbit.co>
  • Loading branch information
jonatanklosko and josevalim authored Nov 25, 2021
1 parent b6ddf18 commit c2636b8
Show file tree
Hide file tree
Showing 34 changed files with 951 additions and 1,557 deletions.
36 changes: 0 additions & 36 deletions assets/js/cell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,6 @@ const Cell = {
});
}

if (this.props.type === "input") {
const input = getInput(this);

input.addEventListener("blur", (event) => {
// Wait for other handlers to complete and if still in insert
// force focus
setTimeout(() => {
if (this.state.isFocused && this.state.insertMode) {
input.focus();
}
}, 0);
});
}

this._unsubscribeFromNavigationEvents = globalPubSub.subscribe(
"navigation",
(event) => {
Expand Down Expand Up @@ -185,14 +171,6 @@ function getProps(hook) {
};
}

function getInput(hook) {
if (hook.props.type === "input") {
return hook.el.querySelector(`[data-element="input"]`);
} else {
return null;
}
}

/**
* Handles client-side navigation event.
*/
Expand Down Expand Up @@ -231,8 +209,6 @@ function handleElementFocused(hook, focusableId, scroll) {
}

function handleInsertModeChanged(hook, insertMode) {
const input = getInput(hook);

if (hook.state.isFocused && !hook.state.insertMode && insertMode) {
hook.state.insertMode = insertMode;

Expand All @@ -255,24 +231,12 @@ function handleInsertModeChanged(hook, insertMode) {

broadcastSelection(hook);
}

if (input) {
input.focus();
// selectionStart is only supported on text based input
if (input.selectionStart !== null) {
input.selectionStart = input.selectionEnd = input.value.length;
}
}
} else if (hook.state.insertMode && !insertMode) {
hook.state.insertMode = insertMode;

if (hook.state.liveEditor) {
hook.state.liveEditor.blur();
}

if (input) {
input.blur();
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions assets/js/session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,15 @@ function handleDocumentKeyDown(hook, event) {
saveNotebook(hook);
}
} else {
// Ignore inputs and notebook/section title fields
// Ignore keystrokes on input fields
if (isEditableElement(event.target)) {
keyBuffer.reset();

// Use Escape for universal blur
if (key === "Escape") {
event.target.blur();
}

return;
}

Expand Down Expand Up @@ -429,7 +435,7 @@ function handleDocumentMouseDown(hook, event) {
function editableElementClicked(event, element) {
if (element) {
const editableElement = element.querySelector(
`[data-element="editor-container"], [data-element="input"], [data-element="heading"]`
`[data-element="editor-container"], [data-element="heading"]`
);
return editableElement.contains(event.target);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/livebook/evaluator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ defmodule Livebook.Evaluator do
state = put_in(state.contexts[ref], result_context)

Evaluator.IOProxy.flush(state.io_proxy)
Evaluator.IOProxy.clear_input_buffers(state.io_proxy)
Evaluator.IOProxy.clear_input_cache(state.io_proxy)

output = state.formatter.format_response(response)
metadata = %{evaluation_time_ms: evaluation_time_ms}
Expand Down
Loading

0 comments on commit c2636b8

Please sign in to comment.