From d55a5b21096d10813f7f0aa87739036c265fcbfd Mon Sep 17 00:00:00 2001 From: Mats Estensen Date: Wed, 18 Mar 2026 00:36:36 +0100 Subject: [PATCH 1/6] feat: allow changing default toc view, and disabling view selector --- app/assets/stylesheets/toc.css | 76 ++++++++++++++++++ app/controllers/books_controller.rb | 2 +- .../controllers/toc_view_controller.js | 4 +- app/models/book.rb | 1 + app/views/books/_form.html.erb | 40 ++++++++++ app/views/books/show.html.erb | 28 +++---- ...260317235838_add_view_settings_to_books.rb | 6 ++ db/schema.rb | 78 ++++++++++--------- 8 files changed, 181 insertions(+), 54 deletions(-) create mode 100644 db/migrate/20260317235838_add_view_settings_to_books.rb diff --git a/app/assets/stylesheets/toc.css b/app/assets/stylesheets/toc.css index f55fb934..87a39e35 100644 --- a/app/assets/stylesheets/toc.css +++ b/app/assets/stylesheets/toc.css @@ -248,6 +248,82 @@ --btn-background: var(--color-link); } +/* Forced view states when selector is disabled */ +.toc__container--force-list { + .toc { + display: flex; + flex-direction: column; + row-gap: calc(var(--block-space) * 0.3); + } + + .toc__leaf { + align-items: center; + column-gap: 1ch; + flex-direction: row; + justify-content: center; + row-gap: calc(var(--block-space) * 0.3); + text-align: start; + } + + .toc__title { + flex-direction: row; + flex-grow: 1; + font-size: inherit; + white-space: nowrap; + + &:is(a) { + @media (hover: hover) { + &:hover { + text-decoration-line: underline; + text-decoration-thickness: 0.075em; + } + } + } + + :is(.input) { + field-sizing: content; + min-inline-size: 20ch; + } + + &::after { + border-block-end: 1px dotted var(--title-border-color); + content: ""; + flex-grow: 1; + margin-block-end: 0.25em; + } + + .toc__leaf--section & { + font-weight: 750; + } + } + + .toc__thumbnail { + display: none; + } + + .toc__wordcount { + font-size: 0.8em; + } +} + +.toc__container--force-grid { + .toc__bookmark { + inset: calc(var(--icon-size) * -0.33) 0 auto calc(var(--icon-size) * -0.5); + position: absolute; + } + + .toc__title { + font-weight: 700; + } + + .leaf__delete .btn { + margin-block-end: calc(-0.5 * var(--btn-size)); + margin-inline: auto; + position: relative; + z-index: 1; + } +} + body:not(:has(.toc__leaf)) { .disable-when-empty { cursor: not-allowed; diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index eda26210..62174439 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -67,7 +67,7 @@ def ensure_index_is_not_empty end def book_params - params.require(:book).permit(:title, :subtitle, :author, :cover, :remove_cover, :everyone_access, :theme) + params.require(:book).permit(:title, :subtitle, :author, :cover, :remove_cover, :everyone_access, :theme, :default_view, :allow_view_selector) end def update_accesses(book) diff --git a/app/javascript/controllers/toc_view_controller.js b/app/javascript/controllers/toc_view_controller.js index 5785d370..da0f95b8 100644 --- a/app/javascript/controllers/toc_view_controller.js +++ b/app/javascript/controllers/toc_view_controller.js @@ -3,7 +3,7 @@ import { readCookie, setCookie } from "helpers/cookie_helpers" export default class extends Controller { static targets = [ "switch" ] - static values = { id: String } + static values = { id: String, default: String } connect() { this.#restoreViewPref(this.idValue) @@ -15,7 +15,7 @@ export default class extends Controller { } #restoreViewPref(id) { - const viewType = readCookie(id) || "grid" + const viewType = readCookie(id) || this.defaultValue this.switchTargets.forEach(switchTarget => { switchTarget.checked = switchTarget.dataset.tocViewTypeValue === viewType } diff --git a/app/models/book.rb b/app/models/book.rb index 371b7f11..f719dcc4 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -8,6 +8,7 @@ class Book < ApplicationRecord scope :published, -> { where(published: true) } enum :theme, %w[ black blue green magenta orange violet white ].index_by(&:itself), suffix: true, default: :blue + enum :default_view, %w[ list grid ].index_by(&:itself), suffix: true, default: :grid def press(leafable, leaf_params) leaves.create! leaf_params.merge(leafable: leafable) diff --git a/app/views/books/_form.html.erb b/app/views/books/_form.html.erb index 67780639..0dd7daa8 100644 --- a/app/views/books/_form.html.erb +++ b/app/views/books/_form.html.erb @@ -59,6 +59,46 @@ +
+
+ <%= image_tag "view-grid.svg", aria: { hidden: true }, size: 36, class: "colorize--black" %> + +
+
Default view
+
+ + + +
+ <% Book.default_views.keys.each do |view| %> + + <% end %> +
+
+ +
+ +
+ <%= image_tag "settings.svg", aria: { hidden: true }, size: 36, class: "colorize--black" %> + +
+
View selector
+
+ + + + +
+
+
<%= image_tag "eye.svg", aria: { hidden: true }, size: 36, class: "colorize--black" %> diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index b90644d1..31f7399b 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -73,25 +73,27 @@
- <%= arrangement_tag @book, class: "arrangement__container toc__container full-width txt-align-center" do %> + <%= arrangement_tag @book, class: "arrangement__container toc__container full-width txt-align-center #{'toc__container--force-' + @book.default_view unless @book.allow_view_selector?}" do %>

<%= @book.title %> <%= @book.subtitle %> <%= @book.author %>

-
" data-controller="toc-view" data-toc-view-id-value="<%= dom_id(@book) %>"> - - - +
" data-controller="toc-view" data-toc-view-id-value="<%= dom_id(@book) %>" data-toc-view-default-value="<%= @book.default_view %>"> + <% if @book.allow_view_selector? %> + + + + <% end %> <% if @book.editable? %> <%= render "books/create_buttons", book: @book %> diff --git a/db/migrate/20260317235838_add_view_settings_to_books.rb b/db/migrate/20260317235838_add_view_settings_to_books.rb new file mode 100644 index 00000000..47cb681f --- /dev/null +++ b/db/migrate/20260317235838_add_view_settings_to_books.rb @@ -0,0 +1,6 @@ +class AddViewSettingsToBooks < ActiveRecord::Migration[8.0] + def change + add_column :books, :default_view, :string, default: "grid", null: false + add_column :books, :allow_view_selector, :boolean, default: true, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index e0331993..cf0b7839 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,42 +10,42 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2024_09_28_005927) do +ActiveRecord::Schema[8.2].define(version: 2026_03_17_235838) do create_table "accesses", force: :cascade do |t| - t.integer "user_id", null: false t.integer "book_id", null: false - t.string "level", null: false t.datetime "created_at", null: false + t.string "level", null: false t.datetime "updated_at", null: false + t.integer "user_id", null: false t.index ["book_id"], name: "index_accesses_on_book_id" t.index ["user_id", "book_id"], name: "index_accesses_on_user_id_and_book_id", unique: true t.index ["user_id"], name: "index_accesses_on_user_id" end create_table "accounts", force: :cascade do |t| - t.string "name", null: false - t.string "join_code", null: false t.datetime "created_at", null: false - t.datetime "updated_at", null: false t.text "custom_styles" + t.string "join_code", null: false + t.string "name", null: false + t.datetime "updated_at", null: false end create_table "action_text_markdowns", force: :cascade do |t| - t.string "record_type", null: false - t.integer "record_id", null: false - t.string "name", null: false t.text "content", default: "", null: false t.datetime "created_at", null: false + t.string "name", null: false + t.integer "record_id", null: false + t.string "record_type", null: false t.datetime "updated_at", null: false t.index ["record_type", "record_id"], name: "index_action_text_markdowns_on_record" end create_table "active_storage_attachments", force: :cascade do |t| - t.string "name", null: false - t.string "record_type", null: false - t.bigint "record_id", null: false t.bigint "blob_id", null: false t.datetime "created_at", null: false + t.string "name", null: false + t.bigint "record_id", null: false + t.string "record_type", null: false t.string "slug" t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true @@ -53,14 +53,14 @@ end create_table "active_storage_blobs", force: :cascade do |t| - t.string "key", null: false - t.string "filename", null: false - t.string "content_type" - t.text "metadata" - t.string "service_name", null: false t.bigint "byte_size", null: false t.string "checksum" + t.string "content_type" t.datetime "created_at", null: false + t.string "filename", null: false + t.string "key", null: false + t.text "metadata" + t.string "service_name", null: false t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true end @@ -71,24 +71,26 @@ end create_table "books", force: :cascade do |t| - t.string "title", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "subtitle" + t.boolean "allow_view_selector", default: true, null: false t.string "author" + t.datetime "created_at", null: false + t.string "default_view", default: "grid", null: false + t.boolean "everyone_access", default: true, null: false t.boolean "published", default: false, null: false t.string "slug", null: false - t.boolean "everyone_access", default: true, null: false + t.string "subtitle" t.string "theme", default: "blue", null: false + t.string "title", null: false + t.datetime "updated_at", null: false t.index ["published"], name: "index_books_on_published" end create_table "edits", force: :cascade do |t| - t.integer "leaf_id", null: false - t.string "leafable_type", null: false - t.integer "leafable_id", null: false t.string "action", null: false t.datetime "created_at", null: false + t.integer "leaf_id", null: false + t.integer "leafable_id", null: false + t.string "leafable_type", null: false t.datetime "updated_at", null: false t.index ["leaf_id"], name: "index_edits_on_leaf_id" t.index ["leafable_type", "leafable_id"], name: "index_edits_on_leafable" @@ -96,13 +98,13 @@ create_table "leaves", force: :cascade do |t| t.integer "book_id", null: false - t.string "leafable_type", null: false + t.datetime "created_at", null: false t.integer "leafable_id", null: false + t.string "leafable_type", null: false t.float "position_score", null: false t.string "status", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false t.string "title", null: false + t.datetime "updated_at", null: false t.index ["book_id"], name: "index_leaves_on_book_id" t.index ["leafable_type", "leafable_id"], name: "index_leafs_on_leafable" end @@ -113,37 +115,37 @@ end create_table "pictures", force: :cascade do |t| + t.string "caption" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "caption" end create_table "sections", force: :cascade do |t| + t.text "body" t.datetime "created_at", null: false - t.datetime "updated_at", null: false t.string "theme" - t.text "body" + t.datetime "updated_at", null: false end create_table "sessions", force: :cascade do |t| - t.integer "user_id", null: false - t.string "token", null: false + t.datetime "created_at", null: false t.string "ip_address" - t.string "user_agent" t.datetime "last_active_at", null: false - t.datetime "created_at", null: false + t.string "token", null: false t.datetime "updated_at", null: false + t.string "user_agent" + t.integer "user_id", null: false t.index ["token"], name: "index_sessions_on_token", unique: true t.index ["user_id"], name: "index_sessions_on_user_id" end create_table "users", force: :cascade do |t| - t.string "name", null: false + t.boolean "active", default: true + t.datetime "created_at", null: false t.string "email_address", null: false + t.string "name", null: false t.string "password_digest", null: false t.integer "role", null: false - t.boolean "active", default: true - t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email_address"], name: "index_users_on_email_address", unique: true t.index ["name"], name: "index_users_on_name", unique: true From e89698eb9e5ade8f4cde5e141c12344784910474 Mon Sep 17 00:00:00 2001 From: Mats Estensen Date: Wed, 18 Mar 2026 19:29:07 +0100 Subject: [PATCH 2/6] feat: add tests for default_view and allow_view_selector --- test/controllers/books_controller_test.rb | 45 +++++++++++++++++++++++ test/models/book_test.rb | 11 ++++++ 2 files changed, 56 insertions(+) diff --git a/test/controllers/books_controller_test.rb b/test/controllers/books_controller_test.rb index 81d5f486..3a900348 100644 --- a/test/controllers/books_controller_test.rb +++ b/test/controllers/books_controller_test.rb @@ -114,4 +114,49 @@ class BooksControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_select "link[rel=\"alternate\"][type=\"text/markdown\"][href=\"#{book_slug_path(books(:handbook), format: :md)}\"]" end + + test "update persists view settings" do + book = books(:handbook) + + patch book_path(book), params: { book: { default_view: "list", allow_view_selector: false } } + + assert_redirected_to book_slug_path(book) + book.reload + assert_equal "list", book.default_view + assert_not book.allow_view_selector? + end + + test "show hides view selector when disabled" do + book = books(:handbook) + book.update!(allow_view_selector: false) + + get book_slug_path(book) + + assert_response :success + assert_not_in_body "List view" + assert_not_in_body "Page view" + assert_not_in_body "view-list" + assert_not_in_body "view-grid" + end + + test "show applies forced view class when selector is disabled" do + book = books(:handbook) + book.update!(allow_view_selector: false, default_view: "list") + + get book_slug_path(book) + + assert_response :success + assert_in_body "toc__container--force-list" + end + + test "show shows view selector when enabled" do + book = books(:handbook) + book.update!(allow_view_selector: true) + + get book_slug_path(book) + + assert_response :success + assert_in_body "List view" + assert_in_body "Page view" + end end diff --git a/test/models/book_test.rb b/test/models/book_test.rb index be0e37d2..db2d8567 100644 --- a/test/models/book_test.rb +++ b/test/models/book_test.rb @@ -40,4 +40,15 @@ class BookTest < ActiveSupport::TestCase test "markable returns empty string for book with no leaves" do assert_equal "", books(:manual).markable end + + test "default_view defaults to grid" do + book = Book.create!(title: "Test Book") + assert_equal "grid", book.default_view + assert book.grid_default_view? + end + + test "allow_view_selector defaults to true" do + book = Book.create!(title: "Test Book") + assert book.allow_view_selector? + end end From 1d2b9c19731c839170a372e0dd506840214122c8 Mon Sep 17 00:00:00 2001 From: Mats Estensen Date: Wed, 18 Mar 2026 19:55:23 +0100 Subject: [PATCH 3/6] fix: reduce css duplication Review suggestion by Copilot Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- app/assets/stylesheets/toc.css | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/app/assets/stylesheets/toc.css b/app/assets/stylesheets/toc.css index 87a39e35..52ab727a 100644 --- a/app/assets/stylesheets/toc.css +++ b/app/assets/stylesheets/toc.css @@ -20,7 +20,7 @@ outline: none; } - .toc__container:where(:has(#toc-list:checked)) & { + .toc__container:where(:has(#toc-list:checked), .toc__container--force-list) & { display: flex; flex-direction: column; row-gap: calc(var(--block-space) * 0.3); @@ -250,12 +250,6 @@ /* Forced view states when selector is disabled */ .toc__container--force-list { - .toc { - display: flex; - flex-direction: column; - row-gap: calc(var(--block-space) * 0.3); - } - .toc__leaf { align-items: center; column-gap: 1ch; From 343da20eec58cb313672ead1033c885b3098739d Mon Sep 17 00:00:00 2001 From: Mats Estensen Date: Wed, 18 Mar 2026 20:03:38 +0100 Subject: [PATCH 4/6] fix: still set initial checked state in html based on @book.default_view Review suggestion by copilot Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- app/views/books/show.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 31f7399b..b2fc57e7 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -83,13 +83,13 @@
" data-controller="toc-view" data-toc-view-id-value="<%= dom_id(@book) %>" data-toc-view-default-value="<%= @book.default_view %>"> <% if @book.allow_view_selector? %> From 07da2c0859440cc40b0aa08a0bb491611e6ea66c Mon Sep 17 00:00:00 2001 From: Mats Estensen Date: Wed, 18 Mar 2026 20:07:40 +0100 Subject: [PATCH 5/6] fix: reduce css duplication --- app/assets/stylesheets/toc.css | 84 +++------------------------------- 1 file changed, 7 insertions(+), 77 deletions(-) diff --git a/app/assets/stylesheets/toc.css b/app/assets/stylesheets/toc.css index 52ab727a..44674ae2 100644 --- a/app/assets/stylesheets/toc.css +++ b/app/assets/stylesheets/toc.css @@ -50,7 +50,7 @@ display: inline-flex; } - .toc__container:where(:has(#toc-grid:checked)) & { + .toc__container:where(:has(#toc-grid:checked), .toc__container--force-grid) & { inset: calc(var(--icon-size) * -0.33) 0 auto calc(var(--icon-size) * -0.5); position: absolute; } @@ -85,7 +85,7 @@ scroll-margin-top: 8em; text-align: center; - .toc__container:where(:has(#toc-list:checked)) & { + .toc__container:where(:has(#toc-list:checked), .toc__container--force-list) & { align-items: center; column-gap: 1ch; flex-direction: row; @@ -107,12 +107,12 @@ text-decoration: none; .leaf--section &, - .toc__container:where(:has(#toc-grid:checked)) & { + .toc__container:where(:has(#toc-grid:checked), .toc__container--force-grid) & { font-weight: 700; } #sidebar &, - .toc__container:where(:has(#toc-list:checked)) & { + .toc__container:where(:has(#toc-list:checked), .toc__container--force-list) & { flex-direction: row; flex-grow: 1; font-size: inherit; @@ -194,7 +194,7 @@ } } - .toc__container:where(:has(#toc-list:checked)) & { + .toc__container:where(:has(#toc-list:checked), .toc__container--force-list) & { display: none; } @@ -211,7 +211,7 @@ .toc__wordcount { white-space: nowrap; - .toc__container:where(:has(#toc-list:checked)) & { + .toc__container:where(:has(#toc-list:checked), .toc__container--force-list) & { font-size: 0.8em; } } @@ -232,7 +232,7 @@ } } - .toc__container:where(:has(#toc-grid:checked)) & .btn { + .toc__container:where(:has(#toc-grid:checked), .toc__container--force-grid) & .btn { margin-block-end: calc(-0.5 * var(--btn-size)); margin-inline: auto; position: relative; @@ -248,76 +248,6 @@ --btn-background: var(--color-link); } -/* Forced view states when selector is disabled */ -.toc__container--force-list { - .toc__leaf { - align-items: center; - column-gap: 1ch; - flex-direction: row; - justify-content: center; - row-gap: calc(var(--block-space) * 0.3); - text-align: start; - } - - .toc__title { - flex-direction: row; - flex-grow: 1; - font-size: inherit; - white-space: nowrap; - - &:is(a) { - @media (hover: hover) { - &:hover { - text-decoration-line: underline; - text-decoration-thickness: 0.075em; - } - } - } - - :is(.input) { - field-sizing: content; - min-inline-size: 20ch; - } - - &::after { - border-block-end: 1px dotted var(--title-border-color); - content: ""; - flex-grow: 1; - margin-block-end: 0.25em; - } - - .toc__leaf--section & { - font-weight: 750; - } - } - - .toc__thumbnail { - display: none; - } - - .toc__wordcount { - font-size: 0.8em; - } -} - -.toc__container--force-grid { - .toc__bookmark { - inset: calc(var(--icon-size) * -0.33) 0 auto calc(var(--icon-size) * -0.5); - position: absolute; - } - - .toc__title { - font-weight: 700; - } - - .leaf__delete .btn { - margin-block-end: calc(-0.5 * var(--btn-size)); - margin-inline: auto; - position: relative; - z-index: 1; - } -} - body:not(:has(.toc__leaf)) { .disable-when-empty { cursor: not-allowed; From 56e22d6bd38de3beeb41630eda9c41e4b6149ed8 Mon Sep 17 00:00:00 2001 From: Mats Estensen Date: Wed, 18 Mar 2026 20:11:27 +0100 Subject: [PATCH 6/6] fix: match brackets --- app/javascript/controllers/toc_view_controller.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/javascript/controllers/toc_view_controller.js b/app/javascript/controllers/toc_view_controller.js index da0f95b8..4545bf97 100644 --- a/app/javascript/controllers/toc_view_controller.js +++ b/app/javascript/controllers/toc_view_controller.js @@ -18,6 +18,6 @@ export default class extends Controller { const viewType = readCookie(id) || this.defaultValue this.switchTargets.forEach(switchTarget => { switchTarget.checked = switchTarget.dataset.tocViewTypeValue === viewType - } - )} + }) + } }