Skip to content

Commit 6e3597e

Browse files
committed
fix: Fix fullscreen command setSize bug in react 18 (#177)
1 parent 81ebad3 commit 6e3597e

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/commands/fullscreen.tsx

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@ type Options = {
66
preview?: HTMLDivElement | null | undefined;
77
container?: HTMLDivElement | null | undefined;
88
editor?: CodeMirror.Editor;
9-
}
9+
};
1010

11-
const Fullscreen: React.FC<{ command: ICommand, editorProps: IMarkdownEditor & Options }> = (props) => {
12-
const { editorProps: { container, preview, editor } } = props;
11+
const Fullscreen: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & Options }> = (props) => {
12+
const {
13+
editorProps: { container, preview, editor },
14+
} = props;
1315
const [full, setFull] = useState(false);
1416
const initEditorHeight = useRef(0);
1517
const containerRef = useRef<HTMLDivElement | null>();
1618
const editorRef = useRef<CodeMirror.Editor>();
1719
function handleResize() {
18-
if (containerRef.current && editorRef.current) {
20+
if (containerRef.current && editorRef.current && editorRef.current.setSize) {
1921
editorRef.current.setSize('initial', containerRef.current.clientHeight - 35);
2022
}
2123
}
2224
useEffect(() => {
2325
window && window.addEventListener('resize', handleResize, true);
2426
return () => {
2527
window && window.removeEventListener('resize', handleResize, true);
26-
}
28+
};
2729
}, []);
2830
useEffect(() => {
2931
if (editor) {
@@ -34,7 +36,7 @@ const Fullscreen: React.FC<{ command: ICommand, editorProps: IMarkdownEditor & O
3436
}, [editor]);
3537
useEffect(() => {
3638
if (!document) return;
37-
containerRef.current = container
39+
containerRef.current = container;
3840
document.body.style.overflow = full ? 'hidden' : 'initial';
3941
if (container && full) {
4042
container.style.zIndex = '999';
@@ -43,35 +45,41 @@ const Fullscreen: React.FC<{ command: ICommand, editorProps: IMarkdownEditor & O
4345
container.style.bottom = '0px';
4446
container.style.left = '0px';
4547
container.style.right = '0px';
46-
editor!.setSize('initial', container.clientHeight - 35);
48+
if (editor) {
49+
editor.setSize('initial', container.clientHeight - 35);
50+
}
4751
} else if (container) {
4852
container.style.position = 'initial';
4953
container.style.top = 'initial';
5054
container.style.bottom = 'initial';
5155
container.style.left = 'initial';
5256
container.style.right = 'initial';
53-
editor!.setSize('initial', initEditorHeight.current);
57+
if (editor) {
58+
editor.setSize('initial', initEditorHeight.current);
59+
}
5460
}
55-
}, [full, container, preview]);
61+
}, [full, container, preview, editor]);
5662
return (
57-
<button
58-
onClick={() => setFull(!full)}
59-
type="button"
60-
className={full ? 'active' : ''}
61-
>
63+
<button onClick={() => setFull(!full)} type="button" className={full ? 'active' : ''}>
6264
{props.command.icon}
6365
</button>
6466
);
65-
}
67+
};
6668

6769
export const fullscreen: ICommand = {
6870
name: 'fullscreen',
6971
keyCommand: 'fullscreen',
7072
button: (command, props, opts) => <Fullscreen command={command} editorProps={{ ...props, ...opts }} />,
7173
icon: (
7274
<svg width="16" height="16" viewBox="0 0 1024 1024">
73-
<path fill="currentColor" d="M189.75 428.89a36.87 36.87 0 0 0 36.84-36.85V228.12h164a36.85 36.85 0 1 0 0-73.7H189.75a36.82 36.82 0 0 0-36.8 36.85v200.8a36.83 36.83 0 0 0 36.8 36.82zM834.26 595.06a36.82 36.82 0 0 0-36.8 36.84v164H633.41a36.85 36.85 0 0 0 0 73.7h200.85a36.87 36.87 0 0 0 36.84-36.85V631.9a36.86 36.86 0 0 0-36.84-36.84zM797.46 228.12v179.31a36.82 36.82 0 1 0 73.64 0V191.24a36.86 36.86 0 0 0-36.84-36.85H602.33a36.85 36.85 0 0 0 0 73.7zM421.62 795.9H226.54V616.56a36.82 36.82 0 1 0-73.64 0v216.19a36.83 36.83 0 0 0 36.85 36.85h231.87a36.85 36.85 0 0 0 0-73.7z" />
74-
<path fill="currentColor" d="M306.5 307.94m32.95 0l345.1 0q32.95 0 32.95 32.95l0 342.22q0 32.95-32.95 32.95l-345.1 0q-32.95 0-32.95-32.95l0-342.22q0-32.95 32.95-32.95Z" />
75+
<path
76+
fill="currentColor"
77+
d="M189.75 428.89a36.87 36.87 0 0 0 36.84-36.85V228.12h164a36.85 36.85 0 1 0 0-73.7H189.75a36.82 36.82 0 0 0-36.8 36.85v200.8a36.83 36.83 0 0 0 36.8 36.82zM834.26 595.06a36.82 36.82 0 0 0-36.8 36.84v164H633.41a36.85 36.85 0 0 0 0 73.7h200.85a36.87 36.87 0 0 0 36.84-36.85V631.9a36.86 36.86 0 0 0-36.84-36.84zM797.46 228.12v179.31a36.82 36.82 0 1 0 73.64 0V191.24a36.86 36.86 0 0 0-36.84-36.85H602.33a36.85 36.85 0 0 0 0 73.7zM421.62 795.9H226.54V616.56a36.82 36.82 0 1 0-73.64 0v216.19a36.83 36.83 0 0 0 36.85 36.85h231.87a36.85 36.85 0 0 0 0-73.7z"
78+
/>
79+
<path
80+
fill="currentColor"
81+
d="M306.5 307.94m32.95 0l345.1 0q32.95 0 32.95 32.95l0 342.22q0 32.95-32.95 32.95l-345.1 0q-32.95 0-32.95-32.95l0-342.22q0-32.95 32.95-32.95Z"
82+
/>
7583
</svg>
7684
),
77-
};
85+
};

0 commit comments

Comments
 (0)