Skip to content

Commit

Permalink
add trix reach text editor: rails action_text:install
Browse files Browse the repository at this point in the history
  • Loading branch information
osaienko committed Sep 9, 2022
1 parent eaf44ca commit 8a50688
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 3 deletions.
37 changes: 37 additions & 0 deletions app/assets/stylesheets/actiontext.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Provides a drop-in pointer for the default Trix stylesheet that will format the toolbar and
// the trix-editor content (whether displayed or under editing). Feel free to incorporate this
// inclusion directly in any other asset bundle and remove this file.
//
//= require trix/dist/trix
@import "trix/dist/trix";

// We need to override trix.css’s image gallery styles to accommodate the
// <action-text-attachment> element we wrap around attachments. Otherwise,
// images in galleries will be squished by the max-width: 33%; rule.
.trix-content {
.attachment-gallery {
> action-text-attachment,
> .attachment {
flex: 1 0 33%;
padding: 0 0.5em;
max-width: 33%;
}

&.attachment-gallery--2,
&.attachment-gallery--4 {
> action-text-attachment,
> .attachment {
flex-basis: 50%;
max-width: 50%;
}
}
}

action-text-attachment {
.attachment {
padding: 0 !important;
max-width: 100% !important;
}
}
}
3 changes: 3 additions & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ import "@fortawesome/fontawesome-free/css/all"
Rails.start()
Turbolinks.start()
ActiveStorage.start()

require("trix")
require("@rails/actiontext")
2 changes: 1 addition & 1 deletion app/javascript/stylesheets/application.scss
Original file line number Diff line number Diff line change
@@ -1 +1 @@

@import "app/assets/stylesheets/actiontext.scss";
1 change: 1 addition & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class Course < ApplicationRecord
def to_s
title
end
has_rich_text :description
end
14 changes: 14 additions & 0 deletions app/views/active_storage/blobs/_blob.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>

<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
</figure>
3 changes: 2 additions & 1 deletion app/views/courses/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

.form-inputs
= f.input :title
= f.input :description
= f.label :description
= f.rich_text_area :description

.form-actions
= f.button :submit
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [ :key ], unique: true
end

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records do |t|
t.belongs_to :blob, null: false, index: false
t.string :variation_digest, null: false

t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
end
14 changes: 14 additions & 0 deletions db/migrate/20220909135650_create_action_text_tables.action_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This migration comes from action_text (originally 20180528164100)
class CreateActionTextTables < ActiveRecord::Migration[6.0]
def change
create_table :action_text_rich_texts do |t|
t.string :name, null: false
t.text :body, size: :long
t.references :record, null: false, polymorphic: true, index: false

t.timestamps

t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true
end
end
end
42 changes: 41 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"@fortawesome/fontawesome-free": "^6.2.0",
"@popperjs/core": "^2.11.6",
"@rails/actioncable": "^6.0.0",
"@rails/actiontext": "^6.1.6-1",
"@rails/activestorage": "^6.0.0",
"@rails/ujs": "^6.0.0",
"@rails/webpacker": "5.4.3",
"bootstrap": "^5.2.1",
"jquery": "^3.6.1",
"trix": "^1.2.0",
"turbolinks": "^5.2.0",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12"
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/action_text/rich_texts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# one:
# record: name_of_fixture (ClassOfFixture)
# name: content
# body: <p>In a <i>million</i> stars!</p>
12 changes: 12 additions & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8a50688

Please sign in to comment.