1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00
vault-frontend/src/hooks/dom/useFormatWrapper.ts
2025-01-24 17:51:59 +07:00

50 lines
1.1 KiB
TypeScript

import { useCallback } from 'react';
/** wraps text inside textarea with tags */
export const useFormatWrapper = (onChange: (val: string) => void) => {
return useCallback(
(
target: HTMLTextAreaElement,
prefix = '',
suffix = '',
) =>
(event) => {
event.preventDefault();
wrapTextInsideInput(target, prefix, suffix, onChange);
},
[onChange],
);
};
/** wraps text inside textarea with tags */
export const wrapTextInsideInput = (
target: HTMLTextAreaElement,
prefix: string,
suffix: string,
onChange: (val: string) => void,
) => {
if (!target) return;
const start = target.selectionStart;
const end = target.selectionEnd;
const selection = target.value.substring(start, end);
const replacement = prefix + selection + suffix;
onChange(
target.value.substring(0, start) +
replacement +
target.value.substring(end, target.value.length),
);
target.focus();
setTimeout(() => {
if (start === end) {
target.selectionEnd = end + prefix.length;
} else {
target.selectionEnd = end + prefix.length + suffix.length;
}
}, 0);
};