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

Replace text angular editor with trix editor in About Us and Shopfront message fields #12734

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
6 changes: 1 addition & 5 deletions app/assets/javascripts/admin/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//= require angular
//= require angular-resource
//= require angular-animate
//= require angular-sanitize
//= require angularjs-file-upload
//= require ../shared/ng-infinite-scroll.min.js
//= require ../shared/ng-tags-input.min.js
Expand Down Expand Up @@ -61,11 +62,6 @@
//= require ./variant_overrides/variant_overrides

// text, dates and translations
//= require textAngular-rangy.min.js
// This replaces angular-sanitize. We should include only one.
// https://github.com/textAngular/textAngular#where-to-get-it
//= require textAngular-sanitize.min.js
//= require textAngular.min.js
//= require i18n/translations
//= require darkswarm/i18n.translate.js

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
angular.module("admin.enterprise_groups", ["admin.side_menu", "admin.users", "textAngular"])
angular.module("admin.enterprise_groups", ["admin.side_menu", "admin.users", "ngSanitize"])
14 changes: 0 additions & 14 deletions app/assets/javascripts/admin/enterprises/enterprises.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,10 @@ angular.module("admin.enterprises", [
"admin.utils",
"admin.shippingMethods",
"admin.users",
"textAngular",
"admin.side_menu",
"admin.taxons",
'admin.indexUtils',
'admin.tagRules',
'admin.dropdown',
'ngSanitize']
)
# For more options: https://github.com/textAngular/textAngular/blob/master/src/textAngularSetup.js
.config [
'$provide', ($provide) ->
$provide.decorator 'taTranslations', [
'$delegate'
(taTranslations) ->
taTranslations.insertLink = {
tooltip: t('admin.enterprises.form.shop_preferences.shopfront_message_link_tooltip'),
dialogPrompt: t('admin.enterprises.form.shop_preferences.shopfront_message_link_prompt')
}
taTranslations
]
]
2 changes: 1 addition & 1 deletion app/assets/javascripts/admin/products/products.js.coffee
Original file line number Diff line number Diff line change
@@ -1 +1 @@
angular.module("admin.products", ["textAngular", "admin.utils", "OFNShared"])
angular.module("admin.products", ["ngSanitize", "admin.utils", "OFNShared"])

This file was deleted.

This file was deleted.

13 changes: 11 additions & 2 deletions app/models/enterprise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,21 @@ def self.distinct_count

# Remove any unsupported HTML.
def long_description
HtmlSanitizer.sanitize(super)
HtmlSanitizer.sanitize_and_enforce_link_target_blank(super)
dacook marked this conversation as resolved.
Show resolved Hide resolved
end

# Remove any unsupported HTML.
def long_description=(html)
super(HtmlSanitizer.sanitize(html))
super(HtmlSanitizer.sanitize_and_enforce_link_target_blank(html))
end

def preferred_shopfront_message=(html)
self.prefers_shopfront_message = HtmlSanitizer.sanitize_and_enforce_link_target_blank(html)
end

def preferred_shopfront_closed_message=(html)
self.prefers_shopfront_closed_message =
HtmlSanitizer.sanitize_and_enforce_link_target_blank(html)
end

def contact
Expand Down
4 changes: 2 additions & 2 deletions app/models/enterprise_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def to_param

# Remove any unsupported HTML.
def long_description
HtmlSanitizer.sanitize(super)
HtmlSanitizer.sanitize_and_enforce_link_target_blank(super)
end

# Remove any unsupported HTML.
def long_description=(html)
super(HtmlSanitizer.sanitize(html))
super(HtmlSanitizer.sanitize_and_enforce_link_target_blank(html))
end

private
Expand Down
12 changes: 12 additions & 0 deletions app/services/html_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ def self.sanitize(html)
html, tags: ALLOWED_TAGS, attributes: (ALLOWED_ATTRIBUTES + ALLOWED_TRIX_DATA_ATTRIBUTES)
)
end

def self.sanitize_and_enforce_link_target_blank(html)
sanitize(enforce_link_target_blank(html))
end

def self.enforce_link_target_blank(html)
return if html.nil?

Nokogiri::HTML::DocumentFragment.parse(html).tap do |document|
document.css("a").each { |link| link["target"] = "_blank" }
end.to_s
end
end
5 changes: 2 additions & 3 deletions app/views/admin/enterprise_groups/_form_about.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
%fieldset.alpha.no-border-bottom#about_panel{ data: { "tabs-and-panels-target": "panel" } }
%legend= t('.about')
= f.field_container :long_description do
%text-angular{'id' => 'enterprise_group_long_description', 'name' => 'enterprise_group[long_description]', 'class' => 'text-angular', "textangular-links-target-blank" => true,
'ta-toolbar' => "[['h1','h2','h3','h4','p'],['bold','italics','underline','clear'],['insertLink']]"}
!= @enterprise_group[:long_description]
= f.hidden_field :long_description, id: "enterprise_group_long_description"
%trix-editor{ input: "enterprise_group_long_description", "data-controller": "trixeditor" }
13 changes: 3 additions & 10 deletions app/views/admin/enterprises/form/_about_us.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@
.omega.eight.columns
= f.text_field :description, maxlength: 255, placeholder: t('.desc_short_placeholder')
.row
.alpha.three.columns
.alpha.eleven.columns
= f.label :long_description, t('.desc_long')
.omega.eight.columns
-# textAngular toolbar options, add to the ta-toolbar array below and separate into groups with extra ],[ if needed:
-# ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'pre', 'quote'],
-# ['bold', 'italics', 'underline', 'strikeThrough', 'ul', 'ol', 'redo', 'undo', 'clear'],
-# ['justifyLeft','justifyCenter','justifyRight','indent','outdent'],
-# ['html', 'insertImage', 'insertLink', 'insertVideo']
%text-angular{'ng-model' => 'Enterprise.long_description', 'id' => 'enterprise_long_description', 'name' => 'enterprise[long_description]', 'class' => 'text-angular', "textangular-links-target-blank" => true,
'ta-toolbar' => "[['h1','h2','h3','h4','p'],['bold','italics','underline','clear'],['insertLink']]",
'placeholder' => t('.desc_long_placeholder')}
= f.hidden_field :long_description, id: "enterprise_long_description"
%trix-editor{ input: "enterprise_long_description", "data-controller": "trixeditor" }
18 changes: 7 additions & 11 deletions app/views/admin/enterprises/form/_shop_preferences.html.haml
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
.row
.three.columns.alpha
.eleven.columns.alpha
= f.label "enterprise_preferred_shopfront_message", t('.shopfront_message')
.eight.columns.omega
%text-angular{'ng-model' => 'Enterprise.preferred_shopfront_message', 'id' => 'enterprise_preferred_shopfront_message', 'name' => 'enterprise[preferred_shopfront_message]', 'class' => 'text-angular textangular-strip', 'ta-paste' => "stripFormatting($html)", "textangular-links-target-blank" => true,
'ta-toolbar' => "[['h1','h2','h3','h4','p'],['bold','italics','underline','clear'],['insertLink']]",
'placeholder' => t('.shopfront_message_placeholder')}
= f.hidden_field :preferred_shopfront_message, id: "enterprise_preferred_shopfront_message"
%trix-editor{ input: "enterprise_preferred_shopfront_message", "data-controller": "trixeditor", placeholder: t('.shopfront_message_placeholder') }
.row
.three.columns.alpha
.eleven.columns.alpha
= f.label "enterprise_preferred_shopfront_closed_message", t('.shopfront_closed_message')
.eight.columns.omega
%text-angular{'ng-model' => 'Enterprise.preferred_shopfront_closed_message', 'id' => 'enterprise_preferred_shopfront_closed_message', 'name' => 'enterprise[preferred_shopfront_closed_message]', 'class' => 'text-angular textangular-strip', 'ta-paste' => "stripFormatting($html)", "textangular-links-target-blank" => true,
'ta-toolbar' => "[['h1','h2','h3','h4','p'],['bold','italics','underline','clear'],['insertLink']]",
'placeholder' => t('.shopfront_closed_message_placeholder')}
= f.hidden_field :preferred_shopfront_closed_message, id: "enterprise_preferred_shopfront_closed_message"
%trix-editor{ input: "enterprise_preferred_shopfront_closed_message", "data-controller": "trixeditor", placeholder: t('.shopfront_closed_message_placeholder') }

.row
.text-normal
Expand Down Expand Up @@ -77,7 +73,7 @@
= f.radio_button :preferred_product_low_stock_display, true, 'ng-model' => 'Enterprise.preferred_product_low_stock_display', 'ng-value' => 'true'
= f.label :preferred_product_low_stock_display, t('.enabled'), value: :true
.five.columns.omega
= f.radio_button :preferred_product_low_stock_display, false, 'ng-model' => 'Enterprise.preferred_product_low_stock_display', 'ng-value' => 'false'
= f.radio_button :preferred_product_low_stock_display, false, 'ng-model' => 'Enterprise.preferred_product_low_stock_display', 'ng-value' => 'false'
= f.label :preferred_product_low_stock_display, t('.disabled'), value: :false

.row
Expand Down
55 changes: 55 additions & 0 deletions app/webpacker/controllers/trixeditor_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default class extends Controller {
) {
this.#addHRButton();
}

this.#setTranslations();
};

#addHRButton = () => {
Expand All @@ -42,4 +44,57 @@ export default class extends Controller {
this.#trixActionInvoke(event);
});
};

#setTranslation(selector, attribute, value) {
let element = this.element.parentElement.querySelector(selector);
if(element && element.hasAttribute(attribute)) {
element.setAttribute(attribute, value);
}
}

#setTranslations() {
if(I18n.t("js.trix")) {
// Calling 'Trix.config.lang = I18n.t("js.trix")' doesn't work due to read-only error, so set
// translations one at a time.
for (const [key, translation] of Object.entries(I18n.t("js.trix"))) {

// Set all translations (only works in Firefox for some reason)
Trix.config.lang[key] = translation;

// Set toolbar translations (Chrome)
this.#setTranslation(
`[data-trix-action="${this.#translationKeyToAttributeMappings(key)}"]`,
"title",
translation
);
this.#setTranslation(
`[data-trix-attribute="${this.#translationKeyToAttributeMappings(key)}"]`,
"title",
translation
);
}

// Set translations for link dialog (Chrome)
this.#setTranslation(`[data-trix-dialog="href"] input`,
"aria-label", I18n.t("js.trix.url"));
this.#setTranslation(`[data-trix-dialog="href"] input`,
"placeholder", I18n.t("js.trix.urlPlaceholder"));
this.#setTranslation('.trix-dialog--link input[data-trix-method="setAttribute"]',
"value", I18n.t("js.trix.link"));
this.#setTranslation('.trix-dialog--link input[data-trix-method="removeAttribute"]',
"value", I18n.t("js.trix.unlink"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trix.config.lang[key] = translation; only seems to work in Firefox for some reason. The translations have to be set more manually for Chrome.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting, and unfortunate because you have map quite a lot data.

According to this wiki, it suggests overwriting the lang config in one command.

Trix.config.lang = translation
https://github.com/basecamp/trix/wiki/I18n

But you tried that and it didn't work, so it would be worth raising an issue.
I had a look for issues and could only find one.
The final comment shows another method using Object.assign. I don't know if that would make a different, but perhaps it's worth trying.
basecamp/trix#874 (comment)

Copy link
Contributor Author

@cillian cillian Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find, fixed by 63c62ca and it works in Firefox and Chrome.

}
}

#translationKeyToAttributeMappings(key) {
let mapping = {
"bullets": "bullet",
"link": "href",
"numbers": "number",
"indent": "increaseNestingLevel",
"outdent": "decreaseNestingLevel"
}[key];

return mapping || key;
}
}
2 changes: 0 additions & 2 deletions app/webpacker/css/admin/all.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@import "~jquery-ui/themes/base/resizable";
@import "vendor/assets/stylesheets/jquery-ui-theme";
@import "~jquery-ui/themes/base/dialog";
@import "../shared/textAngular";
@import "../shared/ng-tags-input.min";
@import "vendor/assets/stylesheets/select2.css.scss";
@import "~flatpickr/dist/flatpickr";
Expand Down Expand Up @@ -72,7 +71,6 @@
@import "components/todo";
@import "components/tooltip";
@import "components/wizard_progress";
@import "components/text-angular";

@import "pages/enterprise_form";
@import "pages/subscription_form";
Expand Down
43 changes: 0 additions & 43 deletions app/webpacker/css/admin/components/text-angular.scss

This file was deleted.

6 changes: 5 additions & 1 deletion app/webpacker/css/admin/trix.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
trix-toolbar .trix-button-row {
flex-wrap: wrap;
Copy link
Contributor Author

@cillian cillian Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevents horizontal scrollber on Trix button toolbar in smaller screens.

}

trix-toolbar [data-trix-button-group="file-tools"] {
display: none;
}
Expand All @@ -11,7 +15,7 @@ trix-editor {
color: #222;

a {
color: #f27052; // Equivalent to text-angular a
color: #f27052;
}

@include trix-styles;
Expand Down
2 changes: 0 additions & 2 deletions app/webpacker/css/admin_v3/all.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@import "~jquery-ui/themes/base/resizable";
@import "vendor/assets/stylesheets/jquery-ui-theme";
@import "~jquery-ui/themes/base/dialog";
@import "../shared/textAngular";
@import "../shared/ng-tags-input.min";
@import "vendor/assets/stylesheets/select2.css.scss";
@import "~flatpickr/dist/flatpickr";
Expand Down Expand Up @@ -76,7 +75,6 @@
@import "../admin/components/todo";
@import "../admin/components/tooltip";
@import "../admin/components/wizard_progress";
@import "components/text-angular"; // admin_v3

@import "../admin/pages/enterprise_form";
@import "../admin/pages/subscription_form";
Expand Down
43 changes: 0 additions & 43 deletions app/webpacker/css/admin_v3/components/text-angular.scss

This file was deleted.

Loading
Loading