-
Hello ! I need to implement a feature which basically converts to uppercase the text within a selection. Ive tried several ways using Transforms but I'm not being able to achieve this. For example, I was just deleting the selection and inserting new text but this was causing problems when the selection covered more than one paragraph (basically the end result was merging paragraphs). I know Editor.nodes can be used to get the involved nodes but I don't know how to transform the selected portion within the node itself. Thanks a lot ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Ok, I think I got it. Sharing the code in case anyone finds this useful: const capitalizeSelection = (editor: Editor) => {
if (!editor.selection) {
return;
}
// save the original selection
const selection = { ...editor.selection };
// get the affected nodes
for (const [node, path] of Editor.nodes(editor, {
at: selection,
match: (n) => Text.isText(n)
})) {
// define a range containing the whole text
const nodeRange: Range = {
anchor: {
path: path,
offset: 0
},
focus: {
path: path,
offset: (node as Text).text.length
}
};
// obtain the part of the selection withing this node containing range
const intersection = Range.intersection(nodeRange, selection);
if (intersection) {
Transforms.select(editor, intersection);
const originalText = getSelectionText(editor);
Transforms.delete(editor, {
at: intersection
});
Transforms.insertText(editor, originalText.toUpperCase());
}
}
}; |
Beta Was this translation helpful? Give feedback.
Ok, I think I got it. Sharing the code in case anyone finds this useful: