1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-19 05:31:56 +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 isUrl from 'is-url'
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 { Button, Icon, Toolbar } from '../components'
@@ -85,9 +85,20 @@ const wrapLink = (editor, url) => {
unwrapLink(editor)
}
const link = { type: 'link', url, children: [] }
Editor.wrapNodes(editor, link, { split: true })
Editor.collapse(editor, { edge: 'end' })
const { selection } = editor
const isCollapsed = Range.isCollapsed(selection)
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 }) => {