mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
added hotkeys for textarea formatter
This commit is contained in:
parent
a7d890aeec
commit
154a35c957
2 changed files with 78 additions and 29 deletions
|
@ -1,7 +1,7 @@
|
||||||
import React, { FC, useCallback } from 'react';
|
import React, { FC, useCallback, useEffect } from 'react';
|
||||||
import { ButtonGroup } from '~/components/input/ButtonGroup';
|
import { ButtonGroup } from '~/components/input/ButtonGroup';
|
||||||
import { Button } from '~/components/input/Button';
|
import { Button } from '~/components/input/Button';
|
||||||
import { useFormatWrapper } from '~/utils/hooks/useFormatWrapper';
|
import { useFormatWrapper, wrapTextInsideInput } from '~/utils/hooks/useFormatWrapper';
|
||||||
import styles from './styles.module.scss';
|
import styles from './styles.module.scss';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
|
@ -15,10 +15,51 @@ const CommentFormFormatButtons: FC<IProps> = ({ element, handler }) => {
|
||||||
[element, handler]
|
[element, handler]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const wrapBold = useCallback(
|
||||||
|
event => {
|
||||||
|
event.preventDefault();
|
||||||
|
wrapTextInsideInput(element, '**', '**', handler);
|
||||||
|
},
|
||||||
|
[wrap, handler]
|
||||||
|
);
|
||||||
|
|
||||||
|
const wrapItalic = useCallback(
|
||||||
|
event => {
|
||||||
|
event.preventDefault();
|
||||||
|
wrapTextInsideInput(element, '*', '*', handler);
|
||||||
|
},
|
||||||
|
[wrap, handler]
|
||||||
|
);
|
||||||
|
|
||||||
|
const onKeyPress = useCallback(
|
||||||
|
(event: KeyboardEvent) => {
|
||||||
|
if (!event.ctrlKey) return;
|
||||||
|
|
||||||
|
if (event.code === 'KeyB') {
|
||||||
|
wrapBold(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.code === 'KeyI') {
|
||||||
|
wrapItalic(event);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[wrapBold, wrapItalic]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!element) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
element.addEventListener('keypress', onKeyPress);
|
||||||
|
|
||||||
|
return () => element.removeEventListener('keypress', onKeyPress);
|
||||||
|
}, [element, onKeyPress]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ButtonGroup className={styles.wrap}>
|
<ButtonGroup className={styles.wrap}>
|
||||||
<Button
|
<Button
|
||||||
onClick={wrap('**', '**')}
|
onClick={wrapBold}
|
||||||
iconLeft="bold"
|
iconLeft="bold"
|
||||||
size="small"
|
size="small"
|
||||||
color="gray"
|
color="gray"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { MouseEventHandler, useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
export const useFormatWrapper = (
|
export const useFormatWrapper = (
|
||||||
target: HTMLTextAreaElement,
|
target: HTMLTextAreaElement,
|
||||||
|
@ -6,10 +6,21 @@ export const useFormatWrapper = (
|
||||||
prefix = '',
|
prefix = '',
|
||||||
suffix = ''
|
suffix = ''
|
||||||
) => {
|
) => {
|
||||||
return useCallback<MouseEventHandler>(
|
return useCallback(
|
||||||
event => {
|
event => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
wrapTextInsideInput(target, prefix, suffix, onChange);
|
||||||
|
},
|
||||||
|
[target, onChange, prefix, suffix]
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const wrapTextInsideInput = (
|
||||||
|
target: HTMLTextAreaElement,
|
||||||
|
prefix: string,
|
||||||
|
suffix: string,
|
||||||
|
onChange: (val: string) => void
|
||||||
|
) => {
|
||||||
if (!target) return;
|
if (!target) return;
|
||||||
|
|
||||||
const start = target.selectionStart;
|
const start = target.selectionStart;
|
||||||
|
@ -33,7 +44,4 @@ export const useFormatWrapper = (
|
||||||
target.selectionEnd = end + prefix.length + suffix.length;
|
target.selectionEnd = end + prefix.length + suffix.length;
|
||||||
}
|
}
|
||||||
}, 0);
|
}, 0);
|
||||||
},
|
|
||||||
[target, onChange, prefix, suffix]
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue