| 1 | /*! |
| 2 | * VisualEditor UserInterface LinkAction class. |
| 3 | * |
| 4 | * @copyright See AUTHORS.txt |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Link action. |
| 9 | * This action transforms or inspects links (or potential links). |
| 10 | * |
| 11 | * @class |
| 12 | * @extends ve.ui.Action |
| 13 | * @constructor |
| 14 | * @param {ve.ui.Surface} surface Surface to act on |
| 15 | * @param {string} [source] |
| 16 | */ |
| 17 | ve.ui.LinkAction = function VeUiLinkAction() { |
| 18 | // Parent constructor |
| 19 | ve.ui.LinkAction.super.apply( this, arguments ); |
| 20 | }; |
| 21 | |
| 22 | /* Inheritance */ |
| 23 | |
| 24 | OO.inheritClass( ve.ui.LinkAction, ve.ui.Action ); |
| 25 | |
| 26 | /* Static Properties */ |
| 27 | |
| 28 | ve.ui.LinkAction.static.name = 'link'; |
| 29 | |
| 30 | /** |
| 31 | * @property {RegExp} RegExp matching an autolink + trailing space. |
| 32 | * |
| 33 | * @private |
| 34 | */ |
| 35 | ve.ui.LinkAction.static.autolinkRegExp = null; // Initialized below. |
| 36 | |
| 37 | ve.ui.LinkAction.static.methods = [ 'autolinkUrl' ]; |
| 38 | |
| 39 | /* Methods */ |
| 40 | |
| 41 | /** |
| 42 | * Autolink the selected URL (which may have trailing whitespace). |
| 43 | * |
| 44 | * @return {boolean} |
| 45 | * True if the selection is a valid URL and the autolink action was |
| 46 | * executed; otherwise false. |
| 47 | */ |
| 48 | ve.ui.LinkAction.prototype.autolinkUrl = function () { |
| 49 | // Make sure we still have a real URL after trail removal, and not |
| 50 | // a bare protocol (or no protocol at all, if we stripped the last |
| 51 | // colon from the protocol) |
| 52 | return this.autolink( ( linktext ) => ve.ui.LinkAction.static.autolinkRegExp.test( linktext ) ); |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * Autolink the selection, which may have trailing whitespace. |
| 57 | * |
| 58 | * @private |
| 59 | * @param {Function} validateFunc |
| 60 | * A function used to validate the given linktext. |
| 61 | * @param {string} validateFunc.linktext |
| 62 | * Linktext with trailing whitespace and punctuation stripped. |
| 63 | * @param {boolean} validateFunc.return |
| 64 | * True iff the given linktext is valid. If false, no linking will be done. |
| 65 | * @param {Function} [txFunc] |
| 66 | * An optional function to create a transaction to perform the autolink. |
| 67 | * If not provided, a transaction will be created which applies the |
| 68 | * annotations returned by {@link ve.ui.LinkAction#getLinkAnnotation}. |
| 69 | * @param {ve.dm.Document} txFunc.documentModel |
| 70 | * The document model to modify. |
| 71 | * @param {ve.Range} txFunc.range |
| 72 | * The range to autolink. |
| 73 | * @param {string} txFunc.linktext |
| 74 | * The text string to autolink. |
| 75 | * @param {ve.dm.Transaction} txFunc.return |
| 76 | * The transaction to perform the autolink operation. |
| 77 | * @return {boolean} Selection was valid and link action was executed. |
| 78 | */ |
| 79 | ve.ui.LinkAction.prototype.autolink = function ( validateFunc, txFunc ) { |
| 80 | const surfaceModel = this.surface.getModel(), |
| 81 | selection = surfaceModel.getSelection(); |
| 82 | |
| 83 | if ( !( selection instanceof ve.dm.LinearSelection ) ) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | function isLinkAnnotation( annotation ) { |
| 88 | return /^link/.test( annotation.name ); |
| 89 | } |
| 90 | |
| 91 | let range = selection.getRange(); |
| 92 | const rangeEnd = range.end; |
| 93 | |
| 94 | const documentModel = surfaceModel.getDocument(); |
| 95 | let linktext = documentModel.data.getText( true, range ); |
| 96 | |
| 97 | // Eliminate trailing whitespace. |
| 98 | linktext = linktext.replace( /\s+$/, '' ); |
| 99 | |
| 100 | // Eliminate trailing punctuation. |
| 101 | linktext = linktext.replace( this.getTrailingPunctuation( linktext ), '' ); |
| 102 | |
| 103 | // Validate the stripped text. |
| 104 | if ( !validateFunc( linktext ) ) { |
| 105 | // Don't autolink this. |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // Shrink range to match new linktext. |
| 110 | range = range.truncate( linktext.length ); |
| 111 | |
| 112 | // If there are word characters (but not punctuation) immediately past the range, don't autolink. |
| 113 | // The user did something silly like type a link in the middle of a word. |
| 114 | if ( |
| 115 | range.end + 1 < documentModel.data.getLength() && |
| 116 | /\w/.test( documentModel.data.getText( true, new ve.Range( range.end, range.end + 1 ) ) ) |
| 117 | ) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | // Check that none of the range has an existing link annotation. |
| 122 | // Otherwise we could autolink an internal link, which would be ungood. |
| 123 | for ( let i = range.start; i < range.end; i++ ) { |
| 124 | if ( documentModel.data.getAnnotationsFromOffset( i ).containsMatching( isLinkAnnotation ) ) { |
| 125 | // Don't autolink this. |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Make sure `undo` doesn't expose the selected linktext. |
| 131 | surfaceModel.setLinearSelection( new ve.Range( rangeEnd ) ); |
| 132 | |
| 133 | // Annotate the (previous) range. |
| 134 | if ( txFunc ) { |
| 135 | // TODO: Change this API so that 'txFunc' is given a surface fragment |
| 136 | // as an argument, and uses the fragment to directly edit the document. |
| 137 | surfaceModel.change( txFunc( documentModel, range, linktext ) ); |
| 138 | } else { |
| 139 | surfaceModel.getLinearFragment( range, true ).annotateContent( 'set', this.getLinkAnnotation( linktext ) ); |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | }; |
| 144 | |
| 145 | /** |
| 146 | * Return an appropriate "trailing punctuation" set, which will |
| 147 | * get stripped from possible autolinks. |
| 148 | * |
| 149 | * @param {string} candidate |
| 150 | * The candidate text. Some users may not wish to include closing |
| 151 | * brackets/braces/parentheses in the stripped character class if an |
| 152 | * opening bracket/brace/parenthesis in present in the candidate link |
| 153 | * text. |
| 154 | * @return {RegExp} |
| 155 | * A regular expression matching trailing punctuation which will be |
| 156 | * stripped from an autolink. |
| 157 | */ |
| 158 | ve.ui.LinkAction.prototype.getTrailingPunctuation = function () { |
| 159 | return /[,;.:!?)\]}"'”’»]+$/; |
| 160 | }; |
| 161 | |
| 162 | /** |
| 163 | * Return an appropriate annotation for the given link text. |
| 164 | * |
| 165 | * @param {string} linktext The link text to annotate. |
| 166 | * @return {ve.dm.LinkAnnotation} The annotation to use. |
| 167 | */ |
| 168 | ve.ui.LinkAction.prototype.getLinkAnnotation = function ( linktext ) { |
| 169 | return new ve.dm.LinkAnnotation( { |
| 170 | type: 'link', |
| 171 | attributes: { |
| 172 | href: linktext |
| 173 | } |
| 174 | } ); |
| 175 | }; |
| 176 | |
| 177 | /* Registration */ |
| 178 | |
| 179 | ve.ui.actionFactory.register( ve.ui.LinkAction ); |
| 180 | |
| 181 | // Delayed initialization (wait until ve.init.platform exists) |
| 182 | ve.init.Platform.static.initializedPromise.then( () => { |
| 183 | ve.ui.LinkAction.static.autolinkRegExp = |
| 184 | |
| 185 | new RegExp( |
| 186 | '\\b' + ve.init.platform.getUnanchoredExternalLinkUrlProtocolsRegExp().source + '\\S+$', |
| 187 | 'i' |
| 188 | ); |
| 189 | |
| 190 | ve.ui.sequenceRegistry.register( |
| 191 | new ve.ui.Sequence( |
| 192 | 'autolinkUrl', 'autolinkUrl', ve.ui.LinkAction.static.autolinkRegExp, 0, |
| 193 | { |
| 194 | setSelection: true, |
| 195 | delayed: true |
| 196 | } |
| 197 | ) |
| 198 | ); |
| 199 | } ); |