Improve link search

- Fix the query string
- Make menu bigger
- Create new page with label
This commit is contained in:
Peter Stuifzand 2020-11-14 08:15:54 +01:00
parent 75209250c3
commit bedba1e27f
5 changed files with 38 additions and 45 deletions

View File

@ -123,22 +123,24 @@ function showSearchResultsExtended(element, template, searchTool, query, input,
$lc.offset(pos)
}
let templateText = he.decode(document.getElementById(template).innerHTML);
let rendered = Mustache.render(templateText, {
page: value.trim().replace(/\s+/g, '_'),
results: results
}, {}, ['[[', ']]']);
let selectedPos = 0;
let selected = $lc.find('li.selected');
if (selected) {
let selectedPos = $lc.find('li').index(selected[0])
rendered = $(rendered)
const $lis = $lc.find('li')
if ($lis.length >= 1) {
selectedPos = Math.min(selectedPos, $lis.length - 1)
rendered.find('li')[selectedPos].classList.add('selected')
selectedPos = $lc.find('li').index(selected[0])
}
}
$lc.html(rendered).fadeIn()
let $ul = el('ul',
_.map(results, (hit, i) => {
console.log(hit)
const li = el('li', [document.createTextNode(hit.item.label || hit.item.title)])
if (selectedPos === i) li.classList.add('selected')
li.dataset['new_page'] = hit.item.title
return li
})
)
$lc.html($ul).fadeIn()
return results
})
}
@ -425,7 +427,7 @@ function Editor(holder, input) {
if (resultType === 'link') {
let start = value.lastIndexOf("[[", end)
end += 2
let startAndLink = value.substring(0, start) + "[[" + linkName + "]]"
let startAndLink = value.substring(0, start) + "[[" + _.trim(element[0].dataset['new_page']) + "]]"
input.value = startAndLink + value.substring(end)
input.selectionStart = startAndLink.length
input.selectionEnd = startAndLink.length
@ -585,7 +587,7 @@ function Editor(holder, input) {
})
return true
} else if (insideLink) {
let query = value.substring(start + 2, end);
let query = value.substring(start, end);
showSearchResults(titleSearch, query, input, value, 'link');
return true
} else {
@ -607,8 +609,7 @@ function Editor(holder, input) {
Promise.any([
match(query, /{{query(!?):\s*([^}]+)}}/)
])
.then(res => {
]).then(res => {
if (res[1] === '!') {
return search.startQuery(res[2])
.then(hits => _.uniqBy(_.flatMap(hits, formatTitleResult), _.property('text')))
@ -620,8 +621,7 @@ function Editor(holder, input) {
.then(results => editor.replaceChildren(id, results))
.finally(() => editor.render())
}
})
.catch(() => console.log('match error'))
}).catch(() => console.log('match error'))
});
return editor
})

View File

@ -28,7 +28,10 @@ function createTitleSearch() {
documents,
titleSearch: query => {
return new Promise((resolve, reject) => {
resolve(titleFuse.search(query))
let search = titleFuse.search(query);
search.unshift({item: {title: query, label: "Create page '" + query + "'"}})
search = search.slice(0, 25)
resolve(search)
})
},
commandSearch: query => {

View File

@ -2,13 +2,12 @@ import $ from 'jquery'
import 'jquery-contextmenu'
import copy from 'copy-text-to-clipboard'
function renderTree(tree)
{
function renderTree(tree) {
if (!tree) return []
let recRenderTree = (tree, indent) => {
return _.flatMapDeep(tree, (item) => [
_.repeat(" ", item.indented-indent) + item.text,
_.repeat(" ", item.indented - indent) + item.text,
recRenderTree(item.children, indent),
])
}

View File

@ -296,7 +296,7 @@ mark {
}
#link-complete {
z-index: 1;
width: 217px;
width: 280px;
overflow-x: hidden;
overflow-y: auto;
height: 300px;
@ -308,6 +308,7 @@ mark {
#link-complete li {
padding: 4px 16px;
white-space: nowrap;
}
#link-complete li.selected {
background: lightblue;

View File

@ -83,16 +83,6 @@
<li><a href="/edit/[[page]]">Create a page</a></li>
</ul>
</div>
<div id="link-template" class="hide">
<ul>
[[#results]]
<li>[[item.title]]</li>
[[/results]]
[[^results]]
<li>No results</li>
[[/results]]
</ul>
</div>
<script async src="/public/vendors.bundle.js"></script>
<script async src="/public/index.bundle.js"></script>
</body>