import { css } from '@emotion/css' import { isKeyHotkey } from 'is-hotkey' import isUrl from 'is-url' import React, { MouseEvent, useMemo } from 'react' import { createEditor, Descendant, Editor, Element as SlateElement, Range, Transforms, } from 'slate' import { withHistory } from 'slate-history' import { Editable, RenderElementProps, RenderLeafProps, useSelected, useSlate, withReact, } from 'slate-react' import * as SlateReact from 'slate-react' import { Button, Icon, Toolbar } from './components' import { BadgeElement, ButtonElement, CustomEditor, CustomElement, LinkElement, RenderElementPropsFor, } from './custom-types.d' const initialValue: Descendant[] = [ { type: 'paragraph', children: [ { text: 'In addition to block nodes, you can create inline nodes. Here is a ', }, { type: 'link', url: 'https://en.wikipedia.org/wiki/Hypertext', children: [{ text: 'hyperlink' }], }, { text: ', and here is a more unusual inline: an ', }, { type: 'button', children: [{ text: 'editable button' }], }, { text: '! Here is a read-only inline: ', }, { type: 'badge', children: [{ text: 'Approved' }], }, { text: '.', }, ], }, { type: 'paragraph', children: [ { text: 'There are two ways to add links. You can either add a link via the toolbar icon above, or if you want in on a little secret, copy a URL to your keyboard and paste it while a range of text is selected. ', }, // The following is an example of an inline at the end of a block. // This is an edge case that can cause issues. { type: 'link', url: 'https://twitter.com/JustMissEmma/status/1448679899531726852', children: [{ text: 'Finally, here is our favorite dog video.' }], }, { text: '' }, ], }, ] const InlinesExample = () => { const editor = useMemo( () => withInlines(withHistory(withReact(createEditor()))) as CustomEditor, [] ) const onKeyDown: React.KeyboardEventHandler = event => { const { selection } = editor // Default left/right behavior is unit:'character'. // This fails to distinguish between two cursor positions, such as // foo vs foo. // Here we modify the behavior to unit:'offset'. // This lets the user step into and out of the inline without stepping over characters. // You may wish to customize this further to only use unit:'offset' in specific cases. if (selection && Range.isCollapsed(selection)) { const { nativeEvent } = event if (isKeyHotkey('left', nativeEvent)) { event.preventDefault() Transforms.move(editor, { unit: 'offset', reverse: true }) return } if (isKeyHotkey('right', nativeEvent)) { event.preventDefault() Transforms.move(editor, { unit: 'offset' }) return } } } return ( } renderLeaf={props => } placeholder="Enter some text..." onKeyDown={onKeyDown} /> ) } const withInlines = (editor: CustomEditor) => { const { insertData, insertText, isInline, isElementReadOnly, isSelectable } = editor editor.isInline = (element: CustomElement) => ['link', 'button', 'badge'].includes(element.type) || isInline(element) editor.isElementReadOnly = (element: CustomElement) => element.type === 'badge' || isElementReadOnly(element) editor.isSelectable = (element: CustomElement) => element.type !== 'badge' && isSelectable(element) editor.insertText = text => { if (text && isUrl(text)) { wrapLink(editor, text) } else { insertText(text) } } editor.insertData = data => { const text = data.getData('text/plain') if (text && isUrl(text)) { wrapLink(editor, text) } else { insertData(data) } } return editor } const insertLink = (editor: CustomEditor, url: string) => { if (editor.selection) { wrapLink(editor, url) } } const insertButton = (editor: CustomEditor) => { if (editor.selection) { wrapButton(editor) } } const isLinkActive = (editor: CustomEditor): boolean => { const [link] = Editor.nodes(editor, { match: n => !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'link', }) return !!link } const isButtonActive = (editor: CustomEditor): boolean => { const [button] = Editor.nodes(editor, { match: n => !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'button', }) return !!button } const unwrapLink = (editor: CustomEditor) => { Transforms.unwrapNodes(editor, { match: n => !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'link', }) } const unwrapButton = (editor: CustomEditor) => { Transforms.unwrapNodes(editor, { match: n => !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'button', }) } const wrapLink = (editor: CustomEditor, url: string) => { if (isLinkActive(editor)) { unwrapLink(editor) } const { selection } = editor const isCollapsed = selection && Range.isCollapsed(selection) const link: LinkElement = { type: 'link', url, children: isCollapsed ? [{ text: url }] : [], } if (isCollapsed) { Transforms.insertNodes(editor, link) } else { Transforms.wrapNodes(editor, link, { split: true }) Transforms.collapse(editor, { edge: 'end' }) } } const wrapButton = (editor: CustomEditor) => { if (isButtonActive(editor)) { unwrapButton(editor) } const { selection } = editor const isCollapsed = selection && Range.isCollapsed(selection) const button: ButtonElement = { type: 'button', children: isCollapsed ? [{ text: 'Edit me!' }] : [], } if (isCollapsed) { Transforms.insertNodes(editor, button) } else { Transforms.wrapNodes(editor, button, { split: true }) Transforms.collapse(editor, { edge: 'end' }) } } // Put this at the start and end of an inline component to work around this Chromium bug: // https://bugs.chromium.org/p/chromium/issues/detail?id=1249405 const InlineChromiumBugfix = () => ( {String.fromCodePoint(160) /* Non-breaking space */} ) const allowedSchemes = ['http:', 'https:', 'mailto:', 'tel:'] const LinkComponent = ({ attributes, children, element, }: RenderElementPropsFor) => { const selected = useSelected() const safeUrl = useMemo(() => { let parsedUrl: URL | null = null try { parsedUrl = new URL(element.url) // eslint-disable-next-line no-empty } catch {} if (parsedUrl && allowedSchemes.includes(parsedUrl.protocol)) { return parsedUrl.href } return 'about:blank' }, [element.url]) return ( {children} ) } const EditableButtonComponent = ({ attributes, children, }: RenderElementProps) => { return ( /* Note that this is not a true button, but a span with button-like CSS. True buttons are display:inline-block, but Chrome and Safari have a bad bug with display:inline-block inside contenteditable: - https://bugs.webkit.org/show_bug.cgi?id=105898 - https://bugs.chromium.org/p/chromium/issues/detail?id=1088403 Worse, one cannot override the display property: https://github.com/w3c/csswg-drafts/issues/3226 The only current workaround is to emulate the appearance of a display:inline button using CSS. */ ev.preventDefault()} // Margin is necessary to clearly show the cursor adjacent to the button className={css` margin: 0 0.1em; background-color: #efefef; padding: 2px 6px; border: 1px solid #767676; border-radius: 2px; font-size: 0.9em; `} > {children} ) } const BadgeComponent = ({ attributes, children, element, }: RenderElementProps) => { const selected = useSelected() return ( {children} ) } const Element = (props: RenderElementProps) => { const { attributes, children, element } = props switch (element.type) { case 'link': return case 'button': return case 'badge': return default: return

{children}

} } const Text = (props: RenderLeafProps) => { const { attributes, children, leaf } = props return ( {children} ) } const AddLinkButton = () => { const editor = useSlate() return ( ) } const RemoveLinkButton = () => { const editor = useSlate() return ( ) } const ToggleEditableButtonButton = () => { const editor = useSlate() return ( ) } export default InlinesExample