diff --git a/app/controllers/concerns/uploads_gallery_images.rb b/app/controllers/concerns/uploads_gallery_images.rb new file mode 100644 index 000000000..2e67f6e1d --- /dev/null +++ b/app/controllers/concerns/uploads_gallery_images.rb @@ -0,0 +1,21 @@ +# Saves files posted by a page's form (the gallery's no-JavaScript fallback) +# the same way the gallery's drop zone does: through ImageUploadService, so +# upload bandwidth is charged and gallery activity is recorded. +module UploadsGalleryImages + extend ActiveSupport::Concern + + private + + # Returns the error message for each file that couldn't be saved. + def upload_gallery_images(content, files) + Array(files).compact_blank.filter_map do |file| + result = ImageUploadService.upload(user: current_user, content: content, file: file) + if result.success? + GalleryActivity.record!(content, user: current_user, action: :image_added, image: result.image) + nil + else + result.error + end + end + end +end diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index b1b653e6e..06e4c4de9 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -3,6 +3,8 @@ # TODO: we should probably spin off an Api::ContentController for #api_sort and anything else # api-wise we need class ContentController < ApplicationController + include UploadsGalleryImages + before_action :authenticate_user!, except: [:show, :changelog, :api_sort] \ + Rails.application.config.content_types[:all_non_universe].map { |type| type.name.downcase.pluralize.to_sym } @@ -516,16 +518,7 @@ def upload_files image_uploads_list, content_type, content_id content = content_type.to_s.safe_constantize&.find_by(id: content_id) return if content.nil? - upload_errors = [] - - image_uploads_list.each do |image_data| - result = ImageUploadService.upload(user: current_user, content: content, file: image_data) - if result.success? - GalleryActivity.record!(content, user: current_user, action: :image_added, image: result.image) - else - upload_errors << result.error - end - end + upload_errors = upload_gallery_images(content, image_uploads_list) if upload_errors.any? { |error| error.include?('upload bandwidth') } upload_errors << "Get more" diff --git a/app/controllers/timelines_controller.rb b/app/controllers/timelines_controller.rb index d8b8a570f..cad117fbd 100644 --- a/app/controllers/timelines_controller.rb +++ b/app/controllers/timelines_controller.rb @@ -1,15 +1,16 @@ class TimelinesController < ApplicationController include ApplicationHelper + include UploadsGalleryImages before_action :authenticate_user!, except: [:show] - before_action :set_timeline, only: [:show, :edit, :update, :destroy, :toggle_archive, :toggle_favorite] + before_action :set_timeline, only: [:show, :edit, :update, :destroy, :toggle_archive, :toggle_favorite, :gallery, :cover] before_action :set_navbar_color before_action :set_sidenav_expansion before_action :require_premium_plan, only: [:new, :create] before_action :require_timeline_read_permission, only: [:show] - before_action :require_timeline_edit_permission, only: [:edit, :update] + before_action :require_timeline_edit_permission, only: [:edit, :update, :gallery, :cover] before_action :require_timeline_delete_permission, only: [:destroy] # GET /timelines @@ -83,6 +84,22 @@ def show @page_title = @timeline.name end + # GET /timelines/1/gallery + # The editor's Images window loads this over XHR when it first opens; a + # plain request (no JavaScript) gets it as a page of its own. + def gallery + if request.xhr? + render partial: 'timelines/gallery_panel', locals: { timeline: @timeline } + else + @page_title = "Images for #{@timeline.name.presence || 'Untitled Timeline'}" + end + end + + # GET /timelines/1/cover + def cover + render partial: 'timelines/edit/cover', locals: { timeline: @timeline } + end + # GET /timelines/new def new timeline = current_user.timelines.create(name: '', universe: @universe_scope).reload @@ -149,6 +166,15 @@ def create # PATCH/PUT /timelines/1 def update + # The gallery's no-JavaScript form posts files here; everything else on + # the editor saves over XHR and gets JSON back. + gallery_files = params.dig(:timeline, :image_uploads_attributes)&.values&.map { |attrs| attrs[:src] }&.compact_blank + if gallery_files.present? + errors = upload_gallery_images(@timeline, gallery_files) + flash[:alert] = errors.to_sentence if errors.any? + return redirect_to gallery_timeline_path(@timeline) + end + # Moving a timeline between universes is only for its owner attributes = @timeline.user_id == current_user.id ? timeline_params : timeline_params.except(:universe_id) diff --git a/app/views/content/_hero_header.html.erb b/app/views/content/_hero_header.html.erb index e83c48590..9e9d977ec 100644 --- a/app/views/content/_hero_header.html.erb +++ b/app/views/content/_hero_header.html.erb @@ -4,6 +4,16 @@ can_see_private = user_signed_in? && ContentImageAuthorization.can_manage?(current_user, content) # Shared with every page-banner header; see ImagePresets::BANNER_BOX_CLASSES. hero_classes = local_assigns[:height_classes] || ImagePresets::BANNER_BOX_CLASSES + + # Pages without a ContentSerializer (timelines) pass these directly. + hero_icon = local_assigns[:icon] || serialized_content.class_icon + hero_color = local_assigns[:color] || serialized_content.class_color + hero_title = local_assigns[:title] || content.name + # Optional: an Alpine expression that keeps the title in step with inline renames. + title_x_text = local_assigns[:title_x_text] + # Optional: HTML for the banner's top-right corner (the timeline editor's images button). The + # corner, not the title bar, because the editors' fixed side tabs overlap the title bar. + corner_actions = local_assigns[:corner_actions] %>
<%= content_image_tag content, :banner, @@ -11,6 +21,12 @@ class: "w-full h-full object-cover", alt: "#{content.name} banner image" %> + <% if corner_actions.present? %> +
+ <%= corner_actions %> +
+ <% end %> +
@@ -19,8 +35,8 @@
-
- <%= serialized_content.class_icon %> +
+ <%= hero_icon %>
@@ -28,7 +44,7 @@

- <%= link_to content.name, content %> + <%= link_to hero_title, content, 'x-text': title_x_text %>

diff --git a/app/views/timelines/_gallery_panel.html.erb b/app/views/timelines/_gallery_panel.html.erb new file mode 100644 index 000000000..aa7ae0a2a --- /dev/null +++ b/app/views/timelines/_gallery_panel.html.erb @@ -0,0 +1,6 @@ +<%# The shared content gallery for a timeline. Used by the editor's Images window (loaded over XHR) and timelines#gallery. %> +<%# Locals: timeline %> +<%# The form only matters to the gallery's no-JavaScript upload fallback; timelines#update saves its files. %> +<%= form_for timeline, url: timeline_path(timeline), method: :patch, html: { multipart: true } do |f| %> + <%= render partial: 'content/edit/gallery_panel', locals: { raw_content: timeline, content: timeline, f: f } %> +<% end %> diff --git a/app/views/timelines/edit.html.erb b/app/views/timelines/edit.html.erb index 227906802..238205677 100644 --- a/app/views/timelines/edit.html.erb +++ b/app/views/timelines/edit.html.erb @@ -1,4 +1,13 @@
+ <%# Banner, as on other content editors. Narrows with the side panels like the main column does. %> + <%# Its button opens the Images window; the banner is re-fetched from timelines#cover when that window closes. %> +
+ <%= render 'timelines/edit/cover', timeline: @timeline %> +
+
@@ -127,7 +136,9 @@
-
otherwise can't shrink below its content's widest unbreakable row, %> + <%# which made the editor about 150px wider than a phone screen. %> +
el.offsetParent !== null); + if (focusable.length === 0) return; + + const first = focusable[0]; + const last = focusable[focusable.length - 1]; + if (event.shiftKey && (document.activeElement === first || !modal.contains(document.activeElement))) { + event.preventDefault(); + last.focus(); + } else if (!event.shiftKey && (document.activeElement === last || !modal.contains(document.activeElement))) { + event.preventDefault(); + first.focus(); + } + }, + + // On open: remember what had focus, stop the page scrolling behind, focus the window. + // On close: put everything back. + modalToggled(open, modal) { + if (open) { + this.modalReturnFocus = document.activeElement; + document.body.style.overflow = 'hidden'; + this.$nextTick(() => { + const target = modal.querySelector('[aria-label="Close"]') || modal.querySelector('button, a[href], input'); + target?.focus(); + }); + } else { + document.body.style.overflow = ''; + const returnTo = this.modalReturnFocus; + this.modalReturnFocus = null; + if (returnTo && document.contains(returnTo)) returnTo.focus(); + } + }, + // Initialize method init() { + this.$watch('showGalleryModal', open => this.modalToggled(open, this.$refs.galleryModal)); + this.$watch('showShareModal', open => this.modalToggled(open, this.$refs.shareModal)); + // Initialize sidebar state this.initializeSidebarState(); this.setupResizeListener(); diff --git a/app/views/timelines/edit/_cover.html.erb b/app/views/timelines/edit/_cover.html.erb new file mode 100644 index 000000000..7020fa703 --- /dev/null +++ b/app/views/timelines/edit/_cover.html.erb @@ -0,0 +1,24 @@ +<%# The editor's banner: the same hero header as other content editors, plus a button into the Images window. %> +<%# Locals: timeline %> +<%# Re-rendered from timelines#cover when the Images window closes. The button links to timelines#gallery so it %> +<%# still works without JavaScript; the editor intercepts clicks on [data-open-gallery]. %> +<% has_cover = content_cover_image?(timeline, include_private: true) %> +<% images_button = capture do %> + <%= link_to gallery_timeline_path(timeline), + data: { open_gallery: true }, + class: "inline-flex items-center px-3 py-1.5 rounded-lg bg-white bg-opacity-90 hover:bg-opacity-100 text-sm font-medium text-gray-800 shadow focus:outline-none focus:ring-2 focus:ring-white transition-colors" do %> + <%= has_cover ? 'photo_library' : 'add_photo_alternate' %> + <%= has_cover ? 'Edit images' : 'Add cover image' %> + <% end %> +<% end %> + +<%= render partial: 'content/hero_header', locals: { + content: timeline, + serialized_content: nil, + icon: Timeline.icon, + color: Timeline.color, + title: timeline.name.presence || 'Untitled Timeline', + title_x_text: "timelineName || 'Untitled Timeline'", + height_classes: 'h-32 md:h-40 lg:h-48', + corner_actions: images_button + } %> diff --git a/app/views/timelines/edit/_gallery_modal.html.erb b/app/views/timelines/edit/_gallery_modal.html.erb new file mode 100644 index 000000000..17beee4b7 --- /dev/null +++ b/app/views/timelines/edit/_gallery_modal.html.erb @@ -0,0 +1,60 @@ +<%# Images Modal Partial: the shared content gallery (upload, reorder, set cover) for a timeline. %> +<%# Locals: timeline %> +<%# The gallery is fetched from timelines#gallery the first time the window opens (see openGallery in edit.html.erb). %> +<%# No `transform` on the modal box: the gallery's framing editor is a position:fixed child until it moves itself to . %> + + diff --git a/app/views/timelines/edit/_share_modal.html.erb b/app/views/timelines/edit/_share_modal.html.erb index 349da2481..0f4bd844e 100644 --- a/app/views/timelines/edit/_share_modal.html.erb +++ b/app/views/timelines/edit/_share_modal.html.erb @@ -9,7 +9,12 @@ x-transition:leave="transition ease-in duration-150" x-transition:leave-start="opacity-100" x-transition:leave-end="opacity-0" - class="fixed inset-0 z-50 overflow-y-auto" + x-ref="shareModal" + @keydown="modalKeydown($event, $el, 'share')" + class="fixed inset-0 z-60 overflow-y-auto" + role="dialog" + aria-modal="true" + aria-labelledby="timeline-share-modal-title" style="display: none;">
@@ -22,7 +27,7 @@ share
-

Share Timeline

+

Share Timeline

diff --git a/app/views/timelines/gallery.html.erb b/app/views/timelines/gallery.html.erb new file mode 100644 index 000000000..3c1043b20 --- /dev/null +++ b/app/views/timelines/gallery.html.erb @@ -0,0 +1,24 @@ +<%# Images for a timeline as a page of its own: what the editor's cover slot links to when JavaScript is off. %> +
+
+ <%= link_to edit_timeline_path(@timeline), class: "inline-flex items-center text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 mb-4" do %> + arrow_back + Back to <%= @timeline.name.presence || 'Untitled Timeline' %> + <% end %> + +

+ photo_library + Timeline Images +

+ + <% if flash[:alert].present? %> +
+ <%= flash[:alert] %> +
+ <% end %> + +
+ <%= render 'timelines/gallery_panel', timeline: @timeline %> +
+
+
diff --git a/app/views/timelines/show.html.erb b/app/views/timelines/show.html.erb index 544ccf682..919917214 100644 --- a/app/views/timelines/show.html.erb +++ b/app/views/timelines/show.html.erb @@ -1,3 +1,9 @@ +<% + timeline_description = @timeline.subtitle.presence || @timeline.description.presence || + "A timeline by #{@timeline.user.display_name} on Notebook.ai" + set_meta_tags({ title: @timeline.name.presence || 'Untitled Timeline', + description: timeline_description.truncate(160) }.merge(content_social_meta(@timeline))) +%>
@@ -53,6 +59,15 @@
+ <%# Banner, as on other content pages: the cover, or the stock timeline image without one. %> + <%= render partial: 'content/hero_header', locals: { + content: @timeline, + serialized_content: nil, + icon: Timeline.icon, + color: Timeline.color, + title: @timeline.name.presence || 'Untitled Timeline' + } %> +
diff --git a/config/routes.rb b/config/routes.rb index 6db15013f..f184f67df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -397,6 +397,10 @@ resources :timelines, only: [:index, :show, :new, :update, :edit, :destroy] do get '/tagged/:slug', action: :index, on: :collection, as: :page_tag get 'tag_suggestions', to: 'timelines#tag_suggestions', on: :member + # The image gallery: a full page without JavaScript, the editor's Images window body over XHR + get :gallery, on: :member + # The editor's cover slot, re-fetched after the Images window closes + get :cover, on: :member post :toggle_archive, on: :member post :toggle_favorite, on: :member end diff --git a/test/controllers/timeline_images_test.rb b/test/controllers/timeline_images_test.rb new file mode 100644 index 000000000..7d73351ea --- /dev/null +++ b/test/controllers/timeline_images_test.rb @@ -0,0 +1,139 @@ +require 'test_helper' + +# Adding images to a timeline and seeing them where a timeline is shown: +# the editor's banner and Images window, the no-JavaScript gallery page +# and upload, and the cover on the timeline page and its link previews. +class TimelineImagesTest < ActionDispatch::IntegrationTest + include Devise::Test::IntegrationHelpers + + XHR = { 'X-Requested-With' => 'XMLHttpRequest' }.freeze + + setup do + @owner = users(:one) + @stranger = users(:two) + @timeline = Timeline.create!(name: 'Red Thread', user: @owner, privacy: 'public') + end + + def add_image(privacy: 'public') + result = ImageUploadService.upload(user: @owner, content: @timeline, + file: fixture_file_upload('gallery_test.png', 'image/png'), + privacy: privacy) + assert result.success?, result.error + result.image + end + + test "the editor prompts for a cover image and loads the gallery only when asked" do + sign_in @owner + get edit_timeline_path(@timeline) + assert_response :success + + assert_select "[data-open-gallery][href='#{gallery_timeline_path(@timeline)}']", text: /Add cover image/ + assert_select "img[alt='Red Thread banner image'][src*='card-headers/timelines']" + assert_select "[data-gallery-url='#{gallery_timeline_path(@timeline)}']" + assert_select "[data-controller~=gallery]", count: 0 + end + + test "the editor shows the cover image once there is one" do + add_image + sign_in @owner + get edit_timeline_path(@timeline) + + assert_select "[data-open-gallery]", text: /Edit images/ + assert_select "img[alt='Red Thread banner image']:not([src*='card-headers'])" + end + + test "the Images window gets the timeline's gallery without the page around it" do + add_image + sign_in @owner + get gallery_timeline_path(@timeline), headers: XHR + + assert_response :success + assert_no_match(/ { @timeline.image_uploads.count }, 1 do + patch timeline_path(@timeline), params: { + timeline: { image_uploads_attributes: { '0' => { src: fixture_file_upload('gallery_test.png', 'image/png') } } } + } + end + + assert_redirected_to gallery_timeline_path(@timeline) + assert_operator @owner.reload.upload_bandwidth_kb, :<, bandwidth_before + end + + test "the editor's own saves still get JSON back" do + sign_in @owner + patch timeline_path(@timeline), params: { timeline: { name: 'Renamed' } }, as: :json + + assert_response :success + assert_equal 'Renamed', @timeline.reload.name + end + + test "people who can't edit the timeline can't reach its gallery or upload to it" do + sign_in @stranger + + get gallery_timeline_path(@timeline), headers: XHR + assert_redirected_to root_path + get cover_timeline_path(@timeline), headers: XHR + assert_redirected_to root_path + + assert_no_difference -> { ImageUpload.count } do + patch timeline_path(@timeline), params: { + timeline: { image_uploads_attributes: { '0' => { src: fixture_file_upload('gallery_test.png', 'image/png') } } } + } + end + end + + test "the timeline page shows the cover and uses it for link previews" do + add_image + get timeline_path(@timeline) + + assert_response :success + assert_select "img[alt='Red Thread banner image']:not([src*='card-headers'])" + assert_select "meta[property='og:image']" do |tags| + assert tags.none? { |tag| tag['content'].include?('card-headers/') }, 'og:image should be the uploaded cover' + end + end + + test "without a cover the timeline page shows the stock banner, and visitors don't see private images" do + stock_banner = "img[alt='Red Thread banner image'][src*='card-headers/timelines']" + + get timeline_path(@timeline) + assert_select stock_banner + + add_image(privacy: 'private') + get timeline_path(@timeline) + assert_select stock_banner + + sign_in @owner + get timeline_path(@timeline) + assert_select "img[alt='Red Thread banner image']:not([src*='card-headers'])" + end +end