From b5feb52b6e8190bb46adab05e56de564b1d72da6 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 22 May 2024 15:37:09 +0200 Subject: [PATCH] wip: Add alchemy-action Allows to call functions that are registered as action from a `turbo_stream` response. --- .../alchemy_admin/components/action.js | 26 +++++++++++++++++++ .../alchemy_admin/components/index.js | 1 + 2 files changed, 27 insertions(+) create mode 100644 app/javascript/alchemy_admin/components/action.js diff --git a/app/javascript/alchemy_admin/components/action.js b/app/javascript/alchemy_admin/components/action.js new file mode 100644 index 0000000000..4621f421d8 --- /dev/null +++ b/app/javascript/alchemy_admin/components/action.js @@ -0,0 +1,26 @@ +import { reloadPreview } from "alchemy_admin/components/preview_window" + +class Action extends HTMLElement { + constructor() { + super() + this.actions = { + reloadPreview + } + } + + connectedCallback() { + const func = this.actions[this.name] + + if (func) { + func.call() + } else { + console.error(`Unknown Alchemy action: ${this.name}`) + } + } + + get name() { + return this.getAttribute("name") + } +} + +customElements.define("alchemy-action", Action) diff --git a/app/javascript/alchemy_admin/components/index.js b/app/javascript/alchemy_admin/components/index.js index eff07702e2..384da76f82 100644 --- a/app/javascript/alchemy_admin/components/index.js +++ b/app/javascript/alchemy_admin/components/index.js @@ -1,3 +1,4 @@ +import "alchemy_admin/components/action" import "alchemy_admin/components/attachment_select" import "alchemy_admin/components/button" import "alchemy_admin/components/char_counter"