diff --git a/src/main.ts b/src/main.ts index b3426fe..53015b8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -464,21 +464,25 @@ export default class VariantEditor extends Plugin { new TextInputModal( this.app, initialText, - (variantText, activeIndex, commitVariant, currentFrom, currentTo) => { + (variantText, activeIndex, commitVariant, currentFrom, currentTo, modalClosed) => { // Use the updated cursor positions if provided, otherwise use the original positions const updateFrom = currentFrom || from; const updateTo = currentTo || to; - if (commitVariant) { - // Replace the variant with just the active variant text + if (modalClosed) { + // Modal was closed without committing (via ESC key or clicking outside) + // Clear the highlights + this.clearHighlight(); + } else if (commitVariant === true) { + // Replace the variant with just the active variant text (commit action) if (variantText) { editor.replaceRange(variantText, updateFrom, updateTo); new Notice(`Committed variant: "${variantText}"`); - // Only clear highlights when committing + // Clear highlights when committing this.clearHighlight(); } } else { - // Create or update the variant syntax + // Create or update the variant syntax (normal variant creation/update) const variants = variantText.split('|').filter(v => v); if (variants.length > 0) { diff --git a/src/modals/TextInputModal.ts b/src/modals/TextInputModal.ts index ad78d89..5846c44 100644 --- a/src/modals/TextInputModal.ts +++ b/src/modals/TextInputModal.ts @@ -8,7 +8,7 @@ export class TextInputModal extends Modal { private variants: string[] = []; private activeVariantIndex: number = 0; private lastNonEmptyVariantIndex: number = 0; // Track the last non-empty variant index - private onSubmit: (result: string, activeIndex?: number, commitVariant?: boolean, currentFrom?: EditorPosition | null, currentTo?: EditorPosition | null) => void; + private onSubmit: (result: string, activeIndex?: number, commitVariant?: boolean, currentFrom?: EditorPosition | null, currentTo?: EditorPosition | null, modalClosed?: boolean) => void; private originalText: string; private variantContainer: HTMLElement; private cursorPosition: EditorPosition | null; @@ -19,7 +19,7 @@ export class TextInputModal extends Modal { constructor( app: App, originalText: string, - onSubmit: (result: string, activeIndex?: number, commitVariant?: boolean, currentFrom?: EditorPosition | null, currentTo?: EditorPosition | null) => void, + onSubmit: (result: string, activeIndex?: number, commitVariant?: boolean, currentFrom?: EditorPosition | null, currentTo?: EditorPosition | null, modalClosed?: boolean) => void, cursorPosition: EditorPosition | null = null, initialActiveIndex: number = 0, currentFrom: EditorPosition | null = null, @@ -535,5 +535,9 @@ export class TextInputModal extends Modal { onClose() { const {contentEl} = this; contentEl.empty(); + + // Notify the parent that the modal was closed without committing + // We pass an explicit modalClosed=true flag to indicate this was triggered by modal closing + this.onSubmit(this.variants[this.activeVariantIndex], this.activeVariantIndex, false, this.currentFrom, this.currentTo, true); } }