Skip to content

Commit ebb84f1

Browse files
committed
Add ability to save chosen syntax highlight
1 parent bfd095d commit ebb84f1

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed

app/controllers/settings/syntax_highlights_controller.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@ def show
1212
end
1313

1414
def update
15-
# highlight = params[:syntax_highlight]
16-
# if available_highlight_styles.include?(highlight)
17-
# current_user.update(syntax_highlight: highlight)
18-
# flash[:notice] = "Syntax highlight updated to #{highlight}"
19-
# else
20-
# flash[:alert] = "Invalid syntax highlight choice"
21-
# end
22-
# redirect_to action: :show
23-
24-
syntax_highlight = params.fetch(:settings, {}).permit(:syntax_highlight)
15+
syntax_highlight = params.fetch(:settings, {}).permit(:syntax_highlight_name)[:syntax_highlight_name]
2516
@syntax_highlight = Settings::SyntaxHighlight.find(syntax_highlight)
2617

2718
redirect_to settings_syntax_highlight_path unless @syntax_highlight.present?

app/views/settings/syntax_highlights/form.rb

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def initialize(
1717
@available_highlights = available_highlights
1818
@preview_syntax_highlight = preview_syntax_highlight
1919
@session_syntax_highlight = session_syntax_highlight
20+
@default_syntax_highlight = default_syntax_highlight
2021
end
2122

2223
def view_template
@@ -29,37 +30,28 @@ def view_template
2930
syntax_highlight_preview_name_value: @current_highlight.name
3031
}
3132
) do
33+
h2 { "Want to preview a new syntax highlighting theme?" }
34+
3235
markdown do
33-
"You are currently previewing **#{@current_highlight.display_name}** as your syntax highlighting theme."
36+
"This site uses [Pygments-style CSS](https://pygments.org/) for syntax highlighting. You can use the select menu to preview a new syntax highlighting theme. I have curated over 90 options for you from sources around the web."
3437
end
3538

36-
if preserving?
39+
if previewing?
3740
markdown do
38-
"You have saved **#{@session_highlight_light.display_name}** for syntax highlighting across the site."
41+
"You are currently previewing **#{@current_highlight.display_name}** as your syntax highlighting theme."
3942
end
4043
end
4144

42-
form_with(
43-
model: @settings,
44-
url: settings_syntax_highlight_path,
45-
method: :get
46-
) do |form|
47-
fieldset do
48-
flex_block do
49-
form.label :syntax_highlight_name, "Choose another syntax highlighting theme to preview:"
50-
form.select :syntax_highlight_name,
51-
syntax_highlight_options_for_select,
52-
{
53-
selected: @current_highlight.name
54-
},
55-
onchange: "this.form.requestSubmit()"
56-
end
45+
if preserving?
46+
markdown do
47+
"You have saved **#{@session_syntax_highlight.display_name}** for syntax highlighting across the site."
5748
end
5849
end
50+
preview_select
5951

6052
if previewing?
6153
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"}."
54+
"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, **#{@session_syntax_highlight.display_name}**" : "the default color scheme, **#{@default_syntax_highlight.display_name}**"}."
6355
end
6456

6557
div(class: "outside") { reset_button }
@@ -72,13 +64,14 @@ def view_template
7264

7365
div(class: "outside") do
7466
button_to("Save #{@current_highlight.display_name}",
75-
settings_color_scheme_path(settings: {syntax_highlight_name: @current_highlight.name}),
67+
settings_syntax_highlight_path(settings: {syntax_highlight_name: @current_highlight.name}),
7668
method: :patch,
7769
class: "button primary")
7870
end
7971
end
8072

8173
h2 { %(Preview) }
74+
8275
h3 { %(Ruby) }
8376

8477
render CodeBlock::Article.new(language: "ruby") do |code|
@@ -145,6 +138,26 @@ def m
145138
Markdown::Application.new(yield).call.html_safe
146139
end
147140

141+
def preview_select
142+
form_with(
143+
model: @settings,
144+
url: settings_syntax_highlight_path,
145+
method: :get
146+
) do |form|
147+
fieldset do
148+
flex_block do
149+
form.label :syntax_highlight_name, "Choose another syntax highlighting theme to preview:"
150+
form.select :syntax_highlight_name,
151+
syntax_highlight_options_for_select,
152+
{
153+
selected: @current_highlight.name
154+
},
155+
onchange: "this.form.requestSubmit()"
156+
end
157+
end
158+
end
159+
end
160+
148161
def flex_block(options = {}, &)
149162
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]}", &)
150163
end

app/views/settings/syntax_highlights/show_view.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(
1616
end
1717

1818
def view_template
19-
render Pages::Header.new(title: "Settings: Syntax Highlight")
19+
render Pages::Header.new(title: "Settings: Syntax Highlighting")
2020

2121
section(class: %(secton-content container py-gap)) do
2222
turbo_frame_tag "syntax-highlight-form", data: {turbo_action: "advance"} do

spec/system/settings/syntax_highlights_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
click_link "Syntax Highlighting"
1010

11-
expect(page).to have_content("You are currently previewing Dracula as your syntax highlighting theme")
11+
expect(page).to have_css("link[data-syntax-highlight*=dracula]", visible: false)
12+
1213
expect(page).to have_content("Choose another syntax highlighting theme to preview")
1314

1415
select "Fruity", from: "settings[syntax_highlight_name]"
@@ -17,5 +18,10 @@
1718
expect(page).not_to have_content("currently previewing Dracula")
1819

1920
expect(page).to have_css("link[data-syntax-highlight*=fruity]", visible: false)
21+
22+
click_button "Save Fruity"
23+
24+
expect(page).to have_content("You have saved Fruity for syntax highlighting across the site")
25+
expect(page).to have_css("link[data-syntax-highlight*=fruity]", visible: false)
2026
end
2127
end

0 commit comments

Comments
 (0)