Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit ec5e689

Browse files
authored
Merge pull request #84 from withspectrum/fix-code-block
Fix code block
2 parents d0c5dcb + b45cf44 commit ec5e689

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/components/Code/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class CodeBlock extends PureComponent {
4747
focusKey: blockKey,
4848
});
4949

50+
this.setState({ isOpen: false });
51+
5052
let content = editorState.getCurrentContent();
5153
content = Modifier.mergeBlockData(
5254
content,
@@ -67,6 +69,7 @@ class CodeBlock extends PureComponent {
6769
cancelClicks = event => event.preventDefault();
6870

6971
onSelectClick = event => {
72+
this.setState({ isOpen: true });
7073
const { setReadOnly } = this.props.blockProps;
7174
event.stopPropagation();
7275
setReadOnly(true);
@@ -76,9 +79,7 @@ class CodeBlock extends PureComponent {
7679
};
7780

7881
onClickOutside = () => {
79-
if (!this.state.isOpen) {
80-
return;
81-
}
82+
if (this.state.isOpen === false) return;
8283
this.setState({
8384
isOpen: false,
8485
});
@@ -90,6 +91,8 @@ class CodeBlock extends PureComponent {
9091

9192
setReadOnly(false);
9293

94+
this.setState({ isOpen: false });
95+
9396
const editorState = getEditorState();
9497
const selection = editorState.getSelection();
9598

@@ -100,6 +103,7 @@ class CodeBlock extends PureComponent {
100103
const {
101104
languages,
102105
renderLanguageSelect,
106+
getReadOnly,
103107
language: _language,
104108
} = this.props.blockProps;
105109

@@ -125,12 +129,13 @@ class CodeBlock extends PureComponent {
125129
onClickOutside={this.onClickOutside}
126130
onClick={this.onSelectClick}
127131
>
128-
{renderLanguageSelect({
129-
selectedLabel,
130-
selectedValue,
131-
onChange: this.onChange,
132-
options,
133-
})}
132+
{!getReadOnly() &&
133+
renderLanguageSelect({
134+
selectedLabel,
135+
selectedValue,
136+
onChange: this.onChange,
137+
options,
138+
})}
134139
</CodeSwitchContainer>
135140
</div>
136141
);

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ const createMarkdownPlugin = (_config = {}) => {
260260

261261
blockRendererFn(
262262
block,
263-
{ setReadOnly, setEditorState, getEditorState, getEditorRef }
263+
{ setReadOnly, getReadOnly, setEditorState, getEditorState, getEditorRef }
264264
) {
265265
switch (block.getType()) {
266266
case CHECKABLE_LIST_ITEM: {
@@ -282,6 +282,7 @@ const createMarkdownPlugin = (_config = {}) => {
282282
setEditorState,
283283
renderLanguageSelect: config.renderLanguageSelect,
284284
languages: config.languages,
285+
getReadOnly,
285286
setReadOnly,
286287
language: block.getData().get("language"),
287288
getEditorState,

src/modifiers/handleNewCodeBlock.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import changeCurrentBlockType from "./changeCurrentBlockType";
22
import insertEmptyBlock from "./insertEmptyBlock";
3-
import splitBlockAndChange from './splitBlockAndChange';
3+
import splitBlockAndChange from "./splitBlockAndChange";
44
import { CODE_BLOCK_REGEX } from "../constants";
55

66
const handleNewCodeBlock = editorState => {
@@ -20,8 +20,18 @@ const handleNewCodeBlock = editorState => {
2020
if (language) {
2121
data.language = language;
2222
}
23-
const editorStateWithCodeBlock = changeCurrentBlockType(editorState, "code-block", "", data);
24-
return splitBlockAndChange(editorStateWithCodeBlock, undefined, undefined, false)
23+
const editorStateWithCodeBlock = changeCurrentBlockType(
24+
editorState,
25+
"code-block",
26+
"",
27+
data
28+
);
29+
return splitBlockAndChange(
30+
editorStateWithCodeBlock,
31+
undefined,
32+
undefined,
33+
false
34+
);
2535
}
2636
const type = currentBlock.getType();
2737
if (type === "code-block" && isLast) {

0 commit comments

Comments
 (0)