Skip to content

Commit 8a5950d

Browse files
committed
Add id to patterns
1 parent 4096096 commit 8a5950d

7 files changed

Lines changed: 25 additions & 25 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"check-format": "prettier --check ./src",
1515
"test": "vitest run --passWithNoTests",
1616
"test:watch": "vitest --watch",
17-
"check": "pnpm run lint && pnpm run check-ts && pnpm run find-unused && pnpm run check-format && pnpm run test"
17+
"check": "pnpm run check-lint && pnpm run check-ts && pnpm run find-unused && pnpm run check-format && pnpm run test"
1818
},
1919
"dependencies": {
2020
"@tailwindcss/vite": "^4.1.7",

src/App.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
findClickedScreenOrPattern,
77
getMousePoint,
88
getRelativePatternPosition,
9+
randomId,
910
} from './functions'
1011
import { useStableFunction } from './hooks'
1112
import { Preview } from './preview'
@@ -30,10 +31,10 @@ const getDraftState = (state: State, draftClick: DraftClick, mousePosition: Abso
3031

3132
let newDraft = getRelativePatternPosition(draftPatternRelative, state.screens[screenIndex]!)
3233
for (const k of nestedPath) {
33-
newDraft = getRelativePatternPosition(newDraft, state.patterns[k]!)
34+
newDraft = getRelativePatternPosition(newDraft, state.patterns[k]!.pattern)
3435
}
3536

36-
return { ...state, patterns: state.patterns.concat(newDraft) }
37+
return { ...state, patterns: state.patterns.concat({ id: randomId(), pattern: newDraft }) }
3738
}
3839

3940
type DraftClick = {
@@ -187,19 +188,6 @@ function App() {
187188

188189
const mousePoint = getMousePoint(ctx, e)
189190

190-
console.log(
191-
'pointerdown',
192-
e.clientX,
193-
e.width,
194-
e.pageX,
195-
e.screenX,
196-
e.movementX,
197-
ctx.canvas.width,
198-
ctx.canvas.clientWidth,
199-
e,
200-
getMousePoint(ctx, e)
201-
)
202-
203191
// create draft screen based on current cursor position
204192
setDraftClick({
205193
anchor: mousePoint,

src/draw/patterns.worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ self.onmessage = event => {
7777
function generatePatterns() {
7878
for (const { depth, patterns } of streamBatchedDrawablePatterns({
7979
state: {
80-
patterns: [{ anchor: [0, 0], target: [0.5, 0.5] } as RelativePattern],
80+
patterns: [{ id: 'debug', pattern: { anchor: [0, 0], target: [0.5, 0.5] } as RelativePattern }],
8181
screens: [{ anchor: [0.2, 0.2], target: [0.8, 0.8] } as AbsolutePattern],
8282
},
8383
chunkSize: 5,

src/draw/stream-drawable-patterns.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function* streamDrawablePatterns({
9797
break
9898
}
9999

100-
for (const pattern of state.patterns) {
100+
for (const { pattern } of state.patterns) {
101101
// Get new pattern
102102
const newViewportPattern = combinePatterns(entry.currentPattern, pattern)
103103

src/functions.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ export const findClickedScreenOrPattern = (
247247
let best: ClickedPath | undefined = undefined
248248
for (let i = 0; i < screens.length; i++) {
249249
const screen = screens[i]!
250-
const clickedPath = findClickedPattern(screen, patterns, point)
250+
const clickedPath = findClickedPattern(
251+
screen,
252+
patterns.map(p => p.pattern),
253+
point
254+
)
251255

252256
if (clickedPath !== undefined) {
253257
if (best === undefined || clickedPath.length > best.nestedPath.length) {
@@ -269,3 +273,8 @@ export const findClickedScreenOrPattern = (
269273

270274
return best
271275
}
276+
277+
export function randomId(): string {
278+
// todo: use uuid or something
279+
return Math.random().toString(36).substring(2, 15)
280+
}

src/preview.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function getViewBox(state: State): string {
99
let yMax = 1
1010

1111
// Make sure that default viewBox contains entire pattern
12-
for (const p of state.patterns) {
13-
const b = getBoundariesFromPattern(p)
12+
for (const { pattern } of state.patterns) {
13+
const b = getBoundariesFromPattern(pattern)
1414

1515
xMin = Math.min(xMin, b.xMin)
1616
xMax = Math.max(xMax, b.xMax)
@@ -71,8 +71,8 @@ export const Preview: React.FC<{ state: State }> = ({ state }) => {
7171
/>
7272
<PartialLine anchor={[0, 0]} target={[1, 1]} />
7373

74-
{state.patterns.map((p, i) => {
75-
const { xMin, xMax, yMin, yMax } = getBoundariesFromPattern(p)
74+
{state.patterns.map(({ pattern }, i) => {
75+
const { xMin, xMax, yMin, yMax } = getBoundariesFromPattern(pattern)
7676

7777
const width = xMax - xMin
7878
const height = yMax - yMin
@@ -90,7 +90,7 @@ export const Preview: React.FC<{ state: State }> = ({ state }) => {
9090
height={height.toFixed(3)}
9191
/>
9292
{/* Partial line from anchor to target in order to show the direction */}
93-
<PartialLine anchor={p.anchor} target={p.target} />
93+
<PartialLine anchor={pattern.anchor} target={pattern.target} />
9494

9595
{/* Click surfaces for rotation and resizing actions */}
9696
{[

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,8 @@ export type State = {
8484
screens: AbsolutePattern[]
8585

8686
/** These are nested patterns. They are applied recursively to root-level patterns.*/
87-
patterns: RelativePattern[]
87+
patterns: {
88+
id: string
89+
pattern: RelativePattern
90+
}[]
8891
}

0 commit comments

Comments
 (0)