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

add paste linkifying to link example

This commit is contained in:
Ian Storm Taylor
2016-07-18 13:06:10 -07:00
parent 730b731b84
commit c717f8429f
2 changed files with 38 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ import { Editor, Mark, Raw } from '../..'
import React from 'react' import React from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import initialState from './state.json' import initialState from './state.json'
import isUrl from 'is-url'
import { Map } from 'immutable' import { Map } from 'immutable'
/** /**
@@ -49,6 +50,16 @@ class Links extends React.Component {
return state.inlines.some(inline => inline.type == 'link') return state.inlines.some(inline => inline.type == 'link')
} }
/**
* On change.
*
* @param {State} state
*/
onChange = (state) => {
this.setState({ state })
}
/** /**
* When clicking a link, if the selection has a link in it, remove the link. * When clicking a link, if the selection has a link in it, remove the link.
* Otherwise, add a new link with an href and text. * Otherwise, add a new link with an href and text.
@@ -91,6 +102,31 @@ class Links extends React.Component {
this.setState({ state }) this.setState({ state })
} }
/**
* On paste, if the text is a link, wrap the selection in a link.
*
* @param {Event} e
* @param {Object} paste
* @param {State} state
*/
onPaste = (e, paste, state) => {
if (state.isCollapsed) return
if (paste.type != 'text' && paste.type != 'html') return
if (!isUrl(paste.text)) return
let transform = state.transform()
if (this.hasLinks()) {
transform = transform.unwrapInline('link')
}
return transform
.wrapInline('link', { href: paste.text })
.collapseToEnd()
.apply()
}
/** /**
* Render the app. * Render the app.
* *
@@ -136,6 +172,7 @@ class Links extends React.Component {
state={this.state.state} state={this.state.state}
renderNode={this.renderNode} renderNode={this.renderNode}
onChange={this.onChange} onChange={this.onChange}
onPaste={this.onPaste}
/> />
</div> </div>
) )
@@ -152,16 +189,6 @@ class Links extends React.Component {
return NODES[node.type] return NODES[node.type]
} }
/**
* On change.
*
* @param {State} state
*/
onChange = (state) => {
this.setState({ state })
}
} }
/** /**

View File

@@ -41,6 +41,7 @@
"exorcist": "^0.4.0", "exorcist": "^0.4.0",
"gh-pages": "^0.11.0", "gh-pages": "^0.11.0",
"http-server": "^0.9.0", "http-server": "^0.9.0",
"is-url": "^1.2.2",
"mocha": "^2.5.3", "mocha": "^2.5.3",
"prismjs": "^1.5.1", "prismjs": "^1.5.1",
"react": "^15.2.0", "react": "^15.2.0",