Skip to content

Instantly share code, notes, and snippets.

@channyeintun
channyeintun / showPositionMarker.js
Created April 15, 2022 14:33 — forked from jh3y/showPositionMarker.js
show text cursor position marker
/**
* shows a position marker that highlights where the cursor is
* @param {object} e - the input or click event that has been fired
*/
const showPositionMarker = e => {
// grab the input element
const { currentTarget: input } = e
// create a function that will handle clicking off of the input and hide the marker
const processClick = evt => {
if (e !== evt && evt.target !== e.target) {
@channyeintun
channyeintun / getCursorXY.js
Created April 15, 2022 14:30 — forked from jh3y/getCursorXY.js
get text cursor position
/**
* returns x, y coordinates for absolute positioning of a span within a given text input
* at a given selection point
* @param {object} input - the input element to obtain coordinates for
* @param {number} selectionPoint - the selection point for the input
*/
const getCursorXY = (input, selectionPoint) => {
const {
offsetLeft: inputX,
offsetTop: inputY,
@channyeintun
channyeintun / eventIterator.js
Created September 2, 2021 05:28 — forked from jkempff/eventIterator.js
Async generator for DOM events
async function* eventIterator(element, eventName, subscribed) {
while(true) {
yield new Promise(resolve => {
const listener = e => {
element.removeEventListener(eventName, listener);
resolve(e);
};
element.addEventListener(eventName, listener);
})
@channyeintun
channyeintun / EventRecorder.js
Created September 2, 2021 05:28 — forked from jkempff/EventRecorder.js
Records DOM-events and replays them on demand. Nice for server side rendered pages: record during page-load, replay after all javascript was initialized.
/**
* EventRecorder
* @class
* @classdesc An EventRecorder can record and replay any event on any DOM node
* @param {string} [eventName=click] - Name of the events to record
* @param {class} [EventClass=MouseEvent] - The class that should be used to recreate the events
* @param {object} [context=self] - The context DOM element, the events should be fetched from
* @example
* // Create a recorder for click events
* const clickRecorder = new EventRecorder('click', MouseEvent, window);