Focus notebook name after creation (#122)

This commit is contained in:
Jonatan Kłosko 2021-03-29 19:50:46 +02:00 committed by GitHub
parent fdadefae32
commit e335ecb791
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -47,8 +47,6 @@ const Session = {
handleSectionListClick(this, event); handleSectionListClick(this, event);
}); });
updateSectionListHighlight();
getNotebook().addEventListener("scroll", (event) => { getNotebook().addEventListener("scroll", (event) => {
updateSectionListHighlight(); updateSectionListHighlight();
}); });
@ -59,6 +57,11 @@ const Session = {
toggleSectionsPanel(this); toggleSectionsPanel(this);
}); });
// DOM setup
updateSectionListHighlight();
focusNotebookNameIfNew();
// Server events // Server events
this.handleEvent("cell_inserted", ({ cell_id: cellId }) => { this.handleEvent("cell_inserted", ({ cell_id: cellId }) => {
@ -470,6 +473,16 @@ function handleSectionDeleted(hook, sectionId) {
} }
} }
function focusNotebookNameIfNew() {
const sections = getSections();
const nameElement = document.querySelector(`[data-element="notebook-name"]`);
if (sections.length === 0 && nameElement.innerText === "Untitled notebook") {
nameElement.focus();
selectElementContent(nameElement);
}
}
// Helpers // Helpers
function nearbyCellId(cellId, offset) { function nearbyCellId(cellId, offset) {

View file

@ -124,6 +124,7 @@ defmodule LivebookWeb.SessionLive do
<div class="pb-4 mb-6 border-b border-gray-200"> <div class="pb-4 mb-6 border-b border-gray-200">
<h1 class="text-gray-800 font-semibold text-3xl p-1 -ml-1 rounded-lg border border-transparent hover:border-blue-200 focus:border-blue-300" <h1 class="text-gray-800 font-semibold text-3xl p-1 -ml-1 rounded-lg border border-transparent hover:border-blue-200 focus:border-blue-300"
id="notebook-name" id="notebook-name"
data-element="notebook-name"
contenteditable contenteditable
spellcheck="false" spellcheck="false"
phx-blur="set_notebook_name" phx-blur="set_notebook_name"