1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-11 17:53:59 +02:00

Allow custom placeholder styles (#2667)

* Allow custom placeholder styles

* lint fixes

* review feedback

* lint
This commit is contained in:
Hanna Greaves
2019-04-02 14:23:50 +01:00
committed by Ian Storm Taylor
parent 2380aa094f
commit c688ad7c72
5 changed files with 63 additions and 4 deletions

View File

@@ -34,6 +34,7 @@ import InputTester from './input-tester'
import SyncingOperations from './syncing-operations'
import Tables from './tables'
import Mentions from './mentions'
import Placeholder from './placeholder'
/**
* Examples.
@@ -58,6 +59,7 @@ const EXAMPLES = [
['Markdown Shortcuts', MarkdownShortcuts, '/markdown-shortcuts'],
['Mentions', Mentions, '/mentions'],
['Paste HTML', PasteHtml, '/paste-html'],
['Placeholders', Placeholder, '/placeholders'],
['Plain Text', PlainText, '/plain-text'],
['Plugins', Plugins, '/plugins'],
['Read-only', ReadOnly, '/read-only'],

View 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

View File

@@ -0,0 +1,16 @@
{
"document": {
"nodes": [
{
"object": "block",
"type": "paragraph",
"nodes": [
{
"object": "text",
"leaves": []
}
]
}
]
}
}

View File

@@ -86,7 +86,8 @@
"slate-dev-test-utils": "*",
"slate-html-serializer": "*",
"slate-plain-serializer": "*",
"slate-react": "*"
"slate-react": "*",
"slate-react-placeholder": "*"
},
"scripts": {
"benchmark": "cross-env COMPARE=compare node --expose-gc ./tmp/benchmark/index.js",

View File

@@ -21,7 +21,7 @@ function SlateReactPlaceholder(options = {}) {
data: { key: instanceId },
}
const { placeholder, when } = options
const { placeholder, when, style = {} } = options
invariant(
placeholder,
@@ -72,18 +72,19 @@ function SlateReactPlaceholder(options = {}) {
const { children, mark } = props
if (mark.type === 'placeholder' && mark.data.get('key') === instanceId) {
const style = {
const placeHolderStyle = {
pointerEvents: 'none',
display: 'inline-block',
width: '0',
maxWidth: '100%',
whiteSpace: 'nowrap',
opacity: '0.333',
...style,
}
return (
<span>
<span contentEditable={false} style={style}>
<span contentEditable={false} style={placeHolderStyle}>
{placeholder}
</span>
{children}