generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
566 lines (511 loc) · 18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import React from 'react';
import { v4 as uuid } from 'uuid';
import { Icon } from '@plone/volto/components';
import { default as TokenWidgetEdit } from '@plone/volto/components/manage/Widgets/TokenWidget';
import SelectAutoCompleteWidget from '@plone/volto/components/manage/Widgets/SelectAutoComplete';
import { serializeNodesToText } from '@plone/volto-slate/editor/render';
import { nanoid } from '@plone/volto-slate/utils';
import InpageNavigation from '@eeacms/volto-eea-design-system/ui/InpageNavigation/InpageNavigation';
import CustomCSS from '@eeacms/volto-eea-website-theme/components/theme/CustomCSS/CustomCSS';
import DraftBackground from '@eeacms/volto-eea-website-theme/components/theme/DraftBackground/DraftBackground';
import HomePageInverseView from '@eeacms/volto-eea-website-theme/components/theme/Homepage/HomePageInverseView';
import HomePageView from '@eeacms/volto-eea-website-theme/components/theme/Homepage/HomePageView';
import NotFound from '@eeacms/volto-eea-website-theme/components/theme/NotFound/NotFound';
import { TokenWidget } from '@eeacms/volto-eea-website-theme/components/theme/Widgets/TokenWidget';
import { TopicsWidget } from '@eeacms/volto-eea-website-theme/components/theme/Widgets/TopicsWidget';
import { DateWidget } from '@eeacms/volto-eea-website-theme/components/theme/Widgets/DateWidget';
import { DatetimeWidget } from '@eeacms/volto-eea-website-theme/components/theme/Widgets/DatetimeWidget';
import CreatableSelectWidget from '@eeacms/volto-eea-website-theme/components/theme/Widgets/CreatableSelectWidget';
import Tag from '@eeacms/volto-eea-design-system/ui/Tag/Tag';
import {
addStylingFieldsetSchemaEnhancer,
addStylingFieldsetSchemaEnhancerImagePosition,
} from '@eeacms/volto-eea-website-theme/helpers/schema-utils';
import installLayoutSettingsBlock from '@eeacms/volto-eea-website-theme/components/manage/Blocks/LayoutSettings';
import installCustomTitle from '@eeacms/volto-eea-website-theme/components/manage/Blocks/Title';
import FlexGroup from '@eeacms/volto-eea-website-theme/components/manage/Blocks/GroupBlockTemplate/FlexGroup/FlexGroup';
import BaseTag from './components/theme/BaseTag';
import SubsiteClass from './components/theme/SubsiteClass';
import contentBoxSVG from './icons/content-box.svg';
import okMiddleware from './middleware/ok';
import voltoCustomMiddleware from './middleware/voltoCustom';
import installSlate from './slate';
import { print } from './reducers';
import * as eea from './config';
const restrictedBlocks = ['imagesGrid', 'teaser', 'dataFigure', 'plotly_chart'];
/**
* Customizes the variations of a tabs block by modifying their schema and semantic icons.
*
* @param {Array} tabs_block_variations - An array of variations for the tabs block.
* @param {Object} config - The Volto configuration object.
*/
function tabVariationCustomization(tabs_block_variations, config) {
if (!tabs_block_variations) return;
const defaultVariation = tabs_block_variations.find(
({ id }) => id === 'default',
);
const accordionVariation = tabs_block_variations.find(
({ id }) => id === 'accordion',
);
const horizontalVariation = tabs_block_variations.find(
({ id }) => id === 'horizontal-responsive',
);
if (accordionVariation) {
accordionVariation.semanticIcon = {
opened: 'ri-arrow-up-s-line',
closed: 'ri-arrow-down-s-line',
};
}
const oldSchemaEnhancer =
config.blocks.blocksConfig.tabs_block.schemaEnhancer;
config.blocks.blocksConfig.tabs_block.schemaEnhancer = (props) => {
const schema = (oldSchemaEnhancer ? oldSchemaEnhancer(props) : props)
.schema;
const oldSchemaExtender = schema.properties?.data?.schemaExtender;
//check before if schema.properties.data exists
if (!schema?.properties?.data) {
schema.properties = {
...schema.properties,
data: {},
};
}
schema.properties.data.schemaExtender = (schema, child) => {
const innerSchema = oldSchemaExtender
? oldSchemaExtender(schema, child)
: schema;
innerSchema.properties.icon.description = (
<>
Ex. ri-home-line. See{' '}
<a target="_blank" rel="noopener" href="https://remixicon.com/">
Remix Icon set
</a>
</>
);
return innerSchema;
};
return schema;
};
const oldDefaultSchemaEnhancer = defaultVariation.schemaEnhancer;
defaultVariation.schemaEnhancer = (props) => {
const newSchema = oldDefaultSchemaEnhancer(props);
const menuFieldset = newSchema.fieldsets.find(({ id }) => id === 'menu');
menuFieldset.fields = [
'menuAlign',
'menuPosition',
'menuColor',
'menuInverted',
];
return newSchema;
};
const oldHorizontalSchemaEnhancer = horizontalVariation.schemaEnhancer;
horizontalVariation.schemaEnhancer = (props) => {
const newSchema = oldHorizontalSchemaEnhancer(props);
const menuFieldset = newSchema.fieldsets.find(({ id }) => id === 'menu');
menuFieldset.fields = [
'menuAlign',
'menuPosition',
'menuColor',
'menuInverted',
];
return newSchema;
};
}
const applyConfig = (config) => {
// EEA specific settings
config.settings.eea = {
...eea,
...(config.settings.eea || {}),
};
//include site title in <title>
if (!config.settings.siteTitleFormat) {
config.settings.siteTitleFormat = { includeSiteTitle: true };
} else config.settings.siteTitleFormat.includeSiteTitle = true;
config.settings.titleAndSiteTitleSeparator = '|';
// #160689 Redirect contact-form to contact-us
config.settings.contactForm = '/contact';
// Insert scripts on Error pages
if (config.settings?.serverConfig?.extractScripts) {
config.settings.serverConfig.extractScripts.errorPages = true;
}
// Set cloneData function for slate block, in order to change the uuids of fragments in the copy process
if (config.blocks?.blocksConfig?.slate) {
config.blocks.blocksConfig.slate.cloneData = (data) => {
const replaceAllUidsWithNewOnes = (value) => {
if (value?.children?.length > 0) {
const newChildren = value.children.map((childrenData) => {
if (childrenData?.data?.uid) {
return {
...childrenData,
data: { ...childrenData.data, uid: nanoid(5) },
};
}
return childrenData;
});
return {
...value,
children: newChildren,
};
}
return value;
};
return [
uuid(),
{
...data,
value: [
...(data?.value || []).map((value) =>
replaceAllUidsWithNewOnes(value),
),
],
},
];
};
}
// Disable tags on View
config.settings.showTags = false;
// Disable some blocks
restrictedBlocks.forEach((block) => {
if (config.blocks.blocksConfig[block]) {
config.blocks.blocksConfig[block].restricted = true;
}
});
//Apply the image position style for image and leadimage blocks
if (config.blocks.blocksConfig.leadimage) {
config.blocks.blocksConfig.leadimage.schemaEnhancer =
addStylingFieldsetSchemaEnhancerImagePosition;
}
if (config.blocks.blocksConfig.image) {
config.blocks.blocksConfig.image.schemaEnhancer =
addStylingFieldsetSchemaEnhancerImagePosition;
config.blocks.blocksConfig.image.getSizes = function (data) {
if (data.size === 'm' || data.size === 's') return undefined;
if (data.align === 'left' || data.align === 'right') {
if (data.size === 'l') return '400px';
if (data.size === 'm') return '200px';
if (data.size === 's') return '200px';
}
if (data.size === 'l') {
return '(max-width: 600px) 400px, (max-width: 1440px) 800px, 100vw';
}
};
}
// Set Languages in nextcloud-video-block
if (
config?.blocks?.blocksConfig?.nextCloudVideo?.subtitlesLanguages &&
config?.settings?.eea?.languages?.length > 0
)
config.blocks.blocksConfig.nextCloudVideo.subtitlesLanguages =
config.settings.eea.languages.map((el) => [el.code, el.name]);
// Enable Title block
config.blocks.blocksConfig.title.restricted = false;
// Enable description block (also for cypress)
config.blocks.blocksConfig.description.restricted = false;
config.blocks.requiredBlocks = [];
// Date format for EU
config.settings.dateLocale = 'en-gb';
// Custom Homepage layouts
config.views.layoutViews = {
...(config.views.layoutViews || {}),
homepage_view: HomePageView,
homepage_inverse_view: HomePageInverseView,
};
config.views.layoutViewsNamesMapping = {
...(config.views.layoutViewsNamesMapping || {}),
homepage_view: 'Homepage view',
homepage_inverse_view: 'Homepage white view',
};
config.views.errorViews = {
...config.views.errorViews,
404: NotFound,
};
// Apply slate text block customization
if (config.blocks.blocksConfig.slate) {
config.blocks.blocksConfig.slate.tocEntry = (block = {}) => {
const { value, override_toc, entry_text, level } = block;
const plaintext =
serializeNodesToText(block.value || []) || block.plaintext;
const type = value?.[0]?.type;
return override_toc && level
? [parseInt(level.slice(1)), entry_text]
: config.settings.slate.topLevelTargetElements.includes(type)
? [parseInt(type.slice(1)), plaintext]
: null;
};
}
// Apply accordion block customization
if (config.blocks.blocksConfig.accordion) {
config.blocks.blocksConfig.accordion.titleIcons = {
closed: {
leftPosition: 'ri-arrow-down-s-line',
rightPosition: 'ri-arrow-down-s-line',
},
opened: {
leftPosition: 'ri-arrow-up-s-line',
rightPosition: 'ri-arrow-up-s-line',
},
unfiltered: {
leftPosition: 'ri-filter-3-line',
rightPosition: 'ri-filter-3-line',
},
filtered: {
leftPosition: 'ri-close-line',
rightPosition: 'ri-close-line',
},
iconComponent: 'SemanticIcon',
};
config.blocks.blocksConfig.accordion.options = {};
config.blocks.blocksConfig.accordion.defaults.theme = 'secondary';
}
// Apply tabs block customization
if (config.blocks.blocksConfig.tabs_block) {
const tabs_block_variations =
config.blocks.blocksConfig.tabs_block.variations;
tabVariationCustomization(tabs_block_variations, config);
}
//Group block flex variation
if (config.blocks.blocksConfig.group) {
config.blocks.blocksConfig.group.variations = [
...(config.blocks.blocksConfig.group.variations || []),
{
id: 'flex group',
isDefault: false,
title: 'Flex Group',
template: FlexGroup,
schemaEnhancer: ({ schema, formData, intl }) => {
schema.fieldsets[0].fields.push('no_of_columns');
schema.properties.no_of_columns = {
title: 'No. of columns',
description: 'Choose the number of flex columns',
choices: [
[2, 2],
[3, 3],
[4, 4],
[5, 5],
],
};
return schema;
},
},
];
}
// Apply columns block customization
if (config.blocks.blocksConfig.columnsBlock) {
config.blocks.blocksConfig.columnsBlock.available_colors = eea.colors;
}
// Description block custom CSS
config.blocks.blocksConfig.description.className =
'documentDescription eea callout';
// Hero block copyright prefix
if (config.blocks.blocksConfig.hero) {
config.blocks.blocksConfig.hero.copyrightPrefix = 'Image';
}
// Custom Widgets
// config.widgets.id.other_organisations = TokenWidgetEdit;
config.widgets.vocabulary['eea.coremetadata.other_organisations'] =
TokenWidgetEdit;
config.widgets.views.widget.datetime = DatetimeWidget;
config.widgets.views.widget.date = DateWidget;
config.widgets.views.id.topics = TopicsWidget;
config.widgets.views.id.subjects = TokenWidget;
config.widgets.views.widget.tags = TokenWidget;
config.widgets.widget.creatable_select = CreatableSelectWidget;
config.widgets.vocabulary['plone.app.vocabularies.Users'] =
SelectAutoCompleteWidget;
// /voltoCustom.css express-middleware
// /ok express-middleware - see also: https://github.com/plone/volto/pull/4432
if (__SERVER__) {
const express = require('express');
config.settings.expressMiddleware = [
...(config.settings.expressMiddleware || []),
voltoCustomMiddleware(express),
okMiddleware(express),
];
}
// InPage navigation, Custom CSS voltoCustom.css and Draft Background
config.settings.appExtras = [
...(config.settings.appExtras || []),
{
match: '/**',
component: InpageNavigation,
},
{
match: '',
component: CustomCSS,
},
{
match: '',
component: DraftBackground,
},
{
match: '',
component: SubsiteClass,
},
{
match: '',
component: BaseTag,
},
];
// Install slate
config = installSlate(config);
// Custom block-style colors
config.settings.available_colors = eea.colors;
// hide language dropdown by default
config.settings.hasLanguageDropdown = false;
// Site theme colors
config.settings.themeColors = [
{ value: undefined, title: 'No theme' },
{ value: 'primary', title: 'Primary' },
{ value: 'secondary', title: 'Secondary' },
{ value: 'tertiary', title: 'Tertiary' },
];
// Custom preset styles - content-box
config.settings.previewText = '';
config.settings.pluggableStyles = [
...(config.settings.pluggableStyles || []),
{
id: 'content-box-gray',
title: 'Default',
previewComponent: () => (
<Icon name={contentBoxSVG} size="88px" className="default" />
),
viewComponent: (props) => {
return (
<div className="content-box">
<div className="content-box-inner">{props.children}</div>
</div>
);
},
},
{
id: 'content-box-primary',
title: 'Primary',
previewComponent: () => (
<Icon name={contentBoxSVG} size="88px" className="primary" />
),
viewComponent: (props) => {
return (
<div className="content-box primary">
<div className="content-box-inner">{props.children}</div>
</div>
);
},
},
{
id: 'content-box-secondary',
title: 'Secondary',
previewComponent: () => (
<Icon name={contentBoxSVG} size="88px" className="secondary" />
),
viewComponent: (props) => {
return (
<div className="content-box secondary">
<div className="content-box-inner">{props.children}</div>
</div>
);
},
},
{
id: 'content-box-tertiary',
title: 'Tertiary',
previewComponent: () => (
<Icon name={contentBoxSVG} size="88px" className="tertiary" />
),
viewComponent: (props) => {
return (
<div className="content-box tertiary">
<div className="content-box-inner">{props.children}</div>
</div>
);
},
},
];
// mega menu layout settings
config.settings.menuItemsLayouts = {
'/en/topics': {
menuItemChildrenListColumns: [1, 4],
menuItemColumns: [
'at-a-glance three wide column',
'topics-right-column nine wide column',
],
hideChildrenFromNavigation: false,
},
'/en/countries': {
menuItemColumns: ['eight wide column', 'four wide column'],
menuItemChildrenListColumns: [5, 2],
appendExtraMenuItemsToLastColumn: true,
hideChildrenFromNavigation: false,
},
'/en/about': {
hideChildrenFromNavigation: false,
},
// if you want to set default settings for all menu items that don't have a specific path
// '*': {
// hideChildrenFromNavigation: false,
// },
};
// layout settings
config = [installLayoutSettingsBlock].reduce(
(acc, apply) => apply(acc),
config,
);
// Group
if (config.blocks.blocksConfig.group) {
config.blocks.blocksConfig.group.schemaEnhancer =
addStylingFieldsetSchemaEnhancer;
}
// Columns
if (config.blocks.blocksConfig.columnsBlock) {
config.blocks.blocksConfig.columnsBlock.mostUsed = true;
config.blocks.blocksConfig.columnsBlock.schemaEnhancer =
addStylingFieldsetSchemaEnhancer;
}
// Listing
if (config.blocks.blocksConfig.listing) {
config.blocks.blocksConfig.listing.title = 'Listing (Content)';
config.blocks.blocksConfig.listing.schemaEnhancer =
addStylingFieldsetSchemaEnhancer;
}
// Block chooser
config.blocks.blocksConfig.image.mostUsed = false;
config.blocks.blocksConfig.video.mostUsed = false;
// Divider
if (config.blocks.blocksConfig.dividerBlock) {
config.blocks.blocksConfig.dividerBlock.mostUsed = true;
}
// Call to Action
if (config.blocks.blocksConfig.callToActionBlock) {
config.blocks.blocksConfig.callToActionBlock.mostUsed = true;
}
// Accordion
if (config.blocks.blocksConfig.accordion) {
config.blocks.blocksConfig.accordion.mostUsed = true;
}
// Teaser block changes
if (config.blocks.blocksConfig.teaser) {
// Use volto-eea-design-system Tag component for rendering teaser tags
config.blocks.blocksConfig.teaser.renderTag = (tag, index) => {
return (
<Tag
href={`https://www.eea.europa.eu/en/advanced-search?filters[0][field]=topic&filters[0][values][0]=${tag}&filters[0][type]=any&filters[1][field]=language&filters[1][type]=any&filters[1][values][0]=en&filters[2][field]=issued.date&filters[2][values][0]=Last 5 years&filters[2][type]=any&sort-field=issued.date&sort-direction=desc`}
key={index}
aria-label={`Search for content tagged with ${tag}`}
>
{tag}
</Tag>
);
};
}
// addonReducers
config.addonReducers = {
...(config.addonReducers || {}),
print,
};
// Breadcrumbs
config.settings.apiExpanders.push({
match: '',
GET_CONTENT: ['breadcrumbs'], // 'navigation', 'actions', 'types'],
});
// Custom blocks: Title
return [installCustomTitle].reduce((acc, apply) => apply(acc), config);
};
export default applyConfig;