naturalcrit / homebrewery

Create authentic looking D&D homebrews using only markdown
https://homebrewery.naturalcrit.com
MIT License
1.1k stars 326 forks source link

Insert Link snippet should linkify selected text #3201

Closed ericscheid closed 10 months ago

ericscheid commented 10 months ago

Your idea:

Currently, selecting the Insert Link to Page snippet inserts [Click here](#p3) to go to page 3. This will appear in front of the cursor or current selection.

It would be more useful if the editor detects that some text is selected, and linkifies that text. Thus, selecting "some text" would insert [some text](#p3), replacing the selection. (This is how inserting links in many editors work.)

Gazook89 commented 10 months ago

Try the ctrl k (or cmd k) hotkey for inserting a link. Is that what you expect the Link to Page snippet to do? If so, i assume it can be ported over quickly.

makeLink : function() {
        const isLink = /^\[(.*)\]\((.*)\)$/;
        const selection = this.codeMirror.getSelection().trim();
        let match;
        if(match = isLink.exec(selection)){
            const altText = match[1];
            const url     = match[2];
            this.codeMirror.replaceSelection(`${altText} ${url}`);
            const cursor = this.codeMirror.getCursor();
            this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - url.length }, { line: cursor.line, ch: cursor.ch });
        } else {
            this.codeMirror.replaceSelection(`[${selection || 'alt text'}](url)`);
            const cursor = this.codeMirror.getCursor();
            this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - 4 }, { line: cursor.line, ch: cursor.ch - 1 });
        }
    },
Gazook89 commented 10 months ago

Actually maybe it is harder. The snippets are just text things, and the little JS involved is just for generating text strings. I'm not sure that it is good to conflate "plain text insertions" with "buttons that manipulate your existing text", it would be unexpected.

I do think that it would be valuable to have the "normal" toolbar of functions that you get in most WYSIWYG editors, with "Underline", "Bold", "Italic" and "add link", etc. But I think that is separate from text insertions.

ericscheid commented 10 months ago

Ah .. I see we do have a hot-key for insert link (ctrl-K), and it does the desired. For the life of me I couldn't remember if there was one, and the nearest thing was the Insert Link to Page snippet.

OK, don't change the insert snippet behaviour.