diff --git a/src/components/orchestrator/sources/element.js b/src/components/orchestrator/sources/element.js index ad60481d..01b09dcc 100644 --- a/src/components/orchestrator/sources/element.js +++ b/src/components/orchestrator/sources/element.js @@ -1,5 +1,6 @@ import deepDiff from "@fry69/deep-diff"; +import * as Output from "~/common/output.js"; import { BroadcastableDiffuseElement, query } from "~/common/element.js"; import { groupTracksPerScheme } from "~/common/utils.js"; import { signal } from "~/common/signal.js"; @@ -15,6 +16,7 @@ import { signal } from "~/common/signal.js"; class Sources extends BroadcastableDiffuseElement { static NAME = "diffuse/orchestrator/sources"; + static DISABLED_KEY = "sh.diffuse.input.disabled.uris"; // SIGNALS @@ -24,6 +26,60 @@ class Sources extends BroadcastableDiffuseElement { sources = this.#sources.get; + #output = signal(/** @type {OutputElement | null} */ (null)); + + // METHODS + + /** + * @param {string} uri + */ + async toggle(uri) { + const output = this.#output.value; + if (!output) { + console.warn("Output element is not available yet."); + return; + } + + const settings = await Output.data(output.settings); + const existing = settings.find((s) => s.key === Sources.DISABLED_KEY); + + /** @type {string[]} */ + let disabled = []; + if (existing) { + try { + const parsed = JSON.parse(existing.value); + disabled = Array.isArray(parsed) ? parsed : []; + } catch { + disabled = []; + } + } + + if (disabled.includes(uri)) { + disabled = disabled.filter((u) => u !== uri); + } else { + disabled = [...disabled, uri]; + } + + const value = JSON.stringify(disabled); + const updated = existing + ? settings.map((s) => + s.key === Sources.DISABLED_KEY ? { ...s, value } : s + ) + : [ + ...settings, + { + $type: /** @type {"sh.diffuse.output.setting"} */ ( + "sh.diffuse.output.setting" + ), + id: crypto.randomUUID(), + key: Sources.DISABLED_KEY, + value, + }, + ]; + + await output.settings.save(updated); + } + // LIFECYCLE /** @@ -42,6 +98,10 @@ class Sources extends BroadcastableDiffuseElement { await customElements.whenDefined(input.localName); await customElements.whenDefined(output.localName); + // Signals + this.#output.value = output; + + // Single input mode + dependencies const singleInputMode = !!input.SCHEME; const deps = /** @type {{ [k: string]: InputElement }} */ (singleInputMode