Slate: Documentation: removeMark() checks `data` along with `type`

Created on 18 May 2017  Â·  7Comments  Â·  Source: ianstormtaylor/slate

It was not at all clear to me that when you call removeMark(type), it also checks mark's data parameter and will not remove a mark even if it matches the type if the data doesn't match as well.

docs ♥ help

Most helpful comment

Open to fixing this if anyone wants to make a PR.

All 7 comments

The use case here is: the editor is in the readOnly state, but the user can highlight a section and click "add comment". Then I want to add a mark to show the selection. This is a very important feature for my project, but right now there is no way to easily create/get Slate's Selection object.

(By the way, thank you to everyone who is contributing to this project. This is the best editor!)

See also #873, which evolved into a discussion about this. (Unfortunately without any clear decision)

You can also follow this discussion to see the different cases removeMark needs to cover.

The decision so far is to have removeMark remove _specific_ Marks (that is, the type + associated data), and iterating state.marks manually and removing the marks as necessary based on your custom condition.

Slate _may_ eventually get a removeMark(String) that removes all marks of a given type, but I think that's still an open subject.

I understand there's no direct API to remove marks with a certain type. But is there any workaround for this?

I'm using the mark to toggle text colors. I put the color into the data of the mark. And everytime the color is changed, I would like to remove the mark and replace it with a new one with updated data. Is this the right way of implementing such functions in slate? Please help to advise.

Thanks in advance and looking forward to suggestions.

Hey folks—just wanted to chime in here. Just got bit by this and it took a lot of source exploration to track down. The Slate removeMark and replaceMark methods imply that the arguments can be string types or mark objects, but it turns out you really cannot use the string version of the function if your mark has a data payload, since leaf.marks.has(mark) is always false when comparing the value marks to the inflated just-a-type-and-no-data mark.

In case it's helpful, here's what I was naiively trying to do. I think this API behavior is fine but it should definitely be documented that the string shorthand only works when the mark has no data.

  editor.replaceMark(EMOJI_TYPING_TYPE, {
    type: EMOJI_TYPING_TYPE,
    data: {  // updated data // },
  });

Open to fixing this if anyone wants to make a PR.

As of https://github.com/ianstormtaylor/slate/pull/3093 (which was just merged), I believe this issue is no longer applicable, because a lot has changed. I'm going through and closing out any potential issues that are not out of date with the overhaul. Thanks for understanding.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexeiAndreev picture AlexeiAndreev  Â·  3Comments

ianstormtaylor picture ianstormtaylor  Â·  3Comments

adrianclay picture adrianclay  Â·  3Comments

chriserickson picture chriserickson  Â·  3Comments

JSH3R0 picture JSH3R0  Â·  3Comments