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

[Solved] Stimulus controller cannot access element.editor after v2.0.0-beta.1 #1038

Open
goulvench opened this issue Jan 9, 2023 · 1 comment

Comments

@goulvench
Copy link

goulvench commented Jan 9, 2023

I'm using Trix in a Rails/webpacker app to give users a nice-looking WYSIWYG when generating PDF documents. In particular, users can insert references (implemented following Chris Oliver's tutorial) whose value gets calculated when viewing the PDF document.

Since Trix version 2.0.0-beta.1, my stimulus controller can no longer access this.element.editor when connecting, which returns undefined. I'm guessing this happened after changing how Trix gets started (in #1008)?

Calling document.getElementById('#trix-editor').editor works from a browser console, so I guess it's just that the Stimulus controller is attached before Trix has been applied to the base element.

I've tried calling Trix.start() at various steps, but that makes the JS crash, even if I disable chunking in webpacker config.

I found a workaround while researching this issue, see below.

Steps to Reproduce
// app/assets/javascripts/packs/application.js
import Trix from "trix"
document.addEventListener("trix-before-initialize", () => {
  // I18n config
})
import("controllers")
// app/assets/javascripts/controllers/placeholder_controller.js
import { Controller } from "@hotwired/stimulus";
import Trix from "trix"

export default class extends Controller {
  connect() {
    this.editor = this.element.editor // -> undefined after 2.0.0-beta.1
  }
}
Solution

I was able to work around this by calling this.element.editor dynamically when interacting with the controller, instead of storing it when it is connected.

export default class extends Controller {
  connect() {
    // Don't store a reference to this.element.editor
  }
 
  get editor() {
    return this.element.editor
  }
}
Details
  • Trix version: 2.0.4
  • Browser name and version: Firefox 108.0.1
  • Operating system: MacOS Monterey 12.6.2
@pkayokay
Copy link

pkayokay commented Oct 2, 2023

Another work-around is to add an event listener of trix-initialize to the trix-editor element.

import { Controller } from "@hotwired/stimulus";
import Trix from "trix"

export default class extends Controller {
  connect() {
    this.element.addEventListener("trix-initialize", this.trixInitialize.bind(this))
  }

  disconnect() {
    this.contentFieldTarget.removeEventListener("trix-initialize", this.trixInitialize)
  }

  trixInitialize() {
    this.editor = this.element.editor
    // Do some stuff
  }
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants