mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 19:52:32 +02:00
fix "secret" behavior of image example
This commit is contained in:
@@ -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 isImage from 'is-image-url'
|
||||||
import { Map } from 'immutable'
|
import { Map } from 'immutable'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,6 +84,7 @@ class Images 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>
|
||||||
)
|
)
|
||||||
@@ -119,18 +121,35 @@ class Images extends React.Component {
|
|||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const src = window.prompt('Enter the URL of the image:')
|
const src = window.prompt('Enter the URL of the image:')
|
||||||
if (!src) return
|
if (!src) return
|
||||||
this.insertImage(src)
|
let { state } = this.state
|
||||||
|
state = this.insertImage(state, src)
|
||||||
|
this.onChange(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On paste, if the pasted content is an image URL, insert it.
|
||||||
|
*
|
||||||
|
* @param {Event} e
|
||||||
|
* @param {Object} paste
|
||||||
|
* @param {State} state
|
||||||
|
* @return {State}
|
||||||
|
*/
|
||||||
|
|
||||||
|
onPaste = (e, paste, state) => {
|
||||||
|
if (paste.type != 'text') return
|
||||||
|
if (!isImage(paste.text)) return
|
||||||
|
return this.insertImage(state, paste.text)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert an image with `src` at the current selection.
|
* Insert an image with `src` at the current selection.
|
||||||
*
|
*
|
||||||
|
* @param {State} state
|
||||||
* @param {String} src
|
* @param {String} src
|
||||||
|
* @return {State}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
insertImage = (src) => {
|
insertImage = (state, src) => {
|
||||||
let { state } = this.state
|
|
||||||
|
|
||||||
if (state.isExpanded) {
|
if (state.isExpanded) {
|
||||||
state = state
|
state = state
|
||||||
.transform()
|
.transform()
|
||||||
@@ -141,10 +160,13 @@ class Images extends React.Component {
|
|||||||
const { anchorBlock, selection } = state
|
const { anchorBlock, selection } = state
|
||||||
let transform = state.transform()
|
let transform = state.transform()
|
||||||
|
|
||||||
if (anchorBlock.text != '') {
|
if (anchorBlock.type == 'image') {
|
||||||
|
transform = transform.splitBlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (anchorBlock.text != '') {
|
||||||
if (selection.isAtEndOf(anchorBlock)) {
|
if (selection.isAtEndOf(anchorBlock)) {
|
||||||
transform = transform
|
transform = transform.splitBlock()
|
||||||
.splitBlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (selection.isAtStartOf(anchorBlock)) {
|
else if (selection.isAtStartOf(anchorBlock)) {
|
||||||
@@ -161,15 +183,13 @@ class Images extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state = transform
|
return transform
|
||||||
.setBlock({
|
.setBlock({
|
||||||
type: 'image',
|
type: 'image',
|
||||||
isVoid: true,
|
isVoid: true,
|
||||||
data: { src }
|
data: { src }
|
||||||
})
|
})
|
||||||
.apply()
|
.apply()
|
||||||
|
|
||||||
this.setState({ state })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
"detect-browser": "^1.3.3",
|
"detect-browser": "^1.3.3",
|
||||||
"esrever": "^0.2.0",
|
"esrever": "^0.2.0",
|
||||||
"immutable": "^3.8.1",
|
"immutable": "^3.8.1",
|
||||||
|
"is-image-url": "^1.1.8",
|
||||||
"keycode": "^2.1.2",
|
"keycode": "^2.1.2",
|
||||||
"lodash": "^4.13.1",
|
"lodash": "^4.13.1",
|
||||||
"react-portal": "^2.2.0",
|
"react-portal": "^2.2.0",
|
||||||
|
Reference in New Issue
Block a user