From 336ae74fb6dff7cb1bc265200f72f9612f573e9b Mon Sep 17 00:00:00 2001 From: Jannik Becher Date: Mon, 13 Mar 2023 22:37:11 +0100 Subject: [PATCH] Add keyboard shortcut for collapse/expand section (#1779) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonatan Kłosko --- assets/css/js_interop.css | 5 +- assets/js/hooks/session.js | 48 +++++++++++++++++++ lib/livebook_web/live/session_live.ex | 10 +++- .../live/session_live/shortcuts_component.ex | 2 + 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/assets/css/js_interop.css b/assets/css/js_interop.css index 144a25881..818f45bee 100644 --- a/assets/css/js_interop.css +++ b/assets/css/js_interop.css @@ -65,7 +65,9 @@ solely client-side operations. [data-el-section-headline]:not(:hover):not([data-js-focused]) [data-el-section-collapse-button], [data-el-section]:not([data-js-collapsed]) [data-el-section-expand-button], -[data-el-section][data-js-collapsed] > [data-el-section-content], +[data-el-session]:not([data-js-code-zen]) + [data-el-section][data-js-collapsed] + > [data-el-section-content], [data-el-section]:not([data-js-collapsed]) > [data-el-section-subheadline-collapsed] { @apply hidden; @@ -276,6 +278,7 @@ solely client-side operations. [data-el-session][data-js-code-zen] [data-el-section-headline], [data-el-session][data-js-code-zen] [data-el-section-subheadline], +[data-el-session][data-js-code-zen] [data-el-section-subheadline-collapsed], [data-el-session][data-js-code-zen] [data-el-cell][data-type="markdown"], [data-el-session][data-js-code-zen] [data-el-actions], [data-el-session][data-js-code-zen] [data-el-insert-buttons] { diff --git a/assets/js/hooks/session.js b/assets/js/hooks/session.js index 17f29f68e..35a9e3b0e 100644 --- a/assets/js/hooks/session.js +++ b/assets/js/hooks/session.js @@ -146,6 +146,11 @@ const Session = { (event) => this.el.toggleAttribute("data-js-no-outputs") ); + this.getElement("section-toggle-collapse-all-button").addEventListener( + "click", + (event) => this.toggleCollapseAllSections() + ); + window.addEventListener( "phx:page-loading-stop", () => { @@ -419,6 +424,10 @@ const Session = { !this.codeZen && this.insertCellAboveFocused("markdown"); } else if (keyBuffer.tryMatch(["z"])) { this.setCodeZen(!this.codeZen); + } else if (keyBuffer.tryMatch(["c"])) { + !this.codeZen && this.toggleCollapseSection(); + } else if (keyBuffer.tryMatch(["C"])) { + !this.codeZen && this.toggleCollapseAllSections(); } } }, @@ -979,6 +988,45 @@ const Session = { } }, + toggleCollapseSection() { + if (this.focusedId) { + const sectionId = this.getSectionIdByFocusableId(this.focusedId); + const section = this.getSectionById(sectionId); + + if (section) { + if (section.hasAttribute("data-js-collapsed")) { + section.removeAttribute("data-js-collapsed"); + } else { + section.setAttribute("data-js-collapsed", ""); + this.setFocusedEl(sectionId, { scroll: true }); + } + } + } + }, + + toggleCollapseAllSections() { + const allCollapsed = this.getSections().every((section) => + section.hasAttribute("data-js-collapsed") + ); + + if (allCollapsed) { + this.getSections().forEach((section) => { + section.removeAttribute("data-js-collapsed"); + }); + } else { + this.getSections().forEach((section) => { + section.setAttribute("data-js-collapsed", ""); + }); + } + if (this.focusedId) { + const focusedSectionId = this.getSectionIdByFocusableId(this.focusedId); + + if (focusedSectionId) { + this.setFocusedEl(focusedSectionId, { scroll: true }); + } + } + }, + // Server event handlers handleCellInserted(cellId) { diff --git a/lib/livebook_web/live/session_live.ex b/lib/livebook_web/live/session_live.ex index 21c837be7..98e2a4de0 100644 --- a/lib/livebook_web/live/session_live.ex +++ b/lib/livebook_web/live/session_live.ex @@ -185,7 +185,7 @@ defmodule LivebookWeb.SessionLive do class="flex flex-col h-full w-full max-w-xs absolute z-30 top-0 left-[64px] overflow-y-auto shadow-xl md:static md:shadow-none bg-gray-50 border-r border-gray-100 px-6 pt-16 md:py-8" data-el-side-panel > -
+
<.sections_list data_view={@data_view} />
@@ -600,6 +600,14 @@ defmodule LivebookWeb.SessionLive do <.remix_icon icon="add-line" class="text-lg align-center" /> New section +
+
""" end diff --git a/lib/livebook_web/live/session_live/shortcuts_component.ex b/lib/livebook_web/live/session_live/shortcuts_component.ex index dfc87c506..9fca0bb2b 100644 --- a/lib/livebook_web/live/session_live/shortcuts_component.ex +++ b/lib/livebook_web/live/session_live/shortcuts_component.ex @@ -92,6 +92,8 @@ defmodule LivebookWeb.SessionLive.ShortcutsComponent do %{seq: ["N"], desc: "Insert Code cell above"}, %{seq: ["M"], desc: "Insert Markdown cell above"}, %{seq: ["z"], desc: "Toggle code zen"}, + %{seq: ["c"], desc: "Expand/collapse section"}, + %{seq: ["C"], desc: "Expand/collapse all sections"}, %{seq: ["d", "d"], desc: "Delete cell", basic: true}, %{seq: ["e", "e"], desc: "Evaluate cell"}, %{seq: ["e", "s"], desc: "Evaluate section"},