Skip to content

Commit d34cf52

Browse files
committed
Define TrixToolbarElement.editorElements
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.
1 parent 98f7d67 commit d34cf52

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/test/system/installation_process_test.js

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ testGroup("Installation process with specified elements", { template: "editor_wi
4646
assert.equal(editorElement.value, "<div>Hello world</div>")
4747
})
4848

49+
test("trix-toolbar can reference editorElements and editorElement", () => {
50+
const editorElement = getEditorElement()
51+
const toolbarElement = editorElement.toolbarElement
52+
53+
assert.equal(toolbarElement, document.getElementById("my_toolbar"))
54+
assert.deepEqual(Array.from(toolbarElement.editorElements), [ editorElement ])
55+
assert.equal(toolbarElement.editorElement, editorElement)
56+
})
57+
4958
test("can be cloned", async () => {
5059
const originalElement = document.getElementById("my_editor")
5160
const clonedElement = originalElement.cloneNode(true)

src/trix/elements/trix_toolbar_element.js

+19
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,23 @@ export default class TrixToolbarElement extends HTMLElement {
3232
this.innerHTML = config.toolbar.getDefaultHTML()
3333
}
3434
}
35+
36+
// Properties
37+
38+
get editorElements() {
39+
if (this.hasAttribute("id")) {
40+
const selector = `[toolbar="${this.getAttribute("id")}"]`
41+
const nodeList = this.ownerDocument?.querySelectorAll(selector)
42+
43+
return Array.from(nodeList)
44+
} else {
45+
return []
46+
}
47+
}
48+
49+
get editorElement() {
50+
const [ editorElement ] = this.editorElements
51+
52+
return editorElement
53+
}
3554
}

0 commit comments

Comments
 (0)