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

fix(slate): fix insert/remove element edgecase bug in slate #5926

Merged
merged 6 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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/5926.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix insert/remove element edgecase bug in slate. @razvanMiu
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What edge case does this fix? Please describe the edge case, and how does what you did fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make a video demo cuze it's hard to explain

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevepiercy I updated the description and the changelog.

29 changes: 24 additions & 5 deletions packages/volto-slate/src/elementEditor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ export const _insertElement = (elementType) => (editor, data) => {
match: (node) => {
return Node.string(node).length !== 0;
},
}, //,
},
);
}

const sel = JSON.parse(JSON.stringify(rangeRef.current));
Transforms.select(editor, sel);
editor.setSavedSelection(sel);

setTimeout(() => {
Transforms.select(editor, sel);
editor.setSavedSelection(sel);
});

return true;
}
Expand All @@ -69,10 +72,26 @@ export const _insertElement = (elementType) => (editor, data) => {
* @returns {Object|null} - current node
*/
export const _unwrapElement = (elementType) => (editor) => {
const [link] = Editor.nodes(editor, {
at: editor.selection,
const selection = editor.selection || editor.getSavedSelection();
let [link] = Editor.nodes(editor, {
at: selection,
match: (node) => node?.type === elementType,
});
const isAtStart =
selection.anchor.offset === 0 && selection.focus.offset === 0;

if (!link && !isAtStart) return false;

if (!link) {
try {
link = Editor.previous(editor, {
at: selection.anchor.path,
});
} catch (ex) {
link = [];
}
}

const [, path] = link;
const [start, end] = Editor.edges(editor, path);
const range = { anchor: start, focus: end };
Expand Down
Loading