1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

added format buttons

This commit is contained in:
Fedor Katurov 2021-02-26 14:54:40 +07:00
parent 3fdbf6208b
commit 5cf3d22466
9 changed files with 158 additions and 15 deletions

View file

@ -0,0 +1,39 @@
import { MouseEventHandler, useCallback } from 'react';
export const useFormatWrapper = (
target: HTMLTextAreaElement,
onChange: (val: string) => void,
prefix = '',
suffix = ''
) => {
return useCallback<MouseEventHandler>(
event => {
event.preventDefault();
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);
},
[target, onChange, prefix, suffix]
);
};