Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/components/GradientStops.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 || '')
Expand Down Expand Up @@ -299,7 +322,7 @@
{/if}
<div class="chip color-stop" use:tooltip={{content: 'Gamut: '+ whatsTheGamutDamnit(stop.color), placement: 'top-start',}}>
<button class="round" style="background-color: {stop.color}" onclick={e => pickColor(stop,e)} aria-label="Pick color"></button>
<input type="text" class="color-string" style="caret-color: {stop.color}" bind:value={stop.color}/>
<input type="text" class="color-string" style="caret-color: {stop.color}" bind:value={stop.color} onfocus={() => rememberColor(stop)} onblur={() => removeIfColorEmptied(stop)}/>
</div>
<div class="positions-pair">
<button class="linked round" use:tooltip={{html: true, content: '<span class="rich-tooltip wide">A color can be a point in the line or span a chunk of it. Use the 2nd slider to span an area.<br><br>The sliders are 0 - 100% but you can manually input values beyond this.<br><br>Click to relink positions.</span>', placement: 'top-start',}} onclick={() => stop.position2 = stop.position1}>
Expand Down
36 changes: 36 additions & 0 deletions src/utils/removeStop.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect } from 'vitest'
import { removeStopAt } from './stops'

const stop = (color: string) => ({ kind: 'stop', color })
const hint = () => ({ kind: 'hint' })
const kinds = (list: any[]) => list.map(s => s.kind === 'stop' ? s.color : 'h').join(' ')

describe('removeStopAt (#81)', () => {
const three = () => [stop('a'), hint(), stop('b'), hint(), stop('c')]

it('removes a first or middle stop with its following hint', () => {
expect(kinds(removeStopAt(three(), 0))).toBe('b h c')
expect(kinds(removeStopAt(three(), 2))).toBe('a h c')
})

it('removes the last stop with the hint before it, leaving no dangling hint', () => {
expect(kinds(removeStopAt(three(), 4))).toBe('a h b')
})

it('never removes the only color stop', () => {
const one = [stop('a')]
expect(removeStopAt(one, 0)).toBe(one)
})

it('ignores hints and out of range indexes', () => {
const list = three()
expect(removeStopAt(list, 1)).toBe(list)
expect(removeStopAt(list, 9)).toBe(list)
})

it('does not mutate the input', () => {
const list = three()
removeStopAt(list, 2)
expect(kinds(list)).toBe('a h b h c')
})
})
15 changes: 15 additions & 0 deletions src/utils/stops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,18 @@ export function reverseStops(stops: Stop[]): Stop[] {
})
return updateStops(reversed as Stop[])
}

// Remove the color stop at `index` along with one neighboring transition hint, so the list keeps
// exactly one hint between each pair of stops. A stop's own hint follows it; the last stop has no
// following hint, so it takes the one before it instead (otherwise a dangling hint would be left
// at the end). Returns a new array, or the original when it isn't a removable stop.
export function removeStopAt<T extends { kind: string }>(stops: T[], index: number): T[] {
if (stops[index]?.kind !== 'stop') return stops
if (stops.filter(s => s?.kind === 'stop').length <= 1) return stops

const next = [...stops]
if (next[index + 1]?.kind === 'hint') next.splice(index, 2)
else if (next[index - 1]?.kind === 'hint') next.splice(index - 1, 2)
else next.splice(index, 1)
return next
}
Loading