From 6a86202875908f4d9c889aa8b7d8c9d280775258 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Sun, 21 Jan 2024 13:24:54 -0500 Subject: [PATCH] Define `TrixToolbarElement.editorElements` property Define properties for accessing all `` elements that declare a relationship with a `` element through a `[toolbar]`-`[id]` attribute relationship. Since multiple `` elements can reference a `` 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. --- src/test/system/installation_process_test.js | 9 +++++++++ src/trix/elements/trix_toolbar_element.js | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/test/system/installation_process_test.js b/src/test/system/installation_process_test.js index fc6195fbd..fdb78c0dd 100644 --- a/src/test/system/installation_process_test.js +++ b/src/test/system/installation_process_test.js @@ -46,6 +46,15 @@ testGroup("Installation process with specified elements", { template: "editor_wi assert.equal(editorElement.value, "
Hello world
") }) + 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) diff --git a/src/trix/elements/trix_toolbar_element.js b/src/trix/elements/trix_toolbar_element.js index 80a362c1e..a429add5d 100644 --- a/src/trix/elements/trix_toolbar_element.js +++ b/src/trix/elements/trix_toolbar_element.js @@ -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 + } }