Skip to content

Commit be2751e

Browse files
committed
Iterate on ux for syntax highlight preview
1 parent efc4e78 commit be2751e

File tree

6 files changed

+108
-18
lines changed

6 files changed

+108
-18
lines changed

app/controllers/concerns/syntax_highlighting.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module SyntaxHighlighting
66
end
77

88
def find_syntax_highlight
9-
@syntax_highlight ||= preview_syntax_highlight || default_syntax_highlight
9+
@syntax_highlight ||= preview_syntax_highlight || session_syntax_highlight || default_syntax_highlight
1010
end
1111

1212
def preview_syntax_highlight_name = params.fetch(:settings, {}).permit(:syntax_highlight_name)[:syntax_highlight_name]

app/controllers/settings/syntax_highlights_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
class Settings::SyntaxHighlightsController < ApplicationController
22
def show
3+
@syntax_highlight = find_syntax_highlight
4+
35
render Settings::SyntaxHighlights::ShowView.new(
4-
settings: Settings.new(syntax_highlight: find_syntax_highlight),
5-
available_highlights: Settings::SyntaxHighlight.curated
6+
settings: Settings.new(syntax_highlight: @syntax_highlight),
7+
available_highlights: Settings::SyntaxHighlight.curated,
8+
preview_syntax_highlight: preview_syntax_highlight,
9+
session_syntax_highlight: session_syntax_highlight,
10+
default_syntax_highlight: default_syntax_highlight
611
)
712
end
813

app/views/color_schemes/form.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ def preserving? = @session_color_scheme.present?
202202

203203
def default_color_scheme? = @color_scheme.id == @default_color_scheme
204204

205-
def markdown(&block)
206-
render Markdown::Application.new(block.call)
205+
def markdown
206+
render Markdown::Application.new(yield)
207207
end
208208

209209
def flex_block(options = {}, &)

app/views/settings/syntax_highlights/form.rb

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,48 @@ class Settings::SyntaxHighlights::Form < ApplicationView
55
include Phlex::Rails::Helpers::ContentFor
66
include Phlex::Rails::Helpers::StylesheetLinkTag
77

8-
def initialize(settings:, available_highlights:)
8+
def initialize(
9+
settings:,
10+
available_highlights: [],
11+
default_syntax_highlight: Settings::SyntaxHighlight.default,
12+
preview_syntax_highlight: nil,
13+
session_syntax_highlight: nil
14+
)
915
@settings = settings
1016
@current_highlight = settings.syntax_highlight
1117
@available_highlights = available_highlights
18+
@preview_syntax_highlight = preview_syntax_highlight
19+
@session_syntax_highlight = session_syntax_highlight
1220
end
1321

1422
def view_template
1523
stylesheet_link_tag @current_highlight.asset_path, data: {syntax_highlight: @current_highlight.name}
1624

17-
div(class: "grid grid-content", data: {controller: "syntax-highlight-preview", syntax_highlight_preview_name_value: @current_highlight.name}) do
18-
p do
19-
plain %(Current syntax highlight style:)
20-
whitespace
21-
strong { @current_highlight.display_name }
25+
div(
26+
class: "grid grid-content",
27+
data: {
28+
controller: "syntax-highlight-preview",
29+
syntax_highlight_preview_name_value: @current_highlight.name
30+
}
31+
) do
32+
markdown do
33+
"You are currently previewing **#{@current_highlight.display_name}** as your syntax highlighting theme."
2234
end
35+
36+
if preserving?
37+
markdown do
38+
"You have saved **#{@session_highlight_light.display_name}** for syntax highlighting across the site."
39+
end
40+
end
41+
2342
form_with(
2443
model: @settings,
2544
url: settings_syntax_highlight_path,
2645
method: :get
2746
) do |form|
2847
fieldset do
2948
flex_block do
30-
form.label :syntax_highlight_name, "Choose a syntax highlight style to preview:"
49+
form.label :syntax_highlight_name, "Choose another syntax highlighting theme to preview:"
3150
form.select :syntax_highlight_name,
3251
syntax_highlight_options_for_select,
3352
{
@@ -38,6 +57,27 @@ def view_template
3857
end
3958
end
4059

60+
if previewing?
61+
markdown do
62+
"You can preview what the site looks with this syntax highlighting theme while you remain on this page. Click the **Reset preview** button to go back to #{@session_syntax_highlight ? "your saved color scheme" : "the default color scheme"}."
63+
end
64+
65+
div(class: "outside") { reset_button }
66+
67+
darkmode_section
68+
69+
markdown do
70+
"Click the Save button to preserve **#{@current_highlight.display_name}** as your new syntax highlighting theme."
71+
end
72+
73+
div(class: "outside") do
74+
button_to("Save #{@current_highlight.display_name}",
75+
settings_color_scheme_path(settings: {syntax_highlight_name: @current_highlight.name}),
76+
method: :patch,
77+
class: "button primary")
78+
end
79+
end
80+
4181
h2 { %(Preview) }
4282
h3 { %(Ruby) }
4383

@@ -76,8 +116,35 @@ def each(&)
76116
end
77117
end
78118

119+
def darkmode_section
120+
noscript do
121+
markdown do
122+
"*Disclaimer: The Light/Dark mode switch is not available without JavaScript enabled.*"
123+
end
124+
end
125+
126+
markdown do
127+
"You can toggle the dark mode switch to see how the syntax highlighting looks in light or dark mode. Choosing **Light Mode** or **Dark Mode** will save in your browser local storage and will persist across page views on your current device. Choose **System Mode** to remove the saved choice and fall back to your system preference."
128+
end
129+
div(class: "outside") do
130+
render "darkmode/switch", enable_description: true, enable_outline: true
131+
end
132+
end
133+
79134
private
80135

136+
def previewing? = @preview_syntax_highlight.present?
137+
138+
def preserving? = @session_syntax_highlight.present?
139+
140+
def markdown
141+
render Markdown::Application.new(yield)
142+
end
143+
144+
def m
145+
Markdown::Application.new(yield).call.html_safe
146+
end
147+
81148
def flex_block(options = {}, &)
82149
div(class: "flex items-start flex-col space-col-4 grid-cols-12 md:items-center md:flex-row md:space-row-4 #{options[:class]}", &)
83150
end
@@ -87,4 +154,10 @@ def syntax_highlight_options_for_select
87154
.group_by { |sh| sh.mode }
88155
.map { |mode, syntaxes| [mode.titleize, syntaxes.map { |sh| [sh.name.titleize, sh.name] }] }
89156
end
157+
158+
def reset_button
159+
link_to "Reset preview",
160+
url_for,
161+
class: "button tertiary"
162+
end
90163
end

app/views/settings/syntax_highlights/show_view.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
class Settings::SyntaxHighlights::ShowView < ApplicationView
22
include Phlex::Rails::Helpers::TurboFrameTag
33

4-
def initialize(settings:, available_highlights:)
4+
def initialize(
5+
settings:,
6+
available_highlights:,
7+
preview_syntax_highlight:,
8+
session_syntax_highlight:,
9+
default_syntax_highlight:
10+
)
511
@settings = settings
612
@available_highlights = available_highlights
13+
@preview_syntax_highlight = preview_syntax_highlight
14+
@session_syntax_highlight = session_syntax_highlight
15+
@default_syntax_highlight = default_syntax_highlight
716
end
817

918
def view_template
@@ -13,7 +22,10 @@ def view_template
1322
turbo_frame_tag "syntax-highlight-form", data: {turbo_action: "advance"} do
1423
render Settings::SyntaxHighlights::Form.new(
1524
settings: @settings,
16-
available_highlights: @available_highlights
25+
available_highlights: @available_highlights,
26+
preview_syntax_highlight: @preview_syntax_highlight,
27+
session_syntax_highlight: @session_syntax_highlight,
28+
default_syntax_highlight: @default_syntax_highlight
1729
)
1830
end
1931
end

spec/system/settings/syntax_highlights_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
click_link "Syntax Highlighting"
1010

11-
expect(page).to have_content("Choose a syntax highlight style to preview")
12-
expect(page).to have_content("Current syntax highlight style: Dracula")
11+
expect(page).to have_content("You are currently previewing Dracula as your syntax highlighting theme")
12+
expect(page).to have_content("Choose another syntax highlighting theme to preview")
1313

1414
select "Fruity", from: "settings[syntax_highlight_name]"
1515

16-
expect(page).to have_content("Current syntax highlight style: Fruity")
17-
expect(page).not_to have_content("Current syntax highlight style: Dracula")
16+
expect(page).to have_content("You are currently previewing Fruity as your syntax highlighting theme")
17+
expect(page).not_to have_content("currently previewing Dracula")
1818

1919
expect(page).to have_css("link[data-syntax-highlight*=fruity]", visible: false)
2020
end

0 commit comments

Comments
 (0)