-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Make Trix compatible with morphing #1227
base: main
Are you sure you want to change the base?
Conversation
@seanpdoyle I would love your eyes on the approach 🙏 |
// Element callbacks | ||
|
||
attributeChangedCallback(name, oldValue, newValue) { | ||
if (name === "initialized" && oldValue && oldValue !== newValue) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens to an editor with the [initialized]
attribute that is disconnected from the page, written to a cache (like Turbo's cache), then re-connected with the [initialized]
attribute already encoded into the HTML?
Would the [initialized]
attribute need to be removed in the custom element's disconnectedCallback()?
@@ -485,6 +497,8 @@ export default class TrixEditorElement extends HTMLElement { | |||
} | |||
this.editorController.registerSelectionManager() | |||
this.#delegate.connectedCallback() | |||
|
|||
this.setAttribute("initialized", "true") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is to be treated as a boolean attribute, would Element.toggleAttribute with true
as an argument be more appropriate?
this.setAttribute("initialized", "true") | |
this.toggleAttribute("initialized", true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could even base the check simply on whether the attribute is present or not, and it could be blank.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tweaked it to use toogleAttribute
and changed the check accordingly.
this.disconnectedCallback() | ||
this.connectedCallback() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this approach work with an existing <trix-toolbar>
element? What about a <trix-editor>
element with a [toolbar]
attribute that references another element elsewhere in the page? Are the toolbars re-initialized as expected without attaching a duplicate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good points, I'll see how to make these cases work. You are right that it will lose bootstrapped toolbars. I'll make sure it works well with both scenarios (existing toolbar and bootstrapped).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some logic to (1) flag internal (bootstrapped) trix toolbars and (2) remove those when reconnecting. This works well in my test.s
I'd opened hotwired/turbo-rails#688 on the |
989bf70
to
6ffd337
Compare
Thanks for the exploration @seanpdoyle. I prefer trying to fix this as the Trix level and keep the turbo Rails gem ignorant about this, as much as possible. |
6c6b54d
to
98b4d0c
Compare
@@ -494,6 +507,18 @@ export default class TrixEditorElement extends HTMLElement { | |||
this.#delegate.disconnectedCallback() | |||
} | |||
|
|||
async reconnect() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the significance of defining this function as asynchronous?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it still function if omitted?
async reconnect() { | |
reconnect() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, good catch. That's a remaining bit from some earlier explorations 👍
@@ -410,6 +412,7 @@ export default class TrixEditorElement extends HTMLElement { | |||
} else if (this.parentNode) { | |||
const toolbarId = `trix-toolbar-${this.trixId}` | |||
this.setAttribute("toolbar", toolbarId) | |||
this.toggleAttribute("internal-toolbar", true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line toggles the [internal-toolbar]
attribute on the the <trix-editor>
(since this
references that element).
However, the removeInternalToolbar()
function defined below checks this.toolbarElement
(the <trix-toolbar>
if the <trix-editor>
element is using an "internal" toolbar).
Should this line set the attribute on the toolbar, or should the check be switched from this.toolbarElement
to this
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Furthermore, is it important that the attribute be set? Could this same kind of internal state tracking be achieved by a JavaScript object property that isn't represented in the DOM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh I think I messed that up when switching it to use toogleAttribute. I'll also make sure the tests catch this. Thanks 👍
Morphing the trix editor will leave it in an unusuable state because morphing will replace the bootstrapping elements without triggering the connection logic. The solution is based on adding an attribute to flag the editor as initialized and detect when that attribute is modified. See hotwired/turbo-rails#533 (comment)
Morphing the trix editor will leave it in an unusuable state because morphing will replace the bootstrapping elements without triggering the connection logic.
The solution is based on adding an attribute to flag the editor as initialized and detect when that attribute is modified.
See hotwired/turbo-rails#533 (comment)
Pending: