forked from google/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: scaffolding an enum default select #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seankmartin
wants to merge
7
commits into
feature/annotation-schema
Choose a base branch
from
fix/enum-default-select
base: feature/annotation-schema
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1c4d9bc
feat: scaffolding an enum default select
seankmartin 3a83fb1
Merge branch 'feature/annotation-schema' into fix/enum-default-select
seankmartin 6eb6f41
feat: hook up selector properly for default
seankmartin 56afaea
fix: don't update default on enums in UI
seankmartin e65fed3
fix: allow schema to move in default
seankmartin 38348b1
feat: explain default updates
seankmartin 7471b55
fix: small css fixes
seankmartin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,7 +196,14 @@ class AnnotationUIProperty extends RefCounted { | |
// For numeric types, we can set the default value directly | ||
const type = this.spec.type; | ||
if (isAnnotationTypeNumeric(type)) { | ||
this.defaultValueElements[0].value = numberToStringFixed(defaultValue, 4); | ||
const enumLabels = (this.spec as AnnotationNumericPropertySpec) | ||
.enumLabels; | ||
if (!isEnumType(enumLabels)) { | ||
this.defaultValueElements[0].value = numberToStringFixed( | ||
defaultValue, | ||
4, | ||
); | ||
} | ||
} | ||
// For color types, we need to unpack the color and set the RGB values | ||
else if (type.startsWith("rgb")) { | ||
|
@@ -511,6 +518,11 @@ class AnnotationUIProperty extends RefCounted { | |
|
||
const inputs: (HTMLInputElement | HTMLTextAreaElement)[] = []; | ||
|
||
if (!this.readonly) { | ||
const defaultSelector = this.createEnumDefaultSelector(oldProperty); | ||
enumContainer.appendChild(defaultSelector); | ||
} | ||
|
||
for (let i = 0; i < enumValues!.length; i++) { | ||
const entryInputs = this.createEnumEntry( | ||
enumValues![i], | ||
|
@@ -532,6 +544,48 @@ class AnnotationUIProperty extends RefCounted { | |
return inputs; | ||
} | ||
|
||
private createEnumDefaultSelector( | ||
enumProperty: AnnotationNumericPropertySpec, | ||
): HTMLDivElement { | ||
const selectorContainer = document.createElement("div"); | ||
selectorContainer.className = | ||
"neuroglancer-annotation-schema-enum-default-selector"; | ||
|
||
const label = document.createElement("label"); | ||
label.textContent = "Default:"; | ||
label.className = "neuroglancer-annotation-schema-enum-default-label"; | ||
|
||
const select = document.createElement("select"); | ||
select.className = "neuroglancer-annotation-schema-enum-default-select"; | ||
|
||
enumProperty.enumValues?.forEach((value: number, index: number) => { | ||
const option = document.createElement("option"); | ||
option.value = String(value); | ||
if (!enumProperty.enumLabels) { | ||
option.textContent = "Non-schema value"; | ||
} else { | ||
option.textContent = enumProperty.enumLabels[index]; | ||
} | ||
option.selected = value === enumProperty.default; | ||
select.appendChild(option); | ||
}); | ||
|
||
select.addEventListener("change", (event) => { | ||
const selectedValue = parseFloat( | ||
(event.target as HTMLSelectElement).value, | ||
); | ||
const existingProperty = this.getPropertyByIdentifier( | ||
enumProperty.identifier, | ||
) as AnnotationNumericPropertySpec; | ||
this.updateProperty(existingProperty, { default: selectedValue }); | ||
}); | ||
|
||
selectorContainer.appendChild(label); | ||
selectorContainer.appendChild(select); | ||
|
||
return selectorContainer; | ||
} | ||
|
||
private createNumericInputs( | ||
type: AnnotationPropertyType, | ||
oldProperty: any, | ||
|
@@ -585,13 +639,21 @@ class AnnotationUIProperty extends RefCounted { | |
); | ||
const newEnumValues = [...currentEnumValues!, suggestedEnumValue]; | ||
|
||
// Keep the current default value if it's still valid | ||
// otherwise use the first value | ||
// this could occur if the user deletes all values then adds a new one | ||
const currentDefault = currentProperty.default; | ||
const newDefault = newEnumValues.includes(currentDefault) | ||
? currentDefault | ||
: newEnumValues[0]; | ||
|
||
this.updateProperty(currentProperty, { | ||
enumValues: newEnumValues, | ||
enumLabels: [ | ||
...currentProperty.enumLabels!, | ||
`${suggestedEnumValue} (label)`, | ||
], | ||
default: newEnumValues[0], | ||
default: newDefault, | ||
} as AnnotationNumericPropertySpec); | ||
}, | ||
}); | ||
|
@@ -700,9 +762,17 @@ class AnnotationUIProperty extends RefCounted { | |
(_, i) => i !== enumIndex, | ||
); | ||
|
||
// If we're deleting the current default value, set the new default to the first remaining value | ||
const deletedValue = currentProperty.enumValues![enumIndex]; | ||
const newDefault = | ||
currentProperty.default === deletedValue | ||
? (newEnumValues[0] ?? 0) | ||
: currentProperty.default; | ||
|
||
this.updateProperty(currentProperty, { | ||
enumValues: newEnumValues, | ||
enumLabels: newEnumLabels, | ||
default: newDefault, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing property name - should be 'default: newDefault,' to match the object property syntax used in the rest of the codebase. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
}); | ||
}, | ||
}); | ||
|
@@ -1021,7 +1091,10 @@ export class AnnotationSchemaView extends Tab { | |
|
||
private updateAnnotationText() { | ||
const setOrViewText = this.readonly.value ? "View read-only" : "Set"; | ||
this.schemaViewTextElement.textContent = `${setOrViewText} annotation property (metadata) schema for this layer which applies to all annotations in this layer.`; | ||
const setExplainText = this.readonly.value | ||
? "" | ||
: " Changing a default value in the schema is not retroactive and only applies to new annotations."; | ||
this.schemaViewTextElement.textContent = `${setOrViewText} annotation property (metadata) schema for this layer which applies to all annotations in this layer.${setExplainText}`; | ||
} | ||
|
||
private updateElementVisibility() { | ||
|
@@ -1470,7 +1543,7 @@ export class AnnotationSchemaView extends Tab { | |
if (isEnum && isAnnotationTypeNumeric(type)) { | ||
return { | ||
enumValues: [0], | ||
enumLabels: ["Default"], | ||
enumLabels: ["0 (label)"], | ||
}; | ||
} | ||
return {}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fallback text 'Non-schema value' is misleading and could confuse users. If enumLabels is missing, consider using the numeric value itself or a more descriptive fallback like 'Value: {value}'.
Copilot uses AI. Check for mistakes.