Skip to content

Commit

Permalink
Add morph event suppression to trix controller
Browse files Browse the repository at this point in the history
See hotwired/turbo-rails#533 for context.
  • Loading branch information
sfnelson committed Aug 27, 2024
1 parent 286b592 commit 9de99c7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
katalyst-content (2.6.0)
katalyst-content (2.6.1)
active_storage_validations
activerecord (>= 7.0, < 7.2)
katalyst-html-attributes
Expand Down
55 changes: 54 additions & 1 deletion app/javascript/content/editor/trix_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,61 @@ const Trix = window.Trix;
// will be lazy loaded when a trix-editor is added to the dom.
export default class TrixController extends Controller {
trixInitialize(e) {
// noop, useful as an extension point for registering behaviour on load
this.element.addEventListener(
"turbo:before-morph-attribute",
this.suppressMorph,
);
this.element.addEventListener(
"turbo:before-morph-element",
this.suppressMorph,
);

if (this.element.toolbarElement) {
this.element.toolbarElement.addEventListener(
"turbo:before-morph-attribute",
this.suppressMorph,
);
this.element.toolbarElement.addEventListener(
"turbo:before-morph-element",
this.suppressMorph,
);
}
}

disconnect() {
this.element.removeEventListener(
"turbo:before-morph-attribute",
this.suppressMorph,
);
this.element.removeEventListener(
"turbo:before-morph-element",
this.suppressMorph,
);

if (this.element.toolbarElement) {
this.element.toolbarElement.removeEventListener(
"turbo:before-morph-attribute",
this.suppressMorph,
);
this.element.toolbarElement.removeEventListener(
"turbo:before-morph-element",
this.suppressMorph,
);
}
}

suppressMorph = (e) => {
// https://github.com/hotwired/turbo-rails/issues/533
// Note that this will prevent updates from the server from making their way
// to the trix element. Once the upstream issue is resolved we should remove
// this compatibility patch.
if (
e.target.tagName === "TRIX-EDITOR" ||
e.target.tagName === "TRIX-TOOLBAR"
) {
e.preventDefault();
}
};
}

// Add H4 as an acceptable tag
Expand Down
2 changes: 1 addition & 1 deletion katalyst-content.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |spec|
spec.name = "katalyst-content"
spec.version = "2.6.0"
spec.version = "2.6.1"
spec.authors = ["Katalyst Interactive"]
spec.email = ["developers@katalyst.com.au"]

Expand Down

0 comments on commit 9de99c7

Please sign in to comment.