Newer
Older
import { ContentState, EditorState, Modifier } from 'draft-js'
import htmlToDraft from 'html-to-draftjs'
interface EcolyoLinkProps {
onChange?: (editorState: EditorState) => void
editorState: EditorState
}
interface LinkState {
displayTitle: string
link: string
}
const CustomLink: React.FC<EcolyoLinkProps> = ({ onChange, editorState }) => {
const [open, setOpen] = useState<boolean>(false)
const links: LinkState[] = [
{
displayTitle: 'Consommation',
link: 'consumption',
},
{
link: 'ecogestures',
},
{
displayTitle: 'Sélection des astuces',
link: 'ecogesture-selection',
},
{
displayTitle: 'Analyse',
link: 'analysis',
},
{
displayTitle: 'Options',
link: 'options',
},
]
const appendLink = (link: LinkState) => {
const data = `<a href="{cozyUrl}${
link.link
}">${link.displayTitle.toLowerCase()}</a>`
const { contentBlocks, entityMap } = htmlToDraft(data)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const contentState = Modifier.replaceWithFragment(
editorState.getCurrentContent(),
editorState.getSelection(),
ContentState.createFromBlockArray(contentBlocks, entityMap).getBlockMap()
)
const result = EditorState.push(
editorState,
contentState,
'insert-fragment'
)
if (onChange) {
onChange(result)
}
}
return (
<div
onClick={() => setOpen(!open)}
className="rdw-block-wrapper"
aria-label="rdw-block-control"
role="button"
tabIndex={0}
aria-expanded={false}
>
<div
className="rdw-dropdown-wrapper rdw-block-dropdown"
aria-label="rdw-dropdown"
style={{ width: 200 }}
>
<div className="rdw-dropdown-selectedtext">
<span>Lien Ecolyo</span>
<div className={`rdw-dropdown-caretto${open ? 'close' : 'open'}`} />
</div>
{open && (
<ul
className={
open
? 'rdw-dropdown-optionwrapper'
: 'rdw-dropdown-optionwrapper placeholder-ul'
}
>
return (
<li
className="rdw-dropdownoption-default placeholder-li"
onClick={() => appendLink(item)}
key={item.link}
>
{item.displayTitle}
</li>
)
})}
</ul>
)}
</div>
</div>
)
}
export default CustomLink