All files / src/internal/client/dom/elements/bindings universal.js

100% Statements 77/77
100% Branches 16/16
100% Functions 4/4
100% Lines 75/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 762x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 10x 10x 12x 12x 12x 36x 36x 36x 23x 4x 4x 4x 23x 19x 19x 19x 23x 12x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 18x 18x 10x 10x 10x 10x 10x 6x 14x 14x 6x 6x 4x 4x 10x 10x 10x 4x 4x 4x 4x 10x 2x 2x 2x 2x 2x 2x 2x 4x 10x 4x 4x  
import { render_effect, teardown } from '../../../reactivity/effects.js';
import { listen } from './shared.js';
 
/**
 * @param {'innerHTML' | 'textContent' | 'innerText'} property
 * @param {HTMLElement} element
 * @param {() => unknown} get_value
 * @param {(value: unknown) => void} update
 * @returns {void}
 */
export function bind_content_editable(property, element, get_value, update) {
	element.addEventListener('input', () => {
		// @ts-ignore
		update(element[property]);
	});
 
	render_effect(() => {
		var value = get_value();
 
		if (element[property] !== value) {
			if (value === null) {
				// @ts-ignore
				var non_null_value = element[property];
				update(non_null_value);
			} else {
				// @ts-ignore
				element[property] = value + '';
			}
		}
	});
}
 
/**
 * @param {string} property
 * @param {string} event_name
 * @param {Element} element
 * @param {(value: unknown) => void} set
 * @param {() => unknown} [get]
 * @returns {void}
 */
export function bind_property(property, event_name, element, set, get) {
	var handler = () => {
		// @ts-ignore
		set(element[property]);
	};
 
	element.addEventListener(event_name, handler);
 
	if (get) {
		render_effect(() => {
			// @ts-ignore
			element[property] = get();
		});
	} else {
		handler();
	}
 
	// @ts-ignore
	if (element === document.body || element === window || element === document) {
		teardown(() => {
			element.removeEventListener(event_name, handler);
		});
	}
}
 
/**
 * @param {HTMLElement} element
 * @param {(value: unknown) => void} update
 * @returns {void}
 */
export function bind_focused(element, update) {
	listen(element, ['focus', 'blur'], () => {
		update(element === document.activeElement);
	});
}