diff --git a/docs/logo.png b/docs/logo.png new file mode 100644 index 0000000..8325900 Binary files /dev/null and b/docs/logo.png differ diff --git a/src/App.tsx b/src/App.tsx index d4f294a..5243c17 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -13,6 +13,10 @@ import UploadMarkdown from "./uploadMarkdown"; import UploadImage from "./uploadImage"; type positionInfo = null | { top: number; left: number }; +type TabAreaArgs = { + e: React.KeyboardEvent; + ref: React.MutableRefObject; +}; export default function App() { const [markdown, setMarkdown] = useState(""); @@ -29,6 +33,29 @@ export default function App() { const textAreaRef = useRef(null); const inputRef = useRef(null); const previewRef = useRef(null); + const [focusedElement, setFocusedElement] = useState< + "input" | "textarea" | "textarea2" | null + >(null); + + const handleTabArea = ({ e, ref }: TabAreaArgs) => { + if (e.key !== "Tab") return; // Tabキー以外は処理しない + + e.preventDefault(); // デフォルトのタブ挿入動作を止める + + if (!ref.current) return; + + const textarea = ref.current; + const cursorPosition = textarea.selectionStart; + const cursorLeft = textarea.value.substring(0, cursorPosition); + const cursorRight = textarea.value.substring(cursorPosition); + + // タブの代わりに半角スペース3つを挿入 + const tabSpaces = " "; + textarea.value = cursorLeft + tabSpaces + cursorRight; + + // カーソルを3文字分進める + textarea.selectionStart = textarea.selectionEnd = cursorPosition + 3; + }; // 2/3追加・テンプレート部分 @@ -123,15 +150,16 @@ export default function App() { }; const insertDollarSignsAtCursor = (command: string) => { - if (inputRef.current) { + console.log(focusedElement); + if (focusedElement === "input" && inputRef.current) { const input = inputRef.current; - console.log(input.selectionStart, input.selectionEnd); const start = input.selectionStart ?? 0; const end = input.selectionEnd ?? 0; const newText = `${inputValue.slice(0, start)}$$ \n ${command} \n $$${inputValue.slice(end)}`; setInputValue(newText); - } else if (textAreaRef.current) { - // textAreaRef.currentがnullでないことを確認 + } + + if (focusedElement === "textarea" && textAreaRef.current) { const textarea = textAreaRef.current; const start = textarea.selectionStart; const end = textarea.selectionEnd; @@ -142,6 +170,18 @@ export default function App() { textarea.focus(); }, 0); } + + if (focusedElement === "textarea2" && inputRef.current) { + const input = inputRef.current; + const start = input.selectionStart ?? 0; + const end = input.selectionEnd ?? 0; + const newText = `${fixedInputValue.slice(0, start)}$$ \n ${command} \n $$${fixedInputValue.slice(end)}`; + setFixedInputValue(newText); + setTimeout(() => { + input.selectionStart = input.selectionEnd = start + 3; + input.focus(); + }, 0); + } }; const saveFile = () => { @@ -227,6 +267,13 @@ export default function App() { value={fixedInputValue} ref={inputRef} onChange={handleFixedInputChange} + onFocus={() => { + setIsTextAreaFocused(true); + setFocusedElement("textarea2"); + }} + onBlur={() => { + setIsTextAreaFocused(false); + }} />