Skip to content

Commit 65af3d5

Browse files
authored
Head and Footer Content / Tailwind Configuration (#286)
- adds Tailwind js copied from cdn as a webpack - adds ability to add head and footer html content to website pages [What's the impact of the "Play CDN" version of Tailwind?](https://miro.com/app/board/uXjVO6C1LxA=/?moveToWidget=3458764523961411101&cot=14)
1 parent b9dbfa1 commit 65af3d5

File tree

14 files changed

+265
-3
lines changed

14 files changed

+265
-3
lines changed

app/assets/stylesheets/modules/_code-mirror.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@
3333
box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25); /* Bootstrap 4 */
3434
transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
3535
}
36+
37+
#staff_websites_edit, #staff_websites_new {
38+
.CodeMirror {
39+
height: 15vh;
40+
min-height: 15vh;
41+
}
42+
.CodeMirror-focused {
43+
height: 60vh;
44+
min-height: 60vh;
45+
}
46+
}

app/controllers/staff/websites_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ def website_params
4747
:twitter_handle,
4848
:facebook_url,
4949
:instagram_url,
50+
:head_content,
5051
footer_categories: [],
5152
navigation_links: [],
5253
session_format_configs_attributes: [
5354
:id, :name, :display, :position, :session_format_id
5455
],
5556
fonts_attributes: [
56-
:id, :name, :file, :_destroy, :primary, :secondary
57+
:id, :name, :file, :primary, :secondary, :_destroy
58+
],
59+
contents_attributes: [
60+
:id, :name, :html, :placement, :_destroy
5761
],
5862
)
5963
end

app/decorators/website_decorator.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,12 @@ def font_root_css
135135
}
136136
CSS
137137
end
138+
139+
def head_content
140+
object.contents.for(Website::Content::HEAD).pluck(:html).join.html_safe
141+
end
142+
143+
def footer_content
144+
object.contents.for(Website::Content::FOOTER).pluck(:html).join.html_safe
145+
end
138146
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Controller } from 'stimulus'
2+
import CodeMirror from 'codemirror/lib/codemirror.js'
3+
import 'codemirror/mode/htmlmixed/htmlmixed.js'
4+
5+
export default class extends Controller {
6+
static targets = ['textArea']
7+
8+
initializeCodeMirror() {
9+
var editor = CodeMirror.fromTextArea(this.textAreaTarget, {
10+
mode: "htmlmixed",
11+
lineWrapping: true,
12+
});
13+
editor.on('drop', (e, event) => {
14+
event.preventDefault();
15+
this.uploadFile(event.dataTransfer.files[0], event, e)
16+
})
17+
return editor;
18+
}
19+
20+
uploadFile(file, event, editor) {
21+
let url = '/image_uploads'
22+
let formData = new FormData()
23+
24+
formData.append('file', file)
25+
26+
fetch(url, {
27+
method: 'POST',
28+
body: formData
29+
}).then(response => response.json())
30+
.then(data => {
31+
let newline = `url('${data.location}')`
32+
let doc= editor.getDoc()
33+
editor.focus()
34+
let x = event.pageX
35+
let y = event.pageY
36+
editor.setCursor(editor.coordsChar({left:x,top:y}))
37+
let newpos = editor.getCursor()
38+
doc.replaceRange(newline, newpos)
39+
})
40+
}
41+
42+
connect () {
43+
this.initializeCodeMirror();
44+
}
45+
}

app/javascript/packs/tailwind.js

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/models/website.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ class Website < ApplicationRecord
22
belongs_to :event
33
has_many :pages, dependent: :destroy
44
has_many :fonts, class_name: 'Website::Font', dependent: :destroy
5+
has_many :contents, class_name: 'Website::Content', dependent: :destroy
56

67
has_many :session_formats, through: :event
78
has_many :session_format_configs
89

910
accepts_nested_attributes_for :fonts, reject_if: :all_blank, allow_destroy: true
11+
accepts_nested_attributes_for :contents, reject_if: :all_blank, allow_destroy: true
1012
accepts_nested_attributes_for :session_format_configs
1113

1214
has_one_attached :logo

app/models/website/content.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Website::Content < ApplicationRecord
2+
PLACEMENTS = [
3+
HEAD = "head",
4+
FOOTER = "footer",
5+
].freeze
6+
7+
belongs_to :website
8+
9+
scope :for, -> (placement) { where(placement: placement) }
10+
end
11+
12+
# == Schema Information
13+
#
14+
# Table name: website_contents
15+
#
16+
# id :bigint(8) not null, primary key
17+
# html :text
18+
# placement :string default("head"), not null
19+
# name :string
20+
# website_id :bigint(8)
21+
# created_at :datetime not null
22+
# updated_at :datetime not null
23+
#
24+
# Indexes
25+
#
26+
# index_website_contents_on_website_id (website_id)
27+
#

app/views/layouts/themes/default.html.haml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
:css
99
#{current_website.font_faces_css}
1010
#{current_website.font_root_css}
11-
%script{src: "https://cdn.tailwindcss.com" }
11+
= javascript_pack_tag "tailwind"
12+
= current_website.head_content
13+
1214
%body
1315
- unless @page&.hide_header?
1416
= render 'layouts/themes/default/header'
@@ -37,3 +39,4 @@
3739
document.getElementById("main-nav").addEventListener('click', function(e) {
3840
toggleNav()
3941
})
42+
= current_website.footer_content
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
= content_tag :div,
2+
class: 'nested-fields',
3+
data: { new_record: form.object.new_record? } do
4+
.inline-form
5+
= form.input :name
6+
= form.input :placement, collection: Website::Content::PLACEMENTS
7+
= form.input :html,
8+
as: :text,
9+
input_html: { data: { "content-target": :textArea } },
10+
wrapper_html: { data: { "controller": "content" } }
11+
12+
= link_to "Remove", "#", data: { action: "click->nested-form#remove_association" }
13+
= form.hidden_field :_destroy

app/views/staff/websites/_form.html.haml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@
5858
= link_to "Add Font", "#", class: 'btn btn-success',
5959
data: { action: "click->nested-form#add_association" }
6060

61+
%div{"data-controller": "nested-form"}
62+
%h4 Head and Footer Content
63+
%template{"data-nested-form-target": "template"}
64+
= f.simple_fields_for :contents,
65+
Website::Content.new,
66+
child_index: 'NEW_RECORD' do |content|
67+
= render 'content_fields', form: content
68+
= f.simple_fields_for :contents do |content|
69+
= render 'content_fields', form: content
70+
%div{"data-nested-form-target": "links"}
71+
= link_to "Add Content", "#", class: 'btn btn-success',
72+
data: { action: "click->nested-form#add_association" }
73+
6174
.row
6275
.col-sm-12
6376
= submit_tag("Save", class: "pull-right btn btn-success", type: "submit")

0 commit comments

Comments
 (0)