From c4a53caf5231658c040bba92342a113b3a152ded Mon Sep 17 00:00:00 2001 From: Philipp Rothmann Date: Fri, 4 Aug 2023 10:52:30 +0200 Subject: [PATCH] feat: add actiontext and trix editor --- Dockerfile | 5 +-- Gemfile | 6 ++-- Gemfile.lock | 13 +++++--- app/assets/stylesheets/actiontext.css | 31 +++++++++++++++++++ app/assets/stylesheets/application.css | 1 + app/javascript/application.js | 3 ++ .../action_text/contents/_content.html.erb | 3 ++ config/application.rb | 2 ++ config/importmap.rb | 2 ++ ...6_create_action_text_tables.action_text.rb | 27 ++++++++++++++++ db/schema.rb | 12 ++++++- 11 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 app/assets/stylesheets/actiontext.css create mode 100644 app/views/layouts/action_text/contents/_content.html.erb create mode 100644 db/migrate/20230209105256_create_action_text_tables.action_text.rb diff --git a/Dockerfile b/Dockerfile index 9509c4d3..e8f6a4c0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -49,9 +49,10 @@ RUN export DATABASE_URL=mysql2://localhost/temp?encoding=utf8 && \ rm -Rf /var/lib/apt/lists/* /var/cache/apt/* # Make relevant dirs and files writable for app user -RUN mkdir -p tmp && \ +RUN mkdir -p tmp storage && \ chown nobody config/app_config.yml && \ - chown nobody tmp + chown nobody tmp && \ + chown nobody storage # Run app as unprivileged user USER nobody diff --git a/Gemfile b/Gemfile index 4cc600b0..97422021 100644 --- a/Gemfile +++ b/Gemfile @@ -49,6 +49,8 @@ gem 'whenever', require: false # For defining cronjobs, see config/schedule.rb gem 'exception_notification' gem 'gaffe' gem 'hashie', '~> 3.4.6', require: false # https://github.com/westfieldlabs/apivore/issues/114 +gem "image_processing", "~> 1.12" +gem "importmap-rails", "~> 1.1" gem 'midi-smtp-server' gem 'mime-types' gem 'recurring_select', git: 'https://github.com/gregschmit/recurring_select' @@ -58,6 +60,7 @@ gem 'rswag-api' gem 'rswag-ui' gem 'ruby-filemagic' gem 'spreadsheet' +gem "terser", "~> 1.1" # we use the git version of acts_as_versioned, and need to include it in this Gemfile gem 'acts_as_versioned', git: 'https://github.com/technoweenie/acts_as_versioned.git' @@ -122,6 +125,3 @@ group :test do # api gem 'rswag-specs' end - -gem "importmap-rails", "~> 1.1" -gem "terser", "~> 1.1" diff --git a/Gemfile.lock b/Gemfile.lock index debba0f2..c66901cf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -254,6 +254,9 @@ GEM i18n-spec (0.6.0) iso ice_cube (0.16.4) + image_processing (1.12.2) + mini_magick (>= 4.9.5, < 5) + ruby-vips (>= 2.0.17, < 3) importmap-rails (1.1.5) actionpack (>= 6.0.0) railties (>= 6.0.0) @@ -320,8 +323,8 @@ GEM mime-types (3.4.1) mime-types-data (~> 3.2015) mime-types-data (3.2022.0105) + mini_magick (4.12.0) mini_mime (1.1.2) - mini_portile2 (2.8.2) minitest (5.18.0) mono_logger (1.1.1) msgpack (1.6.0) @@ -339,9 +342,6 @@ GEM net-smtp (0.3.3) net-protocol nio4r (2.5.8) - nokogiri (1.15.2) - mini_portile2 (~> 2.8.2) - racc (~> 1.4) nokogiri (1.15.2-x86_64-linux) racc (~> 1.4) parallel (1.23.0) @@ -499,6 +499,8 @@ GEM ruby-prof (1.4.5) ruby-progressbar (1.13.0) ruby-units (3.0.0) + ruby-vips (2.1.4) + ffi (~> 1.12) ruby2_keywords (0.0.5) rubyzip (2.3.2) sass-rails (6.0.0) @@ -635,6 +637,7 @@ DEPENDENCIES i18n-js (~> 3.0.0.rc8) i18n-spec ice_cube + image_processing (~> 1.12) importmap-rails (~> 1.1) inherited_resources jquery-rails @@ -692,4 +695,4 @@ DEPENDENCIES whenever BUNDLED WITH - 2.4.13 + 2.4.5 diff --git a/app/assets/stylesheets/actiontext.css b/app/assets/stylesheets/actiontext.css new file mode 100644 index 00000000..3cfcb2b7 --- /dev/null +++ b/app/assets/stylesheets/actiontext.css @@ -0,0 +1,31 @@ +/* + * 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 +*/ + +/* + * We need to override trix.css’s image gallery styles to accommodate the + * 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, +.trix-content .attachment-gallery > .attachment { + flex: 1 0 33%; + padding: 0 0.5em; + max-width: 33%; +} + +.trix-content .attachment-gallery.attachment-gallery--2 > action-text-attachment, +.trix-content .attachment-gallery.attachment-gallery--2 > .attachment, .trix-content .attachment-gallery.attachment-gallery--4 > action-text-attachment, +.trix-content .attachment-gallery.attachment-gallery--4 > .attachment { + flex-basis: 50%; + max-width: 50%; +} + +.trix-content action-text-attachment .attachment { + padding: 0 !important; + max-width: 100% !important; +} diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 6bdfecd2..01dba421 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -7,4 +7,5 @@ *= require list.unlist *= require list.missing *= require recurring_select +*= require actiontext */ diff --git a/app/javascript/application.js b/app/javascript/application.js index beff742e..1ba4a01a 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -1 +1,4 @@ // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails +import "trix" +import "@rails/actiontext" +import "./trix-editor-overrides" diff --git a/app/views/layouts/action_text/contents/_content.html.erb b/app/views/layouts/action_text/contents/_content.html.erb new file mode 100644 index 00000000..9e3c0d0d --- /dev/null +++ b/app/views/layouts/action_text/contents/_content.html.erb @@ -0,0 +1,3 @@ +
+ <%= yield -%> +
diff --git a/config/application.rb b/config/application.rb index 5a7edb93..696d6647 100644 --- a/config/application.rb +++ b/config/application.rb @@ -68,6 +68,8 @@ module Foodsoft config.active_record.yaml_column_permitted_classes = [Symbol, BigDecimal] config.autoloader = :zeitwerk + + config.active_storage.variant_processor = :mini_magick end # Foodsoft version diff --git a/config/importmap.rb b/config/importmap.rb index 3ed27f76..f882664b 100644 --- a/config/importmap.rb +++ b/config/importmap.rb @@ -1,2 +1,4 @@ # Pin npm packages by running ./bin/importmap pin "application", preload: true +pin "trix" +pin "@rails/actiontext", to: "actiontext.js" diff --git a/db/migrate/20230209105256_create_action_text_tables.action_text.rb b/db/migrate/20230209105256_create_action_text_tables.action_text.rb new file mode 100644 index 00000000..e9c30fac --- /dev/null +++ b/db/migrate/20230209105256_create_action_text_tables.action_text.rb @@ -0,0 +1,27 @@ +# This migration comes from action_text (originally 20180528164100) +class CreateActionTextTables < ActiveRecord::Migration[6.0] + def change + # Use Active Record's configured type for primary and foreign keys + primary_key_type, foreign_key_type = primary_and_foreign_key_types + + create_table :action_text_rich_texts, id: primary_key_type do |t| + t.string :name, null: false + t.text :body, size: :long + t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type + + t.timestamps + + t.index [:record_type, :record_id, :name], name: "index_action_text_rich_texts_uniqueness", unique: true + end + end + + private + + def primary_and_foreign_key_types + config = Rails.configuration.generators + setting = config.options[config.orm][:primary_key_type] + primary_key_type = setting || :primary_key + foreign_key_type = setting || :bigint + [primary_key_type, foreign_key_type] + end +end diff --git a/db/schema.rb b/db/schema.rb index 50c24c41..9ee6a522 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_01_06_144440) do +ActiveRecord::Schema[7.0].define(version: 2023_02_15_085312) do + create_table "action_text_rich_texts", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.string "name", null: false + t.text "body", size: :long + t.string "record_type", null: false + t.bigint "record_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true + end + create_table "active_storage_attachments", id: :integer, charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false