slack-blocks-to-jsx 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,471 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ Message: () => Message
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+
37
+ // src/utils/date.ts
38
+ var dateToTime = (timestamp) => {
39
+ const date = new Date(timestamp);
40
+ return date.toLocaleTimeString("en-US", {
41
+ hour: "numeric",
42
+ minute: "numeric",
43
+ hour12: false
44
+ });
45
+ };
46
+
47
+ // src/utils/emojis.ts
48
+ var import_node_emoji = require("node-emoji");
49
+ var missing_emojis = {
50
+ "heart_hands": "\u{1FAF6}",
51
+ "white_frowning_face": "\u{1F641}"
52
+ };
53
+ var parseEmojis = (text) => {
54
+ return (0, import_node_emoji.emojify)(text, {
55
+ fallback: (name) => {
56
+ var _a;
57
+ return (_a = missing_emojis[name]) != null ? _a : `:${name}:`;
58
+ }
59
+ });
60
+ };
61
+
62
+ // src/utils/markdown.tsx
63
+ var import_html_react_parser = __toESM(require("html-react-parser"));
64
+ var options = {
65
+ trim: false
66
+ };
67
+ var parseMrkdwn = (text) => {
68
+ if (!text)
69
+ return null;
70
+ const pre = /```(.*?)```/g;
71
+ const code = /`(?!(?:[^<]*>|[^<>]*<\/pre>))([^`]*)`/g;
72
+ const bold = /\*(?!(?:[^<]*>|[^<>]*<\/pre>))([^*]*)\*/g;
73
+ const italic = /_(?!(?:[^<]*>|[^<>]*<\/pre>))([^_]*)_/g;
74
+ const strikethrough = /~(?!(?:[^<]*>|[^<>]*<\/pre>))([^~]*)~/g;
75
+ const blockquote = new RegExp("(?<!<pre>)&gt;(.*)", "g");
76
+ const single_newline = /(\n)/g;
77
+ const consecutive_brs = /(<br \/>){2,}/g;
78
+ let parsed = text;
79
+ parsed = parsed.replace(pre, "<pre>$1</pre>");
80
+ parsed = parsed.replace(code, "<code>$1</code>");
81
+ parsed = parsed.replace(blockquote, "<blockquote>$1</blockquote>");
82
+ parsed = parsed.replace(bold, "<b>$1</b>");
83
+ parsed = parsed.replace(italic, "<i>$1</i>");
84
+ parsed = parsed.replace(strikethrough, "<s>$1</s>");
85
+ parsed = parsed.replace(single_newline, "<br />");
86
+ parsed = parsed.replace(consecutive_brs, (match) => {
87
+ var _a, _b;
88
+ const count = ((_b = (_a = match.match(/<br \/>/g)) == null ? void 0 : _a.length) != null ? _b : 0) - 1;
89
+ let spans = "<br />";
90
+ for (let i = 0; i < count; i++) {
91
+ spans += `<br style="display:block;content:'';margin-top:8px" />`;
92
+ }
93
+ return spans;
94
+ });
95
+ return (0, import_html_react_parser.default)(parsed, options);
96
+ };
97
+
98
+ // src/header.tsx
99
+ var import_jsx_runtime = require("react/jsx-runtime");
100
+ var Header = (props) => {
101
+ const { name, time } = props;
102
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "flex gap-[5px] items-center w-full", children: [
103
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h3", { className: "font-black text-[15px]", children: name }),
104
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "h-[14px] w-[25.31px] leading-[12.5px] py-[1px] px-[3px] uppercase text-[10px] text-black-primary/[0.7] bg-black-primary/[0.13] font-semibold rounded-[2px]", children: "APP" }),
105
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "text-xs text-black-secondary uppercase leading-[17.6px]", children: dateToTime(time) })
106
+ ] });
107
+ };
108
+
109
+ // src/components/blocks/Divider.tsx
110
+ var import_jsx_runtime2 = require("react/jsx-runtime");
111
+ var Divider = (props) => {
112
+ const { data } = props;
113
+ const { block_id } = data;
114
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
115
+ "div",
116
+ {
117
+ id: block_id,
118
+ className: "border-b border-gray-primary border-solid w-full mt-1 mb-2"
119
+ }
120
+ );
121
+ };
122
+
123
+ // src/components/elements/TextObject.tsx
124
+ var import_jsx_runtime3 = require("react/jsx-runtime");
125
+ var TextObject = (props) => {
126
+ const { type, text, emoji } = props.data;
127
+ const emoji_parsed = type == "mrkdwn" ? parseEmojis(text) : emoji === false ? text : parseEmojis(text);
128
+ if (type === "plain_text")
129
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: emoji_parsed });
130
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: parseMrkdwn(emoji_parsed) });
131
+ };
132
+
133
+ // src/components/elements/ImageElement.tsx
134
+ var import_jsx_runtime4 = require("react/jsx-runtime");
135
+ var ImageElement = (props) => {
136
+ const { alt_text, image_url } = props.data;
137
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "relative w-6 h-[25.25px]", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
138
+ "img",
139
+ {
140
+ src: image_url,
141
+ alt: alt_text,
142
+ className: "w-5 h-5 rounded-[2px] overflow-hidden"
143
+ }
144
+ ) });
145
+ };
146
+
147
+ // src/components/elements/ButtonElement.tsx
148
+ var import_jsx_runtime5 = require("react/jsx-runtime");
149
+ var ButtonElement = (props) => {
150
+ const { text, action_id, url, style, accessibility_label } = props.data;
151
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
152
+ "button",
153
+ {
154
+ id: action_id,
155
+ "aria-label": accessibility_label,
156
+ className: `px-2 pt-0 pb-[1px] text-small h-[28px] min-w-[56px] border rounded whitespace-nowrap font-semibold ${style === "primary" ? "border-green-primary.3 text-green-primary" : style === "danger" ? "border-red-primary.3 text-red-primary" : "border-black-primary.3 text-black-primary"}`,
157
+ onClick: () => {
158
+ if (url) {
159
+ window.open(url, "_blank");
160
+ }
161
+ },
162
+ children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(TextObject, { data: text })
163
+ }
164
+ );
165
+ };
166
+
167
+ // src/components/elements/StaticSelectElement.tsx
168
+ var import_react = require("react");
169
+ var import_jsx_runtime6 = require("react/jsx-runtime");
170
+ var StaticSelectElement = (props) => {
171
+ const {
172
+ action_id,
173
+ options: options2,
174
+ // TODO: ADD CONFIRM DIALOG
175
+ // confirm
176
+ focus_on_load,
177
+ // TODO: ADD INITIAL OPTION
178
+ // initial_option,
179
+ // TODO: ADD OPTION GROUPS
180
+ // option_groups,
181
+ placeholder
182
+ } = props.data;
183
+ const [visible, setVisible] = (0, import_react.useState)(focus_on_load != null ? focus_on_load : false);
184
+ const [search, setSearch] = (0, import_react.useState)("");
185
+ const [selected, setSelected] = (0, import_react.useState)("");
186
+ (0, import_react.useEffect)(() => {
187
+ const handleClickOutside = (e) => {
188
+ var _a, _b;
189
+ const target = e.target;
190
+ console.log({ target });
191
+ console.log(target.id);
192
+ if (target.id !== "static_select_popup" && target.id !== "static_select_dropdown_button" && ((_a = target.parentElement) == null ? void 0 : _a.id) !== "static_select_popup" && ((_b = target.parentElement) == null ? void 0 : _b.id) !== "static_select_dropdown_button")
193
+ setVisible(false);
194
+ };
195
+ document.addEventListener("mousedown", handleClickOutside);
196
+ return () => {
197
+ document.removeEventListener("mousedown", handleClickOutside);
198
+ };
199
+ }, []);
200
+ const filteredOptions = options2.filter((option) => {
201
+ const results = option.text.text.toLowerCase().includes(search.toLowerCase());
202
+ return results;
203
+ }, []);
204
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
205
+ "div",
206
+ {
207
+ id: action_id,
208
+ className: "py-1 px-2 h-7 min-h-[28px] relative flex items-center justify-between rounded text-small w-[190px] border border-black-primary.3",
209
+ tabIndex: 0,
210
+ children: [
211
+ visible && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
212
+ "div",
213
+ {
214
+ className: "absolute top-[24px] -left-3 bg-white-secondary border border-gray-primary rounded-[6px] shadow-custom_shadow-1 w-[322px] max-w-[322px] max-h-[240px] overflow-auto py-3 z-20",
215
+ id: "static_select_popup",
216
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "flex flex-col", children: filteredOptions.map((option, i) => {
217
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
218
+ "div",
219
+ {
220
+ className: "h-7 px-6 flex items-center select-none cursor-pointer hover:bg-blue-primary hover:text-white-primary",
221
+ tabIndex: 0,
222
+ onClick: () => {
223
+ setVisible(false);
224
+ setSelected(option.value);
225
+ },
226
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(TextObject, { data: option.text })
227
+ },
228
+ i
229
+ );
230
+ }) })
231
+ }
232
+ ),
233
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
234
+ "input",
235
+ {
236
+ type: "text",
237
+ value: search,
238
+ onChange: (e) => setSearch(e.currentTarget.value),
239
+ className: "w-full py-1 pr-7 focus:outline-none",
240
+ tabIndex: -1,
241
+ "aria-label": "Select an item"
242
+ }
243
+ ),
244
+ !search && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "absolute w-full h-full pointer-events-none flex items-center text-black-primary.3", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
245
+ TextObject,
246
+ {
247
+ data: placeholder != null ? placeholder : {
248
+ type: "plain_text",
249
+ text: "Pick an option"
250
+ }
251
+ }
252
+ ) }),
253
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
254
+ "button",
255
+ {
256
+ className: "absolute right-1 top-1/2 flex justify-center items-center w-5 -translate-y-1/2 text-black-secondary z-10 h-full",
257
+ id: "static_select_dropdown_button",
258
+ onClick: () => {
259
+ setVisible((prev) => !prev);
260
+ },
261
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
262
+ "svg",
263
+ {
264
+ xmlns: "http://www.w3.org/2000/svg",
265
+ fill: "none",
266
+ viewBox: "0 0 24 24",
267
+ strokeWidth: "3",
268
+ stroke: "currentColor",
269
+ className: "w-3 h-3",
270
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
271
+ "path",
272
+ {
273
+ strokeLinecap: "round",
274
+ strokeLinejoin: "round",
275
+ d: "M19.5 8.25l-7.5 7.5-7.5-7.5"
276
+ }
277
+ )
278
+ }
279
+ )
280
+ }
281
+ )
282
+ ]
283
+ }
284
+ );
285
+ };
286
+
287
+ // src/components/blocks/Section.tsx
288
+ var import_jsx_runtime7 = require("react/jsx-runtime");
289
+ var Section = (props) => {
290
+ const { accessory, fields, text, block_id } = props.data;
291
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
292
+ "div",
293
+ {
294
+ id: block_id,
295
+ className: "mt-2 mb-1 text-primary flex w-full text-black-primary",
296
+ children: [
297
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "grow", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "flex flex-col gap-3", children: [
298
+ text && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(TextObject, { data: text }),
299
+ fields && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "grid grid-col-2 w-full", children: fields.map((field, i) => {
300
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(TextObject, { data: field }, i);
301
+ }) })
302
+ ] }) }),
303
+ accessory && accessory.type === "image" && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "ml-2 mb-1 relative shrink-0", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
304
+ "img",
305
+ {
306
+ src: accessory.image_url,
307
+ alt: accessory.alt_text,
308
+ className: "w-[88px] h-[88px] object-cover ml-1 rounded-[8px] overflow-hidden"
309
+ }
310
+ ) })
311
+ ]
312
+ }
313
+ );
314
+ };
315
+
316
+ // src/components/blocks/Image.tsx
317
+ var import_react2 = require("react");
318
+ var import_jsx_runtime8 = require("react/jsx-runtime");
319
+ var Image = (props) => {
320
+ const {
321
+ block_id,
322
+ alt_text,
323
+ image_url,
324
+ image_bytes,
325
+ image_height,
326
+ image_width
327
+ } = props.data;
328
+ const [showImage, setShowImage] = (0, import_react2.useState)(true);
329
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { id: block_id, className: "my-2 flex flex-col gap-2", children: [
330
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "text-black-secondary text-small flex gap-1 items-center", children: [
331
+ image_bytes && `(${Math.round(image_bytes / 1e3)} kB)`,
332
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
333
+ "button",
334
+ {
335
+ className: "w-[15px] h-[15px]",
336
+ onClick: () => setShowImage((prev) => !prev),
337
+ children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
338
+ "svg",
339
+ {
340
+ xmlns: "http://www.w3.org/2000/svg",
341
+ fillRule: "evenodd",
342
+ clipRule: "evenodd",
343
+ className: "text-blue-primary",
344
+ imageRendering: "optimizeQuality",
345
+ shapeRendering: "geometricPrecision",
346
+ textRendering: "geometricPrecision",
347
+ viewBox: "0 0 512 336.36",
348
+ width: 6,
349
+ height: 6,
350
+ style: {
351
+ transform: showImage ? "rotate(0deg)" : "rotate(-90deg)"
352
+ },
353
+ children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
354
+ "path",
355
+ {
356
+ fill: "currentColor",
357
+ fillRule: "nonzero",
358
+ d: "M42.47.01L469.5 0C492.96 0 512 19.04 512 42.5c0 11.07-4.23 21.15-11.17 28.72L294.18 320.97c-14.93 18.06-41.7 20.58-59.76 5.65-1.8-1.49-3.46-3.12-4.97-4.83L10.43 70.39C-4.97 52.71-3.1 25.86 14.58 10.47 22.63 3.46 32.57.02 42.47.01z"
359
+ }
360
+ )
361
+ }
362
+ )
363
+ }
364
+ )
365
+ ] }),
366
+ showImage && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "rounded-md overflow-hidden relative max-w-max", children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
367
+ "img",
368
+ {
369
+ src: image_url,
370
+ style: {
371
+ width: image_width || 400,
372
+ height: image_height || 300
373
+ },
374
+ alt: alt_text
375
+ }
376
+ ) })
377
+ ] });
378
+ };
379
+
380
+ // src/components/blocks/Context.tsx
381
+ var import_jsx_runtime9 = require("react/jsx-runtime");
382
+ var Context = (props) => {
383
+ const { elements, block_id } = props.data;
384
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
385
+ "div",
386
+ {
387
+ id: block_id,
388
+ className: "my-1 text-primary flex w-full text-black-primary flex-wrap items-center",
389
+ children: elements.map((element, i) => {
390
+ if (element.type !== "image") {
391
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
392
+ "span",
393
+ {
394
+ className: "text-black-secondary text-small h-[25.25px] pr-3 flex items-center",
395
+ children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(TextObject, { data: element })
396
+ },
397
+ i
398
+ );
399
+ }
400
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(ImageElement, { data: element }) }, i);
401
+ })
402
+ }
403
+ );
404
+ };
405
+
406
+ // src/components/blocks/Actions.tsx
407
+ var import_jsx_runtime10 = require("react/jsx-runtime");
408
+ var Actions = (props) => {
409
+ const { elements, block_id } = props.data;
410
+ return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
411
+ "div",
412
+ {
413
+ id: block_id,
414
+ className: "mb-2 text-primary flex w-full text-black-primary items-center",
415
+ children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("div", { className: "flex flex-wrap", children: elements.map((element, i) => {
416
+ if (element.type === "button") {
417
+ return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("div", { className: "mt-2 mr-2", children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(ButtonElement, { data: element }) }, i);
418
+ }
419
+ if (element.type === "static_select") {
420
+ return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("div", { className: "mt-2 mr-2", children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(StaticSelectElement, { data: element }) }, i);
421
+ }
422
+ return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("div", { className: "mt-2 mr-2" }, i);
423
+ }) })
424
+ }
425
+ );
426
+ };
427
+
428
+ // src/components/index.tsx
429
+ var import_jsx_runtime11 = require("react/jsx-runtime");
430
+ var getBlockComponent = (block) => {
431
+ if (block.type === "divider")
432
+ return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(Divider, { data: block });
433
+ if (block.type === "section")
434
+ return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(Section, { data: block });
435
+ if (block.type === "image")
436
+ return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(Image, { data: block });
437
+ if (block.type === "context")
438
+ return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(Context, { data: block });
439
+ if (block.type === "actions")
440
+ return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(Actions, { data: block });
441
+ return null;
442
+ };
443
+
444
+ // src/block-wrapper.tsx
445
+ var import_jsx_runtime12 = require("react/jsx-runtime");
446
+ var BlockWrapper = (props) => {
447
+ const { children } = props;
448
+ return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("div", { className: "text-base break-words font-normal", children });
449
+ };
450
+
451
+ // src/message.tsx
452
+ var import_jsx_runtime13 = require("react/jsx-runtime");
453
+ var Message = (props) => {
454
+ const { theme = "light", blocks, logo, name, time = /* @__PURE__ */ new Date() } = props;
455
+ return /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("section", { className: "flex gap-2 w-full max-w-[600px] slack-message", children: [
456
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("div", { className: "shrink-0", children: /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("img", { src: logo, className: "w-9 h-9", alt: name }) }),
457
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("div", { className: "flex flex-col w-full", children: [
458
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(Header, { name, time }),
459
+ blocks.map((block, i) => {
460
+ const element = getBlockComponent(block);
461
+ if (!element)
462
+ return null;
463
+ return /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(BlockWrapper, { children: element }, i);
464
+ })
465
+ ] })
466
+ ] });
467
+ };
468
+ // Annotate the CommonJS export names for ESM import in node:
469
+ 0 && (module.exports = {
470
+ Message
471
+ });