1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-19 13:41:19 +02:00

Allow links to be pasted without a selection (#3297)

* Allow links to be pasted without a selection

* Update links.js
This commit is contained in:
Sidwyn Koh
2019-12-11 10:04:34 -08:00
committed by Ian Storm Taylor
parent 28c31c2c0c
commit 26a91f805a

View File

@@ -1,7 +1,7 @@
import React, { useState, useMemo } from 'react' import React, { useState, useMemo } from 'react'
import isUrl from 'is-url' import isUrl from 'is-url'
import { Slate, Editable, withReact, useSlate } from 'slate-react' import { Slate, Editable, withReact, useSlate } from 'slate-react'
import { Editor, createEditor } from 'slate' import { Editor, Range, createEditor } from 'slate'
import { withHistory } from 'slate-history' import { withHistory } from 'slate-history'
import { Button, Icon, Toolbar } from '../components' import { Button, Icon, Toolbar } from '../components'
@@ -85,9 +85,20 @@ const wrapLink = (editor, url) => {
unwrapLink(editor) unwrapLink(editor)
} }
const link = { type: 'link', url, children: [] } const { selection } = editor
Editor.wrapNodes(editor, link, { split: true }) const isCollapsed = Range.isCollapsed(selection)
Editor.collapse(editor, { edge: 'end' }) const link = {
type: 'link',
url,
children: isCollapsed ? [{ text: url }] : [],
}
if (isCollapsed) {
Editor.insertNodes(editor, link)
} else {
Editor.wrapNodes(editor, link, { split: true })
Editor.collapse(editor, { edge: 'end' })
}
} }
const Element = ({ attributes, children, element }) => { const Element = ({ attributes, children, element }) => {