Skip to content

Commit

Permalink
Merge pull request #373 from boostcampwm-2022/feature/#354-image-click
Browse files Browse the repository at this point in the history
인기 태그 레이아웃 수정 및 줄 수 탭 기능 추가
  • Loading branch information
NaamuKim authored Dec 8, 2022
2 parents 7c68cf2 + 797c25f commit 96087fc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
5 changes: 0 additions & 5 deletions client/src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
@import "@/styles/global-style";
@import "@/styles/theme";

* {
padding: 0;
margin: 0;
}

html,
body {
height: 100%;
Expand Down
5 changes: 3 additions & 2 deletions client/src/components/main/CodeEditor/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import CodeViewer from "@/components/main/CodeViewer/CodeViewer";
import "./CodeEditor.scss";

const CodeEditor = (): JSX.Element => {
const { code, handleCodeChange, language, lineCount } = useCodeEditor();
const { lineRef, textRef, preRef, handleScrollChange } = useEditorScroll();

const { code, language, lineCount, handleCodeChange, handleKeyDown } =
useCodeEditor(textRef);
useEffect(() => {
textRef.current?.focus();
}, []);
Expand All @@ -25,6 +25,7 @@ const CodeEditor = (): JSX.Element => {
ref={textRef}
onScroll={handleScrollChange}
onChange={handleCodeChange}
onKeyDown={handleKeyDown}
value={code}
className="code__textarea"
autoComplete="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const ReviewModal = ({
<div className="review-modal">
<div className="review-modal__code">
<div className="review-modal__code--lines" ref={lineRef}>
{Array.from(Array(getLineCount(code) + 1).keys())
{Array.from(Array(getLineCount(code)).keys())
.slice(1)
.join("\n")}
</div>
Expand Down
31 changes: 29 additions & 2 deletions client/src/hooks/useCodeEditor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ChangeEvent, useCallback, useEffect, useState } from "react";
import {
ChangeEvent,
KeyboardEvent,
useCallback,
useEffect,
useState,
RefObject,
} from "react";
import domtoimage from "dom-to-image";

import { ONE_SNAPSHOT_LINE_COUNT } from "@/constants/code";
Expand All @@ -10,10 +17,13 @@ interface UseCodeEditor {
code: string;
language: string;
handleCodeChange: (e: ChangeEvent<HTMLTextAreaElement>) => void;
handleKeyDown: (e: KeyboardEvent<HTMLTextAreaElement>) => void;
lineCount: number;
}

const useCodeEditor = (): UseCodeEditor => {
const useCodeEditor = (
textAreaRef: RefObject<HTMLTextAreaElement>
): UseCodeEditor => {
const { code, setCode, language, images, setImages, removeImage } =
useCodeEditorStore((state) => ({
code: state.code,
Expand All @@ -25,6 +35,22 @@ const useCodeEditor = (): UseCodeEditor => {
}));
const [lineCount, setLineCount] = useState(0);

const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>): void => {
if (e.key === "Tab") {
if (textAreaRef.current !== null) {
e.preventDefault();
const start = textAreaRef.current.selectionStart;
const end = textAreaRef.current.selectionEnd;
const value = code.substring(0, start) + " " + code.substring(end);
textAreaRef.current.value = value;
textAreaRef.current.selectionStart = textAreaRef.current.selectionEnd =
end + 2 - (end - start);
/* 커서가 뒤로 먼저 가지 않기 위해 value 먼저 변경하고 setCode 실행합니다. */
setCode(value);
}
}
};

const handleCodeChange = useCallback(
(e: ChangeEvent<HTMLTextAreaElement>) => {
setCode(e.target.value);
Expand Down Expand Up @@ -86,6 +112,7 @@ const useCodeEditor = (): UseCodeEditor => {
handleCodeChange,
language,
lineCount,
handleKeyDown,
};
};

Expand Down

0 comments on commit 96087fc

Please sign in to comment.