Skip to content

Commit 565a77c

Browse files
argyleinkclaude
andauthored
Linear angle: keep 45/135/225/315deg instead of snapping to corner keywords (#135)
'to top right' aims at the box corner, so it only equals 45deg on a square. Typing or importing 45deg used to switch the output to 'to top right', rendering a different angle on any non-square element. Only the side keywords (exact 0/90/180/270) snap now; a corner keyword restored with its nominal angle stays. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1d82880 commit 565a77c

3 files changed

Lines changed: 66 additions & 30 deletions

File tree

‎src/components/LinearOverlay.svelte‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,20 @@
4848
: $linear_angle
4949
)
5050
51-
// Snap named angle when numeric angle matches a named value (e.g., via slider)
52-
const NAMED_ANGLES = [
51+
// Snap to a side keyword when the numeric angle matches one (e.g., via slider).
52+
// Corner keywords aren't fixed angles (they aim at the box corner), so 45deg stays 45deg.
53+
const SIDE_ANGLES = [
5354
['to top', 0],
54-
['to top right', 45],
5555
['to right', 90],
56-
['to bottom right', 135],
5756
['to bottom', 180],
58-
['to bottom left', 225],
5957
['to left', 270],
60-
['to top left', 315],
6158
]
6259
6360
$effect(() => {
6461
if (dragulaState.rotating) return
6562
const val = Number($linear_angle)
6663
if (Number.isNaN(val)) return
67-
const match = NAMED_ANGLES.find(([_, deg]) => deg === ((val % 360) + 360) % 360)
64+
const match = SIDE_ANGLES.find(([_, deg]) => deg === ((val % 360) + 360) % 360)
6865
if (match && $linear_named_angle !== match[0]) {
6966
// Update named angle so UI reflects the keyword state
7067
$linear_named_angle = match[0]

‎src/store/linear.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { get } from 'svelte/store'
3+
import { linear_angle, linear_named_angle } from './linear'
4+
import { applyParsedToStores } from '../lib/importGradient'
5+
import { parseGradient } from '../lib/parseGradient'
6+
7+
const state = () => [get(linear_named_angle), String(get(linear_angle))]
8+
9+
describe('linear angle <-> keyword sync', () => {
10+
it('snaps exact side angles to their keyword', () => {
11+
linear_angle.set('10')
12+
expect(state()).toEqual(['--', '10'])
13+
linear_angle.set('90')
14+
expect(state()).toEqual(['to right', '90'])
15+
linear_angle.set('270')
16+
expect(state()).toEqual(['to left', '270'])
17+
})
18+
19+
it('keeps 45/135/225/315deg as angles: corner keywords depend on the box aspect', () => {
20+
for (const deg of ['45', '135', '225', '315']) {
21+
linear_named_angle.set('to right')
22+
linear_angle.set(deg)
23+
expect(state()).toEqual(['--', deg])
24+
}
25+
})
26+
27+
it('keeps a corner keyword restored alongside its nominal angle', () => {
28+
linear_named_angle.set('to top right')
29+
expect(state()).toEqual(['to top right', '45'])
30+
// layer/URL restore writes the keyword, then the angle
31+
linear_angle.set('45')
32+
expect(state()).toEqual(['to top right', '45'])
33+
linear_angle.set('46')
34+
expect(state()).toEqual(['--', '46'])
35+
})
36+
37+
it('imports `45deg` as 45deg, not `to top right`', () => {
38+
linear_named_angle.set('to right')
39+
applyParsedToStores(parseGradient('linear-gradient(45deg, red, blue)'))
40+
expect(state()).toEqual(['--', '45'])
41+
applyParsedToStores(parseGradient('linear-gradient(to top right, red, blue)'))
42+
expect(get(linear_named_angle)).toBe('to top right')
43+
})
44+
})

‎src/store/linear.ts‎

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { writable } from 'svelte/store';
1+
import { writable, get } from 'svelte/store';
22

33
export const linear_named_angle = writable('to right');
44
export const linear_angle = writable(null);
@@ -28,34 +28,29 @@ linear_named_angle.subscribe(v => {
2828
}
2929
})
3030

31-
// When the numeric angle matches a named direction, reflect it in the UI.
31+
// Only the side keywords are fixed angles (`to right` is exactly 90deg). The corner keywords
32+
// (`to top right`...) aim at the box's corner, so they only equal 45/135/225/315deg on a square:
33+
// a typed or imported 45deg must stay 45deg, or the output silently renders a different angle.
34+
const sideToDeg: Record<string, number> = {
35+
'to top': 0,
36+
'to right': 90,
37+
'to bottom': 180,
38+
'to left': 270,
39+
}
40+
41+
// When the numeric angle matches a side keyword, reflect it in the UI.
3242
linear_angle.subscribe(v => {
3343
if (syncing) return
3444
if (v == null) return
3545
const n = Number(v)
3646
if (Number.isNaN(n)) return
3747
// Normalize to [0,360)
38-
let a = ((n % 360) + 360) % 360
39-
const entries = Object.entries(nameToDeg)
40-
const tol = 0.5
41-
for (const [name, deg] of entries) {
42-
const d = Math.abs((((a - deg) % 360) + 540) % 360 - 180)
43-
if (d <= tol) {
44-
if (name !== 'to bottom' || deg !== 180) {
45-
syncing = true
46-
try { linear_named_angle.set(name) }
47-
finally { syncing = false }
48-
} else {
49-
// default direction; show it explicitly too
50-
syncing = true
51-
try { linear_named_angle.set('to bottom') }
52-
finally { syncing = false }
53-
}
54-
return
55-
}
56-
}
57-
// Not a named angle; show custom
48+
const a = ((n % 360) + 360) % 360
49+
// a corner keyword set alongside its nominal angle (layer/URL restore) stays as is
50+
const current = get(linear_named_angle)
51+
if (current && nameToDeg[current] === a) return
52+
const side = Object.keys(sideToDeg).find(name => sideToDeg[name] === a)
5853
syncing = true
59-
try { linear_named_angle.set('--') }
54+
try { linear_named_angle.set(side ?? '--') }
6055
finally { syncing = false }
6156
})

0 commit comments

Comments
 (0)