generated from allen-cell-animated/github-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
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
feature: Sync vector state to URL #488
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3df96ff
feat: Added backdrop visibility config, on/off toggle in settings
ShrimpCryptid 731a07c
feat: Add onscreen toggle for backdrop
ShrimpCryptid 18c59a6
feat: Added custom icons for backdrop visibility
ShrimpCryptid 6ab9937
feat: Added link to viewer settings in backdrop tooltip
ShrimpCryptid 3de2e28
feat: Added backdrop visibility to URL
ShrimpCryptid 9dbadf9
refactor: Fixed linting, removed unused CSS variables
ShrimpCryptid b8c65aa
fix: Viewer will select the default backdrop for a dataset on load
ShrimpCryptid b9a6be6
refactor: Cleanup on SVG icons, changed link color and styling, comments
ShrimpCryptid e2911fd
fix: Backdrop visibility turns off when switching to datasets that do…
ShrimpCryptid 83146ff
refactor: Code cleanup
ShrimpCryptid a125c49
refactor: Code cleanup
ShrimpCryptid 789d041
Merge branch 'feature/vector-tooltips' into feature/vector-urls
ShrimpCryptid f8e7a2e
feat: Added vector state to URL, renamed some decode methods
ShrimpCryptid 1294332
Merge branch 'main' into feature/onscreen-backdrops-toggle
ShrimpCryptid b9efd3b
Merge branch 'feature/onscreen-backdrops-toggle' into feature/vector-…
ShrimpCryptid fe3f2bb
fix: Fixed unit tests, added test for vector data
ShrimpCryptid 4392a17
fix: Removed redundant visibility flag in URL for vectors
ShrimpCryptid ac5f119
refactor: Code cleanup
ShrimpCryptid e7074ec
Merge branch 'main' into feature/vector-urls
ShrimpCryptid 0f392e0
fix: Fixed bug where vectors would always be saved to URL as enabled
ShrimpCryptid 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 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 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 |
---|---|---|
|
@@ -9,8 +9,8 @@ import { | |
getKeyFromPalette, | ||
KNOWN_CATEGORICAL_PALETTES, | ||
} from "../colors/categorical_palettes"; | ||
import { getDefaultViewerConfig } from "../constants"; | ||
import { isTabType, isThresholdCategorical } from "../types"; | ||
import { getDefaultVectorConfig, getDefaultViewerConfig } from "../constants"; | ||
import { isTabType, isThresholdCategorical, isVectorTooltipMode, VectorConfig } from "../types"; | ||
import { | ||
DrawSettings, | ||
FeatureThreshold, | ||
|
@@ -26,6 +26,9 @@ import { nanToNull } from "./data_load_utils"; | |
import { AnyManifestFile } from "./dataset_utils"; | ||
import { numberToStringDecimal } from "./math_utils"; | ||
|
||
// TODO: This file needs to be split up for easier reading and unit testing. | ||
// This could also be a great opportunity to reconsider how we store and manage state. | ||
|
||
enum UrlParam { | ||
TRACK = "track", | ||
DATASET = "dataset", | ||
|
@@ -56,6 +59,12 @@ enum UrlParam { | |
SCATTERPLOT_Y_AXIS = "scatter-y", | ||
SCATTERPLOT_RANGE_MODE = "scatter-range", | ||
OPEN_TAB = "tab", | ||
SHOW_VECTOR = "vc", | ||
VECTOR_KEY = "vc-key", | ||
VECTOR_COLOR = "vc-color", | ||
VECTOR_SCALE = "vc-scale", | ||
VECTOR_TOOLTIP_MODE = "vc-tooltip", | ||
VECTOR_TIME_INTERVALS = "vc-time-int", | ||
} | ||
|
||
const ALLEN_FILE_PREFIX = "/allen/"; | ||
|
@@ -302,6 +311,10 @@ function serializeViewerConfig(config: Partial<ViewerConfig>): string[] { | |
parameters.push(`${UrlParam.OPEN_TAB}=${config.openTab}`); | ||
} | ||
|
||
if (config.vectorConfig) { | ||
parameters.push(...serializeVectorConfig(config.vectorConfig)); | ||
} | ||
|
||
tryAddBooleanParam(parameters, config.backdropVisible, UrlParam.SHOW_BACKDROP); | ||
tryAddBooleanParam(parameters, config.showScaleBar, UrlParam.SHOW_SCALEBAR); | ||
tryAddBooleanParam(parameters, config.showTimestamp, UrlParam.SHOW_TIMESTAMP); | ||
|
@@ -316,6 +329,19 @@ export function isHexColor(value: string | null): value is HexColorString { | |
return value !== null && hexRegex.test(value); | ||
} | ||
|
||
function decodeHexColor(value: string | null): Color | undefined { | ||
value = value?.startsWith("#") ? value : "#" + value; | ||
return isHexColor(value) ? new Color(value) : undefined; | ||
} | ||
|
||
function decodeFloat(value: string | null): number | undefined { | ||
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. how do we not already have these functions in this codebase? surprising! |
||
return value === null ? undefined : parseFloat(value); | ||
} | ||
|
||
function decodeInt(value: string | null): number | undefined { | ||
return value === null ? undefined : parseInt(value, 10); | ||
} | ||
|
||
function parseDrawSettings(color: string | null, mode: string | null, defaultSettings: DrawSettings): DrawSettings { | ||
const modeInt = parseInt(mode || "-1", 10); | ||
const hexColor = "#" + color; | ||
|
@@ -325,7 +351,7 @@ function parseDrawSettings(color: string | null, mode: string | null, defaultSet | |
}; | ||
} | ||
|
||
function getBooleanParam(value: string | null): boolean | undefined { | ||
function decodeBoolean(value: string | null): boolean | undefined { | ||
if (value === null) { | ||
return undefined; | ||
} | ||
|
@@ -334,15 +360,10 @@ function getBooleanParam(value: string | null): boolean | undefined { | |
|
||
function deserializeViewerConfig(params: URLSearchParams): Partial<ViewerConfig> | undefined { | ||
const newConfig: Partial<ViewerConfig> = {}; | ||
if (params.get(UrlParam.BACKDROP_SATURATION)) { | ||
newConfig.backdropSaturation = parseInt(params.get(UrlParam.BACKDROP_SATURATION)!, 10); | ||
} | ||
if (params.get(UrlParam.BACKDROP_BRIGHTNESS)) { | ||
newConfig.backdropBrightness = parseInt(params.get(UrlParam.BACKDROP_BRIGHTNESS)!, 10); | ||
} | ||
if (params.get(UrlParam.FOREGROUND_ALPHA)) { | ||
newConfig.objectOpacity = parseInt(params.get(UrlParam.FOREGROUND_ALPHA)!, 10); | ||
} | ||
newConfig.backdropSaturation = decodeInt(params.get(UrlParam.BACKDROP_SATURATION)); | ||
newConfig.backdropBrightness = decodeInt(params.get(UrlParam.BACKDROP_BRIGHTNESS)); | ||
newConfig.objectOpacity = decodeInt(params.get(UrlParam.FOREGROUND_ALPHA)); | ||
|
||
if (params.get(UrlParam.OUTLIER_COLOR) || params.get(UrlParam.OUTLIER_MODE)) { | ||
newConfig.outlierDrawSettings = parseDrawSettings( | ||
params.get(UrlParam.OUTLIER_COLOR), | ||
|
@@ -357,22 +378,23 @@ function deserializeViewerConfig(params: URLSearchParams): Partial<ViewerConfig> | |
getDefaultViewerConfig().outOfRangeDrawSettings | ||
); | ||
} | ||
if (params.get(UrlParam.OUTLINE_COLOR)) { | ||
const outlineColor = "#" + params.get(UrlParam.OUTLINE_COLOR); | ||
if (isHexColor(outlineColor)) { | ||
newConfig.outlineColor = new Color(outlineColor); | ||
} | ||
} | ||
newConfig.outlineColor = decodeHexColor(params.get(UrlParam.OUTLINE_COLOR)); | ||
|
||
const openTab = params.get(UrlParam.OPEN_TAB); | ||
if (openTab && isTabType(openTab)) { | ||
newConfig.openTab = openTab; | ||
} | ||
|
||
newConfig.backdropVisible = getBooleanParam(params.get(UrlParam.SHOW_BACKDROP)); | ||
newConfig.showScaleBar = getBooleanParam(params.get(UrlParam.SHOW_SCALEBAR)); | ||
newConfig.showTimestamp = getBooleanParam(params.get(UrlParam.SHOW_TIMESTAMP)); | ||
newConfig.showTrackPath = getBooleanParam(params.get(UrlParam.SHOW_PATH)); | ||
newConfig.keepRangeBetweenDatasets = getBooleanParam(params.get(UrlParam.KEEP_RANGE)); | ||
newConfig.backdropVisible = decodeBoolean(params.get(UrlParam.SHOW_BACKDROP)); | ||
newConfig.showScaleBar = decodeBoolean(params.get(UrlParam.SHOW_SCALEBAR)); | ||
newConfig.showTimestamp = decodeBoolean(params.get(UrlParam.SHOW_TIMESTAMP)); | ||
newConfig.showTrackPath = decodeBoolean(params.get(UrlParam.SHOW_PATH)); | ||
newConfig.keepRangeBetweenDatasets = decodeBoolean(params.get(UrlParam.KEEP_RANGE)); | ||
|
||
const vectorConfig = deserializeVectorConfig(params); | ||
if (vectorConfig && Object.keys(vectorConfig).length > 0) { | ||
newConfig.vectorConfig = { ...getDefaultVectorConfig(), ...vectorConfig }; | ||
} | ||
|
||
const finalConfig = removeUndefinedProperties(newConfig); | ||
return Object.keys(finalConfig).length === 0 ? undefined : finalConfig; | ||
|
@@ -407,18 +429,40 @@ function deserializeScatterPlotConfig(params: URLSearchParams): Partial<ScatterP | |
if (rangeString && urlParamToRangeType[rangeString]) { | ||
newConfig.rangeType = urlParamToRangeType[rangeString]; | ||
} | ||
const xAxis = decodePossiblyNullString(params.get(UrlParam.SCATTERPLOT_X_AXIS)); | ||
const yAxis = decodePossiblyNullString(params.get(UrlParam.SCATTERPLOT_Y_AXIS)); | ||
if (xAxis) { | ||
newConfig.xAxis = xAxis; | ||
} | ||
if (yAxis) { | ||
newConfig.yAxis = yAxis; | ||
} | ||
newConfig.xAxis = decodeString(params.get(UrlParam.SCATTERPLOT_X_AXIS)); | ||
newConfig.yAxis = decodeString(params.get(UrlParam.SCATTERPLOT_Y_AXIS)); | ||
|
||
const finalConfig = removeUndefinedProperties(newConfig); | ||
return Object.keys(finalConfig).length === 0 ? undefined : finalConfig; | ||
} | ||
|
||
function serializeVectorConfig(config: Partial<VectorConfig>): string[] { | ||
const parameters: string[] = []; | ||
tryAddBooleanParam(parameters, config.visible, UrlParam.SHOW_VECTOR); | ||
config.color !== undefined && parameters.push(`${UrlParam.VECTOR_COLOR}=${config.color.getHexString()}`); | ||
config.key !== undefined && parameters.push(`${UrlParam.VECTOR_KEY}=${encodeURIComponent(config.key)}`); | ||
config.scaleFactor !== undefined && parameters.push(`${UrlParam.VECTOR_SCALE}=${config.scaleFactor}`); | ||
config.timeIntervals !== undefined && parameters.push(`${UrlParam.VECTOR_TIME_INTERVALS}=${config.timeIntervals}`); | ||
config.tooltipMode !== undefined && parameters.push(`${UrlParam.VECTOR_TOOLTIP_MODE}=${config.tooltipMode}`); | ||
return parameters; | ||
} | ||
|
||
function deserializeVectorConfig(params: URLSearchParams): Partial<VectorConfig> | undefined { | ||
const newConfig: Partial<VectorConfig> = {}; | ||
newConfig.visible = decodeBoolean(params.get(UrlParam.SHOW_VECTOR)); | ||
newConfig.color = decodeHexColor(params.get(UrlParam.VECTOR_COLOR)); | ||
newConfig.key = decodeString(params.get(UrlParam.VECTOR_KEY)); | ||
newConfig.scaleFactor = decodeFloat(params.get(UrlParam.VECTOR_SCALE)); | ||
newConfig.timeIntervals = decodeInt(params.get(UrlParam.VECTOR_TIME_INTERVALS)); | ||
|
||
const tooltip = params.get(UrlParam.VECTOR_TOOLTIP_MODE); | ||
if (tooltip && isVectorTooltipMode(tooltip)) { | ||
newConfig.tooltipMode = tooltip; | ||
} | ||
|
||
return removeUndefinedProperties(newConfig); | ||
} | ||
|
||
/** | ||
* Creates a url query string from parameters that can be appended onto the base URL. | ||
* | ||
|
@@ -549,8 +593,8 @@ export function convertAllenPathToHttps(input: string): string | null { | |
/** | ||
* Decodes strings using `decodeURIComponent`, handling null inputs. | ||
*/ | ||
function decodePossiblyNullString(input: string | null): string | null { | ||
return input === null ? null : decodeURIComponent(input); | ||
function decodeString(input: string | null): string | undefined { | ||
return input === null ? undefined : decodeURIComponent(input); | ||
} | ||
|
||
/** | ||
|
@@ -608,7 +652,7 @@ export function loadFromUrlSearchParams(urlParams: URLSearchParams): Partial<Url | |
const thresholdsParam = deserializeThresholds(urlParams.get(UrlParam.THRESHOLDS)); | ||
|
||
let rangeParam: [number, number] | undefined = undefined; | ||
const rawRangeParam = decodePossiblyNullString(urlParams.get(UrlParam.RANGE)); | ||
const rawRangeParam = decodeString(urlParams.get(UrlParam.RANGE)); | ||
if (rawRangeParam) { | ||
const [min, max] = rawRangeParam.split(","); | ||
rangeParam = [parseFloat(min), parseFloat(max)]; | ||
|
@@ -652,7 +696,7 @@ export function loadFromUrlSearchParams(urlParams: URLSearchParams): Partial<Url | |
} | ||
|
||
const config = deserializeViewerConfig(urlParams); | ||
const selectedBackdropKey = decodePossiblyNullString(urlParams.get(UrlParam.BACKDROP_KEY)) ?? undefined; | ||
const selectedBackdropKey = decodeString(urlParams.get(UrlParam.BACKDROP_KEY)); | ||
const scatterPlotConfig = deserializeScatterPlotConfig(urlParams); | ||
|
||
// Remove undefined entries from the object for a cleaner return value | ||
|
This file contains 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
Oops, something went wrong.
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.
is this the "feature name"?
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.
Yup, it's the equivalent!