-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#107] Allow users to create custom themes for their stories
- Loading branch information
Showing
27 changed files
with
756 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
class CustomThemesController < ApplicationController | ||
|
||
def index | ||
if !current_user | ||
flash[:alert] = "You must be logged in to view your custom themes." | ||
return redirect_to new_user_session_path | ||
end | ||
|
||
@custom_themes = current_user.custom_themes | ||
end | ||
|
||
def show | ||
if custom_theme.user != current_user | ||
flash[:alert] = "You can't view that custom theme." | ||
return redirect_to root_url | ||
end | ||
|
||
@adventure = Adventure.find_by_slug!('lemonade-stand') | ||
render layout: 'custom_theme_preview' | ||
end | ||
|
||
def new | ||
if !current_user | ||
flash[:alert] = "You must be logged in to create a custom theme." | ||
return redirect_to new_user_session_path | ||
end | ||
|
||
@custom_theme = CustomTheme.new | ||
end | ||
|
||
def create | ||
if !current_user | ||
flash[:alert] = "You must be logged in to create a custom theme." | ||
return redirect_to new_user_session_path | ||
end | ||
|
||
@custom_theme = CustomTheme.new(custom_theme_params) | ||
custom_theme.user = current_user | ||
|
||
if custom_theme.save | ||
redirect_to custom_theme | ||
else | ||
display_errors(:create) | ||
render :new | ||
end | ||
end | ||
|
||
def edit | ||
if custom_theme.user != current_user | ||
flash[:alert] = "You can't modify that custom theme." | ||
return redirect_to root_url | ||
end | ||
end | ||
|
||
def update | ||
if custom_theme.user != current_user | ||
flash[:alert] = "You can't modify that custom theme." | ||
return redirect_to root_url | ||
end | ||
|
||
if custom_theme.update(custom_theme_params) | ||
redirect_to custom_theme_path(custom_theme) | ||
else | ||
display_errors(:update) | ||
render :edit | ||
end | ||
end | ||
|
||
private | ||
|
||
def custom_theme | ||
@custom_theme ||= CustomTheme.find_by_slug!(params[:id]) | ||
end | ||
helper_method :custom_theme | ||
|
||
def custom_theme_params | ||
params.require(:custom_theme).permit( | ||
:title, | ||
:background_color, | ||
:border_color, | ||
:intro_image, | ||
:scene_image, | ||
:end_image, | ||
:header_font_family, | ||
:header_font_color, | ||
:body_font_family, | ||
:body_font_color, | ||
:choice_font_family, | ||
:choice_font_color, | ||
:button_color, | ||
:button_font_family, | ||
:button_font_color | ||
) | ||
end | ||
|
||
def display_errors(method) | ||
# the slug validation technically "runs" on create, even though it is not a field | ||
if method == :create | ||
custom_theme.errors.delete(:slug) | ||
end | ||
if custom_theme.errors.any? | ||
flash[:alert] = custom_theme.errors.full_messages.join(", ") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const customThemeSection = document.getElementById("custom_theme_section"); | ||
const themeField = (document.getElementById("adventure_theme")) as HTMLSelectElement; | ||
|
||
if (themeField && customThemeSection) { | ||
themeField.addEventListener("change", function() { | ||
if (themeField.value == "custom") { | ||
customThemeSection.classList.remove("no-display"); | ||
} else { | ||
customThemeSection.classList.add("no-display"); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const headerFontField = (document.getElementById("custom_theme_header_font_family")) as HTMLSelectElement; | ||
const bodyFontField = (document.getElementById("custom_theme_body_font_family")) as HTMLSelectElement; | ||
const choiceFontField = (document.getElementById("custom_theme_choice_font_family")) as HTMLSelectElement; | ||
const buttonFontField = (document.getElementById("custom_theme_button_font_family")) as HTMLSelectElement; | ||
|
||
if (headerFontField) { | ||
headerFontField.addEventListener("change", function() { | ||
headerFontField.style.fontFamily = headerFontField.value; | ||
}); | ||
} | ||
|
||
if (bodyFontField) { | ||
bodyFontField.addEventListener("change", function() { | ||
bodyFontField.style.fontFamily = bodyFontField.value; | ||
}); | ||
} | ||
|
||
if (choiceFontField) { | ||
choiceFontField.addEventListener("change", function() { | ||
choiceFontField.style.fontFamily = choiceFontField.value; | ||
}); | ||
} | ||
|
||
if (buttonFontField) { | ||
buttonFontField.addEventListener("change", function() { | ||
buttonFontField.style.fontFamily = buttonFontField.value; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Important: Order matters here. Otherwise our flow chart's layout | ||
// calculations fail | ||
|
||
import '../src/global.css' | ||
import '../src/button.css' | ||
import '../src/modal.css' | ||
|
||
import * as React from 'react' | ||
import * as ReactDOM from 'react-dom' | ||
|
||
import Player from '../src/components/Player' | ||
import { load } from '../src/persistance' | ||
|
||
const slug = SEED.slug | ||
|
||
async function render() { | ||
const content = await load(slug) | ||
const customThemePreview = document.getElementById('custom-theme-preview') | ||
|
||
if (content) { | ||
ReactDOM.render( | ||
<Player | ||
title={SEED.title} | ||
description='This story about a lemonade stand is here to demo your custom theme.' | ||
story={content.story} | ||
meta={content.meta} | ||
portMeta={content.portMeta} | ||
theme='Custom' | ||
isOffline={false} | ||
backButton={true} | ||
debuggable={true} | ||
characterCard={true} | ||
showSource={false} | ||
/>, | ||
customThemePreview | ||
) | ||
} | ||
} | ||
|
||
render() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,3 +69,7 @@ | |
display: block; | ||
color: white; | ||
} | ||
|
||
.no-display { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.CustomThemeForm select { | ||
font-size: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
class CustomTheme < ApplicationRecord | ||
include Sluggable | ||
|
||
FONTS = [ | ||
["American Typewriter", "American Typewriter, serif"], | ||
["Andale Mono", "Andale Mono, monospace"], | ||
["Apple Chancery", "Apple Chancery, cursive"], | ||
["Arial", "Arial, sans-serif"], | ||
["Arial Narrow", "Arial Narrow, sans-serif"], | ||
["Avantgarde", "Avantgarde, TeX Gyre Adventor, URW Gothic L, sans-serif"], | ||
["Blippo", "Blippo, fantasy"], | ||
["Bookman", "Bookman, URW Bookman L, serif"], | ||
["Bradley Hand", "Bradley Hand, cursive"], | ||
["Brush Script MT", "Brush Script MT, Brush Script Std, cursive"], | ||
["Chalkduster", "Chalkduster, fantasy"], | ||
["Comic Sans MS", "Comic Sans MS, Comic Sans, cursive"], | ||
["Courier", "Courier, monospace"], | ||
["Courier New", "Courier New, monospace"], | ||
["Cursive", "cursive"], | ||
["DejaVu Sans Mono", "DejaVu Sans Mono, monospace"], | ||
["Didot", "Didot, serif"], | ||
["Fantasy", "Fantasy"], | ||
["FreeMono", "FreeMono, monospace"], | ||
["Georgia", "Georgia, serif"], | ||
["Gill Sans", "Gill Sans, sans-serif"], | ||
["Helvetica", "Helvetica, sans-serif"], | ||
["Impact", "Impact, fantasy"], | ||
["Jazz LET", "Jazz LET, fantasy"], | ||
["Luminari", "Luminari, fantasy"], | ||
["Marker Felt", "Marker Felt, fantasy"], | ||
["Monospace", "monospace"], | ||
["New Century Schoolbook", "New Century Schoolbook, TeX Gyre Schola, serif"], | ||
["Noto Sans", "Noto Sans, sans-serif"], | ||
["OCR A Std", "OCR A Std, monospace"], | ||
["Optima", "Optima, sans-serif"], | ||
["Palatino", "Palatino, URW Palladio L, serif"], | ||
["Sans-Serif", "sans-serif"], | ||
["Serif", "serif"], | ||
["Snell Roundhand", "Snell Roundhand, cursive"], | ||
["Stencil Std", "Stencil Std, fantasy"], | ||
["Times", "Times, Times New Roman, serif"], | ||
["Trattatello", "Trattatello, fantasy"], | ||
["Trebuchet MS", "Trebuchet MS, sans-serif"], | ||
["URW Chancery L", "URW Chancery L, cursive"], | ||
["Verdana", "Verdana, sans-serif"] | ||
] | ||
|
||
has_many :adventures | ||
|
||
slug_from :slug_source | ||
|
||
dragonfly_accessor :intro_image | ||
dragonfly_accessor :scene_image | ||
dragonfly_accessor :end_image | ||
|
||
belongs_to :user | ||
|
||
validates :title, presence: true | ||
|
||
def to_s | ||
title | ||
end | ||
|
||
def to_param | ||
slug | ||
end | ||
|
||
private | ||
|
||
def slug_source | ||
title | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.