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

Define TrixToolbarElement.editorElements #1127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 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,23 @@ export default class TrixToolbarElement extends HTMLElement {
this.innerHTML = config.toolbar.getDefaultHTML()
}
}

// Properties

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

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

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

return editorElement
}
}
Loading