mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-12 10:14:02 +02:00
Allow custom placeholder styles (#2667)
* Allow custom placeholder styles * lint fixes * review feedback * lint
This commit is contained in:
committed by
Ian Storm Taylor
parent
2380aa094f
commit
c688ad7c72
@@ -34,6 +34,7 @@ import InputTester from './input-tester'
|
|||||||
import SyncingOperations from './syncing-operations'
|
import SyncingOperations from './syncing-operations'
|
||||||
import Tables from './tables'
|
import Tables from './tables'
|
||||||
import Mentions from './mentions'
|
import Mentions from './mentions'
|
||||||
|
import Placeholder from './placeholder'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Examples.
|
* Examples.
|
||||||
@@ -58,6 +59,7 @@ const EXAMPLES = [
|
|||||||
['Markdown Shortcuts', MarkdownShortcuts, '/markdown-shortcuts'],
|
['Markdown Shortcuts', MarkdownShortcuts, '/markdown-shortcuts'],
|
||||||
['Mentions', Mentions, '/mentions'],
|
['Mentions', Mentions, '/mentions'],
|
||||||
['Paste HTML', PasteHtml, '/paste-html'],
|
['Paste HTML', PasteHtml, '/paste-html'],
|
||||||
|
['Placeholders', Placeholder, '/placeholders'],
|
||||||
['Plain Text', PlainText, '/plain-text'],
|
['Plain Text', PlainText, '/plain-text'],
|
||||||
['Plugins', Plugins, '/plugins'],
|
['Plugins', Plugins, '/plugins'],
|
||||||
['Read-only', ReadOnly, '/read-only'],
|
['Read-only', ReadOnly, '/read-only'],
|
||||||
|
39
examples/placeholder/index.js
Normal file
39
examples/placeholder/index.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { Editor } from 'slate-react'
|
||||||
|
import PlaceholderPlugin from 'slate-react-placeholder'
|
||||||
|
import { Value } from 'slate'
|
||||||
|
import initialValue from './value.json'
|
||||||
|
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
class Placeholder extends React.Component {
|
||||||
|
value = Value.fromJSON(initialValue)
|
||||||
|
plugins = [
|
||||||
|
{
|
||||||
|
queries: {
|
||||||
|
isEmpty: editor => editor.value.document.text === '',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
PlaceholderPlugin({
|
||||||
|
placeholder:
|
||||||
|
'You can extensively customise your placeholder text using the slate-react-placeholder plugin!',
|
||||||
|
when: 'isEmpty',
|
||||||
|
style: { color: '#ADD8E6', opacity: '1', fontFamily: 'monospace' },
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the editor.
|
||||||
|
*
|
||||||
|
* @return {Component} component
|
||||||
|
*/
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return <Editor defaultValue={this.value} plugins={this.plugins} />
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export default Placeholder
|
16
examples/placeholder/value.json
Normal file
16
examples/placeholder/value.json
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"document": {
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"object": "block",
|
||||||
|
"type": "paragraph",
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"object": "text",
|
||||||
|
"leaves": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
@@ -86,7 +86,8 @@
|
|||||||
"slate-dev-test-utils": "*",
|
"slate-dev-test-utils": "*",
|
||||||
"slate-html-serializer": "*",
|
"slate-html-serializer": "*",
|
||||||
"slate-plain-serializer": "*",
|
"slate-plain-serializer": "*",
|
||||||
"slate-react": "*"
|
"slate-react": "*",
|
||||||
|
"slate-react-placeholder": "*"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"benchmark": "cross-env COMPARE=compare node --expose-gc ./tmp/benchmark/index.js",
|
"benchmark": "cross-env COMPARE=compare node --expose-gc ./tmp/benchmark/index.js",
|
||||||
|
@@ -21,7 +21,7 @@ function SlateReactPlaceholder(options = {}) {
|
|||||||
data: { key: instanceId },
|
data: { key: instanceId },
|
||||||
}
|
}
|
||||||
|
|
||||||
const { placeholder, when } = options
|
const { placeholder, when, style = {} } = options
|
||||||
|
|
||||||
invariant(
|
invariant(
|
||||||
placeholder,
|
placeholder,
|
||||||
@@ -72,18 +72,19 @@ function SlateReactPlaceholder(options = {}) {
|
|||||||
const { children, mark } = props
|
const { children, mark } = props
|
||||||
|
|
||||||
if (mark.type === 'placeholder' && mark.data.get('key') === instanceId) {
|
if (mark.type === 'placeholder' && mark.data.get('key') === instanceId) {
|
||||||
const style = {
|
const placeHolderStyle = {
|
||||||
pointerEvents: 'none',
|
pointerEvents: 'none',
|
||||||
display: 'inline-block',
|
display: 'inline-block',
|
||||||
width: '0',
|
width: '0',
|
||||||
maxWidth: '100%',
|
maxWidth: '100%',
|
||||||
whiteSpace: 'nowrap',
|
whiteSpace: 'nowrap',
|
||||||
opacity: '0.333',
|
opacity: '0.333',
|
||||||
|
...style,
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<span>
|
<span>
|
||||||
<span contentEditable={false} style={style}>
|
<span contentEditable={false} style={placeHolderStyle}>
|
||||||
{placeholder}
|
{placeholder}
|
||||||
</span>
|
</span>
|
||||||
{children}
|
{children}
|
||||||
|
Reference in New Issue
Block a user