Skip to content

How to get a "normally" working editor? #5671

Open
@ecosta1020

Description

@ecosta1020

Hi, I'm new to slate (know some JS, and a bit of TS), and I've been struggling to get it to behave as the regular editor I think we're all used to.

Whenever I press enter, say in the rich-text example, a new paragraph is created. But I'd normally expect that to simply insert a line-break instead, though, and only if enter is pressed twice such that there are two in a row, should the interface be allowed to transform these nodes by means of actually removing both line-breaks and letting me enter into a fresh <p>. Or is it that I'm missing something really obvious here?

By perusing the source code, the closest I could see is ZeroWidthString from within packages/slate-react/src/components/string.tsx, but this module is not reachable from user space. Before that, I saw Editor.insertSoftBreak and Editor.insertBreak, but both have the same effect of inserting a paragraph, so I'm a bit lost.

Can somebody give me a clue? maybe even including some pseudo-code at least, on how to intercept the enter key in a non-hacky way (i.e: if (event.keyCode == 13) ...) from within the rich-text examples onKeyDown hook? Though I'm tempted to think there's something I'm not understanding properly and that my approach might be wrong altogether, so setting me on track would be same as
appreciated.

And as I'm here, would also like to take the opportunity to ask where do deleteWordBackward, deleteWordForward, insertLineBreak, etc. come from?

These are in a big switch on export const Editable = (props: EditableProps) at packages/slate-react/src/components/editable.tsx, but I could only trace these back to the type variable, which appears for the first time at the line 540:

const isCompositionChange =
  type === 'insertCompositionText' || type === 'deleteCompositionText'

So even if these are React constants, the line above got me puzzled because type seems to come from nowhere.

Thanks,

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions