From 860c75c0bc4e1478fca2514628923014d0cf1a55 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 12 Aug 2022 14:01:56 +0100 Subject: [PATCH 1/7] Update name of types constant --- packages/server/src/api/controllers/screen.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/controllers/screen.js b/packages/server/src/api/controllers/screen.js index 1d5d876dc4..99d12d064c 100644 --- a/packages/server/src/api/controllers/screen.js +++ b/packages/server/src/api/controllers/screen.js @@ -2,7 +2,7 @@ const { getScreenParams, generateScreenID, getPluginParams, - DocumentTypes, + DocumentType, } = require("../../db/utils") const { AccessController } = require("@budibase/backend-core/roles") const { getAppDB } = require("@budibase/backend-core/context") @@ -60,7 +60,7 @@ exports.save = async ctx => { }) // Update the app metadata - const application = await db.get(DocumentTypes.APP_METADATA) + const application = await db.get(DocumentType.APP_METADATA) let usedPlugins = application.usedPlugins || [] requiredPlugins.forEach(plugin => { From a7c424550c75bb42f525d9c6d8395b946c8a6442 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 12 Aug 2022 14:02:11 +0100 Subject: [PATCH 2/7] Simply logic around handling runtime reloading of custom components --- packages/client/src/stores/components.js | 66 ++++++++++++------------ 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/packages/client/src/stores/components.js b/packages/client/src/stores/components.js index 94dd16a957..8512a371d8 100644 --- a/packages/client/src/stores/components.js +++ b/packages/client/src/stores/components.js @@ -12,7 +12,7 @@ const budibasePrefix = "@budibase/standard-components/" const createComponentStore = () => { const store = writable({ customComponentManifest: {}, - componentsAwaitingConstructors: {}, + customComponentMap: {}, mountedComponents: {}, }) @@ -54,30 +54,37 @@ const createComponentStore = () => { const registerInstance = (id, instance) => { store.update(state => { - // If this is a custom component and does not have an implementation yet, - // store so we can reload this component later + // If this is a custom component, flag it so we can reload this component + // later if required const component = instance.component - let cac = state.componentsAwaitingConstructors - if (!getComponentConstructor(component)) { - if (!cac[component]) { - cac[component] = [] + if (component?.startsWith("plugin")) { + if (!state.customComponentMap[component]) { + state.customComponentMap[component] = [id] + } else { + state.customComponentMap[component].push(id) } - cac[component].push(id) } - return { - ...state, - componentsAwaitingConstructors: cac, - mountedComponents: { - ...state.mountedComponents, - [id]: instance, - }, - } + // Register to mounted components + state.mountedComponents[id] = instance + return state }) } const unregisterInstance = id => { store.update(state => { + // Remove from custom component map if required + const component = state.mountedComponents[id]?.instance?.component + let customComponentMap = state.customComponentMap + if (component?.startsWith("plugin")) { + customComponentMap[component] = customComponentMap[component].filter( + x => { + return x !== id + } + ) + } + + // Remove from mounted components delete state.mountedComponents[id] return state }) @@ -133,34 +140,25 @@ const createComponentStore = () => { return customComponentManifest?.[type]?.Component } - const registerCustomComponent = ({ Component, schema }) => { + const registerCustomComponent = ({ Component, schema, version }) => { if (!Component || !schema?.schema?.name) { return } - const componentName = `plugin/${schema.schema.name}/1.0.0` + const component = `plugin/${schema.schema.name}/${version}` store.update(state => { - if (!state.customComponentManifest) { - state.customComponentManifest = {} - } - state.customComponentManifest[componentName] = { - schema, + state.customComponentManifest[component] = { Component, + schema, + version, } return state }) - // Reload any mounted components which depend on this definition + // Reload any mounted instances of this custom component const state = get(store) - if (state.componentsAwaitingConstructors[componentName]?.length) { - state.componentsAwaitingConstructors[componentName].forEach(id => { - const instance = state.mountedComponents[id] - if (instance) { - instance.reload() - } - }) - store.update(state => { - delete state.componentsAwaitingConstructors[componentName] - return state + if (state.customComponentMap[component]?.length) { + state.customComponentMap[component].forEach(id => { + state.mountedComponents[id]?.reload() }) } } From 233d5e190c2c27a91e8c0d97cda3dacabd115e20 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 12 Aug 2022 14:34:02 +0100 Subject: [PATCH 3/7] Use friendly name in new component panel if available --- .../[componentId]/new/_components/NewComponentPanel.svelte | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/pages/builder/app/[application]/design/[screenId]/components/[componentId]/new/_components/NewComponentPanel.svelte b/packages/builder/src/pages/builder/app/[application]/design/[screenId]/components/[componentId]/new/_components/NewComponentPanel.svelte index a5d77c32ca..b0cd544977 100644 --- a/packages/builder/src/pages/builder/app/[application]/design/[screenId]/components/[componentId]/new/_components/NewComponentPanel.svelte +++ b/packages/builder/src/pages/builder/app/[application]/design/[screenId]/components/[componentId]/new/_components/NewComponentPanel.svelte @@ -58,7 +58,10 @@ enrichedStructure.push({ name: "Custom components", isCategory: true, - children: customComponents.map(x => definitions[x]), + children: customComponents.map(x => ({ + ...definitions[x], + name: definitions[x].friendlyName || definitions[x].name, + })), }) } From 83c072fe48b6e63bc0bb41c48a39b48bba86ca3d Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 12 Aug 2022 14:34:16 +0100 Subject: [PATCH 4/7] Show success after uploading plugin --- .../src/pages/builder/portal/settings/organisation.svelte | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/builder/src/pages/builder/portal/settings/organisation.svelte b/packages/builder/src/pages/builder/portal/settings/organisation.svelte index d34442f886..d52bd66d47 100644 --- a/packages/builder/src/pages/builder/portal/settings/organisation.svelte +++ b/packages/builder/src/pages/builder/portal/settings/organisation.svelte @@ -39,6 +39,7 @@ let data = new FormData() data.append("file", $values.logo) await API.uploadPlugin(data) + notifications.success("Plugin uploaded successfully") } catch (error) { notifications.error("Error uploading logo") } From b549fe114b5fcc3f1394006e4b0ebdf88fe57951 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 12 Aug 2022 14:34:44 +0100 Subject: [PATCH 5/7] Remove uncecessary calls to loadBudibase when registering a plugin at runtime --- packages/client/src/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/client/src/index.js b/packages/client/src/index.js index bef3ab9c12..a795d8d2d9 100644 --- a/packages/client/src/index.js +++ b/packages/client/src/index.js @@ -40,17 +40,17 @@ const loadBudibase = () => { devToolsStore.actions.setEnabled(enableDevTools) // Register any custom components - window.registerCustomComponent = plugin => { - componentStore.actions.registerCustomComponent(plugin) - console.log("registered!") - loadBudibase() - } if (window["##BUDIBASE_CUSTOM_COMPONENTS##"]) { window["##BUDIBASE_CUSTOM_COMPONENTS##"].forEach(component => { componentStore.actions.registerCustomComponent(component) }) } + // Make a callback available for custom component bundles to register + // themselves at runtime + window.registerCustomComponent = + componentStore.actions.registerCustomComponent + // Create app if one hasn't been created yet if (!app) { app = new ClientApp({ From e9fc30afda1d2975eccc45495c4c4c401e53dbcb Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 12 Aug 2022 14:41:37 +0100 Subject: [PATCH 6/7] Overwrite existing plugins of the same name and version --- packages/server/src/api/controllers/plugin.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/controllers/plugin.ts b/packages/server/src/api/controllers/plugin.ts index f76d2871b0..4afa59cd3a 100644 --- a/packages/server/src/api/controllers/plugin.ts +++ b/packages/server/src/api/controllers/plugin.ts @@ -32,19 +32,18 @@ export async function upload(ctx: any) { } const jsFileName = jsFile.name const pluginId = generatePluginID(name, version) - let existing + + // overwrite existing docs entirely if they exist + let rev try { - existing = await db.get(pluginId) + const existing = await db.get(pluginId) + rev = existing._rev } catch (err) { - existing = null - } - if (existing) { - throw new Error( - `Plugin already exists: name: ${name}, version: ${version}` - ) + rev = null } const doc = { _id: pluginId, + _rev: rev, name, version, description, From 449905116eeb0eb10622e1ab5e65e64d8285661d Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 12 Aug 2022 16:19:29 +0100 Subject: [PATCH 7/7] Fix rev when uploading a new plugin --- packages/server/src/api/controllers/plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/plugin.ts b/packages/server/src/api/controllers/plugin.ts index 4afa59cd3a..6b9a2f8803 100644 --- a/packages/server/src/api/controllers/plugin.ts +++ b/packages/server/src/api/controllers/plugin.ts @@ -39,7 +39,7 @@ export async function upload(ctx: any) { const existing = await db.get(pluginId) rev = existing._rev } catch (err) { - rev = null + rev = undefined } const doc = { _id: pluginId,