]>
git.r.bdr.sh - rbdr/canvas/blob - src/lib/stores/widgets.js
1 import { derived
, readable
, writable
} from 'svelte/store';
2 import { createClient
} from '@supabase/supabase-js';
3 import { supabase
} from '$lib/config';
4 import { maxSize
} from '$lib/config';
6 const boxParser
= /\(([0-9]+),([0-9]+)\),\(([0-9]+),([0-9]+)\)/;
7 const client
= createClient(supabase
.url
, supabase
.key
);
9 export const sensor
= writable({ left: 0, top: 0, right: 0, bottom: 0 });
11 const getBoxes
= function getBoxes({ left
, top
, right
, bottom
}) {
13 `box.ov."((${left},${top}),(${right},${bottom}))"`,
14 `box.ov."((${left + maxSize},${top + maxSize}),(${right + maxSize},${bottom + maxSize}))"`,
15 `box.ov."((${left + maxSize},${top}),(${right + maxSize},${bottom}))"`,
16 `box.ov."((${left},${top + maxSize}),(${right},${bottom + maxSize}))"`,
17 `box.ov."((${left - maxSize},${top - maxSize}),(${right - maxSize},${bottom - maxSize}))"`,
18 `box.ov."((${left - maxSize},${top}),(${right - maxSize},${bottom}))"`,
19 `box.ov."((${left},${top - maxSize}),(${right},${bottom - maxSize}))"`
25 const serialize
= function serialize(widget
) {
26 const boxComponents
= widget
.box
.match(boxParser
).slice(1, 5).map(Number
);
28 left: Math
.min(boxComponents
[0], boxComponents
[2]),
29 right: Math
.max(boxComponents
[0], boxComponents
[2]),
30 top: Math
.min(boxComponents
[1], boxComponents
[3]),
31 bottom: Math
.max(boxComponents
[1], boxComponents
[3])
34 return { ...widget
, box
};
38 export const widgets
= derived(sensor
, async
function ($sensor
, set) {
39 const boxes
= getBoxes($sensor
);
41 ac
= new AbortController();
42 const { data
} = await client
.from('widgets').select().or(boxes
.join(',')).abortSignal(ac
.signal
);
44 return set(data
.map(serialize
));
48 export const countElements
= function countElements(left
, top
, right
, bottom
) {
50 return readable(0, (set) => {
52 countAc
&& countAc
.abort();
53 countAc
= new AbortController();
54 const { data
} = await client
56 .select('*', { head: true, count: 'estimated' })
57 .or(`box.ov."((${left},${top}),(${right},${bottom}))"`)
58 .abortSignal(countAc
.signal
);