]> git.r.bdr.sh - rbdr/canvas/blame - src/lib/stores/widgets.js
Partially render the boxes
[rbdr/canvas] / src / lib / stores / widgets.js
CommitLineData
c30e6881
RBR
1import { derived, writable } from 'svelte/store';
2import { createClient } from '@supabase/supabase-js';
3import { supabase } from '$lib/config';
4import { maxSize } from '$lib/config';
5
d7cea969 6const boxParser = /\(([0-9]+),([0-9]+)\),\(([0-9]+),([0-9]+)\)/
c30e6881
RBR
7const client = createClient(supabase.url, supabase.key);
8
9export const sensor = writable({left: 0, top: 0, right: 0, bottom: 0});
10
11const getBoxes = function getBoxes ({left, top, right, bottom}) {
d7cea969
RBR
12 const results = [
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 ];
18
19 if (right > maxSize || bottom > maxSize) {
20 return [...results,
21 `box.ov."((${left-maxSize},${top-maxSize}),(${right-maxSize},${bottom-maxSize}))"`,
22 `box.ov."((${left-maxSize},${top}),(${right-maxSize},${bottom}))"`,
23 `box.ov."((${left},${top-maxSize}),(${right},${bottom-maxSize}))"`
24 ];
25 }
26
27 return results
28};
29
30const serialize = function serialize(widget) {
31
32 const boxComponents = widget.box
33 .match(boxParser)
34 .slice(1,5)
35 .map(Number);
36 const box = {
37 left: Math.min(boxComponents[0], boxComponents[2]),
38 right: Math.max(boxComponents[0], boxComponents[2]),
39 top: Math.min(boxComponents[1], boxComponents[3]),
40 bottom: Math.max(boxComponents[1], boxComponents[3])
41 };
42
43 return {...widget, box }
c30e6881
RBR
44};
45
46let ac = null;
47export const widgets = derived(sensor, async function ($sensor, set) {
48
49 const boxes = getBoxes($sensor);
50 ac && ac.abort()
51 ac = new AbortController();
52 const { data } = await client
53 .from('widgets')
54 .select()
55 .or(boxes.join(','))
56 .abortSignal(ac.signal)
d7cea969
RBR
57 if (data) {
58 return set(data.map(serialize));
59 }
c30e6881 60});