Skip to content

Commit 7ab0dc1

Browse files
authored
Merge pull request #585 from chaibuilder/improvments-to-register-apis
Improvments to register apis
2 parents 47f2968 + f52f5cc commit 7ab0dc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+395
-388
lines changed

docs/registering-custom-blocks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const Config = {
5050
category: "core",
5151
group: "basic",
5252
description: "A clickable button element",
53-
...registerChaiBlockSchema({
53+
props: registerChaiBlockProps({
5454
properties: {
5555
styles: StylesProp(""),
5656
text: {
@@ -287,7 +287,7 @@ const Config = {
287287
category: "core",
288288
group: "layout",
289289
canAcceptBlock: () => true,
290-
...registerChaiBlockSchema({
290+
props: registerChaiBlockProps({
291291
properties: {
292292
styles: StylesProp("p-4 border rounded-lg"),
293293
},

src/actions/classes/chai-ai-chat-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AIChatOptions, ChaiBuilderPagesAIInterface } from "@/actions/export";
1+
import { AIChatOptions, ChaiBuilderPagesAIInterface } from "@/types/actions";
22
import { streamText, StreamTextResult } from "ai";
33
import { noop } from "lodash-es";
44
import { getAskAiSystemPrompt } from "./system-prompt";

src/actions/export.ts

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,6 @@
1-
import { StreamTextResult } from "ai";
21
import ChaiActionsRegistry from "./builder/actions-registery";
32
import { getAskAiSystemPrompt } from "./classes/system-prompt";
43

5-
export interface ChaiLoggedInUser {
6-
id: string;
7-
email: string;
8-
name: string;
9-
avatar?: string;
10-
}
11-
12-
export type ChaiUserInfo = {
13-
id: string;
14-
email: string;
15-
name: string;
16-
avatar?: string;
17-
};
18-
19-
export type ChaiAsset = {
20-
id: string;
21-
name: string;
22-
url: string;
23-
type: string;
24-
createdAt: string;
25-
updatedAt?: string;
26-
createdBy: string;
27-
size?: number;
28-
folderId: string | null;
29-
30-
// Optional
31-
thumbnailUrl?: string;
32-
description?: string;
33-
duration?: number;
34-
format?: string;
35-
36-
width?: number;
37-
height?: number;
38-
};
39-
40-
export type AssetsParams = {
41-
search: string;
42-
limit: number;
43-
page: number;
44-
};
45-
46-
export type AIChatOptions = {
47-
messages: any[];
48-
image?: string;
49-
systemPrompt?: string;
50-
initiator?: string | null;
51-
model?: string;
52-
};
53-
54-
export interface ChaiBuilderPagesAIInterface {
55-
handleRequest(options: AIChatOptions, res: any): Promise<StreamTextResult<any, any>>;
56-
isConfigured(): boolean;
57-
}
58-
59-
export * from "../types/actions";
604
export { initChaiBuilderActionHandler } from "./builder/chai-builder-actions-handler";
615
export { db, safeQuery, schema } from "./db";
626
export { LANGUAGES } from "./LANGUAGES";

src/core/components/canvas/dnd/drag-and-drop/hooks/use-block-drop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { useCanvasIframe } from "@/hooks/use-canvas-iframe";
2424
import { useSelectedBlockIds } from "@/hooks/use-selected-blockIds";
2525
import { useSelectedStylingBlocks } from "@/hooks/use-selected-styling-blocks";
2626
import { useUpdateBlocksProps } from "@/hooks/use-update-blocks-props";
27-
import { syncBlocksWithDefaults } from "@/runtime";
27+
import { syncBlocksWithDefaultProps } from "@/runtime";
2828
import { ChaiBlock } from "@/types/common";
2929
import { useAtom } from "jotai";
3030
import { filter, find, get, has, isFunction } from "lodash-es";
@@ -176,7 +176,7 @@ export const useBlockDrop = () => {
176176
const preBlocks = !draggedBlock?.blocks
177177
? null
178178
: isFunction(draggedBlock?.blocks)
179-
? syncBlocksWithDefaults(draggedBlock?.blocks())
179+
? syncBlocksWithDefaultProps(draggedBlock?.blocks())
180180
: draggedBlock?.blocks;
181181

182182
if (draggedBlockType === "PartialBlock") {

src/core/components/canvas/static/apply-language.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { ChaiBlockDefinition } from "@/types/blocks";
1+
import { ChaiBlockConfig } from "@/types/blocks";
22
import { ChaiBlock } from "@/types/common";
33
import { applyLanguage } from "./new-blocks-render-helpers";
44

55
describe("applyLanguage", () => {
6-
const mockChaiBlock: ChaiBlockDefinition = {
6+
const mockChaiBlock: ChaiBlockConfig = {
77
i18nProps: ["title", "content", "buttonText"],
8-
component: () => null,
98
type: "mock",
109
label: "Mock Block",
1110
group: "Mock",

src/core/components/canvas/static/new-blocks-render-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { DESIGN_TOKEN_PREFIX, STYLES_KEY } from "@/core/constants/STRINGS";
22
import { getSplitChaiClasses } from "@/hooks/get-split-classes";
33
import { getRegisteredChaiBlock } from "@/runtime";
4-
import { ChaiBlockDefinition } from "@/types/blocks";
4+
import { ChaiBlockConfig } from "@/types/blocks";
55
import { ChaiBlock } from "@/types/common";
66
import { ChaiDesignTokens } from "@/types/types";
77
import { cloneDeep, forEach, get, includes, isArray, isEmpty, isString, keys, memoize, startsWith } from "lodash-es";
88
import { twMerge } from "tailwind-merge";
99

10-
export function applyLanguage(_block: ChaiBlock, selectedLang: string, chaiBlock: ChaiBlockDefinition) {
10+
export function applyLanguage(_block: ChaiBlock, selectedLang: string, chaiBlock: ChaiBlockConfig) {
1111
const i18nProps = get(chaiBlock, "i18nProps", []);
1212
if (isEmpty(selectedLang) || isEmpty(i18nProps)) return _block;
1313

src/core/components/chaibuilder-editor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { isPageLoadedAtom } from "@/hooks/use-is-page-loaded";
2323
import { useKeyEventWatcher } from "@/hooks/use-key-event-watcher";
2424
import { useWatchPartailBlocks } from "@/hooks/use-partial-blocks-store";
2525
import { builderSaveStateAtom } from "@/hooks/use-save-page";
26-
import { syncBlocksWithDefaults } from "@/runtime";
26+
import { syncBlocksWithDefaultProps } from "@/runtime";
2727
import { ChaiBuilderEditorProps } from "@/types";
2828
import { ChaiBuilderThemeValues } from "@/types/types";
2929
import { useAtom } from "jotai";
@@ -66,7 +66,7 @@ const ChaiWatchers = (props: ChaiBuilderEditorProps) => {
6666
setIsPageLoaded(false);
6767
// Added delay to allow the pageId to be set
6868
setTimeout(() => {
69-
const withDefaults = syncBlocksWithDefaults(props.blocks || []);
69+
const withDefaults = syncBlocksWithDefaultProps(props.blocks || []);
7070
// @ts-ignore
7171
setAllBlocks(withDefaults);
7272
if (withDefaults && withDefaults.length > 0) {

src/core/components/settings/block-settings.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ import { useSelectedBlock } from "@/hooks/use-selected-blockIds";
55
import { useUpdateBlocksProps, useUpdateBlocksPropsRealtime } from "@/hooks/use-update-blocks-props";
66
import { useWrapperBlock } from "@/hooks/use-wrapper-block";
77
import { getBlockFormSchemas, getRegisteredChaiBlock } from "@/runtime";
8-
import { ChaiBlockDefinition } from "@/types/blocks";
8+
import { ChaiBlockConfig } from "@/types/blocks";
99
import { ChaiBlock } from "@/types/common";
1010
import { ChevronDownIcon, ChevronRightIcon } from "@radix-ui/react-icons";
1111
import { IChangeEvent } from "@rjsf/core";
1212
import { cloneDeep, debounce, forEach, get, includes, isEmpty, keys, set, startCase, startsWith } from "lodash-es";
1313
import { useCallback, useMemo, useState } from "react";
1414

15-
const formDataWithSelectedLang = (
16-
formData: Record<string, any>,
17-
selectedLang: string,
18-
coreBlock: ChaiBlockDefinition,
19-
) => {
15+
const formDataWithSelectedLang = (formData: Record<string, any>, selectedLang: string, coreBlock: ChaiBlockConfig) => {
2016
const updatedFormData = cloneDeep(formData);
2117
forEach(keys(formData), (key) => {
2218
if (includes(get(coreBlock, "i18nProps", []), key) && !isEmpty(selectedLang)) {
@@ -35,13 +31,13 @@ export default function BlockSettings() {
3531
const selectedBlock = useSelectedBlock() as any;
3632
const updateBlockPropsRealtime = useUpdateBlocksPropsRealtime();
3733
const updateBlockProps = useUpdateBlocksProps();
38-
const registeredBlock = getRegisteredChaiBlock(selectedBlock?._type) as ChaiBlockDefinition;
34+
const registeredBlock = getRegisteredChaiBlock(selectedBlock?._type) as ChaiBlockConfig;
3935
const formData = formDataWithSelectedLang(selectedBlock, selectedLang, registeredBlock);
4036
const [prevFormData, setPrevFormData] = useState(formData);
4137

4238
const [showWrapperSetting, setShowWrapperSetting] = useState(false);
4339
const wrapperBlock = useWrapperBlock() as ChaiBlock;
44-
const registeredWrapperBlock = getRegisteredChaiBlock(wrapperBlock?._type) as ChaiBlockDefinition;
40+
const registeredWrapperBlock = getRegisteredChaiBlock(wrapperBlock?._type) as ChaiBlockConfig;
4541
const wrapperFormData = formDataWithSelectedLang(wrapperBlock, selectedLang, registeredWrapperBlock);
4642

4743
const updateProps = ({ formData: newData }: IChangeEvent, prop?: string, oldState?: any) => {

src/core/components/sidepanels/panels/add-blocks/core-block.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useDragAndDrop, useIsDragAndDropEnabled } from "@/core/components/canva
33
import { CHAI_BUILDER_EVENTS } from "@/core/events";
44
import { pubsub } from "@/core/pubsub";
55
import { useAddBlock } from "@/hooks/use-add-block";
6-
import { syncBlocksWithDefaults } from "@/runtime";
6+
import { syncBlocksWithDefaultProps } from "@/runtime";
77
import { BoxIcon } from "@radix-ui/react-icons";
88
import { capitalize, has, isFunction, kebabCase } from "lodash-es";
99
import { createElement } from "react";
@@ -25,7 +25,7 @@ export const CoreBlock = ({
2525
const addBlockToPage = () => {
2626
if (has(block, "blocks")) {
2727
const blocks = isFunction(block.blocks) ? block.blocks() : block.blocks;
28-
addPredefinedBlock(syncBlocksWithDefaults(blocks), parentId || null, position);
28+
addPredefinedBlock(syncBlocksWithDefaultProps(blocks), parentId || null, position);
2929
} else {
3030
addCoreBlock(block, parentId || null, position);
3131
}

src/core/components/sidepanels/panels/add-blocks/import-html.test.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { getBlocksFromHTML } from "@/core/import-html/html-to-json";
2-
import { syncBlocksWithDefaults } from "@/runtime";
2+
import { syncBlocksWithDefaultProps } from "@/runtime";
33
import { describe, expect, it, vi } from "vitest";
44

55
// Mock the runtime module
66
vi.mock("@/runtime", () => ({
7-
syncBlocksWithDefaults: vi.fn((blocks) => blocks),
7+
syncBlocksWithDefaultProps: vi.fn((blocks) => blocks),
88
}));
99

10-
describe("ImportHTML - syncBlocksWithDefaults integration", () => {
11-
it("should call syncBlocksWithDefaults when importing HTML with empty heading", () => {
10+
describe("ImportHTML - syncBlocksWithDefaultProps integration", () => {
11+
it("should call syncBlocksWithDefaultProps when importing HTML with empty heading", () => {
1212
// HTML snippet with an empty heading (common scenario that causes the crash)
1313
const html = '<h1 class="text-2xl"></h1>';
1414

1515
const blocks = getBlocksFromHTML(html);
16-
const syncedBlocks = syncBlocksWithDefaults(blocks);
16+
const syncedBlocks = syncBlocksWithDefaultProps(blocks);
1717

18-
// Verify that syncBlocksWithDefaults was called
19-
expect(vi.mocked(syncBlocksWithDefaults)).toHaveBeenCalledWith(blocks);
18+
// Verify that syncBlocksWithDefaultProps was called
19+
expect(vi.mocked(syncBlocksWithDefaultProps)).toHaveBeenCalledWith(blocks);
2020
expect(syncedBlocks).toBeDefined();
2121
});
2222

23-
it("should call syncBlocksWithDefaults when importing HTML with multiple blocks", () => {
23+
it("should call syncBlocksWithDefaultProps when importing HTML with multiple blocks", () => {
2424
const html = `
2525
<div class="container">
2626
<h1></h1>
@@ -30,19 +30,19 @@ describe("ImportHTML - syncBlocksWithDefaults integration", () => {
3030
`;
3131

3232
const blocks = getBlocksFromHTML(html);
33-
const syncedBlocks = syncBlocksWithDefaults(blocks);
33+
const syncedBlocks = syncBlocksWithDefaultProps(blocks);
3434

35-
expect(vi.mocked(syncBlocksWithDefaults)).toHaveBeenCalled();
35+
expect(vi.mocked(syncBlocksWithDefaultProps)).toHaveBeenCalled();
3636
expect(syncedBlocks).toBeDefined();
3737
});
3838

3939
it("should handle paragraphs with empty content", () => {
4040
const html = '<p class="text-base"></p>';
4141

4242
const blocks = getBlocksFromHTML(html);
43-
const syncedBlocks = syncBlocksWithDefaults(blocks);
43+
const syncedBlocks = syncBlocksWithDefaultProps(blocks);
4444

45-
expect(vi.mocked(syncBlocksWithDefaults)).toHaveBeenCalledWith(blocks);
45+
expect(vi.mocked(syncBlocksWithDefaultProps)).toHaveBeenCalledWith(blocks);
4646
expect(syncedBlocks).toBeDefined();
4747
});
4848

@@ -65,9 +65,9 @@ describe("ImportHTML - syncBlocksWithDefaults integration", () => {
6565
`;
6666

6767
const blocks = getBlocksFromHTML(html);
68-
const syncedBlocks = syncBlocksWithDefaults(blocks);
68+
const syncedBlocks = syncBlocksWithDefaultProps(blocks);
6969

70-
expect(vi.mocked(syncBlocksWithDefaults)).toHaveBeenCalled();
70+
expect(vi.mocked(syncBlocksWithDefaultProps)).toHaveBeenCalled();
7171
expect(syncedBlocks).toBeDefined();
7272
});
7373
});

0 commit comments

Comments
 (0)