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

CMD + K to trigger link dialog #4347

Closed
zurfyx opened this issue Apr 18, 2023 · 5 comments
Closed

CMD + K to trigger link dialog #4347

zurfyx opened this issue Apr 18, 2023 · 5 comments
Labels
enhancement Improvement over existing feature good first issue Good for newcomers playground

Comments

@zurfyx
Copy link
Member

zurfyx commented Apr 18, 2023

Ctrl/CMD + K is a common convention to toggle link

@zurfyx zurfyx added enhancement Improvement over existing feature good first issue Good for newcomers playground labels Apr 18, 2023
@AnuragBalhra
Copy link
Contributor

Sounds like a good first issue, I can pick it up if no one else is working on it yet.

@Nantris
Copy link

Nantris commented Apr 20, 2023

@AnuragBalhra are you planning to work on this? Do you know how the Ctrl/Cmd+B are handled for Bold for an example of how to add this?

@AnuragBalhra
Copy link
Contributor

Yes I'm starting to work on this.
I don't already know how to do it but I'll try to investigate how it's done for Ctrl/Cmd+B. If someone already knows any documentation/references about how they are implemented, that would be really helpful.

@evskorobogatij
Copy link

evskorobogatij commented Jun 6, 2023

I do

import React, { useCallback, useEffect } from 'react'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { $isLinkNode, TOGGLE_LINK_COMMAND } from '@lexical/link'
import { useUnit } from 'effector-react'
import { changeLastKeyInfo } from 'components/Editor/models/keys'

export const HotKeyPlugins = () => {
  const [editor] = useLexicalComposerContext()

  const handleChangeLastKeyInfo = useUnit(changeLastKeyInfo)

  const insertLink = useCallback(() => {
    editor.dispatchCommand(TOGGLE_LINK_COMMAND, 'https://')
  }, [editor])

  const handleKeyPress = useCallback(
    (e: KeyboardEvent) => {
      const { code, ctrlKey, altKey, shiftKey } = e
      handleChangeLastKeyInfo({ code, ctrlKey, altKey, shiftKey })
      if (ctrlKey && code === 'KeyK') {
        insertLink()
        e.preventDefault()
      }
    },
    [handleChangeLastKeyInfo],
  )

  useEffect(() => {
    window.addEventListener('keydown', handleKeyPress)
    return () => {
      window.removeEventListener('keydown', handleKeyPress)
    }
  }, [handleKeyPress])

  return <></>
}

@acywatson
Copy link
Contributor

Closed by #4413

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement over existing feature good first issue Good for newcomers playground
Projects
None yet
Development

No branches or pull requests

5 participants