mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-19 13:41:19 +02:00
Improve slate-react-placeholder component. (#2460)
* Improve slate-react-placeholder component. Now there can be multiple placeholders that do not interfere with each other. No developer facing changes. * Fix lint issue * Unique placeholder marks based on key, not type.
This commit is contained in:
@@ -1,6 +1,12 @@
|
|||||||
import invariant from 'tiny-invariant'
|
import invariant from 'tiny-invariant'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Instance counter to enable unique marks for multiple Placeholder instances.
|
||||||
|
*/
|
||||||
|
|
||||||
|
let instanceCounter = 0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A plugin that renders a React placeholder for a given Slate node.
|
* A plugin that renders a React placeholder for a given Slate node.
|
||||||
*
|
*
|
||||||
@@ -9,6 +15,12 @@ import React from 'react'
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function SlateReactPlaceholder(options = {}) {
|
function SlateReactPlaceholder(options = {}) {
|
||||||
|
const instanceId = instanceCounter++
|
||||||
|
const placeholderMark = {
|
||||||
|
type: 'placeholder',
|
||||||
|
data: { key: instanceId },
|
||||||
|
}
|
||||||
|
|
||||||
const { placeholder, when } = options
|
const { placeholder, when } = options
|
||||||
|
|
||||||
invariant(
|
invariant(
|
||||||
@@ -41,7 +53,7 @@ function SlateReactPlaceholder(options = {}) {
|
|||||||
const decoration = {
|
const decoration = {
|
||||||
anchor: { key: first.key, offset: 0 },
|
anchor: { key: first.key, offset: 0 },
|
||||||
focus: { key: last.key, offset: last.text.length },
|
focus: { key: last.key, offset: last.text.length },
|
||||||
mark: { type: 'placeholder' },
|
mark: placeholderMark,
|
||||||
}
|
}
|
||||||
|
|
||||||
return [...others, decoration]
|
return [...others, decoration]
|
||||||
@@ -59,7 +71,7 @@ function SlateReactPlaceholder(options = {}) {
|
|||||||
function renderMark(props, editor, next) {
|
function renderMark(props, editor, next) {
|
||||||
const { children, mark } = props
|
const { children, mark } = props
|
||||||
|
|
||||||
if (mark.type === 'placeholder') {
|
if (mark.type === 'placeholder' && mark.data.get('key') === instanceId) {
|
||||||
const style = {
|
const style = {
|
||||||
pointerEvents: 'none',
|
pointerEvents: 'none',
|
||||||
display: 'inline-block',
|
display: 'inline-block',
|
||||||
|
Reference in New Issue
Block a user