diff --git a/app/components/application_component.rb b/app/components/application_component.rb index ae9839805..ddbb2d3dc 100644 --- a/app/components/application_component.rb +++ b/app/components/application_component.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true class ApplicationComponent < ViewComponent::Base - delegate :link_to_user, :time_ago_in_words_tagged, :format_text, :external_link_to, :tag_class, to: :helpers - delegate :edit_icon, :delete_icon, :undelete_icon, :flag_icon, :upvote_icon, :downvote_icon, :link_icon, :sticky_icon, :unsticky_icon, :hashtag_icon, to: :helpers + delegate :link_to_user, :time_ago_in_words_tagged, :format_text, :external_link_to, :tag_class, :current_page_path, to: :helpers + delegate :edit_icon, :delete_icon, :undelete_icon, :flag_icon, :upvote_icon, :downvote_icon, :link_icon, :sticky_icon, :unsticky_icon, :hashtag_icon, :caret_down_icon, to: :helpers def policy(subject) Pundit.policy!(current_user, subject) diff --git a/app/components/preview_size_menu_component.rb b/app/components/preview_size_menu_component.rb new file mode 100644 index 000000000..2aa27ffaa --- /dev/null +++ b/app/components/preview_size_menu_component.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# The dropdown menu for selecting the thumbnail size. Used on the post index +# page, the upload index page, and the media assets index page. +class PreviewSizeMenuComponent < ApplicationComponent + attr_reader :current_size + + def initialize(current_size:) + @current_size = current_size.to_i + end +end diff --git a/app/components/preview_size_menu_component/preview_size_menu_component.html.erb b/app/components/preview_size_menu_component/preview_size_menu_component.html.erb new file mode 100644 index 000000000..6dda0e254 --- /dev/null +++ b/app/components/preview_size_menu_component/preview_size_menu_component.html.erb @@ -0,0 +1,32 @@ +<%= render PopupMenuComponent.new(classes: "preview-size-menu") do |menu| %> + <% menu.button do %> + Size <%= caret_down_icon %> + <% end %> + + <% menu.item do %> + <%= link_to "Small", current_page_path(size: 150), class: ("font-bold" if current_size == 150), rel: "nofollow" %> + <% end %> + + <% menu.item do %> + <%= link_to "Medium", current_page_path(size: 180), class: ("font-bold" if current_size == 180), rel: "nofollow" %> + <% end %> + + <% menu.item do %> + <%= link_to "Large", current_page_path(size: 225), class: ("font-bold" if current_size == 225), rel: "nofollow" %> + <% end %> + + <%# Mobile only %> + <% menu.item do %> + <%= link_to "Huge", current_page_path(size: 360), class: ["mobile-only", ("font-bold" if current_size == 360)], rel: "nofollow" %> + <% end %> + + <%# Desktop only %> + <% menu.item do %> + <%= link_to "Huge", current_page_path(size: 270), class: ["desktop-only", ("font-bold" if current_size == 270)], rel: "nofollow" %> + <% end %> + + <%# Desktop only %> + <% menu.item do %> + <%= link_to "Gigantic", current_page_path(size: 360), class: ["desktop-only", ("font-bold" if current_size == 360)], rel: "nofollow" %> + <% end %> +<% end %> diff --git a/app/components/preview_size_menu_component/preview_size_menu_component.js b/app/components/preview_size_menu_component/preview_size_menu_component.js new file mode 120000 index 000000000..28d561b8d --- /dev/null +++ b/app/components/preview_size_menu_component/preview_size_menu_component.js @@ -0,0 +1 @@ +../../javascript/src/javascripts/preview_size_menu_component.js \ No newline at end of file diff --git a/app/controllers/media_assets_controller.rb b/app/controllers/media_assets_controller.rb index d1a121daf..9cc05f93b 100644 --- a/app/controllers/media_assets_controller.rb +++ b/app/controllers/media_assets_controller.rb @@ -5,6 +5,8 @@ class MediaAssetsController < ApplicationController def index @limit = params.fetch(:limit, CurrentUser.user.per_page).to_i.clamp(0, PostSets::Post::MAX_PER_PAGE) + @preview_size = params[:size].presence || cookies[:post_preview_size].presence || MediaAssetGalleryComponent::DEFAULT_SIZE + @media_assets = authorize MediaAsset.visible(CurrentUser.user).paginated_search(params, limit: @limit, count_pages: false) @media_assets = @media_assets.includes(:media_metadata, :post) respond_with(@media_assets) diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 826b17c13..aac128285 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -35,6 +35,7 @@ class UploadsController < ApplicationController @defaults[:uploader_id] = params[:user_id] @defaults[:status] = "completed" if request.format.html? @limit = params.fetch(:limit, CurrentUser.user.per_page).to_i.clamp(0, PostSets::Post::MAX_PER_PAGE) + @preview_size = params[:size].presence || cookies[:post_preview_size].presence || MediaAssetGalleryComponent::DEFAULT_SIZE @uploads = authorize Upload.visible(CurrentUser.user).paginated_search(params, limit: @limit, count_pages: true, defaults: @defaults) @uploads = @uploads.includes(:uploader, media_assets: :post, upload_media_assets: { media_asset: :post }) if request.format.html? @@ -44,6 +45,7 @@ class UploadsController < ApplicationController def show @upload = authorize Upload.find(params[:id]) + @preview_size = params[:size].presence || cookies[:post_preview_size].presence || MediaAssetGalleryComponent::DEFAULT_SIZE if request.format.html? && @upload.media_asset_count == 1 && @upload.media_assets.first&.post.present? flash[:notice] = "Duplicate of post ##{@upload.media_assets.first.post.id}" diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 10a04f7d3..76d5d376f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -62,6 +62,10 @@ module ApplicationHelper html.join(" | ").html_safe end + def current_page_path(**params) + url_for(request.query_parameters.merge(params)) + end + def nav_link_to(text, url, **options) klass = options.delete(:class) diff --git a/app/helpers/icon_helper.rb b/app/helpers/icon_helper.rb index 0af00abd8..babe9e5f0 100644 --- a/app/helpers/icon_helper.rb +++ b/app/helpers/icon_helper.rb @@ -177,6 +177,18 @@ module IconHelper svg_icon_tag("multiple-images-icon", "M8,3 C8.55228475,3 9,3.44771525 9,4 L9,9 C9,9.55228475 8.55228475,10 8,10 L3,10 C2.44771525,10 2,9.55228475 2,9 L6,9 C7.1045695,9 8,8.1045695 8,7 L8,3 Z M1,1 L6,1 C6.55228475,1 7,1.44771525 7,2 L7,7 C7,7.55228475 6.55228475,8 6,8 L1,8 C0.44771525,8 0,7.55228475 0,7 L0,2 C0,1.44771525 0.44771525,1 1,1 Z", viewbox: "0 0 9 10", **options) end + def grid_icon(**options) + svg_icon_tag("grid-icon", "M448 32C483.3 32 512 60.65 512 96V416C512 451.3 483.3 480 448 480H64C28.65 480 0 451.3 0 416V96C0 60.65 28.65 32 64 32H448zM152 96H64V160H152V96zM208 160H296V96H208V160zM448 96H360V160H448V96zM64 288H152V224H64V288zM296 224H208V288H296V224zM360 288H448V224H360V288zM152 352H64V416H152V352zM208 416H296V352H208V416zM448 352H360V416H448V352z", viewbox: "0 0 512 512", **options) + end + + def list_icon(**options) + svg_icon_tag("list-icon", "M88 48C101.3 48 112 58.75 112 72V120C112 133.3 101.3 144 88 144H40C26.75 144 16 133.3 16 120V72C16 58.75 26.75 48 40 48H88zM480 64C497.7 64 512 78.33 512 96C512 113.7 497.7 128 480 128H192C174.3 128 160 113.7 160 96C160 78.33 174.3 64 192 64H480zM480 224C497.7 224 512 238.3 512 256C512 273.7 497.7 288 480 288H192C174.3 288 160 273.7 160 256C160 238.3 174.3 224 192 224H480zM480 384C497.7 384 512 398.3 512 416C512 433.7 497.7 448 480 448H192C174.3 448 160 433.7 160 416C160 398.3 174.3 384 192 384H480zM16 232C16 218.7 26.75 208 40 208H88C101.3 208 112 218.7 112 232V280C112 293.3 101.3 304 88 304H40C26.75 304 16 293.3 16 280V232zM88 368C101.3 368 112 378.7 112 392V440C112 453.3 101.3 464 88 464H40C26.75 464 16 453.3 16 440V392C16 378.7 26.75 368 40 368H88z", viewbox: "0 0 512 512", **options) + end + + def table_icon(**options) + svg_icon_tag("table-icon", "M0 96C0 60.65 28.65 32 64 32H448C483.3 32 512 60.65 512 96V416C512 451.3 483.3 480 448 480H64C28.65 480 0 451.3 0 416V96zM64 160H128V96H64V160zM448 96H192V160H448V96zM64 288H128V224H64V288zM448 224H192V288H448V224zM64 416H128V352H64V416zM448 352H192V416H448V352z", viewbox: "0 0 512 512", **options) + end + def globe_icon(**options) icon_tag("fas fa-globe", **options) end diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 090066c8a..2a6f11ba5 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -48,6 +48,7 @@ import Post from "../src/javascripts/posts.js"; import PostModeMenu from "../src/javascripts/post_mode_menu.js"; import PostTooltip from "../src/javascripts/post_tooltips.js"; import PostVotesTooltipComponent from "../src/javascripts/post_votes_tooltip_component.js"; +import PreviewSizeMenuComponent from "../src/javascripts/preview_size_menu_component.js"; import RelatedTag from "../src/javascripts/related_tag.js"; import Shortcuts from "../src/javascripts/shortcuts.js"; import TagCounter from "../src/javascripts/tag_counter.js"; @@ -75,6 +76,7 @@ Danbooru.Post = Post; Danbooru.PostModeMenu = PostModeMenu; Danbooru.PostTooltip = PostTooltip; Danbooru.PostVotesTooltipComponent = PostVotesTooltipComponent; +Danbooru.PreviewSizeMenuComponent = PreviewSizeMenuComponent; Danbooru.RelatedTag = RelatedTag; Danbooru.Shortcuts = Shortcuts; Danbooru.TagCounter = TagCounter; diff --git a/app/javascript/src/javascripts/preview_size_menu_component.js b/app/javascript/src/javascripts/preview_size_menu_component.js new file mode 100644 index 000000000..49fd9253b --- /dev/null +++ b/app/javascript/src/javascripts/preview_size_menu_component.js @@ -0,0 +1,20 @@ +import Cookie from './cookie'; + +export default class PreviewSizeMenuComponent { + static initialize() { + $(document).on("click.danbooru", ".preview-size-menu .popup-menu-content a", e => PreviewSizeMenuComponent.onClick(e)); + } + + static onClick(e) { + let url = new URL($(e.target).get(0).href); + let size = url.searchParams.get("size"); + + Cookie.put("post_preview_size", size); + url.searchParams.delete("size"); + location.replace(url); + + e.preventDefault(); + } +} + +$(PreviewSizeMenuComponent.initialize); diff --git a/app/views/media_assets/_gallery.html.erb b/app/views/media_assets/_gallery.html.erb index cbe436aee..340360cf4 100644 --- a/app/views/media_assets/_gallery.html.erb +++ b/app/views/media_assets/_gallery.html.erb @@ -1,5 +1,5 @@ -<%= render(MediaAssetGalleryComponent.new) do |gallery| %> - <% @media_assets.each do |media_asset| %> +<%= render(MediaAssetGalleryComponent.new(size: size)) do |gallery| %> + <% media_assets.each do |media_asset| %> <% if policy(media_asset).can_see_image? %> <% gallery.media_asset do %> <%= render "media_assets/preview", media_asset: media_asset, size: gallery.size %> diff --git a/app/views/media_assets/index.html.erb b/app/views/media_assets/index.html.erb index 508ffb8a3..8512bd58a 100644 --- a/app/views/media_assets/index.html.erb +++ b/app/views/media_assets/index.html.erb @@ -1,6 +1,9 @@
Error: <%= @upload.error %>.
@@ -12,7 +18,7 @@ <% end %> <% elsif @upload.media_asset_count > 1 %>