obsidian-dev-utils
Version:
This is the collection of useful functions that you can use for your Obsidian plugin development
123 lines (122 loc) • 4.55 kB
text/typescript
/**
* @packageDocumentation
*
* Helpers for working with HTML elements.
*/
import type { Promisable } from 'type-fest';
/**
* A HTML element that can be validated.
*/
export interface ValidatorElement extends HTMLElement {
/**
* Checks the validity of the element.
*
* @returns True if the element is valid, false otherwise.
*/
checkValidity(): boolean;
/**
* Reports the validity of the element.
*/
reportValidity(): boolean;
/**
* Sets a custom error message on the element.
*
* @param error - The error message to set on the element.
*/
setCustomValidity(error: string): void;
/**
* An error message of the element.
*/
readonly validationMessage: string;
}
/**
* Appends a code block to the given DocumentFragment or HTMLElement.
*
* @param el - The DocumentFragment or HTMLElement to append the code block to.
* @param code - The code to be displayed in the code block.
*/
export declare function appendCodeBlock(el: DocumentFragment | HTMLElement, code: string): void;
/**
* Creates a div asynchronously.
*
* @param o - The element information.
* @param callback - The callback to call when the div is created.
* @returns A {@link Promise} that resolves to the div.
*/
export declare function createDivAsync(o?: DomElementInfo | string, callback?: (el: HTMLDivElement) => Promisable<void>): Promise<HTMLDivElement>;
/**
* Creates an element asynchronously.
*
* @param tag - The tag name of the element to create.
* @param o - The element information.
* @param callback - The callback to call when the element is created.
* @returns A {@link Promise} that resolves to the element.
*/
export declare function createElAsync<K extends keyof HTMLElementTagNameMap>(tag: K, o?: DomElementInfo | string, callback?: (el: HTMLElementTagNameMap[K]) => Promisable<void>): Promise<HTMLElementTagNameMap[K]>;
/**
* Creates a DocumentFragment asynchronously.
*
* @param callback - The callback to call when the DocumentFragment is created.
* @returns A {@link Promise} that resolves to the DocumentFragment.
*/
export declare function createFragmentAsync(callback?: (el: DocumentFragment) => Promisable<void>): Promise<DocumentFragment>;
/**
* Creates a span asynchronously.
*
* @param o - The element information.
* @param callback - The callback to call when the span is created.
* @returns A {@link Promise} that resolves to the span.
*/
export declare function createSpanAsync(o?: DomElementInfo | string, callback?: (el: HTMLSpanElement) => Promisable<void>): Promise<HTMLSpanElement>;
/**
* Creates a svg asynchronously.
*
* @param tag - The tag name of the svg to create.
* @param o - The svg information.
* @param callback - The callback to call when the svg is created.
* @returns A {@link Promise} that resolves to the svg.
*/
export declare function createSvgAsync<K extends keyof SVGElementTagNameMap>(tag: K, o?: string | SvgElementInfo, callback?: (el: SVGElementTagNameMap[K]) => Promisable<void>): Promise<SVGElementTagNameMap[K]>;
/**
* Ensures that the given element is loaded.
*
* @param el - The element to ensure is loaded.
* @returns A {@link Promise} that resolves when the element is loaded.
*/
export declare function ensureLoaded(el: Element): Promise<void>;
/**
* Gets the z-index of the given element.
*
* @param el - The element to get the z-index of.
* @returns The z-index of the element.
*/
export declare function getZIndex(el: Element): number;
/**
* Checks if the element is visible in the offset parent.
*
* @param el - The element to check.
* @returns True if the element is visible in the offset parent, false otherwise.
*/
export declare function isElementVisibleInOffsetParent(el: HTMLElement): boolean;
/**
* Checks if the element is loaded.
*
* @param el - The element to check.
* @returns True if the element is loaded, false otherwise.
*/
export declare function isLoaded(el: Element): boolean;
/**
* Adds an event listener to the ancestor nodes of the given node.
*
* @param node - The node to add the event listener to.
* @param callback - The callback to call when the event is triggered.
* @returns A function to remove the event listener.
*/
export declare function onAncestorScrollOrResize(node: Node, callback: () => void): () => void;
/**
* Converts a number to a string with 'px' appended.
*
* @param value - The number to convert.
* @returns The number as a string with 'px' appended.
*/
export declare function toPx(value: number): string;