diff --git a/src/components/GradientStops.svelte b/src/components/GradientStops.svelte index ad3fb38..add07b6 100644 --- a/src/components/GradientStops.svelte +++ b/src/components/GradientStops.svelte @@ -11,6 +11,7 @@ import {copyToClipboard} from '../utils/clipboard' import {randomNumber} from '../utils/numbers' + import {removeStopAt} from '../utils/stops' import {whatsTheGamutDamnit} from '../utils/colorspace' import RangeSlider from './RangeSlider.svelte' @@ -39,10 +40,7 @@ const target = event.target as HTMLSelectElement switch (target.value) { case 'Remove': - if (colorStopCount() <= 1) break - const newStops = [...$gradient_stops] - newStops.splice(position, 2) - $gradient_stops = newStops + $gradient_stops = removeStopAt($gradient_stops, position) break case 'Reset': const resetStops = [...$gradient_stops] @@ -135,6 +133,31 @@ $active_stop_index = null } + // Emptying a stop's color text and leaving the field deletes that stop (#81). + // The only color stop can't be deleted, so it gets the color it had on focus back. + let colorOnFocus = '' + + function rememberColor(stop: GradientStop) { + colorOnFocus = stop.color + } + + function removeIfColorEmptied(stop: GradientStop) { + if (String(stop.color ?? '').trim() !== '') return + // switching windows/tabs blurs the field too; wait for a real blur when the user comes back + if (!document.hasFocus()) return + const index = $gradient_stops.indexOf(stop) + if (index < 0) return + + if (colorStopCount() > 1) { + $gradient_stops = removeStopAt($gradient_stops, index) + } + else { + // it may already have been empty on focus, so never write an empty color back + stop.color = String(colorOnFocus ?? '').trim() || `oklch(80% 0.3 ${randomNumber(0,360)})` + $gradient_stops = [...$gradient_stops] + } + } + function fixIfEmptied(stop: GradientStop) { if (stop.percentage === null || stop.percentage === undefined) { stop.percentage = String(stop.auto || '') @@ -299,7 +322,7 @@ {/if}
- + rememberColor(stop)} onblur={() => removeIfColorEmptied(stop)}/>