@eeacms/volto-eea-website-theme 4.3.8 → 4.4.0

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,350 @@
1
+ import ReactDOM from 'react-dom';
2
+ import { v4 as uuid } from 'uuid';
3
+ import {
4
+ addBlock,
5
+ changeBlock,
6
+ insertBlock,
7
+ blockHasValue,
8
+ getBlocksFieldname,
9
+ getBlocksLayoutFieldname,
10
+ } from '@plone/volto/helpers/Blocks/Blocks';
11
+ import { Transforms, Editor, Node, Text } from 'slate';
12
+ import { serializeNodesToText } from '@plone/volto-slate/editor/render';
13
+ import omit from 'lodash/omit';
14
+ import config from '@plone/volto/registry';
15
+
16
+ function fromEntries(pairs) {
17
+ const res = {};
18
+ pairs.forEach((p) => {
19
+ res[p[0]] = p[1];
20
+ });
21
+ return res;
22
+ }
23
+
24
+ export function mergeSlateWithBlockBackward(editor, prevBlock, event) {
25
+ // Merge the current block content into the previous block
26
+ // The current block content should be appended to the previous block
27
+ // and the cursor should be positioned at the start of what was the current block content
28
+
29
+ const prevValue = [...prevBlock.value];
30
+ const currentValue = [...editor.children];
31
+
32
+ const lastNode = prevValue[prevValue.length - 1];
33
+ const firstNode = currentValue[0];
34
+ let merged;
35
+ let cursor;
36
+
37
+ if (lastNode && firstNode && lastNode.type === firstNode.type) {
38
+ // If the last node of previous block and first node of current block have the same type,
39
+ // merge their children together
40
+ const mergedFirstNode = {
41
+ ...lastNode,
42
+ children: [...lastNode.children, ...firstNode.children],
43
+ };
44
+
45
+ merged = [
46
+ ...prevValue.slice(0, -1),
47
+ mergedFirstNode,
48
+ ...currentValue.slice(1),
49
+ ];
50
+
51
+ // If the last child of the previous block's last node is a text node,
52
+ // Slate's normalization will merge it with the first child of the
53
+ // current block's first node (if it's also a text node). Point the
54
+ // cursor into the surviving (pre-merge) text node at the offset equal
55
+ // to its text length, so that after normalization the cursor lands at
56
+ // the correct position — the start of what was the current block's
57
+ // content. Without this, the cursor points to a child index that no
58
+ // longer exists after normalization, causing
59
+ // "Cannot find a descendant at path [..]" crashes. (#8347)
60
+ const lastChild = lastNode.children[lastNode.children.length - 1];
61
+ if (Text.isText(lastChild)) {
62
+ cursor = {
63
+ anchor: {
64
+ path: [prevValue.length - 1, lastNode.children.length - 1],
65
+ offset: lastChild.text.length,
66
+ },
67
+ focus: {
68
+ path: [prevValue.length - 1, lastNode.children.length - 1],
69
+ offset: lastChild.text.length,
70
+ },
71
+ };
72
+ } else {
73
+ cursor = {
74
+ anchor: {
75
+ path: [prevValue.length - 1, lastNode.children.length],
76
+ offset: 0,
77
+ },
78
+ focus: {
79
+ path: [prevValue.length - 1, lastNode.children.length],
80
+ offset: 0,
81
+ },
82
+ };
83
+ }
84
+ } else {
85
+ // Otherwise, just append the current block content to the previous block
86
+ merged = [...prevValue, ...currentValue];
87
+ // Position cursor at the start of the merged content (where current block was inserted)
88
+ // The current block content starts at index prevValue.length in the merged array
89
+ cursor = {
90
+ anchor: {
91
+ path: [prevValue.length, 0],
92
+ offset: 0,
93
+ },
94
+ focus: {
95
+ path: [prevValue.length, 0],
96
+ offset: 0,
97
+ },
98
+ };
99
+ }
100
+
101
+ // Update the editor with the merged content
102
+ editor.children = merged;
103
+
104
+ return cursor;
105
+ }
106
+
107
+ export function mergeSlateWithBlockForward(editor, nextBlock, event) {
108
+ // To work around current architecture limitations, read the value from next
109
+ // block. Replace it in the current editor (over which we have control), join
110
+ // with current block value, then use this result for next block, delete
111
+ // current block
112
+
113
+ const next = nextBlock?.value;
114
+ // Safeguard: if next block has no value or an empty value, there is nothing to merge
115
+ if (!Array.isArray(next) || next.length === 0) {
116
+ return;
117
+ }
118
+
119
+ // collapse the selection to its start point
120
+ Transforms.collapse(editor, { edge: 'end' });
121
+ Transforms.insertNodes(editor, next, {
122
+ at: Editor.end(editor, []),
123
+ });
124
+
125
+ Editor.deleteForward(editor, { unit: 'character' });
126
+ }
127
+
128
+ export function syncCreateSlateBlock(value) {
129
+ const id = uuid();
130
+ const block = {
131
+ '@type': 'slate',
132
+ value: JSON.parse(JSON.stringify(value)),
133
+ plaintext: serializeNodesToText(value),
134
+ };
135
+ return [id, block];
136
+ }
137
+
138
+ export function createImageBlock(url, index, props, intl) {
139
+ const { properties, onChangeField, onSelectBlock } = props;
140
+ const blocksFieldname = getBlocksFieldname(properties);
141
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
142
+
143
+ const currBlockId = properties.blocks_layout.items[index];
144
+ const currBlockHasValue = blockHasValue(properties.blocks[currBlockId]);
145
+ let id, newFormData;
146
+
147
+ if (currBlockHasValue) {
148
+ [id, newFormData] = addBlock(properties, 'image', index + 1, null, intl);
149
+ newFormData = changeBlock(newFormData, id, { '@type': 'image', url });
150
+ } else {
151
+ [id, newFormData] = insertBlock(properties, currBlockId, {
152
+ '@type': 'image',
153
+ url,
154
+ });
155
+ }
156
+
157
+ ReactDOM.unstable_batchedUpdates(() => {
158
+ onChangeField(blocksFieldname, newFormData[blocksFieldname]);
159
+ onChangeField(blocksLayoutFieldname, newFormData[blocksLayoutFieldname]);
160
+ onSelectBlock(id);
161
+ });
162
+ }
163
+
164
+ export const createAndSelectNewBlockAfter = (editor, blockValue, intl) => {
165
+ const blockProps = editor.getBlockProps();
166
+
167
+ const { onSelectBlock, properties, index, onChangeField } = blockProps;
168
+
169
+ const [blockId, formData] = addBlock(
170
+ properties,
171
+ 'slate',
172
+ index + 1,
173
+ null,
174
+ intl,
175
+ );
176
+
177
+ const options = {
178
+ '@type': 'slate',
179
+ value: JSON.parse(JSON.stringify(blockValue)),
180
+ plaintext: serializeNodesToText(blockValue),
181
+ };
182
+
183
+ const newFormData = changeBlock(formData, blockId, options);
184
+
185
+ const blocksFieldname = getBlocksFieldname(properties);
186
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
187
+ // console.log('layout', blocksLayoutFieldname, newFormData);
188
+
189
+ ReactDOM.unstable_batchedUpdates(() => {
190
+ blockProps.saveSlateBlockSelection(blockId, 'start');
191
+ onChangeField(blocksFieldname, newFormData[blocksFieldname]);
192
+ onChangeField(blocksLayoutFieldname, newFormData[blocksLayoutFieldname]);
193
+ onSelectBlock(blockId);
194
+ });
195
+ };
196
+
197
+ export function getNextVoltoBlock(index, properties) {
198
+ // TODO: look for any next slate block
199
+ // join this block with previous block, if previous block is slate
200
+ const blocksFieldname = getBlocksFieldname(properties);
201
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
202
+
203
+ const blocks_layout = properties[blocksLayoutFieldname];
204
+
205
+ if (index === blocks_layout.items.length) return;
206
+
207
+ const nextBlockId = blocks_layout.items[index + 1];
208
+ const nextBlock = properties[blocksFieldname][nextBlockId];
209
+
210
+ return [nextBlock, nextBlockId];
211
+ }
212
+
213
+ export function getPreviousVoltoBlock(index, properties) {
214
+ // TODO: look for any prev slate block
215
+ if (index === 0) return;
216
+
217
+ const blocksFieldname = getBlocksFieldname(properties);
218
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
219
+
220
+ const blocks_layout = properties[blocksLayoutFieldname];
221
+ const prevBlockId = blocks_layout.items[index - 1];
222
+ const prevBlock = properties[blocksFieldname][prevBlockId];
223
+
224
+ return [prevBlock, prevBlockId];
225
+ }
226
+
227
+ // //check for existing img children
228
+ // const checkContainImg = (elements) => {
229
+ // var check = false;
230
+ // elements.forEach((e) =>
231
+ // e.children.forEach((c) => {
232
+ // if (c && c.type && c.type === 'img') {
233
+ // check = true;
234
+ // }
235
+ // }),
236
+ // );
237
+ // return check;
238
+ // };
239
+
240
+ // //check for existing table children
241
+ // const checkContainTable = (elements) => {
242
+ // var check = false;
243
+ // elements.forEach((e) => {
244
+ // if (e && e.type && e.type === 'table') {
245
+ // check = true;
246
+ // }
247
+ // });
248
+ // return check;
249
+ // };
250
+
251
+ /**
252
+ * The editor has the properties `dataTransferHandlers` (object) and
253
+ * `dataTransferFormatsOrder` and in `dataTransferHandlers` are functions which
254
+ * sometimes must call this function. Some types of data storeable in Slate
255
+ * documents can be and should be put into separate Volto blocks. The
256
+ * `deconstructToVoltoBlocks` function scans the contents of the Slate document
257
+ * and, through configured Volto block emitters, it outputs separate Volto
258
+ * blocks into the same Volto page form. The `deconstructToVoltoBlocks` function
259
+ * should be called only in key places where it is necessary.
260
+ *
261
+ * @example See the `src/editor/extensions/insertData.js` file.
262
+ *
263
+ * @param {Editor} editor The Slate editor object which should be deconstructed
264
+ * if possible.
265
+ *
266
+ * @returns {Promise}
267
+ */
268
+ export function deconstructToVoltoBlocks(editor) {
269
+ // Explodes editor content into separate blocks
270
+ // If the editor has multiple top-level children, split the current block
271
+ // into multiple slate blocks. This will delete and replace the current
272
+ // block.
273
+ //
274
+ // It returns a promise that, when resolved, will pass a list of Volto block
275
+ // ids that were affected
276
+ //
277
+ // For the Volto blocks manipulation we do low-level changes to the context
278
+ // form state, as that ensures a better performance (no un-needed UI updates)
279
+
280
+ if (!editor.getBlockProps) return;
281
+
282
+ const blockProps = editor.getBlockProps();
283
+ const { slate } = config.settings;
284
+ const { voltoBlockEmiters } = slate;
285
+
286
+ return new Promise((resolve, reject) => {
287
+ if (!editor?.children) return;
288
+
289
+ if (editor.children.length === 1) {
290
+ return resolve([blockProps.block]);
291
+ }
292
+ const { properties, onChangeField, onSelectBlock } = editor.getBlockProps();
293
+ const blocksFieldname = getBlocksFieldname(properties);
294
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
295
+
296
+ const { index } = blockProps;
297
+ let blocks = [];
298
+
299
+ // TODO: should use Editor.levels() instead of Node.children
300
+ const pathRefs = Array.from(Node.children(editor, [])).map(([, path]) =>
301
+ Editor.pathRef(editor, path),
302
+ );
303
+
304
+ for (const pathRef of pathRefs) {
305
+ // extra nodes are always extracted after the text node
306
+ let extras = voltoBlockEmiters
307
+ .map((emit) => emit(editor, pathRef))
308
+ .flat(1);
309
+
310
+ // The node might have been replaced with a Volto block
311
+ if (pathRef.current) {
312
+ const [childNode] = Editor.node(editor, pathRef.current);
313
+ if (childNode && !Editor.isEmpty(editor, childNode))
314
+ blocks.push(syncCreateSlateBlock([childNode]));
315
+ }
316
+ blocks = [...blocks, ...extras];
317
+ }
318
+
319
+ const blockids = blocks.map((b) => b[0]);
320
+
321
+ // TODO: add the placeholder block, because we remove it
322
+ // (when we remove the current block)
323
+
324
+ const blocksData = omit(
325
+ {
326
+ ...properties[blocksFieldname],
327
+ ...fromEntries(blocks),
328
+ },
329
+ blockProps.block,
330
+ );
331
+ const layoutData = {
332
+ ...properties[blocksLayoutFieldname],
333
+ items: [
334
+ ...properties[blocksLayoutFieldname].items.slice(0, index),
335
+ ...blockids,
336
+ ...properties[blocksLayoutFieldname].items.slice(index),
337
+ ].filter((id) => id !== blockProps.block),
338
+ };
339
+
340
+ // TODO: use onChangeFormData instead of this API style
341
+ ReactDOM.unstable_batchedUpdates(() => {
342
+ onChangeField(blocksFieldname, blocksData);
343
+ onChangeField(blocksLayoutFieldname, layoutData);
344
+ onSelectBlock(blockids[blockids.length - 1]);
345
+ // resolve(blockids);
346
+ // or rather this?
347
+ Promise.resolve().then(resolve(blockids));
348
+ });
349
+ });
350
+ }
@@ -0,0 +1,61 @@
1
+ --- node_modules/@plone/volto-slate/src/utils/volto-blocks.js 2026-06-19 16:56:50.131820782 +0300
2
+ +++ src/addons/volto-eea-website-theme/src/customizations/@plone/volto-slate/utils/volto-blocks.js 2026-06-24 18:09:06.099715266 +0300
3
+ @@ -8,7 +8,7 @@
4
+ getBlocksFieldname,
5
+ getBlocksLayoutFieldname,
6
+ } from '@plone/volto/helpers/Blocks/Blocks';
7
+ -import { Transforms, Editor, Node } from 'slate';
8
+ +import { Transforms, Editor, Node, Text } from 'slate';
9
+ import { serializeNodesToText } from '@plone/volto-slate/editor/render';
10
+ import omit from 'lodash/omit';
11
+ import config from '@plone/volto/registry';
12
+ @@ -48,16 +48,39 @@
13
+ ...currentValue.slice(1),
14
+ ];
15
+
16
+ - cursor = {
17
+ - anchor: {
18
+ - path: [prevValue.length - 1, lastNode.children.length],
19
+ - offset: 0,
20
+ - },
21
+ - focus: {
22
+ - path: [prevValue.length - 1, lastNode.children.length],
23
+ - offset: 0,
24
+ - },
25
+ - };
26
+ + // If the last child of the previous block's last node is a text node,
27
+ + // Slate's normalization will merge it with the first child of the
28
+ + // current block's first node (if it's also a text node). Point the
29
+ + // cursor into the surviving (pre-merge) text node at the offset equal
30
+ + // to its text length, so that after normalization the cursor lands at
31
+ + // the correct position — the start of what was the current block's
32
+ + // content. Without this, the cursor points to a child index that no
33
+ + // longer exists after normalization, causing
34
+ + // "Cannot find a descendant at path [..]" crashes. (#8347)
35
+ + const lastChild = lastNode.children[lastNode.children.length - 1];
36
+ + if (Text.isText(lastChild)) {
37
+ + cursor = {
38
+ + anchor: {
39
+ + path: [prevValue.length - 1, lastNode.children.length - 1],
40
+ + offset: lastChild.text.length,
41
+ + },
42
+ + focus: {
43
+ + path: [prevValue.length - 1, lastNode.children.length - 1],
44
+ + offset: lastChild.text.length,
45
+ + },
46
+ + };
47
+ + } else {
48
+ + cursor = {
49
+ + anchor: {
50
+ + path: [prevValue.length - 1, lastNode.children.length],
51
+ + offset: 0,
52
+ + },
53
+ + focus: {
54
+ + path: [prevValue.length - 1, lastNode.children.length],
55
+ + offset: 0,
56
+ + },
57
+ + };
58
+ + }
59
+ } else {
60
+ // Otherwise, just append the current block content to the previous block
61
+ merged = [...prevValue, ...currentValue];
@@ -0,0 +1,9 @@
1
+ Shadow patch for #8347: Fix cursor position after mergeSlateWithBlockBackward
2
+ when merging two same-type blocks. Slate normalization merges adjacent text
3
+ nodes, making the original cursor path invalid. See:
4
+
5
+ - https://github.com/plone/volto/issues/8347
6
+ - https://github.com/plone/volto/pull/8355
7
+
8
+ Remove this shadow once a @plone/volto-slate release containing the fix
9
+ is published and the dependency is bumped in frontend/package.json.
@@ -205,15 +205,13 @@ class Html extends Component {
205
205
  charSet: 'UTF-8',
206
206
  })}
207
207
  {/* Add the crossorigin while in development */}
208
- {this.props.extractScripts !== false
209
- ? extractor.getScriptElements().map((elem) =>
210
- React.cloneElement(elem, {
211
- nonce: nonce,
212
- crossOrigin:
213
- process.env.NODE_ENV === 'production' ? undefined : 'true',
214
- }),
215
- )
216
- : ''}
208
+ {extractor.getScriptElements().map((elem) =>
209
+ React.cloneElement(elem, {
210
+ nonce: nonce,
211
+ crossOrigin:
212
+ process.env.NODE_ENV === 'production' ? undefined : 'true',
213
+ }),
214
+ )}
217
215
  </body>
218
216
  </html>
219
217
  );
@@ -336,10 +336,6 @@ server.get('/*', (req, res) => {
336
336
  nonce={nonce}
337
337
  markup={markup}
338
338
  store={store}
339
- extractScripts={
340
- config.settings.serverConfig.extractScripts?.errorPages ||
341
- process.env.NODE_ENV !== 'production'
342
- }
343
339
  criticalCss={readCriticalCss(req)}
344
340
  apiPath={res.locals.detectedHost || config.settings.apiPath}
345
341
  publicURL={
package/src/index.js CHANGED
@@ -174,10 +174,6 @@ const applyConfig = (config) => {
174
174
  // #160689 Redirect contact-form to contact-us
175
175
  config.settings.contactForm = '/contact';
176
176
 
177
- // Insert scripts on Error pages
178
- if (config.settings?.serverConfig?.extractScripts) {
179
- config.settings.serverConfig.extractScripts.errorPages = true;
180
- }
181
177
  // Set cloneData function for slate block, in order to change the uuids of fragments in the copy process
182
178
  if (config.blocks?.blocksConfig?.slate) {
183
179
  config.blocks.blocksConfig.slate.cloneData = (data) => {
package/src/index.test.js CHANGED
@@ -285,7 +285,9 @@ describe('applyConfig', () => {
285
285
 
286
286
  expect(config.settings.eea).toEqual(eea);
287
287
  expect(config.settings.contactForm).toBe('/contact');
288
- expect(config.settings.serverConfig.extractScripts.errorPages).toBe(true);
288
+ expect(
289
+ config.settings.serverConfig.extractScripts.errorPages,
290
+ ).toBeUndefined();
289
291
  expect(config.settings.showTags).toBe(false);
290
292
  expect(config.blocks.blocksConfig['teaser'].restricted).toBe(true);
291
293
  expect(config.blocks.blocksConfig['title'].restricted).toBe(false);