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

Selection strategy for list commands #588

Open
jbailleul opened this issue Nov 8, 2023 · 3 comments
Open

Selection strategy for list commands #588

jbailleul opened this issue Nov 8, 2023 · 3 comments

Comments

@jbailleul
Copy link

jbailleul commented Nov 8, 2023

Hi!

I would like list commands (unordered list, ordered list and checked list commands) to be applied to the whole line instead of word.
For now, I've achieved that by defining custom commands and overriding the makeList() function:

import { checkedListCommand, orderedListCommand, unorderedListCommand} from '@uiw/react-md-editor';

const makeList = (state: ExecuteState, api: TextAreaTextApi, insertBefore: string | AlterLineFunction): void => {
    // const newSelectionRange = selectWord({ text: state.text, selection: state.selection, prefix: state.command.prefix! });
    const newSelectionRange = selectLine({
        text: state.text,
        selection: state.selection
    });
    
    // ... 
    // copy/paste all the remaining code from source
    // ...
};

export const customUnorderedListCommand: ICommand = {
    ...unorderedListCommand,
    execute: (state: ExecuteState, api: TextAreaTextApi) => {
        makeList(state, api, '- ');
    }
};

export const customOrderedListCommand: ICommand = {
    ...orderedListCommand,
    execute: (state: ExecuteState, api: TextAreaTextApi) => {
        makeList(state, api, (item, index) => `${index + 1}. `);
    }
};

export const customCheckedListCommand: ICommand = {
    ...checkedListCommand,
    execute: (state: ExecuteState, api: TextAreaTextApi) => {
        makeList(state, api, () => `- [ ] `);
    }
};

Would it be possible to have something like a selectionStrategy option on the commands, that could be set to word or line for example? Then the makeList() function would use selectWord() or selectLine() accordingly?

Thanks for you great work!

@jaywcjlove
Copy link
Member

@jbailleul I haven't found a good way to define it yet

@jbailleul
Copy link
Author

@jaywcjlove If adding such an option to the commands is what's bothering you (maybe for genericity/re-usability purpose between commands), what about adding an optional parameter to the makeList() function? I see it is only used by those three commands so could this kind of signature be ok?

const makeList = (
    state: ExecuteState,
    api: TextAreaTextApi,
    insertBefore: string | AlterLineFunction,
    selectionStrategy: 'word' | 'line' = 'word'
): void => {
    // Use `selectWord()` or `selectLine()` based on the `selectionStrategy` param
    ...
}

With this, I would only have to override the commands from my side, and not the makeList() function:

export const customUnorderedListCommand: ICommand = {
    ...unorderedListCommand,
    execute: (state: ExecuteState, api: TextAreaTextApi) => {
        makeList(state, api, '- ', 'line');
    }
};

export const customOrderedListCommand: ICommand = {
    ...orderedListCommand,
    execute: (state: ExecuteState, api: TextAreaTextApi) => {
        makeList(state, api, (item, index) => `${index + 1}. `, 'line');
    }
};

export const customCheckedListCommand: ICommand = {
    ...checkedListCommand,
    execute: (state: ExecuteState, api: TextAreaTextApi) => {
        makeList(state, api, () => `- [ ] `, 'line');
    }
};

@jaywcjlove
Copy link
Member

@jbailleul makeList() method can be provided, welcome to submit PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants