diff --git a/package.json b/package.json
index 5e32827..b0a1713 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "mui-rte",
- "version": "1.17.2",
+ "version": "1.17.3",
"description": "Material-UI Rich Text Editor and Viewer",
"keywords": [
"material-ui",
diff --git a/src/MUIRichTextEditor.tsx b/src/MUIRichTextEditor.tsx
index b93ee00..529d98f 100644
--- a/src/MUIRichTextEditor.tsx
+++ b/src/MUIRichTextEditor.tsx
@@ -95,6 +95,7 @@ export type TAutocompleteStrategy = {
triggerChar: string
items: TAutocompleteItem[]
insertSpaceAfter?: boolean
+ atomicBlockName?: string
}
export type TAutocomplete = {
@@ -386,20 +387,37 @@ const MUIRichTextEditor: RefForwardingComponent
=
'focusOffset': currentSelection.getFocusOffset() + searchTerm.length + 1
})
const currentContentState = editorState.getCurrentContent()
- const entityKey = currentContentState.createEntity('AC_ITEM', 'IMMUTABLE').getLastCreatedEntityKey();
- const contentState = Modifier.replaceText(editorStateRef.current!.getCurrentContent(),
- newSelection,
- item.value,
- editorStateRef.current!.getCurrentInlineStyle(),
- entityKey)
- const newEditorState = EditorState.push(editorStateRef.current!, contentState, "insert-characters")
- if (currentAutocompleteRef.current!.insertSpaceAfter === false) {
- handleChange(newEditorState)
+ if (currentAutocompleteRef.current!.atomicBlockName) {
+ const name = currentAutocompleteRef.current!.atomicBlockName
+ const block = atomicBlockExists(name, props.customControls)
+ if (block) {
+ const contentState = Modifier.removeRange(editorStateRef.current!.getCurrentContent(),
+ newSelection,
+ "forward")
+ const newEditorState = EditorState.push(editorStateRef.current!, contentState, "remove-range")
+ const withAtomicBlock = insertAtomicBlock(newEditorState, name.toUpperCase(), {
+ value: item.value
+ }, {
+ selection: newEditorState.getCurrentContent().getSelectionAfter()
+ })
+ handleChange(withAtomicBlock)
+ }
} else {
- const addSpaceState = Modifier.insertText(newEditorState.getCurrentContent(),
- newEditorState.getSelection(), " ",
- newEditorState.getCurrentInlineStyle())
- handleChange(EditorState.push(newEditorState, addSpaceState, "insert-characters"))
+ const entityKey = currentContentState.createEntity("AC_ITEM", 'IMMUTABLE').getLastCreatedEntityKey();
+ const contentState = Modifier.replaceText(editorStateRef.current!.getCurrentContent(),
+ newSelection,
+ item.value,
+ editorStateRef.current!.getCurrentInlineStyle(),
+ entityKey)
+ const newEditorState = EditorState.push(editorStateRef.current!, contentState, "insert-characters")
+ if (currentAutocompleteRef.current!.insertSpaceAfter === false) {
+ handleChange(newEditorState)
+ } else {
+ const addSpaceState = Modifier.insertText(newEditorState.getCurrentContent(),
+ newEditorState.getSelection(), " ",
+ newEditorState.getCurrentInlineStyle())
+ handleChange(EditorState.push(newEditorState, addSpaceState, "insert-characters"))
+ }
}
}
handleAutocompleteClosed()
@@ -476,7 +494,7 @@ const MUIRichTextEditor: RefForwardingComponent =
if (!block) {
return
}
- const newEditorState = insertAtomicBlock(block.name.toUpperCase(), data, {
+ const newEditorState = insertAtomicBlock(editorState, block.name.toUpperCase(), data, {
selection: editorState.getCurrentContent().getSelectionAfter()
})
updateStateForPopover(newEditorState)
@@ -711,7 +729,7 @@ const MUIRichTextEditor: RefForwardingComponent =
updateStateForPopover(EditorState.forceSelection(newEditorState, newEditorState.getCurrentContent().getSelectionAfter()))
}
else {
- const newEditorState = insertAtomicBlock("IMAGE", data)
+ const newEditorState = insertAtomicBlock(editorState, "IMAGE", data)
updateStateForPopover(EditorState.forceSelection(newEditorState, newEditorState.getCurrentContent().getSelectionAfter()))
}
setFocusMediaKey("")
@@ -794,7 +812,7 @@ const MUIRichTextEditor: RefForwardingComponent =
return null
}
- const insertAtomicBlock = (type: string, data: any, options?: any) => {
+ const insertAtomicBlock = (editorState: EditorState, type: string, data: any, options?: any) => {
const contentState = editorState.getCurrentContent()
const contentStateWithEntity = contentState.createEntity(type, 'IMMUTABLE', data)
const entityKey = contentStateWithEntity.getLastCreatedEntityKey()
diff --git a/src/components/Autocomplete.tsx b/src/components/Autocomplete.tsx
index 3a47ec6..c422579 100644
--- a/src/components/Autocomplete.tsx
+++ b/src/components/Autocomplete.tsx
@@ -6,7 +6,7 @@ import { createStyles, withStyles, WithStyles } from '@material-ui/core/styles'
export type TAutocompleteItem = {
keys: string[]
- value: string
+ value: any
content: string | JSX.Element
}