Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please Explain how to set/test a blank state #713

Closed
thepian opened this issue Apr 5, 2017 · 14 comments
Closed

Please Explain how to set/test a blank state #713

thepian opened this issue Apr 5, 2017 · 14 comments
Labels

Comments

@thepian
Copy link

thepian commented Apr 5, 2017

How do I set state to blank?

How do I test if the state is blank?

@Rebulus
Copy link
Contributor

Rebulus commented Apr 5, 2017

As I see in this comment, it is const emptyState = Plain.deserialize(''). I hope this helps you 😊
But, maybe, it is good point to clarify the empty state.

@thepian
Copy link
Author

thepian commented Apr 6, 2017

I think it would be really helpful to document as I for one spent several hours trying to figure this out. Now that you give me the example I feel dumb for not thinking of it, but when you're learning the mind plays a lot of tricks on you. My solution to illustrate was much more involved...

        return Raw.deserialize({
            nodes: [
                {
                    kind: 'block',
                    type: 'paragraph',
                    nodes: [
                        { kind: 'text', text: '' },
                    ]
                },
            ]
        }, {terse: true});

Does the terse flag apply for the Plain serialiser ?

@oyeanuj
Copy link
Contributor

oyeanuj commented Apr 17, 2017

@thepian

Does the terse flag apply for the Plain serialiser ?

Nope, only for Raw.

@ianstormtaylor What is the recommended way to initialize or set an empty state using Raw?

@ianstormtaylor
Copy link
Owner

@oyeanuj the code sample @thepian has above would be how you'd do it, depending on what your "default" block is for your editor.

Open to PRs that make the docs easier to find this info!

@thepian
Copy link
Author

thepian commented Apr 27, 2017

Actually I find a potential bug relating to this. My current state constructor function uses the Plain serialiser to create a blank value.

    makeState(value) {
        if (value) {
            return Raw.deserialize(value || { nodes: [] }, { terse: true });
        }

        return Plain.deserialize('', { terse: true });
    }

When I added line as a block type I could no longer provoke blank content, so the placeholder never shows up. The line type seems to be hardcoded. I think that the blank state needs to be special, or an isBlank function needs to be a configuration point.

@mxstbr
Copy link
Contributor

mxstbr commented Jun 1, 2017

Running into the same issue as @thepian at the moment, seems like adding a line block type breaks the placeholder. 😕

@PierBover
Copy link
Contributor

PierBover commented Oct 8, 2018

In case anyone stumbles here from Google this worked for me:

import Plain from 'slate-plain-serializer';
const emptyState = Plain.deserialize('');

@DominikSerafin
Copy link
Contributor

DominikSerafin commented Apr 1, 2019

The line issue seems to be fixed now (I'm on Slate 0.44.x).

And for people that would like to avoid additional dependency, the Plain.deserialize('') is equivalent to Value.create(emptyValue) or Value.fromJSON(emptyValue), where emptyValue is as below:

var emptyValue = {
  'document': {
    'nodes': [
      {
        'object': 'block',
        'type': 'line',
        'nodes': [
          {
            'object': 'text',
            'leaves': [
              {
                'text': ''
              },
            ]
          }
        ]
      },
    ]
  }
}

slate-empty-value

@davidwieler
Copy link

Update to @DominikSerafin's answer;
As of slate@0.46, the leaves property of Text nodes has been removed, so his emptyValue will fire a depreciation warning in the console.

I use:

const emptyValue = {
    document: {
        nodes: [
            {
                object: 'block',
                type: 'paragraph',
                nodes: [
                    {
                        object: 'text',
                        text: ''
                    }
                ]
            }
        ]
    }
}

Feel free to change the type from paragraph to text if you don't want to start off with a <p> tag.

@10thfloor
Copy link

Is there an example of code anywhere that demonstrates clearly how to clear the editor?
I can't find one, and the above code does not work.

@hanselke
Copy link

Hi,

so i was looking for a way to clear the editor when a button was pressed.

solution was

const blankSlateValue = [{ type:"paragraph", children: [{ text:"" }] }] setValue(blankSlateValue)

this uses the default setValue method in the slatejs example

@uriva
Copy link

uriva commented Aug 25, 2021

didn't work for me, got

Uncaught Error: Cannot resolve a DOM point from Slate point: {"path":[0,0],"offset":5}

@uriva
Copy link

uriva commented Aug 25, 2021

this worked:

event.preventDefault();
Transforms.select(editor, Editor.start(editor, []));
setValue([
  {
    type: "paragraph",
    children: [{ text: "" }],
  },
]);

@Deliaz
Copy link

Deliaz commented Sep 16, 2021

The only way it works for me:

const point = { path [0, 0], offset: 0 };
editor.selection = { anchor: point, focus: point }; // clean up selection
editor.history = { redos: [], undos: [] }; // clean up history
setEditorValue([{ type:"paragraph", children: [{ text:"" }] }]); // reset to empty state

From this article

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests