RichText/Archived/Rooster/Rooster.gcomp/Support/node_modules/roosterjs/dist/rooster-amd.js
/**
 * Insert a hyperlink at cursor.
 * When there is a selection, hyperlink will be applied to the selection,
 * otherwise a hyperlink will be inserted to the cursor position.
 * @param editor Editor object
 * @param link Link address, can be http(s), mailto, notes, file, unc, ftp, news, telnet, gopher, wais.
 * When protocol is not specified, a best matched protocol will be predicted.
 * @param altText Optional alt text of the link, will be shown when hover on the link
 * @param displayText Optional display text for the link.
 * If specified, the display text of link will be replaced with this text.
 * If not specified and there wasn't a link, the link url will be used as display text.
 */
Insert a hyperlink at cursor.
When there is a selection, hyperlink will be applied to the selection,
otherwise a hyperlink will be inserted to the cursor position.
@param editor Editor object
@param link Link address, can be http(s), mailto, notes, file, unc, ftp, news, telnet, gopher, wais.
When protocol is not specified, a best matched protocol will be predicted.
@param altText Optional alt text of the link, will be shown when hover on the link
@param displayText Optional display text for the link.
If specified, the display text of link will be replaced with this text.
If not specified and there wasn't a link, the link url will be used as display text.
Insert a hyperlink at cursor . When there is a selection hyperlink will be applied to the selection otherwise a hyperlink will be inserted to the cursor position . @param editor Editor object @param link Link address can be http ( s ) mailto notes file unc ftp news telnet gopher wais . When protocol is not specified a best matched protocol will be predicted . @param altText Optional alt text of the link will be shown when hover on the link @param displayText Optional display text for the link . If specified the display text of link will be replaced with this text . If not specified and there wasn ' t a link the link url will be used as display text .
function createLink(editor, link, altText, displayText) {
    editor.focus();
    var url = link ? link.trim() : '';
    if (url) {
        var linkData = roosterjs_editor_dom_1.matchLink(url);
        var normalizedUrl_1 = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);
        var originalUrl_1 = linkData ? linkData.originalUrl : url;
        editor.addUndoSnapshot(function () {
            var range = editor.getSelectionRange();
            var anchor = null;
            if (range && range.collapsed) {
                anchor = getAnchorNodeAtCursor(editor);
                if (anchor) {
                    anchor.href = normalizedUrl_1;
                    updateAnchorDisplayText(anchor, displayText);
                }
                else {
                    anchor = editor.getDocument().createElement('A');
                    anchor.textContent = displayText || originalUrl_1;
                    anchor.href = normalizedUrl_1;
                    editor.insertNode(anchor);
                }
            }
            else {
                editor.getDocument().execCommand("createLink" , false, normalizedUrl_1);
                anchor = getAnchorNodeAtCursor(editor);
                updateAnchorDisplayText(anchor, displayText);
            }
            if (altText && anchor) {
                anchor.removeAttribute(TEMP_TITLE);
                anchor.title = altText;
            }
            return anchor;
        }, "CreateLink" );
    }
}
function createLink ( editor , link , altText , displayText ) { editor . focus ( ) ; var url = link ? link . trim ( ) : '' ; if ( url ) { var linkData = roosterjs_editor_dom_1 . matchLink ( url ) ; var normalizedUrl_1 = linkData ? linkData . normalizedUrl : applyLinkPrefix ( url ) ; var originalUrl_1 = linkData ? linkData . originalUrl : url ; editor . addUndoSnapshot ( function ( ) { var range = editor . getSelectionRange ( ) ; var anchor = null ; if ( range && range . collapsed ) { anchor = getAnchorNodeAtCursor ( editor ) ; if ( anchor ) { anchor . href = normalizedUrl_1 ; updateAnchorDisplayText ( anchor , displayText ) ; } else { anchor = editor . getDocument ( ) . createElement ( 'A' ) ; anchor . textContent = displayText || originalUrl_1 ; anchor . href = normalizedUrl_1 ; editor . insertNode ( anchor ) ; } } else { editor . getDocument ( ) . execCommand ( "createLink" , false , normalizedUrl_1 ) ; anchor = getAnchorNodeAtCursor ( editor ) ; updateAnchorDisplayText ( anchor , displayText ) ; } if ( altText && anchor ) { anchor . removeAttribute ( TEMP_TITLE ) ; anchor . title = altText ; } return anchor ; } , "CreateLink" ) ; } }
Insert a hyperlink at cursor.
Insert a hyperlink at cursor .
// matchLink can match most links, but not all, i.e. if you pass link a link as "abc", it won't match // we know in that case, users will want to insert a link like http://abc // so we have separate logic in applyLinkPrefix to add link prefix depending on the format of the link // i.e. if the link starts with something like abc@xxx, we will add mailto: prefix // if the link starts with ftp.xxx, we will add ftp:// link. For more, see applyLinkPrefix // If there is already a link, just change its href // Change text content if it is specified // the selection is not collapsed, use browser execCommand /* CreateLink */ // Hack: Ideally this should be done by HyperLink plugin. // We make a hack here since we don't have an event to notify HyperLink plugin // before we apply the link. /* CreateLink */