Skip to content

Commit 39dc667

Browse files
committed
Merge branch 'dev-frontend' of https://github.com/boostcamp-2020/Project18-C-Bootion into feat/118
2 parents 5ad506a + 2a3d3c8 commit 39dc667

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

frontend/src/components/atoms/BlockContent/BlockContent.tsx

+55-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { jsx, css, SerializedStyles } from '@emotion/react';
44
import { useEffect, useRef, FormEvent, KeyboardEvent } from 'react';
55
import { useRecoilState, useRecoilValue } from 'recoil';
66

7-
import { blockMapState, blockRefState, throttleState } from '@/stores';
7+
import { blockRefState, throttleState, blockMapState } from '@/stores';
88
import { Block, BlockType } from '@/schemes';
99
import {
1010
regex,
1111
fontSize,
1212
placeHolder,
13-
listComponent,
13+
listBlockType,
1414
} from '@utils/blockContent';
1515
import { useCommand } from '@/hooks';
1616
import { focusState } from '@/stores/page';
@@ -55,8 +55,36 @@ function BlockContent(blockDTO: Block) {
5555
const [blockMap, setBlockMap] = useRecoilState(blockMapState);
5656
const focusId = useRecoilValue(focusState);
5757
const caretRef = useRef(0);
58+
const listCnt = useRef(1);
5859
const [Dispatcher] = useCommand();
5960

61+
const indexInSibling: number = blockMap[
62+
blockDTO.parentId
63+
].childIdList.findIndex((childId: string) => childId === blockDTO.id);
64+
65+
const upperBlocks: Array<Block> = blockMap[blockDTO.parentId].childIdList
66+
.map((childId: any) => blockMap[childId])
67+
.slice(0, indexInSibling)
68+
.reverse();
69+
70+
const isUpperBlockEqualToNumberList = (): boolean => {
71+
if (upperBlocks.length) {
72+
return upperBlocks[0].type === BlockType.NUMBERED_LIST;
73+
}
74+
return false;
75+
};
76+
77+
const cntOfUpperNumberListBlock = (): number => {
78+
let cnt = 0;
79+
for (const upperblock of upperBlocks) {
80+
if (upperblock.type !== BlockType.NUMBERED_LIST) break;
81+
cnt += 1;
82+
}
83+
return cnt;
84+
};
85+
86+
const FIRST_LIST_NUMBER = '1';
87+
6088
const handleBlock = (value: string, type?: BlockType) =>
6189
type
6290
? setBlockMap({
@@ -75,6 +103,19 @@ function BlockContent(blockDTO: Block) {
75103
);
76104

77105
if (newType) {
106+
if (newType[0] === BlockType.NUMBERED_LIST) {
107+
if (!indexInSibling && content[0] !== FIRST_LIST_NUMBER) return;
108+
if (indexInSibling) {
109+
const numberListUpperBlock = isUpperBlockEqualToNumberList();
110+
if (!numberListUpperBlock && content[0] !== FIRST_LIST_NUMBER) return;
111+
if (
112+
numberListUpperBlock &&
113+
cntOfUpperNumberListBlock() + 1 !== +content[0]
114+
)
115+
return;
116+
}
117+
}
118+
78119
handleBlock(
79120
content.slice(content.indexOf(' ') + 1, content.length),
80121
newType[0] as BlockType,
@@ -142,11 +183,22 @@ function BlockContent(blockDTO: Block) {
142183
caretRef.current = nodeLength;
143184
}
144185
selection.collapse(selection.focusNode, caretRef.current);
186+
187+
if (blockDTO.type === BlockType.NUMBERED_LIST) {
188+
const numberListUpperBlock = isUpperBlockEqualToNumberList();
189+
if (!indexInSibling || !numberListUpperBlock) {
190+
listCnt.current = 1;
191+
return;
192+
}
193+
if (numberListUpperBlock) {
194+
listCnt.current = cntOfUpperNumberListBlock() + 1;
195+
}
196+
}
145197
}, [blockDTO.value]);
146198

147199
return (
148200
<div css={blockContentCSS}>
149-
{listComponent[blockDTO.type]}
201+
{listBlockType(blockDTO, listCnt.current)}
150202
<div
151203
ref={contentEditableRef}
152204
css={editableDivCSS(blockDTO)}

frontend/src/utils/blockContent.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
/** @jsx jsx */
22
/** @jsxRuntime classic */
33
import { jsx, css } from '@emotion/react';
4+
5+
import { Block, BlockType } from '@/schemes';
46
import { ReactComponent as Toggle } from '@assets/toggle-default.svg';
57

68
export const regex: { [key: string]: RegExp } = {
79
heading1: /^#\s[^\s.]*/gm,
810
heading2: /^##\s[^\s.]*/gm,
911
heading3: /^###\s[^\s.]*/gm,
1012
bulletedlist: /^-\s[^\s.]*/gm,
11-
numberedlist: /^1.\s[^\s.]*/gm,
13+
numberedlist: /^\d.\s[^\s.]*/gm,
1214
togglelist: /^>\s[^\s.]*/gm,
1315
quote: /^\|\s[^\s.]*/gm,
1416
};
1517

18+
export const listBlockType = (block: Block, idx: number) => {
19+
if (block.type === BlockType.NUMBERED_LIST) {
20+
return <span>{`${idx}. `}</span>;
21+
}
22+
return listComponent[block.type];
23+
};
24+
1625
const divCSS = css`
1726
margin: 0px 4px;
1827
font-size: 18px;
@@ -22,7 +31,6 @@ const divCSS = css`
2231

2332
export const listComponent: { [key: string]: any } = {
2433
bulletedlist: <div css={divCSS}></div>,
25-
numberedlist: <div css={divCSS}> 1. </div>,
2634
togglelist: (
2735
<div css={divCSS}>
2836
<Toggle />

0 commit comments

Comments
 (0)