Skip to content

Commit e31cadb

Browse files
authored
Merge pull request velopert#77 from velopert/fix/link-post-crash
Fix/link post crash
2 parents 1006d7a + 6fb7fd4 commit e31cadb

File tree

10 files changed

+89
-35
lines changed

10 files changed

+89
-35
lines changed

src/components/auth/AuthModal.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ const AuthModalBlock = styled.div<{ visible: boolean }>`
6161
color: ${palette.gray7};
6262
text-align: center;
6363
font-weight: 600;
64-
font-size: 2rem;
6564
}
6665
}
6766
.white-block {

src/components/common/MarkdownRender.tsx

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ const MarkdownRenderBlock = styled.div`
7777
display: flex;
7878
justify-content: center;
7979
align-items: center;
80-
blockquote {
81-
border-left: none;
82-
}
80+
border-left: none;
81+
background: none;
82+
padding: none;
8383
}
8484
`;
8585

@@ -118,6 +118,7 @@ function filter(html: string) {
118118
'pre',
119119
'iframe',
120120
'span',
121+
'img',
121122
],
122123
allowedAttributes: {
123124
a: ['href', 'name', 'target'],
@@ -153,23 +154,23 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
153154
onConvertFinish,
154155
editing,
155156
}) => {
156-
const initialHtml = ssrEnabled
157-
? remark()
158-
.use(breaks)
159-
.use(prismPlugin)
160-
.use(htmlPlugin, {
161-
sanitize: true,
162-
})
163-
.use(embedPlugin)
164-
.use(slug)
165-
.processSync(markdown)
166-
.toString()
167-
: '';
168-
169-
const [element, setElement] = useState<RenderedElement>(
170-
ssrEnabled ? parse(filter(initialHtml)) : null,
157+
const [html, setHtml] = useState(
158+
ssrEnabled
159+
? remark()
160+
.use(breaks)
161+
.use(prismPlugin)
162+
.use(htmlPlugin, {
163+
sanitize: true,
164+
})
165+
.use(embedPlugin)
166+
.use(slug)
167+
.processSync(markdown)
168+
.toString()
169+
: '',
171170
);
172171

172+
const [element, setElement] = useState<RenderedElement>(null);
173+
173174
const applyElement = React.useMemo(() => {
174175
return throttle(250, (el: any) => {
175176
setElement(el);
@@ -194,6 +195,12 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
194195
// if (window && (window as any).twttr) return;
195196
loadScript('https://platform.twitter.com/widgets.js');
196197
}
198+
199+
if (!editing) {
200+
setHtml(html);
201+
return;
202+
}
203+
197204
const el = parse(editing ? html : filter(html));
198205

199206
applyElement(el);
@@ -202,7 +209,13 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
202209

203210
return (
204211
<Typography>
205-
<MarkdownRenderBlock className={codeTheme}>{element}</MarkdownRenderBlock>
212+
{editing ? (
213+
<MarkdownRenderBlock className={codeTheme}>
214+
{element}
215+
</MarkdownRenderBlock>
216+
) : (
217+
<MarkdownRenderBlock dangerouslySetInnerHTML={{ __html: html }} />
218+
)}
206219
</Typography>
207220
);
208221
};

src/components/common/Typography.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import media from '../../lib/styles/media';
55

66
const TypographyBlock = styled.div`
77
font-size: 1.125rem;
8-
color: ${palette.gray7};
8+
color: ${palette.gray8};
99
line-height: 1.85;
1010
letter-spacing: -0.02em;
1111
word-break: keep-all;
@@ -121,6 +121,29 @@ const TypographyBlock = styled.div`
121121
margin-top: 2rem;
122122
}
123123
}
124+
125+
blockquote {
126+
margin-top: 2rem;
127+
margin-bottom: 2rem;
128+
border-left: 4px solid ${palette.teal5};
129+
border-top-right-radius: 4px;
130+
border-bottom-right-radius: 4px;
131+
background: ${palette.gray0};
132+
margin-left: 0;
133+
margin-right: 0;
134+
padding: 1rem;
135+
padding-left: 2rem;
136+
color: ${palette.gray9};
137+
ul, ol {
138+
padding-left: 1rem;
139+
}
140+
*:first-child {
141+
margin-top: 0;
142+
}
143+
*:last-child {
144+
margin-bottom: 0;
145+
}
146+
}
124147
`;
125148

126149
export interface TypographyProps {}

src/components/post/PostCommentItem.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const CommentHead = styled.div`
3434
height: 3.375rem;
3535
display: block;
3636
border-radius: 50%;
37+
object-fit: cover;
3738
${media.small} {
3839
width: 2.5rem;
3940
height: 2.5rem;
@@ -91,7 +92,7 @@ const CommentHead = styled.div`
9192
}
9293
`;
9394

94-
const CommentText = styled.p<{ deleted: boolean }>`
95+
const CommentText = styled.div<{ deleted: boolean }>`
9596
h1,
9697
h2 {
9798
font-size: 1.75rem;

src/components/setting/SettingUserProfile.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ const Section = styled.section`
9797
display: flex;
9898
flex-direction: column;
9999
img {
100+
object-fit: cover;
100101
width: 8rem;
101102
height: 8rem;
102103
border-radius: 50%;
@@ -113,7 +114,7 @@ const Section = styled.section`
113114
margin-bottom: 1rem;
114115
}
115116
button {
116-
width: 8rem;
117+
width: 10rem;
117118
}
118119
align-items: center;
119120
padding-bottom: 1.5rem;

src/components/velog/VelogResponsive.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import media from '../../lib/styles/media';
44

55
const VelogResponsiveBlock = styled.div`
66
width: 768px;
7-
margin: 0 auto;
7+
margin-left: auto;
8+
margin-right: auto;
89
${media.small} {
910
width: 100%;
1011
}

src/components/write/AddLink.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ const AddLinkBlock = styled.div`
5555

5656
interface AddLinkProps {
5757
left: number;
58-
top: number;
58+
top: number | null;
59+
bottom: number | null;
5960
stickToRight?: boolean;
6061
onConfirm: (link: string) => void;
6162
onClose: () => void;
@@ -68,6 +69,7 @@ const { useCallback, useRef, useEffect } = React;
6869
const AddLink: React.FC<AddLinkProps> = ({
6970
left,
7071
top,
72+
bottom,
7173
stickToRight,
7274
onConfirm,
7375
onClose,
@@ -92,7 +94,8 @@ const AddLink: React.FC<AddLinkProps> = ({
9294
<AddLinkBlock
9395
style={{
9496
left: stickToRight ? 'initial' : left,
95-
top,
97+
top: top || 'initial',
98+
bottom: bottom || 'initial',
9699
right: stickToRight ? '3rem' : 'initial',
97100
}}
98101
>

src/components/write/WriteMarkdownEditor.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export interface MarkdownEditorProps {
3434
type MarkdownEditorState = {
3535
shadow: boolean;
3636
addLink: {
37-
top: number;
37+
top: number | null;
38+
bottom: number | null;
3839
left: number;
3940
visible: boolean;
4041
stickToRight: boolean;
@@ -235,6 +236,7 @@ export default class WriteMarkdownEditor extends React.Component<
235236
shadow: false,
236237
addLink: {
237238
top: 0,
239+
bottom: 0,
238240
left: 0,
239241
visible: false,
240242
stickToRight: false,
@@ -367,6 +369,7 @@ export default class WriteMarkdownEditor extends React.Component<
367369
const { lastUploadedImage, initialBody } = this.props;
368370
if (initialBody !== prevProps.initialBody) {
369371
if (!this.codemirror) return;
372+
if (this.codemirror.getValue() === this.props.initialBody) return;
370373
this.codemirror.setValue(this.props.initialBody);
371374
}
372375
if (
@@ -403,15 +406,20 @@ export default class WriteMarkdownEditor extends React.Component<
403406
const cursorPos = this.codemirror.cursorCoords(cursor);
404407
if (!this.block.current) return;
405408
const stickToRight = cursorPos.left > this.block.current.clientWidth - 341;
406-
409+
const calculatedTop =
410+
this.block.current.scrollTop +
411+
cursorPos.top +
412+
this.codemirror.defaultTextHeight() / 2 +
413+
1;
414+
415+
const isAtBottom = calculatedTop + 173 > this.block.current?.clientHeight;
416+
const pos = isAtBottom
417+
? { top: null, bottom: 64 }
418+
: { top: calculatedTop, bottom: null };
407419
this.setState({
408420
addLink: {
409421
visible: true,
410-
top:
411-
this.block.current.scrollTop +
412-
cursorPos.top +
413-
this.codemirror.defaultTextHeight() / 2 +
414-
1,
422+
...pos,
415423
left: cursorPos.left,
416424
stickToRight,
417425
},
@@ -837,6 +845,7 @@ ${selected}
837845
defaultValue=""
838846
left={addLink.left}
839847
top={addLink.top}
848+
bottom={addLink.bottom}
840849
stickToRight={addLink.stickToRight}
841850
onConfirm={this.handleConfirmAddLink}
842851
onClose={this.handleCancelAddLink}

src/containers/write/ActiveEditor.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState, useCallback } from 'react';
1+
import React, { useEffect, useState, useCallback, useRef } from 'react';
22
import { useSelector, useDispatch, shallowEqual } from 'react-redux';
33
import { RootState } from '../../modules';
44
import {
@@ -31,6 +31,7 @@ const ActiveEditor: React.FC<ActiveEditorProps> = () => {
3131
const mode = useSelector((state: RootState) => state.write.mode);
3232
const postId = useSelector((state: RootState) => state.write.postId);
3333
const [askLoadTemp, setAskLoadTemp] = useState(false);
34+
const initialized = useRef(false);
3435

3536
const dispatch = useDispatch();
3637
const location = useLocation();
@@ -75,6 +76,7 @@ const ActiveEditor: React.FC<ActiveEditorProps> = () => {
7576
const post = safe(() => readPostForEdit.data!.post);
7677
useEffect(() => {
7778
if (!post) return;
79+
if (initialized.current) return;
7880
dispatch(
7981
prepareEdit({
8082
id: post.id,
@@ -91,6 +93,7 @@ const ActiveEditor: React.FC<ActiveEditorProps> = () => {
9193
}),
9294
);
9395
dispatch(setInitialBody(post.body));
96+
initialized.current = true;
9497
}, [dispatch, post]);
9598

9699
const lastPostHistory = safe(() => getLastPostHistory.data!.lastPostHistory);
@@ -105,6 +108,7 @@ const ActiveEditor: React.FC<ActiveEditorProps> = () => {
105108
useEffect(() => {
106109
if (!lastPostHistory) return;
107110
if (!post) return;
111+
108112
const equals = shallowEqual(
109113
{
110114
title: lastPostHistory.title,

src/containers/write/MarkdownEditorContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
249249
}
250250
}, [title, postId, onTempSave, lastSavedData, markdown]);
251251

252-
useSaveHotKey(onTempSave);
252+
useSaveHotKey(() => onTempSave(true));
253253

254254
return (
255255
<>

0 commit comments

Comments
 (0)