This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added poweruser menu. * Fix poweruser menu. * [JENKINS] - Fix stylelint * Add poweruser menu review (#238) * New features on table block (#235) * New features on table block * Add 2 new boolean options to table configuration: 'Show headers' and 'Make the table sortable' * Add text align option to table configuration * Clean up * Update tests * Add 'Vertical align' option * Update tests * [JENKINS] - Fix tests to work with Volto 15 * Bump version to 5.4.0 * set showHeaders as true by default * Replace showHeaders with hideHeaders * use align widget * Update tests * fix(table style): change display to inline-block for headers content * Add comment to public.less Co-authored-by: Alin Voinea <contact@avoinea.com> * Automated release 5.4.0 * Add Sonarqube tag using clms-frontend addons list * chore(cypress): Fix paste html * Automated release 5.4.1 * Allow passing custom slate settings * WIP * WIP * Seems to be working * Properly select first option in menu * Add some instructions on slash menu * Try to cancel Esc * Add node about esc canceling * Add one more note * Return from component Co-authored-by: Alin Voinea <contact@avoinea.com> Co-authored-by: Miu Razvan <miu.razvan28@gmail.com> Co-authored-by: EEA Jenkins <@users.noreply.github.com> Co-authored-by: EEA Jenkins <eea-jenkins@users.noreply.github.com> Co-authored-by: Alin Voinea <contact@avoinea.com> Co-authored-by: Tiberiu Ichim <tiberiuichim@users.noreply.github.com> Co-authored-by: Miu Razvan <miu.razvan28@gmail.com> Co-authored-by: EEA Jenkins <eea-jenkins@users.noreply.github.com> Author: Rob Gietema
- Loading branch information
1 parent
cce494f
commit b93deab
Showing
9 changed files
with
274 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { filter, isEmpty } from 'lodash'; | ||
import { Menu } from 'semantic-ui-react'; | ||
import { useIntl, FormattedMessage } from 'react-intl'; | ||
import { Icon } from '@plone/volto/components'; | ||
|
||
const emptySlateBlock = () => ({ | ||
value: [ | ||
{ | ||
children: [ | ||
{ | ||
text: '', | ||
}, | ||
], | ||
type: 'p', | ||
}, | ||
], | ||
plaintext: '', | ||
}); | ||
|
||
const useIsMounted = () => { | ||
const ref = React.useRef(); | ||
React.useEffect(() => { | ||
ref.current = true; | ||
return () => (ref.current = false); | ||
}, []); | ||
return ref.current; | ||
}; | ||
|
||
const SlashMenu = ({ | ||
currentBlock, | ||
onMutateBlock, | ||
selected, | ||
availableBlocks, | ||
}) => { | ||
const intl = useIntl(); | ||
|
||
return ( | ||
<div className="power-user-menu"> | ||
<Menu vertical fluid borderless> | ||
{availableBlocks.map((block, index) => ( | ||
<Menu.Item | ||
key={block.id} | ||
className={block.id} | ||
active={index === selected} | ||
onClick={(e) => { | ||
// onInsertBlock(currentBlock, { '@type': block.id }); | ||
onMutateBlock(currentBlock, { '@type': block.id }); | ||
e.stopPropagation(); | ||
}} | ||
> | ||
<Icon name={block.icon} size="24px" /> | ||
{intl.formatMessage({ | ||
id: block.title, | ||
defaultMessage: block.title, | ||
})} | ||
</Menu.Item> | ||
))} | ||
{availableBlocks.length === 0 && ( | ||
<Menu.Item> | ||
<FormattedMessage | ||
id="No matching blocks" | ||
defaultMessage="No matching blocks" | ||
/> | ||
</Menu.Item> | ||
)} | ||
</Menu> | ||
</div> | ||
); | ||
}; | ||
|
||
SlashMenu.propTypes = { | ||
currentBlock: PropTypes.string.isRequired, | ||
onInsertBlock: PropTypes.func, | ||
selected: PropTypes.number, | ||
blocksConfig: PropTypes.arrayOf(PropTypes.any), | ||
}; | ||
|
||
/** | ||
* A SlashMenu wrapper implemented as a volto-slate PersistentHelper. | ||
*/ | ||
const PersistentSlashMenu = ({ editor }) => { | ||
const props = editor.getBlockProps(); | ||
const { | ||
block, | ||
blocksConfig, | ||
data, | ||
onMutateBlock, | ||
properties, | ||
selected, | ||
allowedBlocks, | ||
detached, | ||
} = props; | ||
const disableNewBlocks = data?.disableNewBlocks || detached; | ||
|
||
const [slashMenuSelected, setSlashMenuSelected] = React.useState(0); | ||
|
||
const useAllowedBlocks = !isEmpty(allowedBlocks); | ||
const slashCommand = data.plaintext?.trim().match(/^\/([a-z]*)$/); | ||
|
||
const availableBlocks = React.useMemo( | ||
() => | ||
filter(blocksConfig, (item) => | ||
useAllowedBlocks | ||
? allowedBlocks.includes(item.id) | ||
: typeof item.restricted === 'function' | ||
? !item.restricted({ properties, block: item }) | ||
: !item.restricted, | ||
) | ||
.filter( | ||
// TODO: make it work with intl? | ||
(block) => slashCommand && block.id.indexOf(slashCommand[1]) === 0, | ||
) | ||
.sort((a, b) => (a.title < b.title ? -1 : 1)), | ||
[allowedBlocks, blocksConfig, properties, slashCommand, useAllowedBlocks], | ||
); | ||
|
||
const slashMenuSize = availableBlocks.length; | ||
const show = selected && slashCommand && !disableNewBlocks; | ||
|
||
const isMounted = useIsMounted(); | ||
|
||
React.useEffect(() => { | ||
if (isMounted && show && slashMenuSelected > slashMenuSize - 1) { | ||
setSlashMenuSelected(slashMenuSize - 1); | ||
} | ||
}, [show, slashMenuSelected, isMounted, slashMenuSize]); | ||
|
||
editor.showSlashMenu = show; | ||
|
||
editor.slashEnter = () => | ||
slashMenuSize > 0 && | ||
onMutateBlock( | ||
block, | ||
{ | ||
'@type': availableBlocks[slashMenuSelected].id, | ||
}, | ||
emptySlateBlock(), | ||
); | ||
|
||
editor.slashArrowUp = () => | ||
setSlashMenuSelected( | ||
slashMenuSelected === 0 ? slashMenuSize - 1 : slashMenuSelected - 1, | ||
); | ||
|
||
editor.slashArrowDown = () => | ||
setSlashMenuSelected( | ||
slashMenuSelected >= slashMenuSize - 1 ? 0 : slashMenuSelected + 1, | ||
); | ||
|
||
return show ? ( | ||
<SlashMenu | ||
currentBlock={block} | ||
onMutateBlock={onMutateBlock} | ||
availableBlocks={availableBlocks} | ||
selected={slashMenuSelected} | ||
/> | ||
) : ( | ||
'' | ||
); | ||
}; | ||
|
||
export default PersistentSlashMenu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const cancelEsc = ({ editor, event }) => { | ||
// TODO: this doesn't work, escape canceling doesn't work. | ||
event.stopPropagation(); | ||
event.nativeEvent.stopImmediatePropagation(); | ||
event.preventDefault(); | ||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const slashMenu = ({ editor, event }) => { | ||
if (!editor.showSlashMenu) return; | ||
|
||
const { slashArrowUp, slashArrowDown, slashEnter } = editor; | ||
|
||
const handlers = { | ||
ArrowUp: slashArrowUp, | ||
ArrowDown: slashArrowDown, | ||
Enter: slashEnter, | ||
}; | ||
|
||
const handler = handlers[event.key]; | ||
if (handler) handler(); | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.