Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle breakList in detached TextBlockEditor #6106

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/volto-slate/news/6106.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle breakList in DetachedTextBlockEditor. @giuliaghisini
13 changes: 12 additions & 1 deletion packages/volto-slate/src/blocks/Text/DetachedTextBlockEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useInView } from 'react-intersection-observer';
import { SlateEditor } from '@plone/volto-slate/editor';
import { serializeNodesToText } from '@plone/volto-slate/editor/render';
import { handleKeyDetached } from './keyboard';
import config from '@plone/volto/registry';

const DEBUG = false;

Expand All @@ -29,6 +30,7 @@ export const DetachedTextBlockEditor = (props) => {
const { value } = data;

const intl = useIntl();
const { textblockExtensions } = config.settings.slate;
const placeholder =
data.placeholder || formTitle || intl.formatMessage(messages.text);
let instructions = data?.instructions?.data || data?.instructions;
Expand All @@ -41,13 +43,22 @@ export const DetachedTextBlockEditor = (props) => {
rootMargin: '0px 0px 200px 0px',
});

const withBlockProperties = React.useCallback(
(editor) => {
editor.getBlockProps = () => props;
return editor;
},
[props],
);

return (
<div className="text-slate-editor-inner detached-slate-editor" ref={ref}>
<SlateEditor
index={index}
readOnly={!inView}
properties={properties}
renderExtensions={[]}
extensions={textblockExtensions}
renderExtensions={[withBlockProperties]}
value={value}
block={block /* is this needed? */}
debug={DEBUG}
Expand Down
33 changes: 24 additions & 9 deletions packages/volto-slate/src/blocks/Text/extensions/breakList.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const breakList = (editor) => {

const { slate } = config.settings;
const { anchor } = editor.selection;
const blockProps = editor.getBlockProps();
const detached = blockProps.detached;

const ref = Editor.rangeRef(editor, editor.selection, {
affinity: 'inward',
Expand Down Expand Up @@ -65,8 +67,7 @@ export const breakList = (editor) => {
return; // applies default behaviour, as defined in insertBreak.js extension
}

if (parent) {
const blockProps = editor.getBlockProps();
if (parent && !detached) {
const { data } = blockProps;
// Don't add new block if not allowed
if (data?.disableNewBlocks) {
Expand All @@ -77,22 +78,36 @@ export const breakList = (editor) => {
Editor.deleteBackward(editor, { unit: 'line' });
// also account for empty nodes [{text: ''}]
if (Editor.isEmpty(editor, parent)) {
createAndSelectNewBlockAfter(editor, [createEmptyParagraph()]);
Transforms.removeNodes(editor, { at: ref.current });
if (detached) {
Transforms.removeNodes(editor, { at: ref.current });

Transforms.insertNodes(editor, createEmptyParagraph(), {
at: [editor.children.length],
});
Transforms.select(editor, Editor.end(editor, []));
} else {
createAndSelectNewBlockAfter(editor, [createEmptyParagraph()]);
Transforms.removeNodes(editor, { at: ref.current });
}
return true;
}

Transforms.removeNodes(editor, { at: ref.current });

if (isCursorAtBlockEnd(editor)) {
createAndSelectNewBlockAfter(editor, [createEmptyParagraph()]);
if (detached) {
Editor.insertNode(editor, createEmptyParagraph());
} else {
createAndSelectNewBlockAfter(editor, [createEmptyParagraph()]);
}
return true;
}

const [top, bottom] = splitEditorInTwoFragments(editor, ref.current);
setEditorContent(editor, top);
createAndSelectNewBlockAfter(editor, bottom);

if (!detached) {
const [top, bottom] = splitEditorInTwoFragments(editor, ref.current);
setEditorContent(editor, top);
createAndSelectNewBlockAfter(editor, bottom);
}
return true;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const withSplitBlocksOnBreak = (editor) => {
const { data } = blockProps;

// Don't add new block if not allowed
if (data?.disableNewBlocks) {
if (data?.disableNewBlocks || blockProps.detached) {
return insertBreak();
}

Expand Down
1 change: 1 addition & 0 deletions packages/volto/src/components/theme/Logo/Logo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
const navroot = useSelector((state) => state.navroot.data);
const dispatch = useDispatch();
const intl = useIntl();
return <></>;
giuliaghisini marked this conversation as resolved.
Show resolved Hide resolved

const messages = defineMessages({

Check warning on line 32 in packages/volto/src/components/theme/Logo/Logo.jsx

View workflow job for this annotation

GitHub Actions / ESlint

Unreachable code
home: {
id: 'Home',
defaultMessage: 'Home',
Expand All @@ -45,7 +46,7 @@
}
}, [dispatch, pathname]);

const navRootPath = flattenToAppURL(navroot?.navroot?.['@id']) || '/';

Check warning on line 49 in packages/volto/src/components/theme/Logo/Logo.jsx

View workflow job for this annotation

GitHub Actions / ESlint

Unreachable code

return (
<Link to={navRootPath} aria-label={intl.formatMessage(messages.home)}>
Expand Down
Loading