@kitconcept/core 1.0.0-alpha.3 → 1.0.0-alpha.30

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,42 @@
1
+ import type { BlocksConfig } from '@plone/types';
2
+ import cloneDeep from 'lodash/cloneDeep';
3
+ import type { MutatorDSL } from '../types';
4
+
5
+ // Utility type for deep recursive Partial
6
+ type DeepPartial<T> = {
7
+ [P in keyof T]?: T[P] extends object
8
+ ? T[P] extends Array<infer U>
9
+ ? Array<DeepPartial<U>>
10
+ : DeepPartial<T[P]>
11
+ : T[P];
12
+ };
13
+
14
+ export function BlocksConfigMerger(
15
+ blocksConfig: DeepPartial<BlocksConfig['blocksConfig']>,
16
+ merger: MutatorDSL,
17
+ ): BlocksConfig['blocksConfig'] {
18
+ const mergedConfig = cloneDeep(blocksConfig);
19
+
20
+ Object.entries(merger).forEach(([blockId, dsl]) => {
21
+ if (!mergedConfig[blockId]) return;
22
+
23
+ // 1. Disable block
24
+ if (dsl.disable) {
25
+ mergedConfig[blockId]!.restricted = true;
26
+ }
27
+
28
+ // 2. Filter variations
29
+ if (Array.isArray(dsl.variations) && mergedConfig[blockId]!.variations) {
30
+ mergedConfig[blockId]!.variations = mergedConfig[
31
+ blockId
32
+ ]!.variations!.filter((v) => dsl.variations!.includes(v.id));
33
+ }
34
+
35
+ // 3. Assign themes
36
+ if (Array.isArray(dsl.themes)) {
37
+ mergedConfig[blockId]!.themes = dsl.themes;
38
+ }
39
+ });
40
+
41
+ return mergedConfig as BlocksConfig['blocksConfig'];
42
+ }
package/src/index.ts CHANGED
@@ -1,9 +1,23 @@
1
1
  import type { ConfigType } from '@plone/registry';
2
-
2
+ import type { CustomInheritBehavior, BlocksConfigSettings } from './types';
3
3
  import installSettings from './config/settings';
4
+ import installSlots from './config/slots';
5
+
6
+ declare module '@plone/types' {
7
+ export interface GetSiteResponse {
8
+ 'kitconcept.intranet.custom_css': string;
9
+ }
10
+
11
+ export interface Expanders {
12
+ inherit: {
13
+ 'kitconcept.blocks.config': CustomInheritBehavior<BlocksConfigSettings>;
14
+ };
15
+ }
16
+ }
4
17
 
5
18
  const applyConfig = (config: ConfigType) => {
6
19
  installSettings(config);
20
+ installSlots(config);
7
21
  return config;
8
22
  };
9
23
 
@@ -0,0 +1,32 @@
1
+ import config from '@plone/volto/registry';
2
+ import { useSelector } from 'react-redux';
3
+ import { BlocksConfigMerger } from '../../helpers/BlocksConfigMerger';
4
+ import type { Content } from '@plone/types';
5
+ import type { MutatorDSL } from '../../types';
6
+
7
+ type FormState = {
8
+ content: {
9
+ data: Content;
10
+ };
11
+ };
12
+
13
+ const ConfigInjector = () => {
14
+ const blockConfigData = useSelector<FormState, MutatorDSL>(
15
+ (state) =>
16
+ state.content.data?.['@components']?.inherit?.['kitconcept.blocks.config']
17
+ ?.data?.blocks_config_mutator,
18
+ );
19
+
20
+ if (blockConfigData) {
21
+ config.blocks.blocksConfig = BlocksConfigMerger(
22
+ config.blocks.blocksConfig,
23
+ blockConfigData,
24
+ );
25
+ }
26
+
27
+ // This component does not render anything, it just injects config from the Redux
28
+ // store in the global config
29
+ return null;
30
+ };
31
+
32
+ export default ConfigInjector;
@@ -0,0 +1,28 @@
1
+ import Helmet from '@plone/volto/helpers/Helmet/Helmet';
2
+ import { useSelector } from 'react-redux';
3
+ import type { GetSiteResponse } from '@plone/types';
4
+
5
+ type FormState = {
6
+ site: { data: GetSiteResponse };
7
+ };
8
+
9
+ const TTWCustomCSS = () => {
10
+ const site = useSelector<FormState, GetSiteResponse>(
11
+ (state) => state.site.data,
12
+ );
13
+ const customCSS = site['kitconcept.custom_css'];
14
+
15
+ return (
16
+ <>
17
+ {customCSS ? (
18
+ <>
19
+ <Helmet>
20
+ <style>{customCSS}</style>
21
+ </Helmet>
22
+ </>
23
+ ) : null}
24
+ </>
25
+ );
26
+ };
27
+
28
+ export default TTWCustomCSS;
package/src/types.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { StyleDefinition } from '@plone/types';
2
+
3
+ export type MutatorDSL = Record<
4
+ string,
5
+ {
6
+ disable?: boolean;
7
+ variations?: string[];
8
+ themes?: StyleDefinition[];
9
+ }
10
+ >;
11
+
12
+ export type BlocksConfigSettings = {
13
+ blocks_config_mutator: MutatorDSL;
14
+ };
15
+
16
+ export type CustomInheritBehavior<T> = {
17
+ data: T;
18
+ from: {
19
+ '@id': string;
20
+ title: string;
21
+ };
22
+ };