Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ui): move custom components - WF-63 #615

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/framework/custom-components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,17 @@ The code above will make Bubble Message available in the Builder.

<Tip>
You can also have a look at the built-in component templates, since their
syntax is equivalent. They can be found in the `ui/src/components/` folder.
syntax is equivalent. They can be found in the `ui/src/components/core` folder.
</Tip>
Go to `ui/src/custom_components/` and open the Vue single-file components, i.e. the
Go to `ui/src/components/custom` and open the Vue single-file components, i.e. the
`.vue` files. These files contain comments that will help you get started. Try editing
the provided templates, you should see changes reflected.

You can get started by duplicating one of these examples. Make sure you add the new template to the entrypoint, as discussed below.

### Define entrypoint

For custom component templates to be taken into account, they need to be accessible from the entrypoint. Edit `ui/src/custom_components/index.ts` to define which templates you wish to export and under which identifiers.
For custom component templates to be taken into account, they need to be accessible from the entrypoint. Edit `ui/src/components/custom/index.ts` to define which templates you wish to export and under which identifiers.

```ts
// Import the templates
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Import the templates

import type { TemplateMap } from "@/writerTypes";
import BubbleMessage from "./BubbleMessage.vue";
import BubbleMessageAdvanced from "./BubbleMessageAdvanced.vue";

// Export an object with the ids and the templates as default

export default {
const CUSTOM_COMPONENTS: TemplateMap = {
bubblemessage: BubbleMessage,
bubblemessageadvanced: BubbleMessageAdvanced,
};

export default CUSTOM_COMPONENTS;
22 changes: 15 additions & 7 deletions src/ui/src/core/templateMap.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Component as VueComponent } from "vue";
// Maps Writer Framework component types to renderable Vue components
// content
import CoreDataframe from "../components/core/content/CoreDataframe.vue";
Expand Down Expand Up @@ -69,10 +70,14 @@ import WorkflowsWorkflow from "../components/workflows/WorkflowsWorkflow.vue";
import WorkflowsNode from "../components/workflows/abstract/WorkflowsNode.vue";
import WorkflowsRoot from "@/components/workflows/WorkflowsRoot.vue";

import { AbstractTemplate, WriterComponentDefinition } from "@/writerTypes";
import type {
AbstractTemplate,
TemplateMap,
WriterComponentDefinition,
} from "@/writerTypes";
import { h } from "vue";

const templateMap = {
const templateMap: TemplateMap = {
root: CoreRoot,
page: CorePage,
sidebar: CoreSidebar,
Expand Down Expand Up @@ -135,14 +140,14 @@ const templateMap = {

const abstractTemplateMap: Record<string, AbstractTemplate> = {};

// eslint-disable-next-line no-undef
if (WRITER_LIVE_CCT === "yes") {
/*
Assigns the components in custom_components to the template map,
Assigns the components in `components/custom` to the template map,
allowing for live updates when developing custom component templates.
*/

const liveCCT: Record<string, any> = (await import("../custom_components"))
.default;
const liveCCT = (await import("../components/custom")).default;
Object.entries(liveCCT).forEach(([componentType, template]) => {
templateMap[`custom_${componentType}`] = template;
});
Expand All @@ -157,7 +162,7 @@ function fallbackTemplate(type: string) {
description: message,
category: "Fallback",
},
setup(props, { slots }) {
setup(_props: never, { slots }) {
return () => {
return h(
"div",
Expand Down Expand Up @@ -212,7 +217,10 @@ export function getSupportedComponentTypes() {
return [...Object.keys(templateMap), ...Object.keys(abstractTemplateMap)];
}

export function registerComponentTemplate(type: string, vueComponent: any) {
export function registerComponentTemplate(
type: string,
vueComponent: VueComponent,
) {
templateMap[type] = vueComponent;
}

Expand Down
14 changes: 12 additions & 2 deletions src/ui/src/writerTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Component as VueComponent } from "vue";
import { generateCore } from "./core";
import { generateBuilderManager } from "./builder/builderManager";

Expand Down Expand Up @@ -101,8 +102,15 @@ export type WriterComponentDefinition = {
>;
previewField?: string; // Which field to use for previewing in the Component Tree
positionless?: boolean; // Whether this type of component is positionless (like Sidebar)
outs?:
Record<string, { name: string; description: string; style: string; field?: keyof WriterComponentDefinition["fields"] }>;
outs?: Record<
string,
{
name: string;
description: string;
style: string;
field?: keyof WriterComponentDefinition["fields"];
}
>;
};

export type BuilderManager = ReturnType<typeof generateBuilderManager>;
Expand Down Expand Up @@ -154,3 +162,5 @@ export type AbstractTemplate = {
baseType: string;
writer: WriterComponentDefinition;
};

export type TemplateMap = Record<string, VueComponent>;
4 changes: 2 additions & 2 deletions src/ui/tools/custom_check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { importVue } from "./core.mjs";

async function checkDeclarationKey() {
let hasFailed = false;
const module = await importVue("../src/custom_components/index.ts");
const module = await importVue("../src/components/custom/index.ts");
const { checkComponentKey } = await importVue(
"../src/core/loadExtensions.ts",
);
Expand All @@ -17,7 +17,7 @@ async function checkDeclarationKey() {
if (invalidCustomComponentKeys.length !== 0) {
// eslint-disable-next-line no-console
console.error(
`ERROR: Invalid component declaration: ${invalidCustomComponentKeys} into 'src/custom_components/index.ts'. Their key must be declared using only lowercase and alphanumeric characters.`,
`ERROR: Invalid component declaration: ${invalidCustomComponentKeys} into 'src/components/custom/index.ts'. Their key must be declared using only lowercase and alphanumeric characters.`,
);
}
return hasFailed;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/vite.config.custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineConfig({
publicDir: false,
build: {
lib: {
entry: ["./src/custom_components"],
entry: ["./src/components/custom"],
formats: ["umd"],
name: "WriterCustomComponentTemplates",
fileName: (format, entryalias: string): string => {
Expand Down
Loading