Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjustable preview sizes #2923

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ gem "pg", "~> 1.0" if ENV["DB"] == "postgresql"

gem "alchemy_i18n", git: "https://github.com/AlchemyCMS/alchemy_i18n.git", branch: "main"

gem "sprockets-rails", "< 3.5.0"

group :development, :test do
gem "execjs", "~> 2.9.1"
gem "rubocop", require: false
Expand Down
5 changes: 3 additions & 2 deletions app/assets/stylesheets/alchemy/selects.scss
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ select {
align-items: center;
margin: 0 3 * $default-margin;

label {
margin-right: 2 * $default-margin;
label,
alchemy-icon {
margin-right: var(--spacing-2);
}
}

Expand Down
16 changes: 6 additions & 10 deletions app/helpers/alchemy/admin/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ module Admin
module PagesHelper
include Alchemy::Admin::BaseHelper

# Returns options tags for the screen sizes select in page edit view.
# Returns screen sizes for the preview size select in page edit view.
#
# You can configure the screen sizes in your +config/alchemy/config.yml+.
#
def preview_sizes_for_select
options_for_select([
"auto",
[Alchemy.t("240", scope: "preview_sizes"), 240],
[Alchemy.t("320", scope: "preview_sizes"), 320],
[Alchemy.t("480", scope: "preview_sizes"), 480],
[Alchemy.t("768", scope: "preview_sizes"), 768],
[Alchemy.t("1024", scope: "preview_sizes"), 1024],
[Alchemy.t("1280", scope: "preview_sizes"), 1280]
])
Alchemy::Config.get(:page_preview_sizes).map do |size|
[Alchemy.t(size, scope: "preview_sizes"), size]
end
end

# Renders a label for page's page layout
Expand Down
5 changes: 2 additions & 3 deletions app/javascript/alchemy_admin/components/preview_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ class PreviewWindow extends HTMLIFrameElement {

key("alt+r", () => this.refresh())

// Need to listen with jQuery here because select2 does not emit native events.
$(this.sizeSelect).on("change", (evt) => {
this.sizeSelect.addEventListener("change", (evt) => {
const select = evt.target
const width = select.value

if (width === "auto") {
if (width === "") {
this.style.width = null
} else {
this.resize(width)
Expand Down
10 changes: 6 additions & 4 deletions app/views/alchemy/admin/pages/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@
<% end %>
<div class="toolbar_spacer"></div>
<div class="select_with_label">
<label><%= Alchemy.t(:preview_size) %></label>
<%= select_tag 'preview_size',
preview_sizes_for_select,
class: 'medium', is: 'alchemy-select' %>
<sl-tooltip content="<%= Alchemy.t(:preview_size) %>" placement="top-start">
<%= render_icon(:computer) %>
<%= select_tag "preview_size",
options_for_select(preview_sizes_for_select),
include_blank: Alchemy.t("auto", scope: "preview_sizes") %>
</sl-tooltip>
</div>
<div class="toolbar_spacer"></div>
<% if @preview_urls.many? %>
Expand Down
9 changes: 9 additions & 0 deletions config/alchemy/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,12 @@ format_matchers:

# The layout used for rendering the +alchemy/admin/pages#show+ action.
admin_page_preview_layout: application

# The sizes for the preview size select in the page editor.
page_preview_sizes:
- 360
- 640
- 768
- 1024
- 1280
- 1440
15 changes: 8 additions & 7 deletions config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -572,14 +572,15 @@ en:
please_confirm: "Please confirm"
please_wait: "Please wait"
position_in_text: "Position in text"
preview_size: "Preview size"
preview_size: "Preview Size"
preview_sizes:
"240": "240px (small phone)"
"320": "320px (iPhone)"
"480": "480px (small Tablet)"
"768": "768px (iPad - Portrait)"
"1024": "1024px (iPad - Landscape)"
"1280": "1280px (Desktop)"
"auto": "Auto"
"360": "Phone (360px)"
"640": "Small Tablet (640px)"
"768": "iPad Portrait (768px)"
"1024": "iPad Landscape (1024px)"
"1280": "Laptop (1280px)"
"1440": "Desktop (1440px)"
preview_url: Preview
publish_page_language_not_public: Cannot publish page if language is not public
publish_page_not_allowed: You have not the permission to publish this page
Expand Down
6 changes: 3 additions & 3 deletions lib/alchemy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ def replaced_config_keys

# Alchemy default configuration
def alchemy_config
read_file(File.join(File.dirname(__FILE__), "..", "..", "config/alchemy/config.yml"))
read_file Engine.root.join("config/alchemy/config.yml")
end

# Application specific configuration
def main_app_config
read_file("#{Rails.root}/config/alchemy/config.yml")
read_file Rails.root.join("config/alchemy/config.yml")
end

# Rails Environment specific configuration
def env_specific_config
read_file("#{Rails.root}/config/alchemy/#{Rails.env}.config.yml")
read_file Rails.root.join("config/alchemy/#{Rails.env}.config.yml")
end

# Tries to load yaml file from given path.
Expand Down
9 changes: 8 additions & 1 deletion spec/helpers/alchemy/admin/pages_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
describe Alchemy::Admin::PagesHelper do
describe "#preview_sizes_for_select" do
it "returns a options string of preview screen sizes for select tag" do
expect(helper.preview_sizes_for_select).to include("option", "auto", "240", "320", "480", "768", "1024", "1280")
expect(helper.preview_sizes_for_select).to match_array([
["Phone (360px)", 360],
["Small Tablet (640px)", 640],
["iPad Portrait (768px)", 768],
["iPad Landscape (1024px)", 1024],
["Laptop (1280px)", 1280],
["Desktop (1440px)", 1440]
])
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/libraries/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module Alchemy
end

describe ".main_app_config" do
let(:main_app_config_path) { "#{Rails.root}/config/alchemy/config.yml" }
let(:main_app_config_path) { Rails.root.join("config/alchemy/config.yml") }

it "should call and return .read_file with the correct config path" do
expect(Config).to receive(:read_file).with(main_app_config_path).once.and_return({setting: "true"})
Expand All @@ -100,7 +100,7 @@ module Alchemy
end

describe ".env_specific_config" do
let(:env_specific_config_path) { "#{Rails.root}/config/alchemy/#{Rails.env}.config.yml" }
let(:env_specific_config_path) { Rails.root.join("config/alchemy/#{Rails.env}.config.yml") }

it "should call and return .read_file with the correct config path" do
expect(Config).to receive(:read_file).with(env_specific_config_path).once.and_return({setting: "true"})
Expand Down