Text in bullet list concatenates to single bullet

Hello there,

I’ve recently created a button that changes the case of some selected text i.e. lowercase to uppercase and vice versa. When used on a bullet list of text nodes however, it concatenates the text e.g. a bullet list of 3 'hi’s would be turned into a single bullet point of ‘HI’. Does anyone know how to ensure that this concatenation does not occur?

Here is my code:

export function caseCommand() : ProsemirrorCommand {
    return (state, dispatch) => {
        const { from, to } = state.selection;
        const text = state.doc.textBetween(from, to);
        if (text.length === 0) {
            return false;
        } else if (text.toLowerCase() === text) {
            const textNode = state.schema.text(text.toUpperCase());
            dispatch(state.tr.replaceWith(from, to, textNode));
        } else {
            const textNode = state.schema.text(text.toLowerCase());
            dispatch(state.tr.replaceWith(from, to, textNode));
        }
        return true;
    }
}

You are replacing the entire selection which may (and in your described scenario does) intersect and contain multiple nodes. So in your example you are replacing a selection of list items with a single text node so the result you describe is correct. Instead you should find the text nodes within the selection and replace those. There’s a similar topic about changing the text case with an approach described here.

1 Like

Thank you very much @andrews - your suggestions have been very helpful