@@ -2,7 +2,6 @@ import { App, Modal, TFile, FuzzySuggestModal, Notice } from 'obsidian';
22import { HledgerSettings } from '../settings' ;
33import moment from 'moment' ;
44
5- // Type definitions
65interface Entry {
76 account : string ;
87 amount : number ;
@@ -301,15 +300,29 @@ export class HledgerEntryModal extends Modal {
301300 return ;
302301 }
303302
304- const zeroAmounts = this . entries . filter ( entry => entry . amount === 0 ) ;
305- if ( zeroAmounts . length > 0 ) {
306- new Notice ( 'Amounts cannot be zero' ) ;
307- return ;
308- }
309-
310- if ( ! this . isExchange ) {
303+ const emptyAmounts = this . entries . filter ( entry => {
304+ const amountInput = entry . amount !== undefined ? entry . amount . toString ( ) : '' ;
305+ return amountInput === '0' || amountInput === '' || amountInput . trim ( ) === '' || isNaN ( entry . amount ) ;
306+ } ) ;
307+
308+ if ( this . isExchange ) {
309+ if ( emptyAmounts . length > 0 ) {
310+ new Notice ( 'Please fill in all amount fields' ) ;
311+ return ;
312+ }
313+
314+ if ( this . entries . every ( entry => entry . amount === 0 ) ) {
315+ new Notice ( 'At least one amount must be non-zero in exchange mode' ) ;
316+ return ;
317+ }
318+ } else {
319+ if ( emptyAmounts . length > 0 ) {
320+ new Notice ( 'Please fill in all amount fields with valid numbers' ) ;
321+ return ;
322+ }
323+
311324 const totalAmount = this . entries . reduce ( ( sum , entry ) => sum + entry . amount , 0 ) ;
312- const epsilon = 0.0001 ; // Small tolerance for floating point comparison
325+ const epsilon = 0.0001 ;
313326 if ( Math . abs ( totalAmount ) > epsilon ) {
314327 new Notice ( `Transaction does not balance. Total is ${ totalAmount . toFixed ( 2 ) } ` ) ;
315328 return ;
@@ -322,7 +335,6 @@ export class HledgerEntryModal extends Modal {
322335
323336 private async loadAccounts ( ) {
324337 if ( ! this . settings . accountsFile || ! this . settings . hledgerFolder ) {
325- console . log ( 'No accounts file or Hledger folder specified' ) ;
326338 return ;
327339 }
328340
@@ -341,9 +353,6 @@ export class HledgerEntryModal extends Modal {
341353 return match ? match [ 1 ] . trim ( ) : null ;
342354 } )
343355 . filter ( ( account ) : account is string => account !== null ) ;
344-
345- } else {
346- console . log ( 'Accounts file not found:' , accountsPath ) ;
347356 }
348357 } catch ( error ) {
349358 console . error ( 'Error loading accounts file:' , error ) ;
@@ -596,23 +605,29 @@ export class HledgerEntryModal extends Modal {
596605 cls : 'text-input hledger-amount-input'
597606 } ) ;
598607
599- amountInput . addEventListener ( 'change' , ( e ) => {
600- this . handleAmountChange ( e , entry , index , container ) ;
608+ amountInput . addEventListener ( 'input' , ( e ) => {
609+ const target = e . target as HTMLInputElement ;
610+ let value = target . value . trim ( ) ;
611+ value = this . processAmountValue ( value , entry , target ) ;
601612 } ) ;
602- }
603-
604- private handleAmountChange ( e : Event , entry : Entry , index : number , container : HTMLElement ) : void {
605- const target = e . target as HTMLInputElement ;
606- let value = target . value . trim ( ) ;
607-
608- value = this . processAmountValue ( value , entry , target ) ;
609613
610- if ( index === 0 && this . entries . length === 2 && ! this . isExchange ) {
611- this . updateSecondRowAmount ( entry . amount , container ) ;
612- }
614+ amountInput . addEventListener ( 'change' , ( e ) => {
615+ const target = e . target as HTMLInputElement ;
616+ let value = target . value . trim ( ) ;
617+ value = this . processAmountValue ( value , entry , target ) ;
618+
619+ if ( index === 0 && this . entries . length === 2 && ! this . isExchange ) {
620+ this . updateSecondRowAmount ( entry . amount , container ) ;
621+ }
622+ } ) ;
613623 }
614624
615625 private processAmountValue ( value : string , entry : Entry , inputElement : HTMLInputElement ) : string {
626+ if ( ! value . trim ( ) ) {
627+ entry . amount = NaN ;
628+ return '' ;
629+ }
630+
616631 let suffix = '' ;
617632 if ( value . toLowerCase ( ) . endsWith ( 'k' ) ) {
618633 suffix = 'k' ;
@@ -622,20 +637,39 @@ export class HledgerEntryModal extends Modal {
622637 value = value . slice ( 0 , - 1 ) ;
623638 }
624639
640+ if ( value . includes ( '.' ) ) {
641+ value = value . replace ( / , / g, '' ) ;
642+ }
643+ else if ( ( value . match ( / , / g) || [ ] ) . length === 1 && ! value . includes ( '.' ) ) {
644+ value = value . replace ( ',' , '.' ) ;
645+ }
646+ else {
647+ value = value . replace ( / , / g, '' ) ;
648+ }
649+
625650 value = value . replace ( / [ ^ \d . - ] / g, '' ) ;
651+
652+ const decimalPoints = ( value . match ( / \. / g) || [ ] ) . length ;
653+ if ( decimalPoints > 1 ) {
654+ const parts = value . split ( '.' ) ;
655+ value = parts [ 0 ] + '.' + parts . slice ( 1 ) . join ( '' ) ;
656+ }
657+
626658 value = value + suffix ;
627659
628660 if ( value . toLowerCase ( ) . endsWith ( 'k' ) ) {
629661 value = value . slice ( 0 , - 1 ) ;
630- entry . amount = ( parseFloat ( value ) || 0 ) * 1000 ;
662+ entry . amount = Math . round ( ( parseFloat ( value ) || 0 ) * 1000 ) ;
631663 inputElement . value = entry . amount . toString ( ) ;
632664 } else if ( value . toLowerCase ( ) . endsWith ( 'm' ) ) {
633665 value = value . slice ( 0 , - 1 ) ;
634- entry . amount = ( parseFloat ( value ) || 0 ) * 1000000 ;
666+ entry . amount = Math . round ( ( parseFloat ( value ) || 0 ) * 1000000 ) ;
635667 inputElement . value = entry . amount . toString ( ) ;
636668 } else {
637669 entry . amount = parseFloat ( value ) || 0 ;
638- inputElement . value = entry . amount . toString ( ) ;
670+ if ( value !== inputElement . value ) {
671+ inputElement . value = value ;
672+ }
639673 }
640674
641675 return value ;
@@ -645,9 +679,9 @@ export class HledgerEntryModal extends Modal {
645679 const secondRow = this . entries [ 1 ] ;
646680 const secondAmountInput = container . querySelectorAll ( '.hledger-amount-input' ) [ 1 ] as HTMLInputElement ;
647681
648- if ( ! secondRow . amount && secondAmountInput && ( secondAmountInput . value === '' || secondAmountInput . value === '0' ) ) {
682+ if ( secondAmountInput && ! Number . isNaN ( firstRowAmount ) && ( secondAmountInput . value === '' || secondAmountInput . value === '0' ) ) {
649683 secondRow . amount = - firstRowAmount ;
650- secondAmountInput . value = secondRow . amount . toString ( ) ;
684+ secondAmountInput . value = ( - firstRowAmount ) . toString ( ) ;
651685 }
652686 }
653687
0 commit comments