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) $lc.offset(pos)
} }
let templateText = he.decode(document.getElementById(template).innerHTML); let selectedPos = 0;
let rendered = Mustache.render(templateText, {
page: value.trim().replace(/\s+/g, '_'),
results: results
}, {}, ['[[', ']]']);
let selected = $lc.find('li.selected'); let selected = $lc.find('li.selected');
if (selected) { if (selected) {
let selectedPos = $lc.find('li').index(selected[0]) 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')
}
} }
$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 return results
}) })
} }
@ -425,7 +427,7 @@ function Editor(holder, input) {
if (resultType === 'link') { if (resultType === 'link') {
let start = value.lastIndexOf("[[", end) let start = value.lastIndexOf("[[", end)
end += 2 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.value = startAndLink + value.substring(end)
input.selectionStart = startAndLink.length input.selectionStart = startAndLink.length
input.selectionEnd = startAndLink.length input.selectionEnd = startAndLink.length
@ -585,7 +587,7 @@ function Editor(holder, input) {
}) })
return true return true
} else if (insideLink) { } else if (insideLink) {
let query = value.substring(start + 2, end); let query = value.substring(start, end);
showSearchResults(titleSearch, query, input, value, 'link'); showSearchResults(titleSearch, query, input, value, 'link');
return true return true
} else { } else {
@ -607,21 +609,19 @@ function Editor(holder, input) {
Promise.any([ Promise.any([
match(query, /{{query(!?):\s*([^}]+)}}/) match(query, /{{query(!?):\s*([^}]+)}}/)
]) ]).then(res => {
.then(res => { if (res[1] === '!') {
if (res[1] === '!') { return search.startQuery(res[2])
return search.startQuery(res[2]) .then(hits => _.uniqBy(_.flatMap(hits, formatTitleResult), _.property('text')))
.then(hits => _.uniqBy(_.flatMap(hits, formatTitleResult), _.property('text'))) .then(results => editor.replaceChildren(id, results))
.then(results => editor.replaceChildren(id, results)) .finally(() => editor.render())
.finally(() => editor.render()) } else {
} else { return search.startQuery(res[2])
return search.startQuery(res[2]) .then(hits => _.flatMap(hits, formatLineResult))
.then(hits => _.flatMap(hits, formatLineResult)) .then(results => editor.replaceChildren(id, results))
.then(results => editor.replaceChildren(id, results)) .finally(() => editor.render())
.finally(() => editor.render()) }
} }).catch(() => console.log('match error'))
})
.catch(() => console.log('match error'))
}); });
return editor return editor
}) })

View File

@ -28,7 +28,10 @@ function createTitleSearch() {
documents, documents,
titleSearch: query => { titleSearch: query => {
return new Promise((resolve, reject) => { 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 => { commandSearch: query => {

View File

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

View File

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

View File

@ -83,16 +83,6 @@
<li><a href="/edit/[[page]]">Create a page</a></li> <li><a href="/edit/[[page]]">Create a page</a></li>
</ul> </ul>
</div> </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/vendors.bundle.js"></script>
<script async src="/public/index.bundle.js"></script> <script async src="/public/index.bundle.js"></script>
</body> </body>