numbas / editor

An editor for the Numbas e-learning system.
http://numbas.mathcentre.ac.uk
Apache License 2.0
59 stars 61 forks source link

fix duplicate name error warning logic #821

Open IsaacJrTypes opened 2 months ago

IsaacJrTypes commented 2 months ago

The following code fixes the duplicate warning logic. Previously, the duplicate error logic would sort the names of the entries in the names input field, turn them into variable objects, and group them. Each group would be processed by the handle_group function, which would trigger updating the duplicateNameError knock-out observable if there more than 1 object in the group. The issue with this method is if the user inputs a,b,a , the a group would trigger the duplicateNameError function, but once the b group is processed, the duplicateNameError observable updates to null.

I used an object to track if there were any duplicates. Each time the user updates input names, a new object is created, and if there are duplicates, it returns the name error message. CleanShot 2024-05-14 at 16 28 07

christianp commented 2 months ago

Thanks for this. I'd rather keep the duplicateNameError observable on the Variable object. You could avoid resetting the duplicateNameError by running through in two passes:

IsaacJrTypes commented 1 month ago

@christianp Thank you for the feedback! I updated the code to your suggestions.

Around line 2005, I subscribed to the Variable object's duplicateNameError observable to console.log the state of the observable.

CleanShot 2024-05-16 at 21 50 18

Any additional feedback is welcomed. I can also remove the comments if you think they aren't necessary.

christianp commented 1 month ago

@IsaacJrTypes Thanks. This isn't quite correct: it only sets the error message on the first Variable object for each repeated name.

I'll add some aesthetic comments to the code as well.