Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
don't let block-style override slate classname
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed Sep 22, 2022
1 parent c9ed44e commit e0fec91
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 113 deletions.
100 changes: 45 additions & 55 deletions src/editor/plugins/StyleMenu/StyleMenu.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { ReactEditor, useSlate } from 'slate-react';
import { useSlate } from 'slate-react';
import { Dropdown } from 'semantic-ui-react';
import { useIntl, defineMessages } from 'react-intl';
import cx from 'classnames';
Expand Down Expand Up @@ -27,19 +27,31 @@ const StyleMenuButton = ({ icon, active, ...props }) => (
<ToolbarButton {...props} icon={icon} active={active} />
);

const StylingsButton = () => {
const MenuOpts = ({ editor, toSelect, option }) => {
const isActive = toSelect.includes(option);
return (
<Dropdown.Item
as="span"
active={isActive}
className={cx({ active: isActive })}
{...option}
onClick={(event, selItem) => {
event.stopPropagation();
toggleStyle(editor, {
cssClass: selItem.value,
isBlock: selItem.isBlock,
});
}}
/>
);
};

const StylingsButton = (props) => {
const editor = useSlate();
const intl = useIntl();

// Converting the settings to a format that is required by dropdowns.
const inlineOpts = [
{
text: intl.formatMessage(messages.inlineStyle),
disabled: false,
selected: false,
style: { opacity: 0.45 },
onClick: (event) => event.preventDefault(),
},
...config.settings.slate.styleMenu.inlineStyles.map((def) => {
return {
value: def.cssClass,
Expand All @@ -50,13 +62,6 @@ const StylingsButton = () => {
}),
];
const blockOpts = [
{
text: intl.formatMessage(messages.paragraphStyle),
disabled: false,
selected: false,
style: { opacity: 0.45 },
onClick: (event) => event.preventDefault(),
},
...config.settings.slate.styleMenu.blockStyles.map((def) => {
return {
value: def.cssClass,
Expand Down Expand Up @@ -84,10 +89,14 @@ const StylingsButton = () => {
}
}

const menuItemProps = {
toSelect,
editor,
};
const showMenu = inlineOpts.length || blockOpts.length;
return showMenu ? (
<Dropdown
className="style-menu"
id="style-menu"
multiple
value={toSelect}
disabled={config.settings.slate.styleMenu.disabled ?? false}
Expand All @@ -101,46 +110,27 @@ const StylingsButton = () => {
}
>
<Dropdown.Menu>
{inlineOpts.map((option, index) => {
const isActive = toSelect.includes(option);
return (
<Dropdown.Item
key={`inline-style-${index}`}
as="span"
active={isActive}
className={cx({ active: isActive })}
onClick={(event, selItem) => {
event.stopPropagation();
toggleStyle(editor, {
cssClass: selItem.value,
isBlock: selItem.isBlock,
});
ReactEditor.focus(editor);
}}
{...option}
{inlineOpts.length && (
<>
<Dropdown.Header
content={intl.formatMessage(messages.inlineStyle)}
/>
);
})}
{blockOpts.map((option, index) => {
const isActive = toSelect.includes(option);
return (
<Dropdown.Item
key={`block-style-${index}`}
as="span"
active={isActive}
className={cx({ active: isActive })}
onClick={(event, selItem) => {
event.stopPropagation();
toggleStyle(editor, {
cssClass: selItem.value,
isBlock: selItem.isBlock,
});
ReactEditor.focus(editor);
}}
{...option}
{inlineOpts.map((option) => (
<MenuOpts {...menuItemProps} option={option} />
))}
</>
)}

{blockOpts.length && (
<>
<Dropdown.Header
content={intl.formatMessage(messages.paragraphStyle)}
/>
);
})}
{blockOpts.map((option) => (
<MenuOpts {...menuItemProps} option={option} />
))}
</>
)}
</Dropdown.Menu>
</Dropdown>
) : (
Expand Down
31 changes: 5 additions & 26 deletions src/editor/plugins/StyleMenu/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from 'react';
import StyleMenu from './StyleMenu';
import './style.css';
import { Icon } from '@plone/volto/components';
import paintSVG from '@plone/volto/icons/paint.svg';
import './style.less';

export default function install(config) {
const { slate } = config.settings;
Expand All @@ -12,29 +10,10 @@ export default function install(config) {
slate.toolbarButtons.push('styleMenu');
slate.expandedToolbarButtons.push('styleMenu');

/* The slate Menu configuration in an addon */

slate.styleMenu = config.settings.slate.styleMenu || {};
slate.styleMenu.inlineStyles = [
{
cssClass: 'cool-inline-text',
label: 'Cool Inline Text',
icon: (props) => <Icon name={paintSVG} size="24px" />,
},
];
slate.styleMenu.blockStyles = [
{
cssClass: 'underline-block-text',
label: 'Cool Block Text',
icon: (props) => <Icon name={paintSVG} size="24px" />,
},
];

// slate.styleMenu = {
// inlineStyles: [],
// blockStyles: [],
// //themeColors = { primary: 'red' };
// };
slate.styleMenu = {
inlineStyles: [],
blockStyles: [],
};

return config;
}
30 changes: 0 additions & 30 deletions src/editor/plugins/StyleMenu/style.css

This file was deleted.

48 changes: 48 additions & 0 deletions src/editor/plugins/StyleMenu/style.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Demo styles */

.green-block-text {
color: green;
}

.underline-block-text {
text-decoration: underline;
}

.cool-inline-text {
font-style: italic;
font-weight: bold;
}

.red-inline-text {
color: red;
}

#style-menu.ui.dropdown .menu {
.item {
display: flex;
user-select: none;

span.text {
margin-top: auto;
line-height: normal;
vertical-align: middle;
}
}

.header {
color: rgb(153, 153, 153);
font-size: 1rem;
font-weight: normal;
}

.active {
z-index: 1;
background: #68778d;
box-shadow: none;
color: #ffffff;
font-weight: 300;
}

overflow: auto;
max-height: 50vh;
}
2 changes: 1 addition & 1 deletion src/editor/plugins/StyleMenu/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
import { Editor, Transforms } from 'slate';
import { isBlockActive } from 'volto-slate/utils';
import config from '@plone/volto/registry';
import { isBlockActive } from 'volto-slate/utils';

/**
* Toggles a style (e.g. in the StyleMenu plugin).
Expand Down
4 changes: 3 additions & 1 deletion src/editor/render.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ export const Element = ({ element, attributes = {}, extras, ...rest }) => {
const El = elements[element.type] || elements['default'];

const out = Object.assign(
element.styleName ? { className: element.styleName } : {},
...Object.keys(attributes || {}).map((k) =>
!isEmpty(attributes[k]) ? { [k]: attributes[k] } : {},
),
element.styleName
? { className: cx(attributes.className, element.styleName) }
: {},
);

return (
Expand Down

0 comments on commit e0fec91

Please sign in to comment.