improved autocomplete

This commit is contained in:
2023-05-17 03:42:10 +02:00
parent a189dac30f
commit add3e944f6
2 changed files with 39 additions and 41 deletions

View File

@@ -51,7 +51,7 @@ defineExpose({ handleKeypress })
tabindex="0" tabindex="0"
class="menu rounded-md border-[1px] bg-base-100 p-2 text-[0.875rem] text-black shadow-md" class="menu rounded-md border-[1px] bg-base-100 p-2 text-[0.875rem] text-black shadow-md"
> >
<li class="flex flex-row"> <li class="flex flex-row" @click="emit('createLink', props.autocompleteText)">
<a class="flex-1 px-2 py-1" :class="!activeResult && 'active'"> <a class="flex-1 px-2 py-1" :class="!activeResult && 'active'">
<span class="flex-1">{{ props.autocompleteText }}</span> <span class="flex-1">{{ props.autocompleteText }}</span>
<i class="fas fa-plus-circle ml-auto text-white" /> <i class="fas fa-plus-circle ml-auto text-white" />
@@ -61,6 +61,7 @@ defineExpose({ handleKeypress })
v-for="result in results" v-for="result in results"
:result="result" :result="result"
:active-result="activeResult" :active-result="activeResult"
@go-to-note="emit('createLink', result.title)"
/> />
</ul> </ul>
</template> </template>

View File

@@ -117,47 +117,44 @@ const handleContextedKeypress = (event: any) => {
const createLink = (link: string) => { const createLink = (link: string) => {
if (!editor) return if (!editor) return
const model = editorInstance.model const model = editorInstance.model
model.change((writer: any) => { const getPosition = () => model.document.selection.anchor
const currentPosition = model.document.selection.getFirstPosition() // const getNodes = () => {
// const node = [currentPosition.textNode, currentPosition.nodeBefore, currentPosition.nodeAfter] // const nodes = [
// console.log(currentPosition, node) // getPosition().nodeBefore,
// writer.insertText(link, currentPosition) // getPosition().nodeAfter,
console.log(model.document.selection.getFirstPosition()) // getPosition().textNnode,
writer.insertText( // ]
link, // return nodes.map((node) => ({
{ contextedLink: true }, // data: node?.data,
currentPosition.nodeBefore, // attrs: Array.from(node?.getAttributes() || []),
'after' // }))
) // }
model.enqueueChange((writer: any) => { // console.log(getNodes())
// const { nextSibling } = contextedLinkTextNode let nodeToRemove: any
// if (!nextSibling || !nextSibling.data || !nextSibling.data.startsWith(']]')) if (getPosition().nodeBefore?.hasAttribute('autocomplete')) {
writer.insertText(']]', model.document.selection.getFirstPosition()) // Insert new link
}) nodeToRemove = getPosition().nodeBefore
writer.remove(currentPosition.nodeBefore) } else if (getPosition().nodeBefore?.hasAttribute('contextedLink')) {
showAutocomplete.value = false // Update existing link from end of existing link (backspace)
nodeToRemove = getPosition().nodeBefore
} else if (getPosition().textNode?.hasAttribute('contextedLink')) {
// Update existing link from middle of existing link
nodeToRemove = getPosition().textNode
} else if (getPosition().nodeAfter?.hasAttribute('contextedLink')) {
// Update existing link from beginning (delete)
nodeToRemove = getPosition().nodeAfter
}
// const node = [ model.change((writer: any) => {
// currentPosition.textNode, if (nodeToRemove) writer.remove(nodeToRemove)
// currentPosition.nodeBefore, writer.insertText(link, { contextedLink: true }, getPosition(), 'after')
// currentPosition.nodeAfter, model.enqueueChange((writer: any) => {
// ].find( const nodeAfter = getPosition().nodeAfter
// (node) => if (!nodeAfter || (nodeAfter && !nodeAfter.data.startsWith(']]'))) {
// node && writer.insertText(']]', model.document.selection.getFirstPosition())
// (node.hasAttribute('contextedLinkAutocomplete') || }
// node.hasAttribute('contextedLink')) })
// ) showAutocomplete.value = false
// const contextedLinkTextNode = writer.createText(title, { contextedLink: true })
// writer.insert(contextedLinkTextNode, node, 'after')
// model.enqueueChange((writer) => {
// const { nextSibling } = contextedLinkTextNode
// if (!nextSibling || !nextSibling.data || !nextSibling.data.startsWith(']]'))
// writer.insertText(']]', model.document.selection.getFirstPosition())
// })
// if (node) writer.remove(node)
// writer.removeSelectionAttribute('contextedLink')
// writer.removeSelectionAttribute('contextedLinkAutocomplete')
// this.autocompleteText = null
}) })
} }
</script> </script>