|
|
@@ -2,6 +2,8 @@ import { useCallback, useState } from 'react';
|
|
|
|
|
|
import { Collapse } from 'reactstrap';
|
|
|
|
|
|
+import { useCodeMirrorEditorIsolated } from '../../../stores';
|
|
|
+
|
|
|
|
|
|
type TogglarProps = {
|
|
|
isOpen: boolean,
|
|
|
@@ -25,26 +27,44 @@ const TextFormatToolsToggler = (props: TogglarProps): JSX.Element => {
|
|
|
);
|
|
|
};
|
|
|
|
|
|
-export const TextFormatTools = (): JSX.Element => {
|
|
|
+type TextFormatToolsType = {
|
|
|
+ editorKey: string,
|
|
|
+}
|
|
|
+
|
|
|
+export const TextFormatTools = (props: TextFormatToolsType): JSX.Element => {
|
|
|
+ const { editorKey } = props;
|
|
|
const [isOpen, setOpen] = useState(false);
|
|
|
+ const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(editorKey);
|
|
|
|
|
|
const toggle = useCallback(() => {
|
|
|
setOpen(bool => !bool);
|
|
|
}, []);
|
|
|
|
|
|
+ const view = codeMirrorEditor?.view;
|
|
|
+
|
|
|
+ const createReplaceSelectionHandler = useCallback((prefix: string, suffix: string) => {
|
|
|
+ return () => {
|
|
|
+ const insertText = view?.state.replaceSelection(`${prefix}${suffix}`);
|
|
|
+ if (insertText) {
|
|
|
+ view?.dispatch(insertText);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }, [view]);
|
|
|
+
|
|
|
+
|
|
|
return (
|
|
|
<div className="d-flex">
|
|
|
<TextFormatToolsToggler isOpen={isOpen} onClick={toggle} />
|
|
|
|
|
|
<Collapse isOpen={isOpen} horizontal>
|
|
|
<div className="d-flex px-1 gap-1" style={{ width: '220px' }}>
|
|
|
- <button type="button" className="btn btn-toolbar-button">
|
|
|
+ <button type="button" className="btn btn-toolbar-button" onClick={() => createReplaceSelectionHandler('**', '**')()}>
|
|
|
<span className="material-icons-outlined fs-5">format_bold</span>
|
|
|
</button>
|
|
|
<button type="button" className="btn btn-toolbar-button">
|
|
|
- <span className="material-icons-outlined fs-5">format_italic</span>
|
|
|
+ <span className="material-icons-outlined fs-5" onClick={() => createReplaceSelectionHandler('*', '*')()}>format_italic</span>
|
|
|
</button>
|
|
|
- <button type="button" className="btn btn-toolbar-button">
|
|
|
+ <button type="button" className="btn btn-toolbar-button" onClick={() => createReplaceSelectionHandler('~', '~')()}>
|
|
|
<span className="material-icons-outlined fs-5">format_strikethrough</span>
|
|
|
</button>
|
|
|
<button type="button" className="btn btn-toolbar-button">
|