(options?: {
nextSelection?: HTMLElement | null;
clearSelection?: boolean;
reselectEditedElement?: boolean;
})
| 482 | } |
| 483 | |
| 484 | function commitAndExit(options?: { |
| 485 | nextSelection?: HTMLElement | null; |
| 486 | clearSelection?: boolean; |
| 487 | reselectEditedElement?: boolean; |
| 488 | }): void { |
| 489 | if (!editingElement) return; |
| 490 | |
| 491 | const newText = lastKnownText; |
| 492 | const cursorOffset = getCaretOffsetWithin(editingElement); |
| 493 | const changed = newText !== originalTextContent; |
| 494 | const textAnchor = changed ? buildTextEditAnchor(originalTextContent, newText) : undefined; |
| 495 | |
| 496 | console.log("[ReactRewrite:textEdit] commitAndExit changed:", changed, "componentInfo:", componentInfo?.componentName, "filePath:", componentInfo?.filePath); |
| 497 | |
| 498 | if (changed && componentInfo) { |
| 499 | // If filePath is empty, try file discovery (grep-based lookup by component name) |
| 500 | if (!componentInfo.filePath && componentInfo.componentName) { |
| 501 | const cached = getCachedFilePath(componentInfo.componentName); |
| 502 | if (cached) { |
| 503 | componentInfo = { ...componentInfo, filePath: cached }; |
| 504 | } else { |
| 505 | // Fire async discovery — won't block, annotation created with empty path for now |
| 506 | requestFileDiscovery(componentInfo.componentName).then((discovered) => { |
| 507 | if (discovered) setCachedFilePath(componentInfo!.componentName, discovered); |
| 508 | }); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // All text edits create annotations — committed via confirm button |
| 513 | { |
| 514 | const ann: TextEditAnnotation = { |
| 515 | type: "textEdit", |
| 516 | id: `text-edit-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, |
| 517 | componentName: componentInfo.componentName, |
| 518 | filePath: componentInfo.filePath || "", |
| 519 | lineNumber: componentInfo.lineNumber || 0, |
| 520 | columnNumber: componentInfo.columnNumber || 0, |
| 521 | originalText: originalTextContent, |
| 522 | newText, |
| 523 | cursorOffset, |
| 524 | textAnchor, |
| 525 | }; |
| 526 | const identity: ElementIdentity = { |
| 527 | componentName: componentInfo.componentName, |
| 528 | filePath: componentInfo.filePath || "", |
| 529 | lineNumber: componentInfo.lineNumber || 0, |
| 530 | columnNumber: componentInfo.columnNumber || 0, |
| 531 | tagName: componentInfo.tagName, |
| 532 | }; |
| 533 | addTextEditAnnotation(ann, identity, originalInnerHTML, { |
| 534 | tagName: editingElement?.tagName.toLowerCase() || componentInfo.tagName, |
| 535 | className: editingElement?.className || undefined, |
| 536 | parentTagName: editingElement?.parentElement?.tagName.toLowerCase(), |
| 537 | parentClassName: editingElement?.parentElement?.className || undefined, |
| 538 | nthOfType: editingElement ? computeNthOfType(editingElement) : undefined, |
| 539 | elementId: editingElement?.id || undefined, |
| 540 | jsxPath: componentInfo.jsxPath, |
| 541 | }); |
no test coverage detected