fix(chart): transaction mapping fix + add cache for suggestions

- Fix transaction.mapping.map usage - it returns a position, not mapping
- Add 5-minute in-memory cache for chart suggestions (max 50 entries)
- Cache key based on content hash + selection
- Significantly improves UX when re-analyzing same content

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Antigravity
2026-05-23 10:12:11 +00:00
parent 468a2bffc8
commit af3a263a54
2 changed files with 60 additions and 7 deletions

View File

@@ -297,7 +297,7 @@ export const RichTextEditor = forwardRef<RichTextEditorHandle, RichTextEditorPro
try {
console.log('[handleSelectChart] Inserting chart type:', chartContent.split('\n')[0])
const { from, to } = editor.state.selection
const { from } = editor.state.selection
const { tr } = editor.state
const { schema } = editor.state
@@ -313,17 +313,21 @@ export const RichTextEditor = forwardRef<RichTextEditorHandle, RichTextEditorPro
return
}
// Replace selection with the chart node
const transaction = tr.replaceWith(from, to, chartNode)
// Insert the chart node at current position
const transaction = tr.insert(from, chartNode)
// Get the position after the inserted chart
const insertPos = transaction.mapping.map(from)
const chartSize = chartNode.nodeSize
// Add a paragraph after for continued typing
const paragraph = schema.nodes.paragraph.create()
transaction.insert(transaction.mapping.map(to).map(to), paragraph)
transaction.insert(insertPos + chartSize, paragraph)
editor.view.dispatch(transaction)
editor.chain().focus().run()
console.log('[handleSelectChart] Chart inserted')
console.log('[handleSelectChart] Chart inserted successfully')
} catch (error) {
console.error('[handleSelectChart] Failed:', error)
toast.error('Failed to insert chart: ' + (error as Error).message)