Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text alignment #4

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app/javascript/richtext.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Trix from "trix"
addHeadingAttributes()
addForegroundColorAttributes()
addBackgroundColorAttributes()
addAlignmentAttributes()

addEventListener("trix-initialize", function (event) {
new RichText(event.target)
Expand All @@ -27,6 +28,7 @@ class RichText {
this.insertHeadingElements()
this.insertDividerElements()
this.insertColorElements()
this.insertAlignmentElements()
}

insertHeadingElements() {
Expand Down Expand Up @@ -64,6 +66,19 @@ class RichText {
this.dialogsElement.insertAdjacentHTML("beforeend", this.dialogColorTemplate)
}

insertAlignmentElements() {
this.insertAlignmentButton()
this.insertDialogAlignment()
}

insertAlignmentButton() {
this.buttonGroupTextTools.insertAdjacentHTML("beforeend", this.alignmentButtonTemplate)
}

insertDialogAlignment() {
this.dialogsElement.insertAdjacentHTML("beforeend", this.dialogAlignmentTemplate)
}

get buttonGroupBlockTools() {
return this.toolbarElement.querySelector("[data-trix-button-group=block-tools]")
}
Expand Down Expand Up @@ -100,6 +115,10 @@ class RichText {
return '<button type="button" class="trix-button trix-button--icon trix-button--icon-color" data-trix-action="x-color" title="Color" tabindex="-1">Color</button>'
}

get alignmentButtonTemplate() {
return '<button type="button" class="trix-button trix-button--icon trix-button--icon-align-center" data-trix-action="x-alignment" title="Alignment" tabindex="-1">Alignment</button>'
}

get dialogHeadingTemplate() {
return `
<div class="trix-dialog trix-dialog--heading" data-trix-dialog="x-heading" data-trix-dialog-attribute="x-heading">
Expand Down Expand Up @@ -149,6 +168,22 @@ class RichText {
</div>
`
}

get dialogAlignmentTemplate() {
return `
<div class="trix-dialog trix-dialog--alignment" data-trix-dialog="x-alignment" data-trix-dialog-attribute="x-alignment">
<div class="trix-dialog__link-fields">
<input type="text" name="x-alignment" class="trix-dialog-hidden__input" data-trix-input>
<div class="trix-button-group">
<button type="button" class="trix-button trix-button--dialog" data-trix-attribute="left">Left</button>
<button type="button" class="trix-button trix-button--dialog" data-trix-attribute="center">Center</button>
<button type="button" class="trix-button trix-button--dialog" data-trix-attribute="right">Right</button>
</div>
</div>
</div>
`
}

}

function addHeadingAttributes() {
Expand All @@ -168,3 +203,16 @@ function addBackgroundColorAttributes() {
Trix.config.textAttributes[`bgColor${(i + 1)}`] = { style: { backgroundColor: color }, inheritable: true, parser: e => e.style.backgroundColor == color }
})
}

function addAlignmentAttributes() {
Array.from(["left", "center", "right"]).forEach((alignment, i) => {
Trix.config.textAttributes[alignment] = {
breakOnReturn: true,
group: false,
tagName: "div",
style: {textAlign: alignment},
terminal: true,
inheritable: true
}
})
}
1 change: 1 addition & 0 deletions assets/images/trix_align_center.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/images/trix_align_justify.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/images/trix_align_left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/images/trix_align_right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions assets/stylesheets/richtext.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
trix-toolbar {
.trix-button--icon-horizontal-rule::before { background-image: image-url("trix_horizontal_rule.svg"); }
.trix-button--icon-color::before { background-image: image-url("trix_color.svg"); }
.trix-button--icon-align-center::before { background-image: image-url("trix_align_center.svg"); }
.trix-button--icon-align-left::before { background-image: image-url("trix_align_left.svg"); }
.trix-button--icon-align-right::before { background-image: image-url("trix_align_right.svg"); }
// .trix-button--icon-align-justify::before { background-image: image-url("trix_align_justify.svg"); }

.trix-dialog--heading { max-width: 180px; }

.trix-dialog--alignment { max-width: 145px; }

.trix-dialog--color {
max-width: 265px;

Expand Down
1 change: 1 addition & 0 deletions config/initializers/action_text.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Rails.application.config.after_initialize do
ActionText::ContentHelper.allowed_attributes << 'style'
ActionText::ContentHelper.allowed_attributes << 'align'
end