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

Start unordered list via * .... #408

Closed
guettli opened this issue Apr 22, 2021 · 5 comments
Closed

Start unordered list via * .... #408

guettli opened this issue Apr 22, 2021 · 5 comments

Comments

@guettli
Copy link

guettli commented Apr 22, 2021

It would be nice if I could start an unordered list without using the menu.

Many applications support it, that you just need to type * and now your text is the first entry of an unordered list.

I tried it in fastmail, but up to now this does not work.

@the-djmaze
Copy link

What if you don't want to, can i disable your feature?

@ghost
Copy link

ghost commented Feb 22, 2023

What you are discribing is markdown behavior. Many editors adopt a markdown parser into their editing modes.

@luantrasel
Copy link

luantrasel commented Aug 17, 2023

I was able to workaround and make it work this way to handle ordered and unordered lists

const handleMarkdownList = useCallback(() => {
        const editor = editorRef.current
        const formatAsList = (listType) => {
            if (listType === 'ordered') {
                editor.makeOrderedList()
            } else if (listType === 'unordered') {
                editor.makeUnorderedList()
            }
        }

        editor.forEachBlock((block) => {
            const handleListToken = (token, listType) => {
                if (block.innerText?.startsWith(`${token} `)) {
                    block.innerText = block.innerText.replace(`${token} `, ' ')
                    formatAsList(listType)
                } else if (block.innerText?.startsWith(`${token}\u00a0`)) {
                    block.innerText = block.innerText.replace(`${token}\u00a0`, ' ')
                    formatAsList(listType)
                }
            }

            handleListToken('1.', 'ordered')
            handleListToken('-', 'unordered')
            handleListToken('*', 'unordered')
        }, false)
    }, [])

...

editor.addEventListener('input', handleMarkdownList)

@ghost
Copy link

ghost commented Aug 18, 2023

Not bad @luantrasel , but I'm slightly worried, about the performance of your approach. maybe you can move your tokenlisthandler up in the scope instead of reinitializing inside every block callback and then calling it 3 times. Perhaps using regex methods may work well for this too. Let me know if I'm missing something, because I may have oversimplified the problem.

const ul = /^[\*\-]{1}\s+/;
const ol = /^[0-9]+\s+/;
const text = block.innerText;

if (ul.test(text)) {
  block.innerText = text.replace(ul, '');
  editor.makeUnorderedList();
  return;
}

if (ol.test(text)) {
  block.innerText = text.replace(ol, '');
  editor.makeOrderedList();
  return;
}

@neilj
Copy link
Member

neilj commented Sep 19, 2023

I have added support for this in Squire 2.1.0.

@neilj neilj closed this as completed Sep 19, 2023
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

4 participants