X-Git-Url: https://git.r.bdr.sh/rbdr/canvas/blobdiff_plain/c30e688113e317ff6aca8a63f16b202cbfff3820..d7cea96940ddeb764c7343e05526f611863019d8:/src/lib/stores/widgets.js?ds=inline diff --git a/src/lib/stores/widgets.js b/src/lib/stores/widgets.js index ff5c53e..4da94ff 100644 --- a/src/lib/stores/widgets.js +++ b/src/lib/stores/widgets.js @@ -3,17 +3,44 @@ import { createClient } from '@supabase/supabase-js'; import { supabase } from '$lib/config'; import { maxSize } from '$lib/config'; +const boxParser = /\(([0-9]+),([0-9]+)\),\(([0-9]+),([0-9]+)\)/ const client = createClient(supabase.url, supabase.key); export const sensor = writable({left: 0, top: 0, right: 0, bottom: 0}); const getBoxes = function getBoxes ({left, top, right, bottom}) { - return [ - `box.ov."(${left},${top},${right},${bottom})"`, - `box.ov."(${left+maxSize},${top+maxSize},${right+maxSize},${bottom+maxSize})"`, - `box.ov."(${left+maxSize},${top},${right+maxSize},${bottom})"`, - `box.ov."(${left},${top+maxSize},${right},${bottom+maxSize})"` - ] + const results = [ + `box.ov."((${left},${top}),(${right},${bottom}))"`, + `box.ov."((${left+maxSize},${top+maxSize}),(${right+maxSize},${bottom+maxSize}))"`, + `box.ov."((${left+maxSize},${top}),(${right+maxSize},${bottom}))"`, + `box.ov."((${left},${top+maxSize}),(${right},${bottom+maxSize}))"` + ]; + + if (right > maxSize || bottom > maxSize) { + return [...results, + `box.ov."((${left-maxSize},${top-maxSize}),(${right-maxSize},${bottom-maxSize}))"`, + `box.ov."((${left-maxSize},${top}),(${right-maxSize},${bottom}))"`, + `box.ov."((${left},${top-maxSize}),(${right},${bottom-maxSize}))"` + ]; + } + + return results +}; + +const serialize = function serialize(widget) { + + const boxComponents = widget.box + .match(boxParser) + .slice(1,5) + .map(Number); + const box = { + left: Math.min(boxComponents[0], boxComponents[2]), + right: Math.max(boxComponents[0], boxComponents[2]), + top: Math.min(boxComponents[1], boxComponents[3]), + bottom: Math.max(boxComponents[1], boxComponents[3]) + }; + + return {...widget, box } }; let ac = null; @@ -27,5 +54,7 @@ export const widgets = derived(sensor, async function ($sensor, set) { .select() .or(boxes.join(',')) .abortSignal(ac.signal) - return set(data); + if (data) { + return set(data.map(serialize)); + } });