-
-
Notifications
You must be signed in to change notification settings - Fork 573
/
index.js
171 lines (158 loc) · 5.15 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
import React from 'react';
import redraft from 'redraft';
import TextBlockView from './TextBlockView';
import TextBlockEdit from './TextBlockEdit';
import TextBlockSchema from './TextBlockSchema';
import {
goDown,
goUp,
backspaceInList,
indentListItems,
joinWithNextBlock,
joinWithPreviousBlock,
softBreak,
moveListItemDown,
moveListItemUp,
traverseBlocks,
unwrapEmptyString,
slashMenu,
cancelEsc,
} from './keyboard';
import { withDeleteSelectionOnEnter } from '@plone/volto-slate/editor/extensions';
import {
breakList,
withDeserializers,
withLists,
withSplitBlocksOnBreak,
withIsSelected,
normalizeExternalData,
} from './extensions';
import { extractImages } from '@plone/volto-slate/editor/plugins/Image/deconstruct';
import { extractTables } from '@plone/volto-slate/blocks/Table/deconstruct';
import textSVG from '@plone/volto/icons/subtext.svg';
export { TextBlockView, TextBlockEdit, TextBlockSchema };
export default function applyConfig(config) {
config.settings.slate = {
// TODO: should we inverse order? First here gets executed last
textblockExtensions: [
withLists,
withSplitBlocksOnBreak, // TODO: do we still need this one?
withDeleteSelectionOnEnter,
withDeserializers,
withIsSelected,
breakList,
normalizeExternalData,
],
// Pluggable handlers for the onKeyDown event of <Editable />
// Order matters here. A handler can return `true` to stop executing any
// following handler
textblockKeyboardHandlers: {
Backspace: [
unwrapEmptyString,
backspaceInList, // Backspace in list item lifts node and breaks Volto blocks
joinWithPreviousBlock, // Backspace at beginning of block joins with previous block
],
Delete: [
unwrapEmptyString,
joinWithNextBlock, // Delete at end of block joins with next block
],
Enter: [
slashMenu,
unwrapEmptyString,
softBreak, // Handles shift+Enter as a newline (<br/>)
],
ArrowUp: [
slashMenu,
moveListItemUp, // Move up a list with with Ctrl+up
goUp, // Select previous block
],
ArrowDown: [
slashMenu,
moveListItemDown, // Move down a list item with Ctrl+down
goDown, // Select next block
],
Tab: [
indentListItems, // <tab> and <c-tab> behaviour for list items
traverseBlocks,
],
Escape: [cancelEsc],
},
textblockDetachedKeyboardHandlers: {
Enter: [
softBreak, // Handles shift+Enter as a newline (<br/>)
],
},
// Used by deconstructToVoltoBlocks to transform tags such as <img> to a Volto image block
// These emiters receive (editor, pathRef), emit [blockid, blockoptions] and
// are allowed to change the editor contents (for the given path)
voltoBlockEmiters: [
...(config.settings.slate.voltoBlockEmiters || []),
extractImages,
extractTables,
],
// These elements will get an id, to make them targets in TOC
topLevelTargetElements: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
...config.settings.slate, // TODO: is this correct for volto-slate addons?
};
const slateBlockConfig = {
id: 'slate',
title: 'Text',
icon: textSVG,
group: 'text',
view: TextBlockView,
edit: TextBlockEdit,
schema: TextBlockSchema,
restricted: false,
mostUsed: false,
blockHasOwnFocusManagement: true,
sidebarTab: 0,
security: {
addPermission: [],
view: [],
},
blockHasValue: (data) => {
// TODO: this should be handled better
return data && !!data.plaintext?.trim();
},
tocEntry: (block = {}) => {
const { value, override_toc, entry_text, level, plaintext } = block;
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;
},
};
// Make draft js compatible with ToC
config.blocks.blocksConfig.text = {
...config.blocks.blocksConfig.text,
restricted: true,
tocEntry: (block = {}) => {
const draft = redraft(
block.text,
config.settings.richtextViewSettings.ToHTMLRenderers,
config.settings.richtextViewSettings.ToHTMLOptions,
);
const type = draft?.[0]?.[0]?.type;
return config.settings.slate.topLevelTargetElements.includes(type)
? [parseInt(type.slice(1)), block.text.blocks[0].text]
: null;
},
};
config.blocks.blocksConfig.slate = slateBlockConfig;
config.blocks.blocksConfig.detachedSlate = {
...config.blocks.blocksConfig.slate,
id: 'detachedSlate',
title: 'Detached Slate',
edit: (props) => <TextBlockEdit {...props} detached />,
restricted: true,
};
if (config.blocks.blocksConfig.gridBlock) {
// This is required in order to initialize the inner blocksConfig
// for the grid block, since the slate block lives in an add-on
// it should be responsible to fill itself into the grid configuration
config.blocks.blocksConfig.gridBlock.blocksConfig.slate = slateBlockConfig;
}
return config;
}