Skip to content

Commit

Permalink
Resize images embedded in Trix (SpinaCMS#602)
Browse files Browse the repository at this point in the history
* Resize images embedded in Trix - Fixes SpinaCMS#601
  • Loading branch information
Bramjetten authored Jul 24, 2020
1 parent 351089e commit 3220a3a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

⚠️ _Beware: lots of changes regarding page content. The old page parts are gone in favor of new JSON-based parts. Read the [Upgrading Guide](https://www.spinacms.com/guide/getting-started/upgrading-from-v1) to learn how to upgrade._

### Unreleased

* Resize images embedded in Trix (configurable in spina.rb)

### 2.0.0.alpha

* __Replaced all page content with JSON-based Spina::Parts__
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

insert_trix(event) {
let customEvent = new CustomEvent("image-insert", {bubbles: true, cancelable: true, detail: {
url: this.trixImage.dataset.fullImageUrl,
url: this.trixImage.dataset.imageUrl,
alt: this.altTarget.value,
link_to_url: this.linkToUrlTarget.value
}})
Expand Down
8 changes: 6 additions & 2 deletions app/assets/stylesheets/spina/_trix_custom.sass
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,14 @@ trix-editor
font-size: 16px

.trix-attachment-spina-image
background: #f5f5f5
border-radius: 6px
cursor: default
display: block
height: 150px
margin-bottom: 40px
position: relative
width: 200px

&[data-label=""]
margin-bottom: 0px
Expand All @@ -181,8 +185,8 @@ trix-editor
img
border-radius: 6px
display: block
max-height: 150px
max-width: 200px
height: 150px
width: 200px

&:before
background: #eee
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/spina/images_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ def thumbnail_url(image)
variant(image.file, resize: "400x300^", crop: "400x300+0+0")
end

def embedded_image_url(image)
return "" if image.nil?
variant(image.file, resize: Spina.config.embedded_image_size)
end

end
end
2 changes: 1 addition & 1 deletion app/views/spina/admin/media_picker/_image.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
%label.media-picker-image{data: {target: "media-picker.image"}}
= check_box_tag :image_id, image.id, image.id.in?(selected_ids), data: {action: "media-picker#choose", image_id: image.id, signed_blob_id: image.file.blob.signed_id, thumbnail_url: thumbnail_url(image), full_image_url: main_app.url_for(image.file), filename: image.file.filename}
= check_box_tag :image_id, image.id, image.id.in?(selected_ids), data: {action: "media-picker#choose", image_id: image.id, signed_blob_id: image.file.blob.signed_id, thumbnail_url: thumbnail_url(image), image_url: embedded_image_url(image), filename: image.file.filename}
%span= image_tag thumbnail_url(image)
7 changes: 6 additions & 1 deletion lib/spina.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Spina
PLUGINS = []
THEMES = []

config_accessor :backend_path, :disable_frontend_routes, :storage, :max_page_depth, :locales
config_accessor :backend_path, :disable_frontend_routes, :storage, :max_page_depth, :locales, :embedded_image_size

self.backend_path = 'admin'

Expand All @@ -24,4 +24,9 @@ module Spina

self.locales = [I18n.default_locale]

# Images that are embedded in the Trix editor are resized to fit
# You can optimize this for your website and go for a smaller (or larger) size
# Default: 2000x2000px
self.embedded_image_size = "2000x2000>"

end

0 comments on commit 3220a3a

Please sign in to comment.