Atsu
Atsu
NNuxt
Created by Atsu on 9/15/2024 in #❓・help
VueUse - useDraggable with dynamic list
I've got it to work with this:
<script setup lang="ts" generic="T extends {
id: string
}">


const container = useTemplateRef<HTMLDivElement>('containerRef')

const props = defineProps<{
fragments: T[]
}>()


const refs = computed(() => {
return props.fragments.map((fragment) => ({
fragment: fragment,
ref: ref<HTMLElement>(),
}));
});


type Draggable = {
position: globalThis.Ref<{
x: number;
y: number;
}>;
isDragging: globalThis.ComputedRef<boolean>;
style: globalThis.ComputedRef<string>;
x: globalThis.Ref<number>;
y: globalThis.Ref<number>;
}

const draggables = ref<Record<string, Draggable>>({})



watch(refs, async (newRefs, oldRefs) => {
await nextTick()
const old = oldRefs ?? []
// check all old refs and see if any of them are not in the new refs
const removedRefs = old.filter((oldRef) => !newRefs.includes(oldRef))
const addedRefs = newRefs.filter((newRef) => !old.includes(newRef))

removedRefs.forEach((ref) => {
delete draggables.value[ref.fragment.id]
})

addedRefs.forEach((ref) => {
draggables.value[ref.fragment.id] = useDraggable(ref.ref, {
containerElement: container,
})
})


}, {
immediate: true
})


</script>

<template>
<div ref="containerRef" class="w-full h-full bg-green-300 relative">
<div v-for="(fragment, index) in fragments" :key="fragment.id" class="absolute" :ref="(el) => refs[index]!.ref.value = el as HTMLElement" :style="(draggables[fragment.id]?.style as any as string)">
<slot :fragment="fragment" :drag="draggables[index]!" />
</div>
</div>
</template>
<script setup lang="ts" generic="T extends {
id: string
}">


const container = useTemplateRef<HTMLDivElement>('containerRef')

const props = defineProps<{
fragments: T[]
}>()


const refs = computed(() => {
return props.fragments.map((fragment) => ({
fragment: fragment,
ref: ref<HTMLElement>(),
}));
});


type Draggable = {
position: globalThis.Ref<{
x: number;
y: number;
}>;
isDragging: globalThis.ComputedRef<boolean>;
style: globalThis.ComputedRef<string>;
x: globalThis.Ref<number>;
y: globalThis.Ref<number>;
}

const draggables = ref<Record<string, Draggable>>({})



watch(refs, async (newRefs, oldRefs) => {
await nextTick()
const old = oldRefs ?? []
// check all old refs and see if any of them are not in the new refs
const removedRefs = old.filter((oldRef) => !newRefs.includes(oldRef))
const addedRefs = newRefs.filter((newRef) => !old.includes(newRef))

removedRefs.forEach((ref) => {
delete draggables.value[ref.fragment.id]
})

addedRefs.forEach((ref) => {
draggables.value[ref.fragment.id] = useDraggable(ref.ref, {
containerElement: container,
})
})


}, {
immediate: true
})


</script>

<template>
<div ref="containerRef" class="w-full h-full bg-green-300 relative">
<div v-for="(fragment, index) in fragments" :key="fragment.id" class="absolute" :ref="(el) => refs[index]!.ref.value = el as HTMLElement" :style="(draggables[fragment.id]?.style as any as string)">
<slot :fragment="fragment" :drag="draggables[index]!" />
</div>
</div>
</template>
It is somewhat clunky but it works.
2 replies
NNuxt
Created by Renato L. on 8/26/2024 in #❓・help
Will Nuxt Auth Utils work with Serverless?
Im using Deno and it works!
2 replies
NNuxt
Created by Atsu on 8/23/2024 in #❓・help
Session and Tokens best practices - nuxt-auth-utils
I'm just not really sure what the workflow is with refresh tokens and so on
5 replies
NNuxt
Created by Atsu on 8/23/2024 in #❓・help
Session and Tokens best practices - nuxt-auth-utils
nuxt-auth-utils provides sealed cookies sessions composables: https://github.com/atinux/nuxt-auth-utils?tab=readme-ov-file#server-utils
5 replies
NNuxt
Created by Atsu on 7/29/2024 in #❓・help
Share zod schema with client and server
Yes i am doing it this way now. Thank you guys very much :)
7 replies
NNuxt
Created by Atsu on 7/29/2024 in #❓・help
Share zod schema with client and server
This should work I read somewhere that only files in the server directory will be included but I haven't had the time to check yet
7 replies
NNuxt
Created by Atsu on 7/29/2024 in #❓・help
Share zod schema with client and server
Maybe my mental model is not accurate, so please correct me if I'm wrong: When JS imports a module, it loads the entire file. So, for example, if I have a schema in a server API route and import that schema to the frontend in a composable, the user could access the code for the API handler. Obviously, there shouldn't be anything sensitive in the handler itself, but the user could get a look into the handler and maybe exploit it somehow.
7 replies