|
1 | | -import { v1 as uuid } from 'uuid'; |
2 | | - |
3 | | -import type { |
4 | | - IActionsBlock, |
5 | | - IBlock, |
6 | | - IConditionalBlock, |
7 | | - IConditionalBlockFilters, |
8 | | - IContextBlock, |
9 | | - IImageBlock, |
10 | | - IInputBlock, |
11 | | - ISectionBlock, |
12 | | -} from '@rocket.chat/apps-engine/definition/uikit/blocks/Blocks'; |
13 | | -import { BlockType } from '@rocket.chat/apps-engine/definition/uikit/blocks/Blocks'; |
14 | | -import type { |
15 | | - IBlockElement, |
16 | | - IButtonElement, |
17 | | - IImageElement, |
18 | | - IInputElement, |
19 | | - IInteractiveElement, |
20 | | - IMultiStaticSelectElement, |
21 | | - IOverflowMenuElement, |
22 | | - IPlainTextInputElement, |
23 | | - ISelectElement, |
24 | | - IStaticSelectElement, |
25 | | -} from '@rocket.chat/apps-engine/definition/uikit/blocks/Elements'; |
26 | | -import { BlockElementType } from '@rocket.chat/apps-engine/definition/uikit/blocks/Elements'; |
27 | | -import { TextObjectType, type ITextObject } from '@rocket.chat/apps-engine/definition/uikit/blocks/Objects'; |
| 1 | +import { BlockBuilder as AppsEngineBlockBuilder } from '@rocket.chat/apps-engine/definition/uikit/blocks/BlockBuilder'; |
28 | 2 |
|
29 | 3 | import { AppObjectRegistry } from '../../../AppObjectRegistry'; |
30 | 4 |
|
31 | | -type BlockFunctionParameter<T extends IBlock> = Omit<T, 'type'>; |
32 | | -type ElementFunctionParameter<T extends IBlockElement> = T extends IInteractiveElement ? Omit<T, 'type' | 'actionId'> | Partial<Pick<T, 'actionId'>> |
33 | | - : Omit<T, 'type'>; |
34 | | - |
35 | | -type SectionBlockParam = BlockFunctionParameter<ISectionBlock>; |
36 | | -type ImageBlockParam = BlockFunctionParameter<IImageBlock>; |
37 | | -type ActionsBlockParam = BlockFunctionParameter<IActionsBlock>; |
38 | | -type ContextBlockParam = BlockFunctionParameter<IContextBlock>; |
39 | | -type InputBlockParam = BlockFunctionParameter<IInputBlock>; |
40 | | - |
41 | | -type ButtonElementParam = ElementFunctionParameter<IButtonElement>; |
42 | | -type ImageElementParam = ElementFunctionParameter<IImageElement>; |
43 | | -type OverflowMenuElementParam = ElementFunctionParameter<IOverflowMenuElement>; |
44 | | -type PlainTextInputElementParam = ElementFunctionParameter<IPlainTextInputElement>; |
45 | | -type StaticSelectElementParam = ElementFunctionParameter<IStaticSelectElement>; |
46 | | -type MultiStaticSelectElementParam = ElementFunctionParameter<IMultiStaticSelectElement>; |
47 | | - |
48 | 5 | /** |
| 6 | + * Local BlockBuilder that extends the apps-engine BlockBuilder. |
| 7 | + * It overrides the constructor to source the appId from the registry |
| 8 | + * instead of requiring it as a constructor argument. |
| 9 | + * |
49 | 10 | * @deprecated please prefer the rocket.chat/ui-kit components |
50 | 11 | */ |
51 | | -export class BlockBuilder { |
52 | | - private readonly blocks: Array<IBlock>; |
53 | | - private readonly appId: string; |
54 | | - |
| 12 | +export class BlockBuilder extends AppsEngineBlockBuilder { |
55 | 13 | constructor() { |
56 | | - this.blocks = []; |
57 | | - this.appId = String(AppObjectRegistry.get('id')); |
58 | | - } |
59 | | - |
60 | | - public addSectionBlock(block: SectionBlockParam): BlockBuilder { |
61 | | - this.addBlock({ type: BlockType.SECTION, ...block } as ISectionBlock); |
62 | | - |
63 | | - return this; |
64 | | - } |
65 | | - |
66 | | - public addImageBlock(block: ImageBlockParam): BlockBuilder { |
67 | | - this.addBlock({ type: BlockType.IMAGE, ...block } as IImageBlock); |
68 | | - |
69 | | - return this; |
70 | | - } |
71 | | - |
72 | | - public addDividerBlock(): BlockBuilder { |
73 | | - this.addBlock({ type: BlockType.DIVIDER }); |
74 | | - |
75 | | - return this; |
76 | | - } |
77 | | - |
78 | | - public addActionsBlock(block: ActionsBlockParam): BlockBuilder { |
79 | | - this.addBlock({ type: BlockType.ACTIONS, ...block } as IActionsBlock); |
80 | | - |
81 | | - return this; |
82 | | - } |
83 | | - |
84 | | - public addContextBlock(block: ContextBlockParam): BlockBuilder { |
85 | | - this.addBlock({ type: BlockType.CONTEXT, ...block } as IContextBlock); |
86 | | - |
87 | | - return this; |
88 | | - } |
89 | | - |
90 | | - public addInputBlock(block: InputBlockParam): BlockBuilder { |
91 | | - this.addBlock({ type: BlockType.INPUT, ...block } as IInputBlock); |
92 | | - |
93 | | - return this; |
94 | | - } |
95 | | - |
96 | | - public addConditionalBlock(innerBlocks: BlockBuilder | Array<IBlock>, condition?: IConditionalBlockFilters): BlockBuilder { |
97 | | - const render = innerBlocks instanceof BlockBuilder ? innerBlocks.getBlocks() : innerBlocks; |
98 | | - |
99 | | - this.addBlock({ |
100 | | - type: BlockType.CONDITIONAL, |
101 | | - render, |
102 | | - when: condition, |
103 | | - } as IConditionalBlock); |
104 | | - |
105 | | - return this; |
106 | | - } |
107 | | - |
108 | | - public getBlocks() { |
109 | | - return this.blocks; |
110 | | - } |
111 | | - |
112 | | - public newPlainTextObject(text: string, emoji = false): ITextObject { |
113 | | - return { |
114 | | - type: TextObjectType.PLAINTEXT, |
115 | | - text, |
116 | | - emoji, |
117 | | - }; |
118 | | - } |
119 | | - |
120 | | - public newMarkdownTextObject(text: string): ITextObject { |
121 | | - return { |
122 | | - type: TextObjectType.MARKDOWN, |
123 | | - text, |
124 | | - }; |
125 | | - } |
126 | | - |
127 | | - public newButtonElement(info: ButtonElementParam): IButtonElement { |
128 | | - return this.newInteractiveElement({ |
129 | | - type: BlockElementType.BUTTON, |
130 | | - ...info, |
131 | | - } as IButtonElement); |
132 | | - } |
133 | | - |
134 | | - public newImageElement(info: ImageElementParam): IImageElement { |
135 | | - return { |
136 | | - type: BlockElementType.IMAGE, |
137 | | - ...info, |
138 | | - }; |
139 | | - } |
140 | | - |
141 | | - public newOverflowMenuElement(info: OverflowMenuElementParam): IOverflowMenuElement { |
142 | | - return this.newInteractiveElement({ |
143 | | - type: BlockElementType.OVERFLOW_MENU, |
144 | | - ...info, |
145 | | - } as IOverflowMenuElement); |
146 | | - } |
147 | | - |
148 | | - public newPlainTextInputElement(info: PlainTextInputElementParam): IPlainTextInputElement { |
149 | | - return this.newInputElement({ |
150 | | - type: BlockElementType.PLAIN_TEXT_INPUT, |
151 | | - ...info, |
152 | | - } as IPlainTextInputElement); |
153 | | - } |
154 | | - |
155 | | - public newStaticSelectElement(info: StaticSelectElementParam): IStaticSelectElement { |
156 | | - return this.newSelectElement({ |
157 | | - type: BlockElementType.STATIC_SELECT, |
158 | | - ...info, |
159 | | - } as IStaticSelectElement); |
160 | | - } |
161 | | - |
162 | | - public newMultiStaticElement(info: MultiStaticSelectElementParam): IMultiStaticSelectElement { |
163 | | - return this.newSelectElement({ |
164 | | - type: BlockElementType.MULTI_STATIC_SELECT, |
165 | | - ...info, |
166 | | - } as IMultiStaticSelectElement); |
167 | | - } |
168 | | - |
169 | | - private newInteractiveElement<T extends IInteractiveElement>(element: T): T { |
170 | | - if (!element.actionId) { |
171 | | - element.actionId = this.generateActionId(); |
172 | | - } |
173 | | - |
174 | | - return element; |
175 | | - } |
176 | | - |
177 | | - private newInputElement<T extends IInputElement>(element: T): T { |
178 | | - if (!element.actionId) { |
179 | | - element.actionId = this.generateActionId(); |
180 | | - } |
181 | | - |
182 | | - return element; |
183 | | - } |
184 | | - |
185 | | - private newSelectElement<T extends ISelectElement>(element: T): T { |
186 | | - if (!element.actionId) { |
187 | | - element.actionId = this.generateActionId(); |
188 | | - } |
189 | | - |
190 | | - return element; |
191 | | - } |
192 | | - |
193 | | - private addBlock(block: IBlock): void { |
194 | | - if (!block.blockId) { |
195 | | - block.blockId = this.generateBlockId(); |
196 | | - } |
197 | | - |
198 | | - block.appId = this.appId; |
199 | | - |
200 | | - this.blocks.push(block); |
201 | | - } |
202 | | - |
203 | | - private generateBlockId(): string { |
204 | | - return uuid(); |
205 | | - } |
206 | | - |
207 | | - private generateActionId(): string { |
208 | | - return uuid(); |
| 14 | + super(String(AppObjectRegistry.get('id') ?? '')); |
209 | 15 | } |
210 | 16 | } |
0 commit comments