mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 13:11:17 +02:00
remove void spacers in readonly, add tests
This commit is contained in:
@@ -825,7 +825,7 @@ class Content extends React.Component {
|
|||||||
key={this.tmp.forces}
|
key={this.tmp.forces}
|
||||||
ref={this.ref}
|
ref={this.ref}
|
||||||
data-key={document.key}
|
data-key={document.key}
|
||||||
contentEditable={!readOnly}
|
contentEditable={readOnly ? null : true}
|
||||||
suppressContentEditableWarning
|
suppressContentEditableWarning
|
||||||
className={className}
|
className={className}
|
||||||
onBeforeInput={this.onBeforeInput}
|
onBeforeInput={this.onBeforeInput}
|
||||||
|
@@ -87,6 +87,7 @@ class Void extends React.Component {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
onDragEnter = (event) => {
|
onDragEnter = (event) => {
|
||||||
|
if (this.props.readOnly) return
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +98,7 @@ class Void extends React.Component {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
onDragOver = (event) => {
|
onDragOver = (event) => {
|
||||||
|
if (this.props.readOnly) return
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,13 +128,9 @@ class Void extends React.Component {
|
|||||||
const { props } = this
|
const { props } = this
|
||||||
const { children, node, readOnly } = props
|
const { children, node, readOnly } = props
|
||||||
const Tag = node.kind == 'block' ? 'div' : 'span'
|
const Tag = node.kind == 'block' ? 'div' : 'span'
|
||||||
let style
|
const style = {
|
||||||
|
height: '0',
|
||||||
if (!readOnly) {
|
color: 'transparent'
|
||||||
style = {
|
|
||||||
height: '0',
|
|
||||||
color: 'transparent'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.debug('render', { props })
|
this.debug('render', { props })
|
||||||
@@ -146,10 +144,10 @@ class Void extends React.Component {
|
|||||||
onDragEnter={this.onDragEnter}
|
onDragEnter={this.onDragEnter}
|
||||||
onDragStart={this.onDragStart}
|
onDragStart={this.onDragStart}
|
||||||
>
|
>
|
||||||
<Tag style={style}>
|
{!readOnly && <Tag style={style}>
|
||||||
{this.renderText()}
|
{this.renderText()}
|
||||||
</Tag>
|
</Tag>}
|
||||||
<Tag contentEditable={false}>
|
<Tag contentEditable={readOnly ? null : false}>
|
||||||
{children}
|
{children}
|
||||||
</Tag>
|
</Tag>
|
||||||
</Tag>
|
</Tag>
|
||||||
|
@@ -0,0 +1,36 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import React from 'react'
|
||||||
|
import h from '../../helpers/h'
|
||||||
|
|
||||||
|
export const schema = {
|
||||||
|
nodes: {
|
||||||
|
image: (props) => {
|
||||||
|
return (
|
||||||
|
React.createElement('img', { src: props.node.data.get('src'), ...props.attributes })
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const props = {
|
||||||
|
readOnly: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const state = (
|
||||||
|
<state>
|
||||||
|
<document>
|
||||||
|
<image src="https://example.com/image.png" />
|
||||||
|
</document>
|
||||||
|
</state>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = `
|
||||||
|
<div data-slate-editor="true">
|
||||||
|
<div data-slate-void="true">
|
||||||
|
<div>
|
||||||
|
<img src="https://example.com/image.png">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`.trim()
|
@@ -0,0 +1,50 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import React from 'react'
|
||||||
|
import h from '../../helpers/h'
|
||||||
|
|
||||||
|
export const schema = {
|
||||||
|
nodes: {
|
||||||
|
emoji: (props) => {
|
||||||
|
return (
|
||||||
|
React.createElement('img', props.attributes)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const props = {
|
||||||
|
readOnly: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const state = (
|
||||||
|
<state>
|
||||||
|
<document>
|
||||||
|
<paragraph>
|
||||||
|
<emoji />
|
||||||
|
</paragraph>
|
||||||
|
</document>
|
||||||
|
</state>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = `
|
||||||
|
<div data-slate-editor="true">
|
||||||
|
<div style="position:relative">
|
||||||
|
<span>
|
||||||
|
<span>
|
||||||
|
<span data-slate-zero-width="true"> </span>
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
<span data-slate-void="true">
|
||||||
|
<span>
|
||||||
|
<img>
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
<span>
|
||||||
|
<span data-slate-zero-width="true"> </span>
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`.trim()
|
@@ -19,14 +19,15 @@ describe('rendering', () => {
|
|||||||
for (const test of tests) {
|
for (const test of tests) {
|
||||||
it(test, async () => {
|
it(test, async () => {
|
||||||
const module = require(resolve(dir, test))
|
const module = require(resolve(dir, test))
|
||||||
const { state, schema, output } = module
|
const { state, schema, output, props } = module
|
||||||
const props = {
|
const p = {
|
||||||
state,
|
state,
|
||||||
schema,
|
schema,
|
||||||
onChange() {},
|
onChange() {},
|
||||||
|
...(props || {}),
|
||||||
}
|
}
|
||||||
|
|
||||||
const string = ReactDOM.renderToStaticMarkup(<Editor {...props} />)
|
const string = ReactDOM.renderToStaticMarkup(<Editor {...p} />)
|
||||||
const expected = parse5.serialize(parse5.parseFragment(output))
|
const expected = parse5.serialize(parse5.parseFragment(output))
|
||||||
.trim()
|
.trim()
|
||||||
.replace(/\n/gm, '')
|
.replace(/\n/gm, '')
|
||||||
|
Reference in New Issue
Block a user