insert new link with autocomplete

This commit is contained in:
2023-05-15 23:16:19 +02:00
parent 5240d61b35
commit c9c460b36c
4 changed files with 55 additions and 5 deletions

View File

@@ -64,6 +64,7 @@ export default defineComponent({
'input', 'input',
'update:modelValue', 'update:modelValue',
'click', 'click',
'editorReady',
'contextedLinkAutocomplete', 'contextedLinkAutocomplete',
'contextedKeypress', 'contextedKeypress',
], ],
@@ -182,6 +183,7 @@ export default defineComponent({
methods: { methods: {
setUpEditorEvents() { setUpEditorEvents() {
const editor = this.instance! const editor = this.instance!
this.$emit('editorReady', editor)
// Use the leading edge so the first event in the series is emitted immediately. // Use the leading edge so the first event in the series is emitted immediately.
// Failing to do so leads to race conditions, for instance, when the component modelValue // Failing to do so leads to race conditions, for instance, when the component modelValue

View File

@@ -126,12 +126,16 @@ export default class ContextedLinkEditing extends Plugin {
) )
if (selectionInContextedLink && keyCodes.includes(keyCode)) { if (selectionInContextedLink && keyCodes.includes(keyCode)) {
if (selection.hasAttribute('contextedLink')) { if (selection.hasAttribute('contextedLink')) {
const autocompleteNode = editor.model.document.selection.focus const autocompleteNode = [
?.textNode as any selection.focus?.nodeBefore,
selection.focus?.textNode,
selection.focus?.nodeAfter,
].find((node) => Boolean(node))
fireAutocompleteEvent(editor, true, autocompleteNode) fireAutocompleteEvent(editor, true, autocompleteNode)
} }
this.editor.model.document.fire('contextedKeypress', { keyCode }) this.editor.model.document.fire('contextedKeypress', { keyCode })
data.preventDefault(evt.stop()) data.preventDefault()
evt.stop()
} }
}, },
{ priority: 'highest' } { priority: 'highest' }

View File

@@ -59,6 +59,7 @@ const editorConfig = {
const editorElement = ref<HTMLInputElement | null>(null) const editorElement = ref<HTMLInputElement | null>(null)
watch(editorData, () => emit('update', htmlToMd(editorData.value))) watch(editorData, () => emit('update', htmlToMd(editorData.value)))
let editorInstance: any
const handleClick = ({ data }: { data: any }) => { const handleClick = ({ data }: { data: any }) => {
const noteTitle = data.domTarget.textContent as string const noteTitle = data.domTarget.textContent as string
@@ -114,7 +115,50 @@ const handleContextedKeypress = (event: any) => {
} }
const createLink = (link: string) => { const createLink = (link: string) => {
console.log(link) if (!editor) return
const model = editorInstance.model
model.change((writer: any) => {
const currentPosition = model.document.selection.getFirstPosition()
// const node = [currentPosition.textNode, currentPosition.nodeBefore, currentPosition.nodeAfter]
// console.log(currentPosition, node)
// writer.insertText(link, currentPosition)
console.log(model.document.selection.getFirstPosition())
writer.insertText(
link,
{ contextedLink: true },
currentPosition.nodeBefore,
'after'
)
model.enqueueChange((writer: any) => {
// const { nextSibling } = contextedLinkTextNode
// if (!nextSibling || !nextSibling.data || !nextSibling.data.startsWith(']]'))
writer.insertText(']]', model.document.selection.getFirstPosition())
})
writer.remove(currentPosition.nodeBefore)
showAutocomplete.value = false
// const node = [
// currentPosition.textNode,
// currentPosition.nodeBefore,
// currentPosition.nodeAfter,
// ].find(
// (node) =>
// node &&
// (node.hasAttribute('contextedLinkAutocomplete') ||
// node.hasAttribute('contextedLink'))
// )
// 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>
<template> <template>
@@ -124,6 +168,7 @@ const createLink = (link: string) => {
:editor="editor" :editor="editor"
v-model="editorData" v-model="editorData"
:config="editorConfig" :config="editorConfig"
@editor-ready="(editor) => (editorInstance = editor)"
@click="handleClick" @click="handleClick"
@contexted-link-autocomplete="handleAutocomplete" @contexted-link-autocomplete="handleAutocomplete"
@contexted-keypress="handleContextedKeypress" @contexted-keypress="handleContextedKeypress"

View File

@@ -91,7 +91,6 @@ export const updateNote = (noteId: string, note: BaseNote) => {
modified: new Date().getTime(), modified: new Date().getTime(),
} }
baseNotes[noteId] = updatedNote baseNotes[noteId] = updatedNote
console.log(note)
} }
export const addNote = (title: string, content: string, goToNote: boolean = false) => { export const addNote = (title: string, content: string, goToNote: boolean = false) => {