Files
directus/app/src/extensions.ts
Nitwel 2ac022d286 Extension Improvements (#16822)
* add link command and small improvements

* put local bundles into own folder on link

* get rid of packs and add bundle support for local extensions

* make bundle type extensions work locally and remove traces of pack

* fix hot reloading of bundles

* fix app.js not refreshing

* fixed linter errors

* add endpoint to install extensions

* update package.json validation and support top level extensions

* update endpoints

* added some URL escapes and ran linter

* remove installation part

* readd endpoint

* update dependencies

* fix types and validation in extension-sdk

* run linter

* fix linter

* add defaults to manifest

* Added missing constant export

* ensure all the extension folders

* ignore unneeded vite error

* update linking process

* run parser separate

* add await

* fixed linter errors

Co-authored-by: Brainslug <tim@brainslug.nl>
Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>
2023-01-04 15:20:33 +01:00

100 lines
3.2 KiB
TypeScript

import { AppExtensionConfigs, RefRecord } from '@directus/shared/types';
import { App, shallowRef, watch } from 'vue';
import { getInternalDisplays, registerDisplays } from './displays';
import { getInternalInterfaces, registerInterfaces } from './interfaces';
import { i18n } from './lang';
import { getInternalLayouts, registerLayouts } from './layouts';
import { getInternalModules, registerModules } from './modules';
import { getInternalOperations, registerOperations } from './operations';
import { getInternalPanels, registerPanels } from './panels';
import { getRootPath } from './utils/get-root-path';
import { translate } from './utils/translate-object-values';
let customExtensions: AppExtensionConfigs | null = null;
const extensions: RefRecord<AppExtensionConfigs> = {
interfaces: shallowRef([]),
displays: shallowRef([]),
layouts: shallowRef([]),
modules: shallowRef([]),
panels: shallowRef([]),
operations: shallowRef([]),
};
const onHydrateCallbacks: (() => Promise<void>)[] = [];
const onDehydrateCallbacks: (() => Promise<void>)[] = [];
export async function loadExtensions(): Promise<void> {
try {
customExtensions = import.meta.env.DEV
? await import(/* @vite-ignore */ '@directus-extensions')
: await import(/* @vite-ignore */ `${getRootPath()}extensions/sources/index.js`);
} catch (err: any) {
// eslint-disable-next-line no-console
console.warn(`Couldn't load extensions`);
// eslint-disable-next-line no-console
console.warn(err);
}
}
export function registerExtensions(app: App): void {
const interfaces = getInternalInterfaces();
const displays = getInternalDisplays();
const layouts = getInternalLayouts();
const modules = getInternalModules();
const panels = getInternalPanels();
const operations = getInternalOperations();
if (customExtensions !== null) {
interfaces.push(...customExtensions.interfaces);
displays.push(...customExtensions.displays);
layouts.push(...customExtensions.layouts);
modules.push(...customExtensions.modules);
panels.push(...customExtensions.panels);
operations.push(...customExtensions.operations);
}
registerInterfaces(interfaces, app);
registerDisplays(displays, app);
registerLayouts(layouts, app);
registerPanels(panels, app);
registerOperations(operations, app);
watch(
i18n.global.locale,
() => {
extensions.interfaces.value = translate(interfaces);
extensions.displays.value = translate(displays);
extensions.layouts.value = translate(layouts);
extensions.panels.value = translate(panels);
extensions.operations.value = translate(operations);
},
{ immediate: true }
);
const { registeredModules, onHydrateModules, onDehydrateModules } = registerModules(modules);
watch(
[i18n.global.locale, registeredModules],
() => {
extensions.modules.value = translate(registeredModules.value);
},
{ immediate: true }
);
onHydrateCallbacks.push(onHydrateModules);
onDehydrateCallbacks.push(onDehydrateModules);
}
export async function onHydrateExtensions() {
await Promise.all(onHydrateCallbacks.map((onHydrate) => onHydrate()));
}
export async function onDehydrateExtensions() {
await Promise.all(onDehydrateCallbacks.map((onDehydrate) => onDehydrate()));
}
export function useExtensions(): RefRecord<AppExtensionConfigs> {
return extensions;
}