@@ -22,8 +22,11 @@ class LinkWizard {
22
22
selected = - 1 ;
23
23
/** @var {Object} selection A DokuWiki selection object holding text positions in the editor */
24
24
selection = null ;
25
- /** @var {Object} val Mechanism to modify the resulting links. See 935ecb0ef751ac1d658932316e06410e70c483e0 */
26
- val = null ;
25
+ /** @var {Object} val The syntax used. See 935ecb0ef751ac1d658932316e06410e70c483e0 */
26
+ val = {
27
+ open : '[[' ,
28
+ close : ']]'
29
+ } ;
27
30
28
31
/**
29
32
* Initialize the LinkWizard by creating the needed HTML
@@ -194,7 +197,7 @@ class LinkWizard {
194
197
this . autocomplete_exec ( ) ;
195
198
} else {
196
199
if ( jQuery ( a . nextSibling ) . is ( 'span' ) ) {
197
- this . insertLink ( a . nextSibling . innerHTML ) ;
200
+ this . insertLink ( a . nextSibling . innerText ) ;
198
201
} else {
199
202
this . insertLink ( '' ) ;
200
203
}
@@ -206,56 +209,39 @@ class LinkWizard {
206
209
* replacing the current selection or at the cursor position.
207
210
* When no selection is available the given title will be used
208
211
* as link title instead
212
+ *
213
+ * @param {string } title The heading text to use as link title if configured
209
214
*/
210
215
insertLink ( title ) {
211
- let link = this . $entry . val ( ) ,
212
- sel , stxt ;
216
+ let link = this . $entry . val ( ) ;
217
+ let selection ;
218
+ let linkTitle ;
213
219
if ( ! link ) {
214
220
return ;
215
221
}
216
222
217
- sel = DWgetSelection ( this . textArea ) ;
218
- if ( sel . start === 0 && sel . end === 0 ) {
219
- sel = this . selection ;
220
- }
221
-
222
- stxt = sel . getText ( ) ;
223
-
224
- // don't include trailing space in selection
225
- if ( stxt . charAt ( stxt . length - 1 ) === ' ' ) {
226
- sel . end -- ;
227
- stxt = sel . getText ( ) ;
223
+ // use the current selection, if not available use the one that was stored when the wizard was opened
224
+ selection = DWgetSelection ( this . textArea ) ;
225
+ if ( selection . start === 0 && selection . end === 0 ) {
226
+ selection = this . selection ;
228
227
}
229
228
230
- if ( ! stxt && ! DOKU_UHC ) {
231
- stxt = title ;
229
+ // if the selection has any text, use it as the link title
230
+ linkTitle = selection . getText ( ) ;
231
+ if ( linkTitle . charAt ( linkTitle . length - 1 ) === ' ' ) {
232
+ // don't include trailing space in selection
233
+ selection . end -- ;
234
+ linkTitle = selection . getText ( ) ;
232
235
}
233
236
234
- // prepend colon inside namespaces for non namespace pages
235
- if ( this . textArea . form . id . value . indexOf ( ':' ) !== - 1 &&
236
- link . indexOf ( ':' ) === - 1 ) {
237
- link = ':' + link ;
237
+ // if there is no selection, and useheading is enabled, use the heading text as the link title
238
+ if ( ! linkTitle && ! DOKU_UHC ) {
239
+ linkTitle = title ;
238
240
}
239
241
240
- let so = link . length ;
241
- let eo = 0 ;
242
- if ( this . val ) {
243
- if ( this . val . open ) {
244
- so += this . val . open . length ;
245
- link = this . val . open + link ;
246
- }
247
- link += '|' ;
248
- so += 1 ;
249
- if ( stxt ) {
250
- link += stxt ;
251
- }
252
- if ( this . val . close ) {
253
- link += this . val . close ;
254
- eo = this . val . close . length ;
255
- }
256
- }
257
-
258
- pasteText ( sel , link , { startofs : so , endofs : eo } ) ;
242
+ // paste the link
243
+ const syntax = this . createLinkSyntax ( link , linkTitle ) ;
244
+ pasteText ( selection , syntax . link , syntax ) ;
259
245
this . hide ( ) ;
260
246
261
247
// reset the entry to the parent namespace
@@ -273,6 +259,41 @@ class LinkWizard {
273
259
this . $entry . val ( entry_value ) ;
274
260
}
275
261
262
+ /**
263
+ * Constructs the full syntax and calculates offsets
264
+ *
265
+ * @param {string } id
266
+ * @param {string } title
267
+ * @returns {{link: string, startofs: number, endofs: number } }
268
+ */
269
+ createLinkSyntax ( id , title ) {
270
+ // construct a relative link, except for external links
271
+ let link = id ;
272
+ if ( ! id . match ( / ^ ( f | h t ) t p s ? : \/ \/ / i) ) {
273
+ const refId = this . textArea . form . id . value ;
274
+ link = LinkWizard . createRelativeID ( refId , id ) ;
275
+ }
276
+
277
+ let startofs = link . length ;
278
+ let endofs = 0 ;
279
+
280
+ if ( this . val . open ) {
281
+ startofs += this . val . open . length ;
282
+ link = this . val . open + link ;
283
+ }
284
+ link += '|' ;
285
+ startofs += 1 ;
286
+ if ( title ) {
287
+ link += title ;
288
+ }
289
+ if ( this . val . close ) {
290
+ link += this . val . close ;
291
+ endofs = this . val . close . length ;
292
+ }
293
+
294
+ return { link, startofs, endofs} ;
295
+ }
296
+
276
297
/**
277
298
* Start the page/namespace lookup timer
278
299
*
@@ -354,7 +375,7 @@ class LinkWizard {
354
375
const sourceNs = ref . split ( ':' ) ;
355
376
[ /*sourcePage*/ ] = sourceNs . pop ( ) ;
356
377
const targetNs = id . split ( ':' ) ;
357
- const targetPage = targetNs . pop ( )
378
+ const targetPage = targetNs . pop ( ) ;
358
379
const relativeID = [ ] ;
359
380
360
381
// Find the common prefix length
0 commit comments