|
|
@ -1,7 +1,5 @@
|
|
|
|
/**
|
|
|
|
/** @import { Action, ActionReturn } from 'svelte/action' */
|
|
|
|
* @import { Attachment } from "./public.js";
|
|
|
|
/** @import { Attachment } from 'svelte/attachments' */
|
|
|
|
* @import { ActionReturn } from "svelte/action";
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { noop, render_effect } from 'svelte/internal/client';
|
|
|
|
import { noop, render_effect } from 'svelte/internal/client';
|
|
|
|
import { ATTACHMENT_KEY } from '../constants.js';
|
|
|
|
import { ATTACHMENT_KEY } from '../constants.js';
|
|
|
|
import { untrack } from 'svelte';
|
|
|
|
import { untrack } from 'svelte';
|
|
|
@ -34,35 +32,41 @@ export function createAttachmentKey() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @template {EventTarget} [Element=HTMLElement]
|
|
|
|
* @template {EventTarget} E
|
|
|
|
* @template {*} [Par=unknown]
|
|
|
|
* @template {unknown} T
|
|
|
|
* @typedef {<Node extends Element, Parameter extends Par>(
|
|
|
|
* @overload
|
|
|
|
* ...args: (undefined extends NoInfer<Parameter>
|
|
|
|
* @param {Action<E, T> | function(E, T): void | ActionReturn<T>} action The action function
|
|
|
|
* ? [
|
|
|
|
* @param {() => T} fn A function that returns the argument for the action
|
|
|
|
* action: (node: Node, parameter?: never) => void | ActionReturn<Parameter>,
|
|
|
|
* @returns {Attachment<E>}
|
|
|
|
* parameter?: () => NoInfer<Parameter>
|
|
|
|
*/
|
|
|
|
* ]
|
|
|
|
/**
|
|
|
|
* : [
|
|
|
|
* @template {EventTarget} E
|
|
|
|
* action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter>,
|
|
|
|
* @overload
|
|
|
|
* parameter: () => NoInfer<Parameter>
|
|
|
|
* @param {Action<E, void> | function(E): void | ActionReturn<void>} action The action function
|
|
|
|
* ])
|
|
|
|
* @returns {Attachment<E>}
|
|
|
|
* ) => Attachment<Node>} FromAction
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Converts an Action into an Attachment keeping the same behavior. It's useful if you want to start using
|
|
|
|
* Converts an Action into an Attachment keeping the same behavior. It's useful if you want to start using
|
|
|
|
* attachments on Components but you have library provided actions.
|
|
|
|
* attachments on Components but you have library provided actions.
|
|
|
|
* @type {FromAction}
|
|
|
|
*
|
|
|
|
|
|
|
|
* Note that the second argument, if provided, must be a function that _returns_ the argument to the
|
|
|
|
|
|
|
|
* action function, not the argument itself.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @template {EventTarget} E
|
|
|
|
|
|
|
|
* @template {unknown} T
|
|
|
|
|
|
|
|
* @param {Action<E, T> | function(E, T): void | ActionReturn<T>} action The action function
|
|
|
|
|
|
|
|
* @param {() => T} fn A function that returns the argument for the action
|
|
|
|
|
|
|
|
* @returns {Attachment<E>}
|
|
|
|
* @since 5.32
|
|
|
|
* @since 5.32
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function fromAction(action, /** @type {() => any} */ get_arg = noop) {
|
|
|
|
export function fromAction(action, fn = /** @type {() => T} */ (noop)) {
|
|
|
|
return (element) => {
|
|
|
|
return (element) => {
|
|
|
|
const { update, destroy } = untrack(() => action(element, get_arg()) ?? {});
|
|
|
|
const { update, destroy } = untrack(() => action(element, fn()) ?? {});
|
|
|
|
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
if (update) {
|
|
|
|
var ran = false;
|
|
|
|
var ran = false;
|
|
|
|
render_effect(() => {
|
|
|
|
render_effect(() => {
|
|
|
|
const arg = get_arg();
|
|
|
|
const arg = fn();
|
|
|
|
if (ran) update(arg);
|
|
|
|
if (ran) update(arg);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
ran = true;
|
|
|
|
ran = true;
|
|
|
|