File size: 3,851 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import isHotkey from 'is-hotkey'
import React, { useCallback, useMemo, useState } from 'react'
import { createPortal } from 'react-dom'
import { Editor, createEditor } from 'slate'
import { withHistory } from 'slate-history'
import { Editable, ReactEditor, Slate, useSlate, withReact } from 'slate-react'
import { Button, Icon, Toolbar } from './components'
const HOTKEYS = {
'mod+b': 'bold',
'mod+i': 'italic',
'mod+u': 'underline',
'mod+`': 'code',
}
const IFrameExample = () => {
const renderElement = useCallback(
({ attributes, children }) => <p {...attributes}>{children}</p>,
[]
)
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
const handleBlur = useCallback(() => ReactEditor.deselect(editor), [editor])
return (
<Slate editor={editor} initialValue={initialValue}>
<Toolbar>
<MarkButton format="bold" icon="format_bold" />
<MarkButton format="italic" icon="format_italic" />
<MarkButton format="underline" icon="format_underlined" />
<MarkButton format="code" icon="code" />
</Toolbar>
<IFrame onBlur={handleBlur}>
<Editable
renderElement={renderElement}
renderLeaf={renderLeaf}
placeholder="Enter some rich text…"
spellCheck
autoFocus
onKeyDown={event => {
for (const hotkey in HOTKEYS) {
if (isHotkey(hotkey, event)) {
event.preventDefault()
const mark = HOTKEYS[hotkey]
toggleMark(editor, mark)
}
}
}}
/>
</IFrame>
</Slate>
)
}
const toggleMark = (editor, format) => {
const isActive = isMarkActive(editor, format)
if (isActive) {
Editor.removeMark(editor, format)
} else {
Editor.addMark(editor, format, true)
}
}
const isMarkActive = (editor, format) => {
const marks = Editor.marks(editor)
return marks ? marks[format] === true : false
}
const Leaf = ({ attributes, children, leaf }) => {
if (leaf.bold) {
children = <strong>{children}</strong>
}
if (leaf.code) {
children = <code>{children}</code>
}
if (leaf.italic) {
children = <em>{children}</em>
}
if (leaf.underline) {
children = <u>{children}</u>
}
return <span {...attributes}>{children}</span>
}
const MarkButton = ({ format, icon }) => {
const editor = useSlate()
return (
<Button
active={isMarkActive(editor, format)}
onMouseDown={event => {
event.preventDefault()
toggleMark(editor, format)
}}
>
<Icon>{icon}</Icon>
</Button>
)
}
const IFrame = ({ children, ...props }) => {
const [iframeBody, setIframeBody] = useState(null)
const handleLoad = e => {
const iframe = e.target
if (!iframe.contentDocument) return
setIframeBody(iframe.contentDocument.body)
}
return (
<iframe srcDoc={`<!DOCTYPE html>`} {...props} onLoad={handleLoad}>
{iframeBody && createPortal(children, iframeBody)}
</iframe>
)
}
const initialValue = [
{
type: 'paragraph',
children: [
{
text: 'In this example, the document gets rendered into a controlled ',
},
{ text: '<iframe>', code: true },
{
text: '. This is ',
},
{
text: 'particularly',
italic: true,
},
{
text: ' useful, when you need to separate the styles for your editor contents from the ones addressing your UI.',
},
],
},
{
type: 'paragraph',
children: [
{
text: 'This also the only reliable method to preview any ',
},
{
text: 'media queries',
bold: true,
},
{
text: ' in your CSS.',
},
],
},
]
export default IFrameExample
|