1/* DOKUWIKI:include_once vendor/jquery.textcomplete.min.js */ 2function linksuggest_escape(text) { 3 return jQuery('<div/>').text(text).html(); 4} 5 6function charAfterCursor() { 7 let editor = jQuery('#wiki__text'); 8 let position = editor.prop('selectionStart'); 9 return editor.prop('value').substring(position, position+1); 10} 11 12function appendTitle(title) { 13 return (title && JSINFO["append_header"] === 1 && charAfterCursor() !== '|')? '|' + title : ''; 14} 15function appendSubtitle(title) { 16 return (title && charAfterCursor() !== '|')? '|' + title : ''; 17} 18function appendClosing() { 19 return (charAfterCursor() === ']' || charAfterCursor() === '|')? '' : ']]'; 20} 21 22jQuery(function () { 23 let $editor = jQuery('#wiki__text'); 24 25 $editor.textcomplete([ 26 { //page search 27 match: /\[{2}([\w\-.:~]*)$/, 28 search: function (term, callback) { 29 if ($editor.data('linksuggest_off') === 1) { 30 callback([]); 31 return; 32 } 33 jQuery.post( 34 DOKU_BASE + 'lib/exe/ajax.php', 35 { 36 call: 'plugin_linksuggest', 37 q: term, 38 ns: JSINFO['namespace'], 39 id: JSINFO['id'], 40 }, 41 function (data) { 42 data = JSON.parse(data); 43 callback(jQuery.map(data.data, function (item) { 44 let id = item.id; 45 46 if (item.type === 'd') { 47 id = id + ':'; 48 } 49 50 return { 51 id: id, 52 ns: item.ns, 53 title: item.title, 54 type: item.type, 55 rootns: item.rootns 56 }; 57 })); 58 } 59 ); 60 }, 61 template: function (item) { //dropdown list 62 let image; 63 let title = item.title ? ' (' + linksuggest_escape(item.title) + ')' : ''; 64 let alt = item.type === 'd' ? 'ns' : 'page'; 65 let value = item.id; 66 67 if (item.rootns) { //page is in root namespace 68 value = ':' + value; 69 } 70 if (item.type === 'd') { //namespace 71 image = 'ns.png'; 72 } else { //file 73 image = 'page.png'; 74 } 75 return '<img alt="' + alt + '" src="' + DOKU_BASE + 'lib/images/' + image + '"> ' + linksuggest_escape(value) + title; 76 }, 77 index: 1, 78 replace: function (item) { //returns what will be put to editor 79 let id = item.id; 80 if (item.ns) { //prefix with already entered ns 81 id = item.ns + id; 82 } 83 if (item.type === 'd') { //namespace 84 setTimeout(function () { 85 $editor.trigger('keyup'); 86 }, 200); 87 return '[[' + id; 88 } else { //file 89 $editor.data('linksuggest_off', 1); 90 91 setTimeout(function () { 92 $editor.data('linksuggest_off', 0); 93 }, 500); 94 return ['[[' + id, appendTitle(item.title) + appendClosing()]; 95 } 96 97 }, 98 cache: false 99 }, { //Page Section Search 100 match: /\[\[([\w\-.:~]+#[\w\-.:]*)$/, 101 index: 1, 102 search: function (term, callback) { 103 if ($editor.data('linksuggest_off') === 1) { 104 callback([]); 105 return; 106 } 107 jQuery.post( 108 DOKU_BASE + 'lib/exe/ajax.php', 109 { 110 call: 'plugin_linksuggest', 111 q: term, 112 ns: JSINFO['namespace'], 113 id: JSINFO['id'], 114 }, 115 function (data) { 116 data = JSON.parse(data); 117 callback(jQuery.map(data.data, function (item) { 118 return { 119 'link': data.link, 120 'toc': item 121 }; 122 })); 123 } 124 ); 125 }, 126 template: function (item) { //dropdown list 127 let toc = item.toc; 128 let title = toc.title ? ' (' + linksuggest_escape(toc.title) + ')' : ''; 129 130 return linksuggest_escape(toc.hid) + title; 131 }, 132 133 replace: function (item) { //returns what will be put to editor 134 let link = item.link; 135 let toc = item.toc; 136 137 $editor.data('linksuggest_off', 1); 138 setTimeout(function () { 139 $editor.data('linksuggest_off', 0); 140 }, 500); 141 142 return '[[' + link + '#' + toc.hid + appendSubtitle(toc.title) + appendClosing(); 143 }, 144 cache: false 145 }, { //media search 146 match: /\{{2}([\w\-.:~]*)$/, 147 search: function (term, callback) { 148 if ($editor.data('linksuggest_off') === 1) { 149 callback([]); 150 return; 151 152 } 153 jQuery.post( 154 DOKU_BASE + 'lib/exe/ajax.php', 155 { 156 call: 'plugin_imglinksuggest', 157 q: term, 158 ns: JSINFO['namespace'], 159 id: JSINFO['id'], 160 }, 161 function (data) { 162 data = JSON.parse(data); 163 callback(jQuery.map(data.data, function (item) { 164 let id = item.id; 165 166 if (item.type === 'd') { 167 id = id + ':'; 168 } 169 170 return { 171 id: id, 172 ns: item.ns, 173 type: item.type, 174 rootns: item.rootns 175 }; 176 })); 177 } 178 ); 179 }, 180 template: function (item) { //dropdown list 181 let image; 182 let value = item.id; 183 let alt = item.type === 'd' ? 'ns' : 'media'; 184 185 if (item.rootns) { //page is in root namespace 186 value = ':' + value; 187 } 188 if (item.type === 'd') { //namespace 189 image = 'ns.png'; 190 } else { //file 191 image = 'media_link_nolnk.png'; 192 } 193 return '<img alt="' + alt + '" src="' + DOKU_BASE + 'lib/images/' + image + '"> ' + linksuggest_escape(value); 194 }, 195 index: 1, 196 replace: function (item) { //returns what will be put to editor 197 let id = item.id; 198 if (item.ns) { //prefix with already entered ns 199 id = item.ns + id; 200 } 201 if (item.type === 'd') { //namespace 202 setTimeout(function () { 203 $editor.trigger('keyup'); 204 }, 200); 205 return '{{' + id; 206 } else { //file 207 $editor.data('linksuggest_off', 1); 208 209 setTimeout(function () { 210 $editor.data('linksuggest_off', 0); 211 }, 500); 212 return ['{{' + id, '}}']; 213 } 214 215 }, 216 cache: false 217 }],{ 218 appendTo: 'body', 219 maxCount: 50, 220 //header:'test', 221 //footer: 'schließen' 222 }); 223}); 224