| import { css } from '@emotion/css' |
| import { isKeyHotkey } from 'is-hotkey' |
| import isUrl from 'is-url' |
| import React, { useMemo } from 'react' |
| import { |
| createEditor, |
| Editor, |
| Element as SlateElement, |
| Range, |
| Transforms, |
| } from 'slate' |
| import { withHistory } from 'slate-history' |
| import { Editable, useSelected, useSlate, withReact } from 'slate-react' |
| import * as SlateReact from 'slate-react' |
| import { Button, Icon, Toolbar } from './components' |
|
|
| const initialValue = [ |
| { |
| 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. ', |
| }, |
| |
| |
| { |
| 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()))), |
| [] |
| ) |
| const onKeyDown = event => { |
| const { selection } = editor |
| |
| |
| |
| |
| |
| |
| 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 ( |
| <SlateReact.Slate editor={editor} initialValue={initialValue}> |
| <Toolbar> |
| <AddLinkButton /> |
| <RemoveLinkButton /> |
| <ToggleEditableButtonButton /> |
| </Toolbar> |
| <Editable |
| renderElement={props => <Element {...props} />} |
| renderLeaf={props => <Text {...props} />} |
| placeholder="Enter some text..." |
| onKeyDown={onKeyDown} |
| /> |
| </SlateReact.Slate> |
| ) |
| } |
| const withInlines = editor => { |
| const { insertData, insertText, isInline, isElementReadOnly, isSelectable } = |
| editor |
| editor.isInline = element => |
| ['link', 'button', 'badge'].includes(element.type) || isInline(element) |
| editor.isElementReadOnly = element => |
| element.type === 'badge' || isElementReadOnly(element) |
| editor.isSelectable = element => |
| 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, url) => { |
| if (editor.selection) { |
| wrapLink(editor, url) |
| } |
| } |
| const insertButton = editor => { |
| if (editor.selection) { |
| wrapButton(editor) |
| } |
| } |
| const isLinkActive = editor => { |
| const [link] = Editor.nodes(editor, { |
| match: n => |
| !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'link', |
| }) |
| return !!link |
| } |
| const isButtonActive = editor => { |
| const [button] = Editor.nodes(editor, { |
| match: n => |
| !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'button', |
| }) |
| return !!button |
| } |
| const unwrapLink = editor => { |
| Transforms.unwrapNodes(editor, { |
| match: n => |
| !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'link', |
| }) |
| } |
| const unwrapButton = editor => { |
| Transforms.unwrapNodes(editor, { |
| match: n => |
| !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'button', |
| }) |
| } |
| const wrapLink = (editor, url) => { |
| if (isLinkActive(editor)) { |
| unwrapLink(editor) |
| } |
| const { selection } = editor |
| const isCollapsed = selection && Range.isCollapsed(selection) |
| const link = { |
| 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 => { |
| if (isButtonActive(editor)) { |
| unwrapButton(editor) |
| } |
| const { selection } = editor |
| const isCollapsed = selection && Range.isCollapsed(selection) |
| const button = { |
| 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' }) |
| } |
| } |
| |
| |
| const InlineChromiumBugfix = () => ( |
| <span |
| contentEditable={false} |
| className={css` |
| font-size: 0; |
| `} |
| > |
| {String.fromCodePoint(160) /* Non-breaking space */} |
| </span> |
| ) |
| const allowedSchemes = ['http:', 'https:', 'mailto:', 'tel:'] |
| const LinkComponent = ({ attributes, children, element }) => { |
| const selected = useSelected() |
| const safeUrl = useMemo(() => { |
| let parsedUrl = null |
| try { |
| parsedUrl = new URL(element.url) |
| |
| } catch {} |
| if (parsedUrl && allowedSchemes.includes(parsedUrl.protocol)) { |
| return parsedUrl.href |
| } |
| return 'about:blank' |
| }, [element.url]) |
| return ( |
| <a |
| {...attributes} |
| href={safeUrl} |
| className={ |
| selected |
| ? css` |
| box-shadow: 0 0 0 3px #ddd; |
| ` |
| : '' |
| } |
| > |
| <InlineChromiumBugfix /> |
| {children} |
| <InlineChromiumBugfix /> |
| </a> |
| ) |
| } |
| const EditableButtonComponent = ({ attributes, children }) => { |
| return ( |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <span |
| {...attributes} |
| onClick={ev => 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; |
| `} |
| > |
| <InlineChromiumBugfix /> |
| {children} |
| <InlineChromiumBugfix /> |
| </span> |
| ) |
| } |
| const BadgeComponent = ({ attributes, children, element }) => { |
| const selected = useSelected() |
| return ( |
| <span |
| {...attributes} |
| contentEditable={false} |
| className={css` |
| background-color: green; |
| color: white; |
| padding: 2px 6px; |
| border-radius: 2px; |
| font-size: 0.9em; |
| ${selected && 'box-shadow: 0 0 0 3px #ddd;'} |
| `} |
| data-playwright-selected={selected} |
| > |
| <InlineChromiumBugfix /> |
| {children} |
| <InlineChromiumBugfix /> |
| </span> |
| ) |
| } |
| const Element = props => { |
| const { attributes, children, element } = props |
| switch (element.type) { |
| case 'link': |
| return <LinkComponent {...props} /> |
| case 'button': |
| return <EditableButtonComponent {...props} /> |
| case 'badge': |
| return <BadgeComponent {...props} /> |
| default: |
| return <p {...attributes}>{children}</p> |
| } |
| } |
| const Text = props => { |
| const { attributes, children, leaf } = props |
| return ( |
| <span |
| // The following is a workaround for a Chromium bug where, |
| // if you have an inline at the end of a block, |
| // clicking the end of a block puts the cursor inside the inline |
| // instead of inside the final {text: ''} node |
| // https://github.com/ianstormtaylor/slate/issues/4704#issuecomment-1006696364 |
| className={ |
| leaf.text === '' |
| ? css` |
| padding-left: 0.1px; |
| ` |
| : undefined |
| } |
| {...attributes} |
| > |
| {children} |
| </span> |
| ) |
| } |
| const AddLinkButton = () => { |
| const editor = useSlate() |
| return ( |
| <Button |
| active={isLinkActive(editor)} |
| onMouseDown={event => { |
| event.preventDefault() |
| const url = window.prompt('Enter the URL of the link:') |
| if (!url) return |
| insertLink(editor, url) |
| }} |
| > |
| <Icon>link</Icon> |
| </Button> |
| ) |
| } |
| const RemoveLinkButton = () => { |
| const editor = useSlate() |
| return ( |
| <Button |
| active={isLinkActive(editor)} |
| onMouseDown={event => { |
| if (isLinkActive(editor)) { |
| unwrapLink(editor) |
| } |
| }} |
| > |
| <Icon>link_off</Icon> |
| </Button> |
| ) |
| } |
| const ToggleEditableButtonButton = () => { |
| const editor = useSlate() |
| return ( |
| <Button |
| active |
| onMouseDown={event => { |
| event.preventDefault() |
| if (isButtonActive(editor)) { |
| unwrapButton(editor) |
| } else { |
| insertButton(editor) |
| } |
| }} |
| > |
| <Icon>smart_button</Icon> |
| </Button> |
| ) |
| } |
| export default InlinesExample |
|
|