From 2b38fb3f3a033dfcd62de8e37b375c73f6ddb6bb Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Thu, 28 Nov 2019 16:18:15 -0500 Subject: [PATCH] fix placeholder prop --- .../slate-react/src/components/children.tsx | 11 +++--- .../slate-react/src/components/editable.tsx | 22 ++++++++++-- packages/slate-react/src/react-editor.ts | 36 +++---------------- packages/slate-react/src/utils/weak-maps.ts | 1 - site/examples/links.js | 6 +++- site/examples/plain-text.js | 2 +- 6 files changed, 36 insertions(+), 42 deletions(-) diff --git a/packages/slate-react/src/components/children.tsx b/packages/slate-react/src/components/children.tsx index 5d75a8ccd..869be8069 100644 --- a/packages/slate-react/src/components/children.tsx +++ b/packages/slate-react/src/components/children.tsx @@ -1,9 +1,9 @@ import React from 'react' import { Editor, Range, Element, NodeEntry, Ancestor, Descendant } from 'slate' -import { ReactEditor } from '..' import ElementComponent from './element' import TextComponent from './text' +import { ReactEditor } from '..' import { useEditor } from '../hooks/use-editor' import { NODE_TO_INDEX, NODE_TO_PARENT } from '../utils/weak-maps' import { @@ -36,6 +36,7 @@ const Children = (props: { } = props const editor = useEditor() const path = ReactEditor.findPath(editor, node) + const decs = decorations.concat(decorate([node, path])) const children = [] const isLeafBlock = Element.isElement(node) && @@ -48,13 +49,13 @@ const Children = (props: { const key = ReactEditor.findKey(editor, n) const range = Editor.range(editor, p) const sel = selection && Range.intersection(range, selection) - const decs = decorate([n, p]) + const ds = decorate([n, p]) - for (const dec of decorations) { + for (const dec of decs) { const d = Range.intersection(dec, range) if (d) { - decs.push(d) + ds.push(d) } } @@ -62,7 +63,7 @@ const Children = (props: { children.push( (null) // Update internal state on each render. - PLACEHOLDER.set(editor, placeholder) IS_READ_ONLY.set(editor, readOnly) // Keep track of some state for the event handler logic. @@ -349,6 +348,23 @@ export const Editable = (props: { [] ) + const decorations = [] + + if ( + placeholder && + editor.children.length === 1 && + Array.from(Node.texts(editor)).length === 1 && + Node.text(editor) === '' + ) { + const start = Editor.start(editor, []) + decorations.push({ + [PLACEHOLDER_SYMBOL]: true, + placeholder, + anchor: start, + focus: start, + }) + } + return (
{ + removals.forEach(el => { el!.parentNode!.removeChild(el) }) diff --git a/packages/slate-react/src/utils/weak-maps.ts b/packages/slate-react/src/utils/weak-maps.ts index 7e2e0d1f0..1ae71d08b 100644 --- a/packages/slate-react/src/utils/weak-maps.ts +++ b/packages/slate-react/src/utils/weak-maps.ts @@ -29,7 +29,6 @@ export const IS_READ_ONLY: WeakMap = new WeakMap() export const IS_FOCUSED: WeakMap = new WeakMap() export const IS_DRAGGING: WeakMap = new WeakMap() export const IS_CLICKING: WeakMap = new WeakMap() -export const PLACEHOLDER: WeakMap = new WeakMap() /** * Symbols. diff --git a/site/examples/links.js b/site/examples/links.js index 0cde86b75..b8b10018a 100644 --- a/site/examples/links.js +++ b/site/examples/links.js @@ -83,7 +83,11 @@ const Element = ({ attributes, children, element }) => { switch (element.type) { case 'link': return ( - + {children} ) diff --git a/site/examples/plain-text.js b/site/examples/plain-text.js index a74914159..e85ecdcd5 100644 --- a/site/examples/plain-text.js +++ b/site/examples/plain-text.js @@ -16,7 +16,7 @@ const initialValue = [ { children: [ { - text: 'This is editable plain text, just like a