Skip to content
This repository was archived by the owner on May 31, 2020. It is now read-only.

Commit 61fab29

Browse files
committed
readonly mode for atomic blocks. Added editorProps to pluginProps
1 parent 8d46027 commit 61fab29

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

packages/atomic-block/.flowconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.*/node_modules/@octokit/.*
55
.*/node_modules/@types/.*
66
.*/node_modules/\([a-c]\|[g-h]\|e\|[j-z]\).*/.*
7+
.*/core/src/.*
78
.*/EditorContainer/.*
89

910

packages/atomic-block/src/index.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,32 @@ class AtomicBlockPlugin extends Component<Props, State> {
141141
}
142142

143143
renderChildren = (props: Object) => {
144-
const { editorState, setEditorState } = this.props
144+
const {
145+
editorProps: { readOnly },
146+
editorState,
147+
setEditorState,
148+
} = this.props
145149

146150
const blockKey = props.block.getKey()
147151
const selection = editorState.getSelection()
148152
const isFocused =
149-
selection.getAnchorKey() === blockKey && selection.isCollapsed()
150-
151-
return (
152-
<AtomicBlock
153-
onDeleteBlock={() => this.deleteAtomicBlock(blockKey)}
154-
setEditorState={setEditorState}
155-
isFocused={isFocused}
156-
onClick={() => this.focusBlock(blockKey)}>
157-
{this.props.children({ ...props, isFocused })}
158-
</AtomicBlock>
159-
)
153+
selection.getAnchorKey() === blockKey &&
154+
selection.isCollapsed() &&
155+
readOnly !== true
156+
157+
if (readOnly) {
158+
return this.props.children({ ...props, isFocused })
159+
} else {
160+
return (
161+
<AtomicBlock
162+
onDeleteBlock={() => this.deleteAtomicBlock(blockKey)}
163+
setEditorState={setEditorState}
164+
isFocused={isFocused}
165+
onClick={() => this.focusBlock(blockKey)}>
166+
{this.props.children({ ...props, isFocused })}
167+
</AtomicBlock>
168+
)
169+
}
160170
}
161171

162172
handleReturn = (event, editorState) => {
@@ -166,7 +176,12 @@ class AtomicBlockPlugin extends Component<Props, State> {
166176
}
167177

168178
blockRendererFn = block => {
169-
const { type, editorState } = this.props
179+
const {
180+
type,
181+
editorProps: { readOnly },
182+
editorState,
183+
} = this.props
184+
170185
const content = editorState.getCurrentContent()
171186

172187
if (block.getType() === 'atomic') {
@@ -180,7 +195,7 @@ class AtomicBlockPlugin extends Component<Props, State> {
180195
if (entityType.toLowerCase() === type.toLowerCase()) {
181196
return {
182197
component: this.renderChildren,
183-
editable: false,
198+
editable: readOnly !== true,
184199
props: data,
185200
}
186201
}

packages/core/src/EditorContainer.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const Context: ContextType = createContext({})
2222

2323
const omitUndefined = obj =>
2424
Object.keys(obj).reduce(
25-
(acc, key) => (obj[key] !== undefined ? { ...acc, key: obj[key] } : acc),
25+
(acc, key) => (obj[key] !== undefined ? { ...acc, [key]: obj[key] } : acc),
2626
{}
2727
)
2828

@@ -119,6 +119,30 @@ export default class EditorContainer extends Component<Props, State> {
119119
webDriverTestID,
120120
} = props
121121

122+
const passedProps = omitUndefined({
123+
autoCapitalize,
124+
autoComplete,
125+
autoCorrect,
126+
readOnly,
127+
spellCheck,
128+
stripPastedStyles,
129+
editorKey,
130+
tabIndex,
131+
placeholder,
132+
textAlignment,
133+
textDirectionality,
134+
135+
ariaActiveDescendantID,
136+
ariaAutoComplete,
137+
ariaControls,
138+
ariaDescribedBy,
139+
ariaExpanded,
140+
ariaLabel,
141+
ariaLabelledBy,
142+
ariaMultiline,
143+
webDriverTestID,
144+
})
145+
122146
return {
123147
...state,
124148
editorState:
@@ -129,29 +153,7 @@ export default class EditorContainer extends Component<Props, State> {
129153
: editorState,
130154
editorProps: {
131155
...state.editorProps,
132-
...omitUndefined({
133-
autoCapitalize,
134-
autoComplete,
135-
autoCorrect,
136-
readOnly,
137-
spellCheck,
138-
stripPastedStyles,
139-
editorKey,
140-
tabIndex,
141-
placeholder,
142-
textAlignment,
143-
textDirectionality,
144-
145-
ariaActiveDescendantID,
146-
ariaAutoComplete,
147-
ariaControls,
148-
ariaDescribedBy,
149-
ariaExpanded,
150-
ariaLabel,
151-
ariaLabelledBy,
152-
ariaMultiline,
153-
webDriverTestID,
154-
}),
156+
...passedProps,
155157
},
156158
}
157159
}
@@ -327,6 +329,7 @@ export default class EditorContainer extends Component<Props, State> {
327329
const pluginProps = {
328330
registerPlugin: this.registerPlugin,
329331
setEditorState: this.onChange,
332+
editorProps: this.state.editorProps,
330333
editorRef: this.editorRef,
331334
editorState: this.state.editorState,
332335
setEditorProps: editorProps => {

packages/core/src/types.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// @flow
22

3-
import type { Context } from 'react'
3+
import type { Ref, Context } from 'react'
44
import type { BidiDirection } from 'fbjs/lib/UnicodeBidiDirection'
55
import type { DraftTextAlignment } from 'draft-js/lib/DraftTextAlignment'
6+
import { Editor as DraftEditor } from 'draft-js'
67
import type { EditorState } from 'draft-js'
78

89
export type StaticProps = {
@@ -32,7 +33,9 @@ export type StaticProps = {
3233
export type PluginProps = {
3334
registerPlugin: DraftEditorProps => () => void,
3435
setEditorState: EditorState => void,
36+
editorRef: Ref<DraftEditor>,
3537
editorState: EditorState,
38+
editorProps: StaticProps,
3639
setEditorProps: (editorProps: StaticProps) => void,
3740
}
3841

0 commit comments

Comments
 (0)