diff --git a/assets/js/session/index.js b/assets/js/session/index.js index 8d63c6b45..2e7433dff 100644 --- a/assets/js/session/index.js +++ b/assets/js/session/index.js @@ -59,6 +59,14 @@ const Session = { handleCellIndicatorsClick(this, event); }); + window.addEventListener( + "phx:page-loading-stop", + () => { + focusCellFromUrl(this); + }, + { once: true } + ); + // DOM setup updateSectionListHighlight(); @@ -282,6 +290,20 @@ function handleCellIndicatorsClick(hook, event) { } } +/** + * Focuses cell based on the given URL. + */ +function focusCellFromUrl(hook) { + const hash = window.location.hash; + + if (hash.startsWith("#cell-")) { + const cellId = hash.replace(/^#cell-/, ""); + if (getCellById(cellId)) { + setFocusedCell(hook, cellId); + } + } +} + /** * Handles the main notebook area being scrolled. */ diff --git a/lib/livebook_web/live/session_live/cell_component.ex b/lib/livebook_web/live/session_live/cell_component.ex index 87a164f35..aad8cb9f7 100644 --- a/lib/livebook_web/live/session_live/cell_component.ex +++ b/lib/livebook_web/live/session_live/cell_component.ex @@ -19,6 +19,7 @@ defmodule LivebookWeb.SessionLive.CellComponent do ~L"""