@plone/volto-slate 18.0.0-alpha.4
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/.eslintrc.js +6 -0
- package/.release-it.json +25 -0
- package/CHANGELOG.md +19 -0
- package/LICENSE.md +21 -0
- package/README.md +10 -0
- package/build/messages/src/blocks/Table/TableBlockEdit.json +90 -0
- package/build/messages/src/blocks/Text/DefaultTextBlockEditor.json +6 -0
- package/build/messages/src/blocks/Text/DetachedTextBlockEditor.json +6 -0
- package/build/messages/src/blocks/Text/SlashMenu.json +6 -0
- package/build/messages/src/editor/plugins/AdvancedLink/index.json +10 -0
- package/build/messages/src/editor/plugins/Link/index.json +10 -0
- package/build/messages/src/editor/plugins/Table/index.json +30 -0
- package/build/messages/src/elementEditor/messages.json +10 -0
- package/build/messages/src/widgets/HtmlSlateWidget.json +6 -0
- package/build/messages/src/widgets/RichTextWidgetView.json +6 -0
- package/locales/de/LC_MESSAGES/volto.po +148 -0
- package/locales/en/LC_MESSAGES/volto.po +148 -0
- package/locales/volto.pot +182 -0
- package/package.json +42 -0
- package/src/actions/content.js +30 -0
- package/src/actions/index.js +3 -0
- package/src/actions/plugins.js +9 -0
- package/src/actions/selection.js +22 -0
- package/src/blocks/Table/Cell.jsx +87 -0
- package/src/blocks/Table/Cell.test.js +54 -0
- package/src/blocks/Table/TableBlockEdit.jsx +694 -0
- package/src/blocks/Table/TableBlockEdit.test.js +40 -0
- package/src/blocks/Table/TableBlockView.jsx +150 -0
- package/src/blocks/Table/TableBlockView.test.js +49 -0
- package/src/blocks/Table/__snapshots__/Cell.test.js.snap +3 -0
- package/src/blocks/Table/__snapshots__/TableBlockEdit.test.js.snap +22 -0
- package/src/blocks/Table/__snapshots__/TableBlockView.test.js.snap +27 -0
- package/src/blocks/Table/deconstruct.js +113 -0
- package/src/blocks/Table/extensions/normalizeTable.js +5 -0
- package/src/blocks/Table/index.js +60 -0
- package/src/blocks/Table/schema.js +122 -0
- package/src/blocks/Text/DefaultTextBlockEditor.jsx +304 -0
- package/src/blocks/Text/DetachedTextBlockEditor.jsx +77 -0
- package/src/blocks/Text/MarkdownIntroduction.jsx +59 -0
- package/src/blocks/Text/PluginSidebar.jsx +18 -0
- package/src/blocks/Text/ShortcutListing.jsx +28 -0
- package/src/blocks/Text/SlashMenu.jsx +203 -0
- package/src/blocks/Text/TextBlockEdit.jsx +38 -0
- package/src/blocks/Text/TextBlockEdit.test.js +107 -0
- package/src/blocks/Text/TextBlockSchema.js +54 -0
- package/src/blocks/Text/TextBlockView.jsx +31 -0
- package/src/blocks/Text/__snapshots__/TextBlockEdit.test.js.snap +62 -0
- package/src/blocks/Text/css/editor.css +18 -0
- package/src/blocks/Text/extensions/Readme.md +49 -0
- package/src/blocks/Text/extensions/breakList.js +100 -0
- package/src/blocks/Text/extensions/index.js +6 -0
- package/src/blocks/Text/extensions/insertBreak.js +57 -0
- package/src/blocks/Text/extensions/isSelected.js +7 -0
- package/src/blocks/Text/extensions/normalizeExternalData.js +7 -0
- package/src/blocks/Text/extensions/withDeserializers.js +87 -0
- package/src/blocks/Text/extensions/withLists.js +5 -0
- package/src/blocks/Text/index.js +171 -0
- package/src/blocks/Text/keyboard/backspaceInList.js +58 -0
- package/src/blocks/Text/keyboard/breakBlocks.js +3 -0
- package/src/blocks/Text/keyboard/cancelEsc.js +7 -0
- package/src/blocks/Text/keyboard/indentListItems.js +240 -0
- package/src/blocks/Text/keyboard/index.js +52 -0
- package/src/blocks/Text/keyboard/joinBlocks.js +180 -0
- package/src/blocks/Text/keyboard/moveListItems.js +124 -0
- package/src/blocks/Text/keyboard/slashMenu.js +19 -0
- package/src/blocks/Text/keyboard/softBreak.js +7 -0
- package/src/blocks/Text/keyboard/traverseBlocks.js +81 -0
- package/src/blocks/Text/keyboard/unwrapEmptyString.js +26 -0
- package/src/blocks/Text/schema.js +39 -0
- package/src/constants.js +123 -0
- package/src/editor/EditorContext.jsx +5 -0
- package/src/editor/EditorReference.jsx +22 -0
- package/src/editor/SlateEditor.jsx +375 -0
- package/src/editor/config.jsx +344 -0
- package/src/editor/decorate.js +68 -0
- package/src/editor/deserialize.js +185 -0
- package/src/editor/extensions/index.js +6 -0
- package/src/editor/extensions/insertBreak.js +15 -0
- package/src/editor/extensions/insertData.js +161 -0
- package/src/editor/extensions/isInline.js +14 -0
- package/src/editor/extensions/normalizeExternalData.js +8 -0
- package/src/editor/extensions/normalizeNode.js +48 -0
- package/src/editor/extensions/withDeserializers.js +15 -0
- package/src/editor/extensions/withTestingFeatures.jsx +84 -0
- package/src/editor/index.js +14 -0
- package/src/editor/less/editor.less +173 -0
- package/src/editor/less/globals.less +18 -0
- package/src/editor/less/slate.less +28 -0
- package/src/editor/plugins/AdvancedLink/deserialize.js +90 -0
- package/src/editor/plugins/AdvancedLink/extensions.js +32 -0
- package/src/editor/plugins/AdvancedLink/index.js +50 -0
- package/src/editor/plugins/AdvancedLink/render.jsx +37 -0
- package/src/editor/plugins/AdvancedLink/schema.js +114 -0
- package/src/editor/plugins/AdvancedLink/styles.less +8 -0
- package/src/editor/plugins/Blockquote/index.js +30 -0
- package/src/editor/plugins/Callout/index.js +34 -0
- package/src/editor/plugins/Image/deconstruct.js +30 -0
- package/src/editor/plugins/Image/extensions.js +51 -0
- package/src/editor/plugins/Image/index.js +11 -0
- package/src/editor/plugins/Image/render.jsx +22 -0
- package/src/editor/plugins/Link/extensions.js +58 -0
- package/src/editor/plugins/Link/index.js +159 -0
- package/src/editor/plugins/Link/render.jsx +54 -0
- package/src/editor/plugins/Markdown/constants.js +81 -0
- package/src/editor/plugins/Markdown/extensions.js +336 -0
- package/src/editor/plugins/Markdown/index.js +28 -0
- package/src/editor/plugins/Markdown/utils.js +198 -0
- package/src/editor/plugins/StyleMenu/StyleMenu.jsx +153 -0
- package/src/editor/plugins/StyleMenu/index.js +19 -0
- package/src/editor/plugins/StyleMenu/style.less +29 -0
- package/src/editor/plugins/StyleMenu/utils.js +168 -0
- package/src/editor/plugins/Table/TableButton.jsx +142 -0
- package/src/editor/plugins/Table/TableCell.jsx +44 -0
- package/src/editor/plugins/Table/TableContainer.jsx +37 -0
- package/src/editor/plugins/Table/TableSizePicker.jsx +83 -0
- package/src/editor/plugins/Table/extensions.js +87 -0
- package/src/editor/plugins/Table/index.js +390 -0
- package/src/editor/plugins/Table/less/public.less +29 -0
- package/src/editor/plugins/Table/less/table.less +28 -0
- package/src/editor/plugins/Table/render.jsx +30 -0
- package/src/editor/plugins/index.js +19 -0
- package/src/editor/render.jsx +224 -0
- package/src/editor/ui/BasicToolbar.jsx +11 -0
- package/src/editor/ui/BlockButton.jsx +31 -0
- package/src/editor/ui/ClearFormattingButton.jsx +21 -0
- package/src/editor/ui/ExpandedToolbar.jsx +18 -0
- package/src/editor/ui/Expando.jsx +5 -0
- package/src/editor/ui/InlineToolbar.jsx +69 -0
- package/src/editor/ui/MarkButton.jsx +23 -0
- package/src/editor/ui/MarkElementButton.jsx +30 -0
- package/src/editor/ui/Menu.jsx +13 -0
- package/src/editor/ui/PositionedToolbar.jsx +32 -0
- package/src/editor/ui/Separator.jsx +7 -0
- package/src/editor/ui/SlateContextToolbar.jsx +13 -0
- package/src/editor/ui/SlateToolbar.jsx +96 -0
- package/src/editor/ui/Toolbar.jsx +103 -0
- package/src/editor/ui/ToolbarButton.jsx +33 -0
- package/src/editor/ui/ToolbarButton.test.js +25 -0
- package/src/editor/ui/__snapshots__/ToolbarButton.test.js.snap +16 -0
- package/src/editor/ui/index.js +15 -0
- package/src/editor/utils.js +248 -0
- package/src/elementEditor/ContextButtons.jsx +57 -0
- package/src/elementEditor/PluginEditor.jsx +124 -0
- package/src/elementEditor/Readme.md +6 -0
- package/src/elementEditor/SchemaProvider.jsx +4 -0
- package/src/elementEditor/SidebarEditor.jsx +46 -0
- package/src/elementEditor/ToolbarButton.jsx +44 -0
- package/src/elementEditor/index.js +5 -0
- package/src/elementEditor/makeInlineElementPlugin.js +100 -0
- package/src/elementEditor/messages.js +14 -0
- package/src/elementEditor/utils.js +227 -0
- package/src/hooks/index.js +3 -0
- package/src/hooks/useEditorContext.js +6 -0
- package/src/hooks/useIsomorphicLayoutEffect.js +7 -0
- package/src/hooks/useSelectionPosition.js +25 -0
- package/src/i18n.js +180 -0
- package/src/icons/hashlink.svg +57 -0
- package/src/index.js +61 -0
- package/src/reducers/content.js +74 -0
- package/src/reducers/index.js +3 -0
- package/src/reducers/plugins.js +17 -0
- package/src/reducers/selection.js +16 -0
- package/src/utils/blocks.js +379 -0
- package/src/utils/blocks.test.js +138 -0
- package/src/utils/editor.js +31 -0
- package/src/utils/image.js +25 -0
- package/src/utils/index.js +11 -0
- package/src/utils/internals.js +46 -0
- package/src/utils/lists.js +92 -0
- package/src/utils/marks.js +104 -0
- package/src/utils/mime-types.js +24 -0
- package/src/utils/nodes.js +4 -0
- package/src/utils/ops.js +20 -0
- package/src/utils/random.js +17 -0
- package/src/utils/selection.js +236 -0
- package/src/utils/slate-string-utils.js +409 -0
- package/src/utils/volto-blocks.js +314 -0
- package/src/widgets/ErrorBoundary.jsx +27 -0
- package/src/widgets/HtmlSlateWidget.jsx +138 -0
- package/src/widgets/ObjectByTypeWidget.jsx +49 -0
- package/src/widgets/RichTextWidget.jsx +72 -0
- package/src/widgets/RichTextWidgetView.jsx +36 -0
- package/src/widgets/style.css +21 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useSlate } from 'slate-react';
|
|
3
|
+
import { Dropdown } from 'semantic-ui-react';
|
|
4
|
+
import { useIntl, defineMessages } from 'react-intl';
|
|
5
|
+
import cx from 'classnames';
|
|
6
|
+
import { omit } from 'lodash';
|
|
7
|
+
import { isBlockStyleActive, isInlineStyleActive, toggleStyle } from './utils';
|
|
8
|
+
import config from '@plone/volto/registry';
|
|
9
|
+
import { ToolbarButton } from '@plone/volto-slate/editor/ui';
|
|
10
|
+
import paintSVG from '@plone/volto/icons/paint.svg';
|
|
11
|
+
|
|
12
|
+
const messages = defineMessages({
|
|
13
|
+
inlineStyle: {
|
|
14
|
+
id: 'Inline Style',
|
|
15
|
+
defaultMessage: 'Inline Style',
|
|
16
|
+
},
|
|
17
|
+
paragraphStyle: {
|
|
18
|
+
id: 'Paragraph Style',
|
|
19
|
+
defaultMessage: 'Paragraph Style',
|
|
20
|
+
},
|
|
21
|
+
additionalStyles: {
|
|
22
|
+
id: 'Additional Styles',
|
|
23
|
+
defaultMessage: 'Additional Styles',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const StyleMenuButton = ({ icon, active, ...props }) => (
|
|
28
|
+
<ToolbarButton {...props} icon={icon} active={active} />
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const MenuOpts = ({ editor, toSelect, option, type }) => {
|
|
32
|
+
const isActive = toSelect.includes(option);
|
|
33
|
+
return (
|
|
34
|
+
<Dropdown.Item
|
|
35
|
+
as="span"
|
|
36
|
+
active={isActive}
|
|
37
|
+
className={cx(`${type}-${option.value}`, { active: isActive })}
|
|
38
|
+
{...omit(option, ['isBlock'])}
|
|
39
|
+
data-isblock={option.isBlock}
|
|
40
|
+
onClick={(event, selItem) => {
|
|
41
|
+
toggleStyle(editor, {
|
|
42
|
+
cssClass: selItem.value,
|
|
43
|
+
isBlock: selItem.isBlock,
|
|
44
|
+
});
|
|
45
|
+
}}
|
|
46
|
+
/>
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const StylingsButton = (props) => {
|
|
51
|
+
const editor = useSlate();
|
|
52
|
+
const intl = useIntl();
|
|
53
|
+
|
|
54
|
+
// Converting the settings to a format that is required by dropdowns.
|
|
55
|
+
const inlineOpts = [
|
|
56
|
+
...config.settings.slate.styleMenu.inlineStyles.map((def) => {
|
|
57
|
+
return {
|
|
58
|
+
value: def.cssClass,
|
|
59
|
+
text: def.label,
|
|
60
|
+
icon: def.icon,
|
|
61
|
+
isBlock: false,
|
|
62
|
+
};
|
|
63
|
+
}),
|
|
64
|
+
];
|
|
65
|
+
const blockOpts = [
|
|
66
|
+
...config.settings.slate.styleMenu.blockStyles.map((def) => {
|
|
67
|
+
return {
|
|
68
|
+
value: def.cssClass,
|
|
69
|
+
text: def.label,
|
|
70
|
+
icon: def.icon,
|
|
71
|
+
isBlock: true,
|
|
72
|
+
};
|
|
73
|
+
}),
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
// Calculating the initial selection.
|
|
77
|
+
const toSelect = [];
|
|
78
|
+
// block styles
|
|
79
|
+
for (const val of blockOpts) {
|
|
80
|
+
const ia = isBlockStyleActive(editor, val.value);
|
|
81
|
+
if (ia) {
|
|
82
|
+
toSelect.push(val);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// inline styles
|
|
86
|
+
for (const val of inlineOpts) {
|
|
87
|
+
const ia = isInlineStyleActive(editor, val.value);
|
|
88
|
+
if (ia) {
|
|
89
|
+
toSelect.push(val);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const menuItemProps = {
|
|
94
|
+
toSelect,
|
|
95
|
+
editor,
|
|
96
|
+
};
|
|
97
|
+
const showMenu = inlineOpts.length || blockOpts.length;
|
|
98
|
+
return showMenu ? (
|
|
99
|
+
<Dropdown
|
|
100
|
+
id="style-menu"
|
|
101
|
+
pointing="top left"
|
|
102
|
+
multiple
|
|
103
|
+
value={toSelect}
|
|
104
|
+
disabled={config.settings.slate.styleMenu.disabled ?? false}
|
|
105
|
+
additionLabel={intl.formatMessage(messages.additionalStyles)}
|
|
106
|
+
trigger={
|
|
107
|
+
<StyleMenuButton
|
|
108
|
+
title={intl.formatMessage(messages.additionalStyles)}
|
|
109
|
+
icon={paintSVG}
|
|
110
|
+
active={toSelect.length > 0}
|
|
111
|
+
/>
|
|
112
|
+
}
|
|
113
|
+
>
|
|
114
|
+
<Dropdown.Menu>
|
|
115
|
+
{inlineOpts.length && (
|
|
116
|
+
<>
|
|
117
|
+
<Dropdown.Header
|
|
118
|
+
content={intl.formatMessage(messages.inlineStyle)}
|
|
119
|
+
/>
|
|
120
|
+
{inlineOpts.map((option, index) => (
|
|
121
|
+
<MenuOpts
|
|
122
|
+
{...menuItemProps}
|
|
123
|
+
type="inline-style"
|
|
124
|
+
option={option}
|
|
125
|
+
key={index}
|
|
126
|
+
/>
|
|
127
|
+
))}
|
|
128
|
+
</>
|
|
129
|
+
)}
|
|
130
|
+
|
|
131
|
+
{blockOpts.length && (
|
|
132
|
+
<>
|
|
133
|
+
<Dropdown.Header
|
|
134
|
+
content={intl.formatMessage(messages.paragraphStyle)}
|
|
135
|
+
/>
|
|
136
|
+
{blockOpts.map((option, index) => (
|
|
137
|
+
<MenuOpts
|
|
138
|
+
{...menuItemProps}
|
|
139
|
+
type="block-style"
|
|
140
|
+
option={option}
|
|
141
|
+
key={index}
|
|
142
|
+
/>
|
|
143
|
+
))}
|
|
144
|
+
</>
|
|
145
|
+
)}
|
|
146
|
+
</Dropdown.Menu>
|
|
147
|
+
</Dropdown>
|
|
148
|
+
) : (
|
|
149
|
+
''
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export default StylingsButton;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import StyleMenu from './StyleMenu';
|
|
3
|
+
import './style.less';
|
|
4
|
+
|
|
5
|
+
export default function install(config) {
|
|
6
|
+
const { slate } = config.settings;
|
|
7
|
+
|
|
8
|
+
slate.buttons.styleMenu = (props) => <StyleMenu {...props} title="Styles" />;
|
|
9
|
+
|
|
10
|
+
slate.toolbarButtons.push('styleMenu');
|
|
11
|
+
slate.expandedToolbarButtons.push('styleMenu');
|
|
12
|
+
|
|
13
|
+
slate.styleMenu = {
|
|
14
|
+
inlineStyles: [],
|
|
15
|
+
blockStyles: [],
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return config;
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#style-menu.ui.dropdown .menu {
|
|
2
|
+
.item {
|
|
3
|
+
display: flex;
|
|
4
|
+
user-select: none;
|
|
5
|
+
|
|
6
|
+
span.text {
|
|
7
|
+
margin-top: auto;
|
|
8
|
+
line-height: normal;
|
|
9
|
+
vertical-align: middle;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.header {
|
|
14
|
+
color: rgb(153, 153, 153);
|
|
15
|
+
font-size: 1rem;
|
|
16
|
+
font-weight: normal;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.active {
|
|
20
|
+
z-index: 1;
|
|
21
|
+
background: #68778d;
|
|
22
|
+
box-shadow: none;
|
|
23
|
+
color: #ffffff;
|
|
24
|
+
font-weight: 300;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
overflow: auto;
|
|
28
|
+
max-height: 50vh;
|
|
29
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
|
|
2
|
+
import { Editor, Transforms } from 'slate';
|
|
3
|
+
import { isBlockActive } from '@plone/volto-slate/utils';
|
|
4
|
+
import config from '@plone/volto/registry';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Toggles a style (e.g. in the StyleMenu plugin).
|
|
8
|
+
* @param {Editor} editor
|
|
9
|
+
* @param {object} options
|
|
10
|
+
* @param {boolean} options.isRequested Whether the given style is requested by
|
|
11
|
+
* the user. The style is only applied if it is requested and only removed if it
|
|
12
|
+
* is not requested.
|
|
13
|
+
*/
|
|
14
|
+
export const toggleStyle = (editor, { cssClass, isBlock, isRequested }) => {
|
|
15
|
+
if (isBlock) {
|
|
16
|
+
toggleBlockStyle(editor, cssClass);
|
|
17
|
+
} else {
|
|
18
|
+
toggleInlineStyle(editor, cssClass);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const toggleBlockStyle = (editor, style) => {
|
|
23
|
+
// We have 6 boolean variables which need to be accounted for.
|
|
24
|
+
// See https://docs.google.com/spreadsheets/d/1mVeMuqSTMABV2BhoHPrPAFjn7zUksbNgZ9AQK_dcd3U/edit?usp=sharing
|
|
25
|
+
const { slate } = config.settings;
|
|
26
|
+
|
|
27
|
+
const isListItem = isBlockActive(editor, slate.listItemType);
|
|
28
|
+
const isActive = isBlockStyleActive(editor, style);
|
|
29
|
+
const wantsList = false;
|
|
30
|
+
|
|
31
|
+
if (isListItem && !wantsList) {
|
|
32
|
+
toggleBlockStyleAsListItem(editor, style);
|
|
33
|
+
} else if (isListItem && wantsList && !isActive) {
|
|
34
|
+
// switchListType(editor, format); // this will deconstruct to Volto blocks
|
|
35
|
+
} else if (!isListItem && wantsList) {
|
|
36
|
+
// changeBlockToList(editor, format);
|
|
37
|
+
} else if (!isListItem && !wantsList) {
|
|
38
|
+
internalToggleBlockStyle(editor, style);
|
|
39
|
+
} else {
|
|
40
|
+
console.warn('toggleBlockStyle case not covered, please examine:', {
|
|
41
|
+
wantsList,
|
|
42
|
+
isActive,
|
|
43
|
+
isListItem,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const toggleInlineStyle = (editor, style) => {
|
|
49
|
+
// We have 6 boolean variables which need to be accounted for.
|
|
50
|
+
// See https://docs.google.com/spreadsheets/d/1mVeMuqSTMABV2BhoHPrPAFjn7zUksbNgZ9AQK_dcd3U/edit?usp=sharing
|
|
51
|
+
const { slate } = config.settings;
|
|
52
|
+
|
|
53
|
+
const isListItem = isBlockActive(editor, slate.listItemType);
|
|
54
|
+
const isActive = isInlineStyleActive(editor, style);
|
|
55
|
+
const wantsList = false;
|
|
56
|
+
|
|
57
|
+
if (isListItem && !wantsList) {
|
|
58
|
+
toggleInlineStyleAsListItem(editor, style);
|
|
59
|
+
} else if (isListItem && wantsList && !isActive) {
|
|
60
|
+
// switchListType(editor, format); // this will deconstruct to Volto blocks
|
|
61
|
+
} else if (!isListItem && wantsList) {
|
|
62
|
+
// changeBlockToList(editor, format);
|
|
63
|
+
} else if (!isListItem && !wantsList) {
|
|
64
|
+
internalToggleInlineStyle(editor, style);
|
|
65
|
+
} else {
|
|
66
|
+
console.warn('toggleInlineStyle case not covered, please examine:', {
|
|
67
|
+
wantsList,
|
|
68
|
+
isActive,
|
|
69
|
+
isListItem,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const isBlockStyleActive = (editor, style) => {
|
|
75
|
+
const keyName = `style-${style}`;
|
|
76
|
+
const sn = Array.from(
|
|
77
|
+
Editor.nodes(editor, {
|
|
78
|
+
match: (n) => {
|
|
79
|
+
const isStyle = typeof n.styleName === 'string' || n[keyName];
|
|
80
|
+
return !Editor.isEditor(n) && isStyle;
|
|
81
|
+
},
|
|
82
|
+
mode: 'all',
|
|
83
|
+
}),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
for (const [n] of sn) {
|
|
87
|
+
if (typeof n.styleName === 'string') {
|
|
88
|
+
if (n.styleName.split(' ').filter((x) => x === style).length > 0) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
} else if (
|
|
92
|
+
n[keyName] &&
|
|
93
|
+
keyName.split('-').filter((x) => x === style).length > 0
|
|
94
|
+
)
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export const isInlineStyleActive = (editor, style) => {
|
|
101
|
+
const m = Editor.marks(editor);
|
|
102
|
+
const keyName = `style-${style}`;
|
|
103
|
+
if (m && m[keyName]) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export const internalToggleBlockStyle = (editor, style) => {
|
|
110
|
+
toggleBlockStyleInSelection(editor, style);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const internalToggleInlineStyle = (editor, style) => {
|
|
114
|
+
toggleInlineStyleInSelection(editor, style);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* Applies a block format unto a list item. Will split the list and deconstruct the
|
|
119
|
+
* block
|
|
120
|
+
*/
|
|
121
|
+
export const toggleBlockStyleAsListItem = (editor, style) => {
|
|
122
|
+
toggleBlockStyleInSelection(editor, style);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/*
|
|
126
|
+
* Applies an inline style unto a list item.
|
|
127
|
+
*/
|
|
128
|
+
export const toggleInlineStyleAsListItem = (editor, style) => {
|
|
129
|
+
toggleInlineStyleInSelection(editor, style);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
function toggleInlineStyleInSelection(editor, style) {
|
|
133
|
+
const m = Editor.marks(editor);
|
|
134
|
+
const keyName = 'style-' + style;
|
|
135
|
+
|
|
136
|
+
if (m && m[keyName]) {
|
|
137
|
+
Editor.removeMark(editor, keyName);
|
|
138
|
+
} else {
|
|
139
|
+
Editor.addMark(editor, keyName, true);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function toggleBlockStyleInSelection(editor, style) {
|
|
144
|
+
const sn = Array.from(
|
|
145
|
+
Editor.nodes(editor, {
|
|
146
|
+
mode: 'highest',
|
|
147
|
+
match: (n) => {
|
|
148
|
+
return !Editor.isEditor(n);
|
|
149
|
+
},
|
|
150
|
+
}),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
for (const [n, p] of sn) {
|
|
154
|
+
let cn = n.styleName;
|
|
155
|
+
if (typeof n.styleName !== 'string') {
|
|
156
|
+
cn = style;
|
|
157
|
+
} else if (n.styleName.split(' ').filter((x) => x === style).length > 0) {
|
|
158
|
+
cn = cn
|
|
159
|
+
.split(' ')
|
|
160
|
+
.filter((x) => x !== style)
|
|
161
|
+
.join(' ');
|
|
162
|
+
} else {
|
|
163
|
+
// the style is not set but other styles are set
|
|
164
|
+
cn = cn.split(' ').concat(style).join(' ');
|
|
165
|
+
}
|
|
166
|
+
Transforms.setNodes(editor, { styleName: cn }, { at: p });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useSlate } from 'slate-react';
|
|
3
|
+
import { Dropdown } from 'semantic-ui-react';
|
|
4
|
+
import { ToolbarButton } from '@plone/volto-slate/editor/ui';
|
|
5
|
+
|
|
6
|
+
import tableSVG from '@plone/volto/icons/table.svg';
|
|
7
|
+
import TableContainer from './TableContainer';
|
|
8
|
+
import './less/table.less';
|
|
9
|
+
import { Editor, Transforms } from 'slate';
|
|
10
|
+
|
|
11
|
+
const TableButton = ({ ...props }) => {
|
|
12
|
+
const editor = useSlate();
|
|
13
|
+
|
|
14
|
+
const [dropdownOpen, setDropdownOpen] = React.useState(false);
|
|
15
|
+
|
|
16
|
+
const [activeRow, setActiveRow] = React.useState(1);
|
|
17
|
+
const [activeColumn, setActiveColumn] = React.useState(1);
|
|
18
|
+
|
|
19
|
+
const defaultRowCount = 5;
|
|
20
|
+
const defaultColumnCount = 5;
|
|
21
|
+
|
|
22
|
+
const [rowCount, setRowCount] = React.useState(defaultRowCount);
|
|
23
|
+
const [columnCount, setColumnCount] = React.useState(defaultColumnCount);
|
|
24
|
+
|
|
25
|
+
const resetState = React.useCallback(() => {
|
|
26
|
+
setRowCount(defaultRowCount);
|
|
27
|
+
setColumnCount(defaultColumnCount);
|
|
28
|
+
setActiveRow(1);
|
|
29
|
+
setActiveColumn(1);
|
|
30
|
+
}, []);
|
|
31
|
+
|
|
32
|
+
const createEmptyCell = React.useCallback((formatAsColumnHeaders = false) => {
|
|
33
|
+
return {
|
|
34
|
+
type: formatAsColumnHeaders ? 'th' : 'td',
|
|
35
|
+
children: [{ type: 'p', children: [{ text: '' }] }],
|
|
36
|
+
};
|
|
37
|
+
}, []);
|
|
38
|
+
|
|
39
|
+
const createEmptyRow = React.useCallback(
|
|
40
|
+
(cellCount, formatAsColumnHeaders = false) => {
|
|
41
|
+
// should contain at least one <td> or it is not valid that children is empty
|
|
42
|
+
const row = { type: 'tr', children: [] };
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < cellCount; ++i) {
|
|
45
|
+
row.children.push(createEmptyCell(formatAsColumnHeaders));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return row;
|
|
49
|
+
},
|
|
50
|
+
[createEmptyCell],
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {number} row Number of rows for the new empty table.
|
|
55
|
+
* @param {number} column Number of columns for the new empty table.
|
|
56
|
+
*/
|
|
57
|
+
const insertEmptyTable = React.useCallback(
|
|
58
|
+
({ row, column }) => {
|
|
59
|
+
const rows = [createEmptyRow(column, true)];
|
|
60
|
+
for (let i = 0; i < row - 1; ++i) {
|
|
61
|
+
rows.push(createEmptyRow(column));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const table = {
|
|
65
|
+
type: 'table',
|
|
66
|
+
children: [
|
|
67
|
+
{
|
|
68
|
+
type: 'tbody',
|
|
69
|
+
children: rows,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
Transforms.insertNodes(editor, [table], {
|
|
75
|
+
at: Editor.end(editor, []),
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
[createEmptyRow, editor],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<>
|
|
83
|
+
<Dropdown
|
|
84
|
+
open={dropdownOpen}
|
|
85
|
+
onClose={() => {
|
|
86
|
+
resetState();
|
|
87
|
+
setDropdownOpen(false);
|
|
88
|
+
}}
|
|
89
|
+
trigger={
|
|
90
|
+
<ToolbarButton
|
|
91
|
+
{...props}
|
|
92
|
+
className="slate-table-dropdown-button"
|
|
93
|
+
onClick={() => {
|
|
94
|
+
if (dropdownOpen) {
|
|
95
|
+
resetState();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
setDropdownOpen(!dropdownOpen);
|
|
99
|
+
}}
|
|
100
|
+
icon={tableSVG}
|
|
101
|
+
></ToolbarButton>
|
|
102
|
+
}
|
|
103
|
+
>
|
|
104
|
+
<Dropdown.Menu className="slate-table-dropdown-menu">
|
|
105
|
+
<TableContainer
|
|
106
|
+
rowCount={rowCount}
|
|
107
|
+
columnCount={columnCount}
|
|
108
|
+
activeColumn={activeColumn}
|
|
109
|
+
activeRow={activeRow}
|
|
110
|
+
onCellMouseEnter={({ row, column }) => {
|
|
111
|
+
if (row > rowCount - 1) {
|
|
112
|
+
setRowCount(row + 1);
|
|
113
|
+
} else if (row < rowCount - 1) {
|
|
114
|
+
setRowCount(defaultRowCount);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (column > columnCount - 1) {
|
|
118
|
+
setColumnCount(column + 1);
|
|
119
|
+
} else if (column < columnCount - 1) {
|
|
120
|
+
setColumnCount(defaultColumnCount);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (row !== activeRow) {
|
|
124
|
+
setActiveRow(row);
|
|
125
|
+
}
|
|
126
|
+
if (column !== activeColumn) {
|
|
127
|
+
setActiveColumn(column);
|
|
128
|
+
}
|
|
129
|
+
}}
|
|
130
|
+
onCellMouseLeave={({ row, column }) => {}}
|
|
131
|
+
// `row` and `column` below are 1-based indices
|
|
132
|
+
onCellClick={({ row, column }) => {
|
|
133
|
+
insertEmptyTable({ row, column });
|
|
134
|
+
}}
|
|
135
|
+
/>
|
|
136
|
+
</Dropdown.Menu>
|
|
137
|
+
</Dropdown>
|
|
138
|
+
</>
|
|
139
|
+
);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export default TableButton;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import cx from 'classnames';
|
|
3
|
+
|
|
4
|
+
export const TableCell = ({
|
|
5
|
+
active,
|
|
6
|
+
row,
|
|
7
|
+
column,
|
|
8
|
+
onClick,
|
|
9
|
+
onMouseEnter,
|
|
10
|
+
onMouseLeave,
|
|
11
|
+
}) => {
|
|
12
|
+
const handleClick = React.useCallback(
|
|
13
|
+
(ev) => {
|
|
14
|
+
onClick({ row, column });
|
|
15
|
+
},
|
|
16
|
+
[column, onClick, row],
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const handleMouseEnter = React.useCallback(
|
|
20
|
+
(ev) => {
|
|
21
|
+
onMouseEnter({ row, column });
|
|
22
|
+
},
|
|
23
|
+
[column, onMouseEnter, row],
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const handleMouseLeave = React.useCallback(
|
|
27
|
+
(ev) => {
|
|
28
|
+
onMouseLeave({ row, column });
|
|
29
|
+
},
|
|
30
|
+
[column, onMouseLeave, row],
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<td
|
|
35
|
+
onMouseEnter={handleMouseEnter}
|
|
36
|
+
onMouseLeave={handleMouseLeave}
|
|
37
|
+
className={cx({ active })}
|
|
38
|
+
>
|
|
39
|
+
<button onClick={handleClick}></button>
|
|
40
|
+
</td>
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default TableCell;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import TableSizePicker from './TableSizePicker';
|
|
3
|
+
|
|
4
|
+
export const TableContainer = ({
|
|
5
|
+
rowCount,
|
|
6
|
+
columnCount,
|
|
7
|
+
activeRow,
|
|
8
|
+
activeColumn,
|
|
9
|
+
onCellClick,
|
|
10
|
+
onCellMouseEnter,
|
|
11
|
+
onCellMouseLeave,
|
|
12
|
+
}) => {
|
|
13
|
+
return (
|
|
14
|
+
<div style={{ padding: '1rem' }}>
|
|
15
|
+
<TableSizePicker
|
|
16
|
+
activeRow={activeRow}
|
|
17
|
+
activeColumn={activeColumn}
|
|
18
|
+
rowCount={rowCount}
|
|
19
|
+
columnCount={columnCount}
|
|
20
|
+
onCellClick={onCellClick}
|
|
21
|
+
onCellMouseEnter={({ row, column }) => {
|
|
22
|
+
onCellMouseEnter({ row, column });
|
|
23
|
+
}}
|
|
24
|
+
onCellMouseLeave={onCellMouseLeave}
|
|
25
|
+
/>
|
|
26
|
+
<p
|
|
27
|
+
style={{
|
|
28
|
+
textAlign: 'center',
|
|
29
|
+
}}
|
|
30
|
+
>
|
|
31
|
+
{activeColumn || 1} × {activeRow || 1}
|
|
32
|
+
</p>
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export default TableContainer;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import TableCell from './TableCell';
|
|
3
|
+
|
|
4
|
+
export const TableSizePicker = ({
|
|
5
|
+
rowCount,
|
|
6
|
+
columnCount,
|
|
7
|
+
activeRow,
|
|
8
|
+
activeColumn,
|
|
9
|
+
onCellClick,
|
|
10
|
+
onCellMouseEnter,
|
|
11
|
+
onCellMouseLeave,
|
|
12
|
+
}) => {
|
|
13
|
+
const handleClick = React.useCallback(
|
|
14
|
+
(...rest) => {
|
|
15
|
+
onCellClick(...rest);
|
|
16
|
+
},
|
|
17
|
+
[onCellClick],
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const handleMouseEnter = React.useCallback(
|
|
21
|
+
(...rest) => {
|
|
22
|
+
onCellMouseEnter(...rest);
|
|
23
|
+
},
|
|
24
|
+
[onCellMouseEnter],
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const handleMouseLeave = React.useCallback(
|
|
28
|
+
(...rest) => {
|
|
29
|
+
onCellMouseLeave(...rest);
|
|
30
|
+
},
|
|
31
|
+
[onCellMouseLeave],
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const createRow = React.useCallback(
|
|
35
|
+
(rowIndex) => {
|
|
36
|
+
const arr = [];
|
|
37
|
+
for (let i = 0; i < columnCount; ++i) {
|
|
38
|
+
const columnIndex = i + 1;
|
|
39
|
+
|
|
40
|
+
arr.push(
|
|
41
|
+
<TableCell
|
|
42
|
+
key={columnIndex}
|
|
43
|
+
row={rowIndex + 1}
|
|
44
|
+
column={columnIndex}
|
|
45
|
+
active={rowIndex + 1 <= activeRow && i + 1 <= activeColumn}
|
|
46
|
+
onClick={handleClick}
|
|
47
|
+
onMouseEnter={handleMouseEnter}
|
|
48
|
+
onMouseLeave={handleMouseLeave}
|
|
49
|
+
></TableCell>,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return <tr key={rowIndex}>{arr}</tr>;
|
|
53
|
+
},
|
|
54
|
+
[
|
|
55
|
+
activeColumn,
|
|
56
|
+
activeRow,
|
|
57
|
+
columnCount,
|
|
58
|
+
handleClick,
|
|
59
|
+
handleMouseEnter,
|
|
60
|
+
handleMouseLeave,
|
|
61
|
+
],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const rows = [];
|
|
65
|
+
for (let i = 0; i < rowCount; ++i) {
|
|
66
|
+
rows.push(createRow(i));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const zoomFactor = 1;
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<table
|
|
73
|
+
style={{
|
|
74
|
+
width: `${columnCount * zoomFactor}rem`,
|
|
75
|
+
height: `${rowCount * zoomFactor}rem`,
|
|
76
|
+
}}
|
|
77
|
+
>
|
|
78
|
+
<tbody>{rows}</tbody>
|
|
79
|
+
</table>
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export default TableSizePicker;
|