From b360d8b50007d467cb7267459bbd9600c745424b Mon Sep 17 00:00:00 2001 From: "Arthur (@Refzlund)" Date: Thu, 23 Jan 2025 20:20:52 +0100 Subject: [PATCH] `.compute()` and types `Float` & `FloatingUI` --- floating-runes/build.ts | 3 +- floating-runes/src/floating-ui.svelte.ts | 17 +++++++- floating-runes/src/index.ts | 4 +- floating-runes/src/wait.svelte.ts | 49 ++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 floating-runes/src/wait.svelte.ts diff --git a/floating-runes/build.ts b/floating-runes/build.ts index 437833a..152a323 100644 --- a/floating-runes/build.ts +++ b/floating-runes/build.ts @@ -80,7 +80,8 @@ for (let [key, path] of Object.entries(json.exports) as [string, string][]) { path = path.replace(/^\.\/src/, './dist') json.exports[key] = { types: path.replace(/\.ts$/, '.d.ts'), - svelte: path.replace(/\.ts$/, '.js') + svelte: path.replace(/\.ts$/, '.js'), + default: path.replace(/\.ts$/, '.js') } } diff --git a/floating-runes/src/floating-ui.svelte.ts b/floating-runes/src/floating-ui.svelte.ts index 7f9bae6..c03cc7c 100644 --- a/floating-runes/src/floating-ui.svelte.ts +++ b/floating-runes/src/floating-ui.svelte.ts @@ -96,7 +96,7 @@ interface FloatOptions { * {/if} * ``` */ -export function floatingUI(options: FloatingRuneOptions = {}) { +function floatingUI(options: FloatingRuneOptions = {}) { /** */ const arrowMap = new WeakMap() const floatMap = new SvelteMap() @@ -242,6 +242,12 @@ export function floatingUI(options: FloatingRuneOptions = {}) { if (arrowMap.has(node.parentElement!)) arrowMap.delete(node.parentElement!) } } + }, + compute() { + if (!ref || floatMap.size === 0) return + for (const [float, floatOptions] of floatMap) { + compute(float, options, floatOptions) + } } } @@ -348,4 +354,11 @@ export function floatingUI(options: FloatingRuneOptions = {}) { type SvelteFloatingUI = typeof setFloat & typeof value return setFloat as SvelteFloatingUI -} \ No newline at end of file +} + +export interface Float extends ReturnType {} +export interface FloatingUI { + (options?: FloatingRuneOptions): Float +} + +export default floatingUI as FloatingUI \ No newline at end of file diff --git a/floating-runes/src/index.ts b/floating-runes/src/index.ts index 215dcb2..1d5bf16 100644 --- a/floating-runes/src/index.ts +++ b/floating-runes/src/index.ts @@ -1,6 +1,6 @@ -import { floatingUI } from './floating-ui.svelte.js' - +import { default as floatingUI } from './floating-ui.svelte.js' export * from './floating-ui.svelte.js' + export default floatingUI export * from './portal.svelte.js' \ No newline at end of file diff --git a/floating-runes/src/wait.svelte.ts b/floating-runes/src/wait.svelte.ts new file mode 100644 index 0000000..f3a585d --- /dev/null +++ b/floating-runes/src/wait.svelte.ts @@ -0,0 +1,49 @@ +let map = new WeakMap>() + +export const wait = (ms: number) => { + let anchor: Node | undefined + try { + anchor = eval('node') + } catch (error) { + console.error(error) + } + + console.log(anchor) + + if(anchor) { + let wait = map.get(anchor) + if(wait) { + return wait + } + } + + var awaited = $state(false) + let cancelled = $state(false) + let timeoutId: ReturnType | null = null + + const wait = { + get awaited() { + return awaited + }, + get cancelled() { return cancelled }, + cancel: () => { + cancelled = true + if (timeoutId) { + clearTimeout(timeoutId) + } + }, + promise: new Promise(res => { + timeoutId = setTimeout(() => { + if (cancelled) return + res(true) + console.log('true') + awaited = true + }, ms) + }) + } + + if(anchor) { + map.set(anchor, wait) + } + return wait +} \ No newline at end of file