@okam/directus-block 1.2.2 → 1.2.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.
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ /// <reference types="react/experimental" />
3
+ import type { TBlockDispatcherProps } from './interface';
4
+ declare const BlockDispatcher: (props: TBlockDispatcherProps) => string | number | boolean | Iterable<import("react").ReactNode> | import("react").PromiseLikeOfReactNode | import("react/jsx-runtime").JSX.Element | (string | number | boolean | Iterable<import("react").ReactNode> | import("react").PromiseLikeOfReactNode | import("react/jsx-runtime").JSX.Element | null | undefined)[] | null | undefined;
5
+ export default BlockDispatcher;
@@ -0,0 +1,3 @@
1
+ import type { TBlockSerializerProps } from './interface';
2
+ declare const BlockSerializer: (props: TBlockSerializerProps) => import("react/jsx-runtime").JSX.Element | null;
3
+ export default BlockSerializer;
@@ -0,0 +1,117 @@
1
+ import { jsxs, Fragment, jsx } from "react/jsx-runtime";
2
+ import { Box, WysiwygBlock } from "@okam/stack-ui";
3
+ import React from "react";
4
+ import { QueryClient, QueryCache } from "@tanstack/react-query";
5
+ import { isEmpty } from "radash";
6
+ import { GraphQLClient } from "graphql-request";
7
+ const BlockSettingsFragmentDoc = { "kind": "Document", "definitions": [{ "kind": "FragmentDefinition", "name": { "kind": "Name", "value": "BlockSettings" }, "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "block_settings" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "tokens" } }, { "kind": "Field", "name": { "kind": "Name", "value": "variant" } }] } }] };
8
+ function useFragment(_documentNode, fragmentType) {
9
+ return fragmentType;
10
+ }
11
+ const GRAPHQL_ENDPOINT = process.env.NEXT_PUBLIC_GRAPHQL_URL;
12
+ const GRAPHQL_ENDPOINT_ADMIN = process.env.NEXT_GRAPHQL_URL_ADMIN;
13
+ const AUTH_TOKEN = process.env.NEXT_PUBLIC_API_TOKEN ?? "";
14
+ const AUTH_TOKEN_ADMIN = process.env.NEXT_PUBLIC_API_TOKEN;
15
+ const graphqlRequestClient = new GraphQLClient(GRAPHQL_ENDPOINT, {
16
+ credentials: "include",
17
+ mode: "cors",
18
+ fetch,
19
+ headers: {
20
+ Authorization: `Bearer ${AUTH_TOKEN}`
21
+ }
22
+ });
23
+ new GraphQLClient(GRAPHQL_ENDPOINT_ADMIN, {
24
+ credentials: "include",
25
+ mode: "cors",
26
+ fetch,
27
+ headers: {
28
+ Authorization: `Bearer ${AUTH_TOKEN_ADMIN}`
29
+ }
30
+ });
31
+ new QueryClient({
32
+ queryCache: new QueryCache({
33
+ onError: (error) => {
34
+ console.error(error);
35
+ }
36
+ }),
37
+ defaultOptions: {
38
+ queries: {
39
+ staleTime: 5 * 1e3
40
+ }
41
+ }
42
+ });
43
+ function queryGql(document, queryKey, client = graphqlRequestClient) {
44
+ return client.request(document, {
45
+ ...queryKey
46
+ });
47
+ }
48
+ process.env.NEXT_PUBLIC_GRAPHQL_URL;
49
+ process.env.NEXT_PUBLIC_API_TOKEN;
50
+ function isVariables(maybeVariables) {
51
+ return !!maybeVariables;
52
+ }
53
+ function isOnlyIdInItem(item) {
54
+ return !isEmpty(item) && Object.keys(item).length === 1 && Object.keys(item)[0] === "id" && !!item.id;
55
+ }
56
+ async function queryFromVariables(params) {
57
+ const { document, blockKey, variables } = params;
58
+ if (!document || !isVariables(variables)) return null;
59
+ const queriedBlockProps = await queryGql(document, variables);
60
+ if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
61
+ const queriedBlockFragment = queriedBlockProps[blockKey];
62
+ return queriedBlockFragment;
63
+ }
64
+ async function getBlockProps(params) {
65
+ const { document, item, blockKey, variables } = params;
66
+ if (item) {
67
+ if (!isOnlyIdInItem(item)) {
68
+ return item;
69
+ }
70
+ const variablesWithFallback = { id: item.id, ...variables };
71
+ if (!isVariables(variablesWithFallback)) return null;
72
+ return queryFromVariables({
73
+ ...params,
74
+ variables: variablesWithFallback
75
+ });
76
+ }
77
+ if (!document || !isVariables(variables)) return null;
78
+ const queriedBlockProps = await queryGql(document, variables);
79
+ if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
80
+ const queriedBlockFragment = queriedBlockProps[blockKey];
81
+ return queriedBlockFragment;
82
+ }
83
+ const BlockWysiwyg = async (props) => {
84
+ const { variables, themeName = "wysiwyg", tokens, item, document } = props;
85
+ const propsWithFallback = await getBlockProps({
86
+ item,
87
+ blockKey: "block_wysiwyg_by_id",
88
+ document,
89
+ variables
90
+ });
91
+ if (!propsWithFallback) return null;
92
+ const { content, title, level, settings } = propsWithFallback;
93
+ const { tokens: cmsTokens } = useFragment(BlockSettingsFragmentDoc, settings) ?? {};
94
+ if (!content && !(title && level)) return null;
95
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
96
+ title && level && /* @__PURE__ */ jsx(Box, { as: "span", themeName, tokens: { ...tokens, ...cmsTokens }, children: React.createElement(level, {}, title) }),
97
+ content && /* @__PURE__ */ jsx(WysiwygBlock, { themeName, tokens: { ...tokens, ...cmsTokens }, content })
98
+ ] });
99
+ };
100
+ const blockWysiwygConfig = {
101
+ block_wysiwyg: {
102
+ default: (props) => /* @__PURE__ */ jsx(BlockWysiwyg, { ...props })
103
+ }
104
+ };
105
+ const baseConfig = {
106
+ components: {
107
+ ...blockWysiwygConfig
108
+ }
109
+ };
110
+ export {
111
+ BlockWysiwyg as B,
112
+ baseConfig as a,
113
+ blockWysiwygConfig as b,
114
+ BlockSettingsFragmentDoc as c,
115
+ getBlockProps as g,
116
+ useFragment as u
117
+ };
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ const jsxRuntime = require("react/jsx-runtime");
3
+ const stackUi = require("@okam/stack-ui");
4
+ const React = require("react");
5
+ const reactQuery = require("@tanstack/react-query");
6
+ const radash = require("radash");
7
+ const graphqlRequest = require("graphql-request");
8
+ const BlockSettingsFragmentDoc = { "kind": "Document", "definitions": [{ "kind": "FragmentDefinition", "name": { "kind": "Name", "value": "BlockSettings" }, "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "block_settings" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "tokens" } }, { "kind": "Field", "name": { "kind": "Name", "value": "variant" } }] } }] };
9
+ function useFragment(_documentNode, fragmentType) {
10
+ return fragmentType;
11
+ }
12
+ const GRAPHQL_ENDPOINT = process.env.NEXT_PUBLIC_GRAPHQL_URL;
13
+ const GRAPHQL_ENDPOINT_ADMIN = process.env.NEXT_GRAPHQL_URL_ADMIN;
14
+ const AUTH_TOKEN = process.env.NEXT_PUBLIC_API_TOKEN ?? "";
15
+ const AUTH_TOKEN_ADMIN = process.env.NEXT_PUBLIC_API_TOKEN;
16
+ const graphqlRequestClient = new graphqlRequest.GraphQLClient(GRAPHQL_ENDPOINT, {
17
+ credentials: "include",
18
+ mode: "cors",
19
+ fetch,
20
+ headers: {
21
+ Authorization: `Bearer ${AUTH_TOKEN}`
22
+ }
23
+ });
24
+ new graphqlRequest.GraphQLClient(GRAPHQL_ENDPOINT_ADMIN, {
25
+ credentials: "include",
26
+ mode: "cors",
27
+ fetch,
28
+ headers: {
29
+ Authorization: `Bearer ${AUTH_TOKEN_ADMIN}`
30
+ }
31
+ });
32
+ new reactQuery.QueryClient({
33
+ queryCache: new reactQuery.QueryCache({
34
+ onError: (error) => {
35
+ console.error(error);
36
+ }
37
+ }),
38
+ defaultOptions: {
39
+ queries: {
40
+ staleTime: 5 * 1e3
41
+ }
42
+ }
43
+ });
44
+ function queryGql(document, queryKey, client = graphqlRequestClient) {
45
+ return client.request(document, {
46
+ ...queryKey
47
+ });
48
+ }
49
+ process.env.NEXT_PUBLIC_GRAPHQL_URL;
50
+ process.env.NEXT_PUBLIC_API_TOKEN;
51
+ function isVariables(maybeVariables) {
52
+ return !!maybeVariables;
53
+ }
54
+ function isOnlyIdInItem(item) {
55
+ return !radash.isEmpty(item) && Object.keys(item).length === 1 && Object.keys(item)[0] === "id" && !!item.id;
56
+ }
57
+ async function queryFromVariables(params) {
58
+ const { document, blockKey, variables } = params;
59
+ if (!document || !isVariables(variables)) return null;
60
+ const queriedBlockProps = await queryGql(document, variables);
61
+ if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
62
+ const queriedBlockFragment = queriedBlockProps[blockKey];
63
+ return queriedBlockFragment;
64
+ }
65
+ async function getBlockProps(params) {
66
+ const { document, item, blockKey, variables } = params;
67
+ if (item) {
68
+ if (!isOnlyIdInItem(item)) {
69
+ return item;
70
+ }
71
+ const variablesWithFallback = { id: item.id, ...variables };
72
+ if (!isVariables(variablesWithFallback)) return null;
73
+ return queryFromVariables({
74
+ ...params,
75
+ variables: variablesWithFallback
76
+ });
77
+ }
78
+ if (!document || !isVariables(variables)) return null;
79
+ const queriedBlockProps = await queryGql(document, variables);
80
+ if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
81
+ const queriedBlockFragment = queriedBlockProps[blockKey];
82
+ return queriedBlockFragment;
83
+ }
84
+ const BlockWysiwyg = async (props) => {
85
+ const { variables, themeName = "wysiwyg", tokens, item, document } = props;
86
+ const propsWithFallback = await getBlockProps({
87
+ item,
88
+ blockKey: "block_wysiwyg_by_id",
89
+ document,
90
+ variables
91
+ });
92
+ if (!propsWithFallback) return null;
93
+ const { content, title, level, settings } = propsWithFallback;
94
+ const { tokens: cmsTokens } = useFragment(BlockSettingsFragmentDoc, settings) ?? {};
95
+ if (!content && !(title && level)) return null;
96
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
97
+ title && level && /* @__PURE__ */ jsxRuntime.jsx(stackUi.Box, { as: "span", themeName, tokens: { ...tokens, ...cmsTokens }, children: React.createElement(level, {}, title) }),
98
+ content && /* @__PURE__ */ jsxRuntime.jsx(stackUi.WysiwygBlock, { themeName, tokens: { ...tokens, ...cmsTokens }, content })
99
+ ] });
100
+ };
101
+ const blockWysiwygConfig = {
102
+ block_wysiwyg: {
103
+ default: (props) => /* @__PURE__ */ jsxRuntime.jsx(BlockWysiwyg, { ...props })
104
+ }
105
+ };
106
+ const baseConfig = {
107
+ components: {
108
+ ...blockWysiwygConfig
109
+ }
110
+ };
111
+ exports.BlockSettingsFragmentDoc = BlockSettingsFragmentDoc;
112
+ exports.BlockWysiwyg = BlockWysiwyg;
113
+ exports.baseConfig = baseConfig;
114
+ exports.blockWysiwygConfig = blockWysiwygConfig;
115
+ exports.getBlockProps = getBlockProps;
116
+ exports.useFragment = useFragment;