Improve serach function with quick suggestions

This commit is contained in:
2026-05-18 15:10:34 +02:00
parent 8244874fe3
commit a25d5434ac
8 changed files with 545 additions and 61 deletions
+57 -29
View File
@@ -64,43 +64,71 @@
if (result) applyResult(result);
}
function promptDisplayText(initial, onDone) {
var input = document.createElement('input');
input.type = 'text';
input.className = 'modal-input';
input.placeholder = 'Display text (optional)';
if (initial) input.value = initial;
var handle = openModal({
title: 'Insert link — display text?',
body: input,
confirm: {
label: 'INSERT',
onConfirm: function () {
handle.close();
onDone(input.value.trim());
}
}
});
// isValidWikiTarget mirrors the Go validator in wikilinks.go — absolute
// path, no empty/dot segments. Used to gate the INSERT confirm button.
function isValidWikiTarget(p) {
if (!p || p[0] !== '/') return false;
var trimmed = p.replace(/^\/+|\/+$/g, '');
if (trimmed === '') return true;
var segs = trimmed.split('/');
for (var i = 0; i < segs.length; i++) {
if (segs[i] === '' || segs[i] === '.' || segs[i] === '..') return false;
}
return true;
}
function insertWikilink() {
var sel = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
openTreePicker({
var container = document.createElement('div');
var targetWrap = document.createElement('div');
var targetInput = document.createElement('input');
targetInput.type = 'text';
targetInput.className = 'modal-input';
targetInput.placeholder = 'Page path or search…';
targetWrap.appendChild(targetInput);
var displayInput = document.createElement('input');
displayInput.type = 'text';
displayInput.className = 'modal-input';
displayInput.placeholder = 'Display text (optional)';
if (sel) displayInput.value = sel;
container.appendChild(targetWrap);
container.appendChild(displayInput);
var handle = openModal({
title: 'Insert link',
mode: 'any',
initialPath: '/',
confirmLabel: 'NEXT',
onSelect: function (path, kind) {
if (kind === 'folder') {
promptDisplayText(sel, function (display) {
insertAtCursor(display ? '[[' + path + '::' + display + ']]' : '[[' + path + ']]');
});
} else {
var name = path.split('/').pop();
insertAtCursor('[' + (sel || name) + '](' + path + ')');
body: container,
confirm: {
label: 'INSERT',
initiallyDisabled: true,
onConfirm: function () {
var target = targetInput.value.trim();
if (!isValidWikiTarget(target)) return;
var display = displayInput.value.trim();
handle.close();
insertAtCursor(display ? '[[' + target + '::' + display + ']]' : '[[' + target + ']]');
}
}
});
function updateConfirm() {
handle.setConfirmDisabled(!isValidWikiTarget(targetInput.value.trim()));
}
targetInput.addEventListener('input', updateConfirm);
window.attachSuggestions(targetInput, {
showFooter: false,
container: targetWrap,
onPick: function (r) {
targetInput.value = '/' + r.path;
updateConfirm();
displayInput.focus();
displayInput.select();
}
});
}
// --- Actions ---