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

Issue #6805 fixed (with additional bug fixed) #6814

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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/6805.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
These changes will ensure that slate text blocks do not behave weirdly when using the 'backspace' or 'delete' keys. @ManojaD2004
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const DefaultTextBlockEditor = (props) => {
formDescription,
navRoot,
contentType,
content,
} = props;

const { slate } = config.settings;
Expand Down Expand Up @@ -242,6 +243,7 @@ export const DefaultTextBlockEditor = (props) => {
placeholder={placeholder}
slateSettings={slateSettings}
editableProps={{ 'aria-multiline': 'false' }}
content={content}
/>
{DEBUG ? <div>{block}</div> : ''}
</>
Expand Down
52 changes: 52 additions & 0 deletions packages/volto-slate/src/editor/SlateEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,51 @@ const handleHotKeys = (editor, event, config) => {
return wasHotkey;
};

const handleWeirdBehaviour = (editor, event, content, index) => {
if (!content) {
return false;
}
// console.log("handleWeirdBehaviour", content, index);
const blocksLayout = content['blocks_layout'].items;
const blocks = content['blocks'];
// backspace key
if (
blocks[blocksLayout[index]]['@type'] !== 'slate' ||
!blocks[blocksLayout[index]].plaintext
) {
return false;
}
const textLen = blocks[blocksLayout[index]].plaintext.length;
if (
isHotkey('backspace', event) &&
editor.selection.anchor.offset === 0 &&
textLen !== 0
) {
// console.log('Backspace key pressed at the start of non-empty text!');
return true;
}
// delete key
if (
index + 1 === blocksLayout.length ||
blocks[blocksLayout[index + 1]]['@type'] !== 'slate' ||
!blocks[blocksLayout[index + 1]].plaintext
) {
return false;
}
const nextTextLen = blocks[blocksLayout[index + 1]].plaintext.length;
// console.log("handleWeirdBehaviour", blocksLayout[index], blocks[blocksLayout[index]]);
// console.log("handleWeirdBehaviour", textLen, nextTextLen);
if (
isHotkey('delete', event) &&
editor.selection.anchor.offset === textLen &&
nextTextLen !== 0
) {
// console.log('Delete key pressed at the end of text, where next text is non-empty!');
return true;
}
return false;
};

// TODO: implement onFocus
class SlateEditor extends Component {
constructor(props) {
Expand Down Expand Up @@ -338,6 +383,13 @@ class SlateEditor extends Component {
onKeyDown={(event) => {
const handled = handleHotKeys(editor, event, slateSettings);
if (handled) return;
const handledWeird = handleWeirdBehaviour(
editor,
event,
this.props.content,
this.props.index,
);
if (handledWeird) return;
onKeyDown && onKeyDown({ editor, event });
}}
{...editableProps}
Expand Down