2026-03-03 07:10:34 +01:00
|
|
|
import { useCallback, useRef } from 'react'
|
|
|
|
|
import { useEditorSave } from './useEditorSave'
|
2026-03-11 22:38:25 +01:00
|
|
|
import { extractOutgoingLinks, extractSnippet, countWords } from '../utils/wikilinks'
|
2026-03-03 07:10:34 +01:00
|
|
|
import type { VaultEntry } from '../types'
|
|
|
|
|
|
|
|
|
|
export function useEditorSaveWithLinks(config: {
|
|
|
|
|
updateEntry: (path: string, patch: Partial<VaultEntry>) => void
|
|
|
|
|
setTabs: Parameters<typeof useEditorSave>[0]['setTabs']
|
|
|
|
|
setToastMessage: (msg: string | null) => void
|
|
|
|
|
onAfterSave: () => void
|
2026-03-11 16:31:03 +01:00
|
|
|
onNotePersisted?: (path: string, content: string) => void
|
2026-03-03 07:10:34 +01:00
|
|
|
}) {
|
2026-03-08 22:15:08 +01:00
|
|
|
const { updateEntry } = config
|
2026-03-03 07:10:34 +01:00
|
|
|
const saveContent = useCallback((path: string, content: string) => {
|
2026-03-11 22:38:25 +01:00
|
|
|
updateEntry(path, {
|
|
|
|
|
outgoingLinks: extractOutgoingLinks(content),
|
|
|
|
|
snippet: extractSnippet(content),
|
|
|
|
|
wordCount: countWords(content),
|
|
|
|
|
})
|
2026-03-08 22:15:08 +01:00
|
|
|
}, [updateEntry])
|
2026-03-03 07:10:34 +01:00
|
|
|
const editor = useEditorSave({ updateVaultContent: saveContent, setTabs: config.setTabs, setToastMessage: config.setToastMessage, onAfterSave: config.onAfterSave, onNotePersisted: config.onNotePersisted })
|
|
|
|
|
const { handleContentChange: rawOnChange } = editor
|
|
|
|
|
const prevLinksKeyRef = useRef('')
|
|
|
|
|
const handleContentChange = useCallback((path: string, content: string) => {
|
|
|
|
|
rawOnChange(path, content)
|
|
|
|
|
const links = extractOutgoingLinks(content)
|
|
|
|
|
const key = links.join('\0')
|
|
|
|
|
if (key !== prevLinksKeyRef.current) {
|
|
|
|
|
prevLinksKeyRef.current = key
|
|
|
|
|
updateEntry(path, { outgoingLinks: links })
|
|
|
|
|
}
|
|
|
|
|
}, [rawOnChange, updateEntry])
|
|
|
|
|
return { ...editor, handleContentChange }
|
|
|
|
|
}
|