Closed ianstormtaylor closed 6 years ago
There are valid use cases for wanting empty inlines.
Can I know the user case of wanting empty inline without isVoid
?
There was someone in chat wanting to essentially use inlines like a <input />
element behaves, but they always start with an empty value. (Although they may have been wanting isVoid
, I'm not sure.)
Either way though I think it would be nice if possible if core was unrestrictive about this.
Hi 👋
I think it was me who asked about requirement for non-empty text
. On the one hand documentation says:
Inlines can't contain no text. Any inline node whose text is an empty string ('') will be automatically removed. This makes sense when you think about a user backspacing through an inline. When they delete that last character, they'd expect the inline to be removed. And when there are no characters, you can't really put your selection into the inline anymore. So Slate removes them from the document automatically, to simplify things.
On the other hand I have code like this one:
const EditableFieldNode = {
nodeType: NODE_TYPE,
createNode: ({ name, placeholder }) => ({
object: "inline",
type: NODE_TYPE,
data: {
name,
placeholder,
},
nodes: [
{
object: "text",
leaves: [
{
// TODO PoC: what to put here? It's not visible, but required to be non-empty
text: name,
},
],
},
],
}),
plugins: [renderPlugin()],
};
I can do it like that:
{
// Any non-empty text is required here, even if it's not rendered. See https://docs.slatejs.org/guides/data-model#documents-and-nodes
text: "x",
},
but then I want to be sure there are not surprising side-effects depending on, for example, length of dummy text 😄
Do you want to request a feature or report a bug?
Improvement.
What's the current behavior?
Right now there is a core schema rule that enforces that inline nodes must have text that is not an empty string, or they will be removed. But this is not ideal, it's not great to force this on users, since it's fairly arbitrary.
There are valid use cases for wanting empty inlines.
The main reason this was done was to eliminate the possibility of having "leftover" inlines in the document if a user has deleted the last character of link (or similar). But this was added in a much older iteration of Slate, before the schema was solidified.
What's the expected behavior?
These days, I think we can achieve the same goal in userland with a fairly simple schema rule with a regex for
text
, like:Which by default will be removed, or in edge cases can be customized.
And that way the core schema that isn't overridable is slightly less restrictive.