question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How can I change highlighted text into link tags with modifier keys?

See original GitHub issue

So I want to be able to add a link to a highlighted text, similar to the example on Link on the website, and that one is pretty straightforward. However I also want to add Cmd+K as a keyboard hotkey to bring up the same prompt menu.

I tried to extend the current Link and then adding my own keys, which made the hotkey work, however I couldn’t figure out how to actually turn the highlighted node into a link node (the example on the website uses the commands from Editor, which I don’t get access to inside keys). Any help here?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
edisonywhcommented, Apr 17, 2020

@philippkuehn that’s amazing! Couldn’t figure out how to use the selections to setNodeMarkup referred to the doc here but wasn’t sure how to actually use it, but using editor directly worked perfectly! It’s basically doing the exact same thing as what the menubar is doing.

Here’s my piece of code that worked if anyone else is struggling:

import { Link as BaseLink } from 'tiptap-extensions';

export default class Link extends BaseLink {
  keys() {
    return {
      'Mod-k': () => {
        const command = this.editor.commands.link
        this.handleAddLink(command)
      }
    }
  }

     handleAddLink(command) {
      const url = prompt('Enter the url you want to link to')
      if (url == "") {
        command({})
      } else {
        if (!this.isValidUrl(url)) {
          alert(`${url} doesn't seem to be a valid URL!`)
        } else {
          command({ "href": url })
        }
      }
    },

  isValidUrl(string) {
    try {
      new URL(string);
    } catch (_) {
      return false;
    }

    return true;
  }
}

Thanks for the help 😃

1reaction
BrianHungcommented, Apr 16, 2020

state, dispatch, view are all given as arguments to keys. Using state.selection, you should be able to the position of the highlighted node, and then use tr.setNodeMarkup to apply the link mark to the node.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SwiftUI: is there exist modifier to highlight substring of Text ...
It's noteworthy here that NSAttributedString is just "an string annotated by attributes over ranges." It's not "a styled string." We can make up ......
Read more >
CSS Basics: Styling Links Like a Boss
Here is the same link we have been looking at. First, try hovering your mouse on top of it without clicking and notice...
Read more >
Chapter 4: Reading text - Microsoft Support
Learn about how to read text using Narrator in Windows, including how to get info ... Tip: The Narrator key is a modifier...
Read more >
Video: Create accessible links in Word - Microsoft Support
Right-click to open the context menu, then find and select Edit Hyperlink. In the dialog box, look for a text box labeled Text...
Read more >
Find and Replace text in InDesign - Adobe Support
Click the Special Characters For Search icon to the right of the Find What option and choose options from the Locations, Repeat, Match, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found