Skip to content

Commit

Permalink
Define TrixToolbarElement.editorElements property
Browse files Browse the repository at this point in the history
Define properties for accessing all `<trix-editor>` elements that
declare a relationship with a `<trix-toolbar>` element through a
`[toolbar]`-`[id]` attribute relationship.

Since multiple `<trix-editor>` elements can reference a `<trix-toolbar>`
element by name, this commit introduces both an `.editorElements` and
`.editorElement` properties.

The `.editorElement` property returns the first item in
`.editorElements`, if there are any.
  • Loading branch information
seanpdoyle committed Sep 30, 2024
1 parent 10573a8 commit 6a86202
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/test/system/installation_process_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ testGroup("Installation process with specified elements", { template: "editor_wi
assert.equal(editorElement.value, "<div>Hello world</div>")
})

test("trix-toolbar can reference editorElements and editorElement", () => {
const editorElement = getEditorElement()
const toolbarElement = editorElement.toolbarElement

assert.equal(toolbarElement, document.getElementById("my_toolbar"))
assert.deepEqual(Array.from(toolbarElement.editorElements), [ editorElement ])
assert.equal(toolbarElement.editorElement, editorElement)
})

test("can be cloned", async () => {
const originalElement = document.getElementById("my_editor")
const clonedElement = originalElement.cloneNode(true)
Expand Down
18 changes: 18 additions & 0 deletions src/trix/elements/trix_toolbar_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ export default class TrixToolbarElement extends HTMLElement {
this.innerHTML = config.toolbar.getDefaultHTML()
}
}

// Properties

get editorElements() {
if (this.id) {
const nodeList = this.ownerDocument?.querySelectorAll(`[toolbar="${this.id}"]`)

return Array.from(nodeList)
} else {
return []
}
}

get editorElement() {
const [ editorElement ] = this.editorElements

return editorElement
}
}

0 comments on commit 6a86202

Please sign in to comment.