@eeacms/volto-eea-website-theme 1.28.2 → 1.28.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/CHANGELOG.md CHANGED
@@ -4,10 +4,11 @@ All notable changes to this project will be documented in this file. Dates are d
4
4
 
5
5
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6
6
 
7
- ### [1.28.2](https://github.com/eea/volto-eea-website-theme/compare/1.28.1...1.28.2) - 29 February 2024
7
+ ### [1.28.3](https://github.com/eea/volto-eea-website-theme/compare/1.28.2...1.28.3) - 5 March 2024
8
8
 
9
9
  #### :bug: Bug Fixes
10
10
 
11
+ - fix(slate): fix copy/paste in slate from Microsoft Word Desktop App - refs #265782 [Miu Razvan - [`f7a128c`](https://github.com/eea/volto-eea-website-theme/commit/f7a128c4ef02207bf6dfd7adf38cb8df8f073d14)]
11
12
  - fix(event): add permalink in customization to the original volto component [kreafox - [`c662aae`](https://github.com/eea/volto-eea-website-theme/commit/c662aae7c271fa1170dd2dbd8cc9a374fed59a2f)]
12
13
 
13
14
  #### :nail_care: Enhancements
@@ -17,6 +18,8 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
17
18
  #### :hammer_and_wrench: Others
18
19
 
19
20
  - Allow hit Enter in the LayoutSettings block to create a new block [Tiberiu Ichim - [`ecde522`](https://github.com/eea/volto-eea-website-theme/commit/ecde52263ce6f989936962bda7bc1213b2750d7d)]
21
+ ### [1.28.2](https://github.com/eea/volto-eea-website-theme/compare/1.28.1...1.28.2) - 29 February 2024
22
+
20
23
  ### [1.28.1](https://github.com/eea/volto-eea-website-theme/compare/1.28.0...1.28.1) - 19 February 2024
21
24
 
22
25
  #### :bug: Bug Fixes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eeacms/volto-eea-website-theme",
3
- "version": "1.28.2",
3
+ "version": "1.28.3",
4
4
  "description": "@eeacms/volto-eea-website-theme: Volto add-on",
5
5
  "main": "src/index.js",
6
6
  "author": "European Environment Agency: IDM2 A-Team",
@@ -0,0 +1,372 @@
1
+ /* eslint no-console: ["error", { allow: ["error", "warn"] }] */
2
+ import { Editor, Transforms, Text } from 'slate'; // Range, RangeRef
3
+ import config from '@plone/volto/registry';
4
+ import {
5
+ getBlocksFieldname,
6
+ getBlocksLayoutFieldname,
7
+ } from '@plone/volto/helpers';
8
+ import _ from 'lodash';
9
+ import { makeEditor } from '@plone/volto-slate/utils/editor';
10
+
11
+ // case sensitive; first in an inner array is the default and preffered format
12
+ // in that array of formats
13
+ const formatAliases = [
14
+ ['strong', 'b'],
15
+ ['em', 'i'],
16
+ ['del', 's'],
17
+ ];
18
+
19
+ /**
20
+ * Excerpt from Slate documentation, kept here for posterity:
21
+ * See https://docs.slatejs.org/concepts/11-normalizing#built-in-constraints
22
+
23
+ ## Built-in Constraints
24
+
25
+ Slate editors come with a few built-in constraints out of the box. These
26
+ constraints are there to make working with content much more predictable than
27
+ standard contenteditable. All of the built-in logic in Slate depends on these
28
+ constraints, so unfortunately you cannot omit them. They are...
29
+
30
+ - All Element nodes must contain at least one Text descendant. If an element node
31
+ does not contain any children, an empty text node will be added as its only
32
+ child. This constraint exists to ensure that the selection's anchor and focus
33
+ points (which rely on referencing text nodes) can always be placed inside any
34
+ node. With this, empty elements (or void elements) wouldn't be selectable.
35
+
36
+ - Two adjacent texts with the same custom properties will be merged. If two
37
+ adjacent text nodes have the same formatting, they're merged into a single text
38
+ node with a combined text string of the two. This exists to prevent the text
39
+ nodes from only ever expanding in count in the document, since both adding and
40
+ removing formatting results in splitting text nodes.
41
+
42
+ - Block nodes can only contain other blocks, or inline and text nodes. For
43
+ example, a paragraph block cannot have another paragraph block element and
44
+ a link inline element as children at the same time. The type of children
45
+ allowed is determined by the first child, and any other non-conforming children
46
+ are removed. This ensures that common richtext behaviors like "splitting
47
+ a block in two" function consistently.
48
+
49
+ - Inline nodes cannot be the first or last child of a parent block, nor can it
50
+ be next to another inline node in the children array. If this is the case, an
51
+ empty text node will be added to correct this to be in compliance with the
52
+ constraint.
53
+
54
+ - The top-level editor node can only contain block nodes. If any of the
55
+ top-level children are inline or text nodes they will be removed. This ensures
56
+ that there are always block nodes in the editor so that behaviors like
57
+ "splitting a block in two" work as expected.
58
+
59
+ - These default constraints are all mandated because they make working with
60
+ Slate documents much more predictable.
61
+
62
+ Although these constraints are the best we've come up with now, we're always
63
+ looking for ways to have Slate's built-in constraints be less constraining if
64
+ possible—as long as it keeps standard behaviors easy to reason about. If you
65
+ come up with a way to reduce or remove a built-in constraint with a different
66
+ approach, we're all ears!
67
+ *
68
+ */
69
+
70
+ export const normalizeExternalData = (editor, nodes) => {
71
+ let fakeEditor = makeEditor({ extensions: editor._installedPlugins });
72
+ // set the nodes to the fakeEditor
73
+ let currentIndex = 0;
74
+ fakeEditor.children = nodes.reduce((nodes, node) => {
75
+ if (Editor.isBlock(fakeEditor, node) && !!node.type) {
76
+ nodes.push(node);
77
+ currentIndex++;
78
+ return nodes;
79
+ }
80
+ if (!nodes[currentIndex]) {
81
+ nodes[currentIndex] = { type: 'p', children: [] };
82
+ }
83
+ nodes[currentIndex].children.push(node);
84
+ return nodes;
85
+ }, []);
86
+
87
+ Editor.normalize(fakeEditor, { force: true });
88
+
89
+ return fakeEditor.children;
90
+ };
91
+
92
+ /**
93
+ * Is it text? Is it whitespace (space, newlines, tabs) ?
94
+ *
95
+ */
96
+ export const isWhitespace = (c) => {
97
+ return (
98
+ typeof c === 'string' &&
99
+ c.replace(/\s/g, '').replace(/\t/g, '').replace(/\n/g, '').length === 0
100
+ );
101
+ };
102
+
103
+ export function createDefaultBlock(children) {
104
+ return {
105
+ type: config.settings.slate.defaultBlockType,
106
+ children: children || [{ text: '' }],
107
+ };
108
+ }
109
+
110
+ export function createBlock(type, children) {
111
+ return {
112
+ type,
113
+ children: children || [{ text: '' }],
114
+ };
115
+ }
116
+
117
+ export function createEmptyParagraph() {
118
+ // TODO: rename to createEmptyBlock
119
+ return {
120
+ type: config.settings.slate.defaultBlockType,
121
+ children: [{ text: '' }],
122
+ };
123
+ }
124
+
125
+ export function createParagraph(text) {
126
+ return {
127
+ type: config.settings.slate.defaultBlockType,
128
+ children: [{ text }],
129
+ };
130
+ }
131
+
132
+ export const isSingleBlockTypeActive = (editor, format) => {
133
+ const [match] = Editor.nodes(editor, {
134
+ match: (n) => n.type === format,
135
+ });
136
+
137
+ return !!match;
138
+ };
139
+
140
+ export const isBlockActive = (editor, format) => {
141
+ const aliasList = _.find(formatAliases, (x) => _.includes(x, format));
142
+
143
+ if (aliasList) {
144
+ const aliasFound = _.some(aliasList, (y) => {
145
+ return isSingleBlockTypeActive(editor, y);
146
+ });
147
+
148
+ if (aliasFound) {
149
+ return true;
150
+ }
151
+ }
152
+
153
+ return isSingleBlockTypeActive(editor, format);
154
+ };
155
+
156
+ export const getBlockTypeContextData = (editor, format) => {
157
+ let isActive, defaultFormat, matcher;
158
+
159
+ const aliasList = _.find(formatAliases, (x) => _.includes(x, format));
160
+
161
+ if (aliasList) {
162
+ const aliasFound = _.some(aliasList, (y) => {
163
+ return isSingleBlockTypeActive(editor, y);
164
+ });
165
+
166
+ if (aliasFound) {
167
+ isActive = true;
168
+ defaultFormat = _.first(aliasList);
169
+ matcher = (n) => _.includes(aliasList, n.type);
170
+
171
+ return { isActive, defaultFormat, matcher };
172
+ }
173
+ }
174
+
175
+ isActive = isBlockActive(editor, format);
176
+ defaultFormat = format;
177
+ matcher = (n) => n.type === format;
178
+
179
+ return { isActive, defaultFormat, matcher };
180
+ };
181
+
182
+ export const toggleInlineFormat = (editor, format) => {
183
+ const { isActive, defaultFormat, matcher } = getBlockTypeContextData(
184
+ editor,
185
+ format,
186
+ );
187
+
188
+ if (isActive) {
189
+ const rangeRef = Editor.rangeRef(editor, editor.selection);
190
+
191
+ Transforms.unwrapNodes(editor, {
192
+ match: matcher,
193
+ split: false,
194
+ });
195
+
196
+ const newSel = JSON.parse(JSON.stringify(rangeRef.current));
197
+
198
+ Transforms.select(editor, newSel);
199
+ editor.setSavedSelection(newSel);
200
+ // editor.savedSelection = newSel;
201
+ return;
202
+ }
203
+
204
+ const exclusiveElements = config.settings.slate.exclusiveElements;
205
+ const matchedElements = exclusiveTags(exclusiveElements, format);
206
+ let alreadyOneIsActive =
207
+ !!matchedElements &&
208
+ (matchedElements.indexOf(format) === 0
209
+ ? isBlockActive(editor, matchedElements[1])
210
+ : isBlockActive(editor, matchedElements[0]));
211
+
212
+ if (alreadyOneIsActive) {
213
+ Transforms.unwrapNodes(editor, {
214
+ match: (n) => matchedElements.includes(n.type),
215
+ split: false,
216
+ });
217
+
218
+ const block = { type: format, children: [] };
219
+ Transforms.wrapNodes(editor, block, { split: true });
220
+ return;
221
+ }
222
+
223
+ // `children` property is added automatically as an empty array then
224
+ // normalized
225
+ const block = { type: defaultFormat };
226
+ Transforms.wrapNodes(editor, block, { split: true });
227
+ };
228
+
229
+ const exclusiveTags = (exclusiveElements, format) => {
230
+ let elements = null;
231
+ for (const item of exclusiveElements) {
232
+ if (item.includes(format)) {
233
+ elements = item;
234
+ break;
235
+ }
236
+ }
237
+
238
+ return elements;
239
+ };
240
+
241
+ export const toggleBlock = (editor, format, allowedChildren) => {
242
+ // We have 6 boolean variables which need to be accounted for.
243
+ // See https://docs.google.com/spreadsheets/d/1mVeMuqSTMABV2BhoHPrPAFjn7zUksbNgZ9AQK_dcd3U/edit?usp=sharing
244
+ const { slate } = config.settings;
245
+ const { listTypes } = slate;
246
+
247
+ const isListItem = isBlockActive(editor, slate.listItemType);
248
+ const isActive = isBlockActive(editor, format);
249
+ const wantsList = listTypes.includes(format);
250
+
251
+ if (isListItem && !wantsList) {
252
+ toggleFormatAsListItem(editor, format);
253
+ } else if (isListItem && wantsList && !isActive) {
254
+ switchListType(editor, format);
255
+ } else if (!isListItem && wantsList) {
256
+ changeBlockToList(editor, format);
257
+ } else if (!isListItem && !wantsList) {
258
+ toggleFormat(editor, format, allowedChildren);
259
+ } else if (isListItem && wantsList && isActive) {
260
+ clearFormatting(editor);
261
+ } else {
262
+ console.warn('toggleBlock case not covered, please examine:', {
263
+ wantsList,
264
+ isActive,
265
+ isListItem,
266
+ });
267
+ }
268
+ };
269
+
270
+ /*
271
+ * Applies a block format to a list item. Will split the list
272
+ */
273
+ export const toggleFormatAsListItem = (editor, format) => {
274
+ Transforms.setNodes(editor, {
275
+ type: format,
276
+ });
277
+
278
+ Editor.normalize(editor);
279
+ };
280
+
281
+ /*
282
+ * Toggles between list types by exploding the block
283
+ */
284
+ export const switchListType = (editor, format) => {
285
+ const { slate } = config.settings;
286
+ Transforms.unwrapNodes(editor, {
287
+ match: (n) => slate.listTypes.includes(n.type),
288
+ split: true,
289
+ });
290
+ const block = { type: format, children: [] };
291
+ Transforms.wrapNodes(editor, block);
292
+ };
293
+
294
+ export const changeBlockToList = (editor, format) => {
295
+ const { slate } = config.settings;
296
+ const [match] = Editor.nodes(editor, {
297
+ match: (n) => n.type === slate.listItemType,
298
+ });
299
+
300
+ if (!match) {
301
+ Transforms.setNodes(editor, {
302
+ type: slate.listItemType,
303
+ // id: nanoid(8),
304
+ });
305
+ }
306
+
307
+ // `children` property is added automatically as an empty array then
308
+ // normalized
309
+ const block = { type: format };
310
+ Transforms.wrapNodes(editor, block);
311
+ };
312
+
313
+ export const toggleFormat = (editor, format, allowedChildren) => {
314
+ const { slate } = config.settings;
315
+ const isActive = isBlockActive(editor, format);
316
+ const type = isActive ? slate.defaultBlockType : format;
317
+ Transforms.setNodes(editor, {
318
+ type,
319
+ });
320
+ allowedChildren?.length &&
321
+ Transforms.unwrapNodes(editor, {
322
+ mode: 'all',
323
+ at: [0],
324
+ match: (n, path) => {
325
+ const isMatch =
326
+ path.length > 1 && // we don't deal with the parent nodes
327
+ !(Text.isText(n) || allowedChildren.includes(n?.type));
328
+ return isMatch;
329
+ },
330
+ });
331
+ };
332
+
333
+ /**
334
+ * @param {object} properties A prop received by the View component
335
+ * which is read by the `getBlocksFieldname` and
336
+ * `getBlocksLayoutFieldname` Volto helpers to produce the return value.
337
+ * @returns {Array} All the blocks data taken from the Volto form.
338
+ */
339
+ export const getAllBlocks = (properties, blocks) => {
340
+ const blocksFieldName = getBlocksFieldname(properties);
341
+ const blocksLayoutFieldname = getBlocksLayoutFieldname(properties);
342
+
343
+ for (const n of properties?.[blocksLayoutFieldname]?.items || []) {
344
+ const block = properties[blocksFieldName][n];
345
+ // TODO Make this configurable via block config getBlocks
346
+ if (
347
+ block?.data?.[blocksLayoutFieldname] &&
348
+ block?.data?.[blocksFieldName]
349
+ ) {
350
+ getAllBlocks(block.data, blocks);
351
+ } else if (block?.[blocksLayoutFieldname] && block?.[blocksFieldName]) {
352
+ getAllBlocks(block, blocks);
353
+ }
354
+ blocks.push({
355
+ id: n,
356
+ ...block,
357
+ });
358
+ }
359
+ return blocks;
360
+ };
361
+
362
+ export const clearFormatting = (editor) => {
363
+ const { slate } = config.settings;
364
+ Transforms.setNodes(editor, {
365
+ type: slate.defaultBlockType,
366
+ });
367
+ Transforms.unwrapNodes(editor, {
368
+ match: (n) => n.type && n.type !== slate.defaultBlockType,
369
+ mode: 'all',
370
+ split: false,
371
+ });
372
+ };