I’m working on having the text-align and indentation of paragraphs stay the same after a user clicks Enter at the end of a paragraph. I know there’s a chain of commands that get run when a user clicks Enter, with the last one ending with splitBlock.
I want to write a similar method as splitBlockKeepMarks where I can keep the previous paragraph’s node’s attributes in the next paragraph node but am having trouble. My first thought was to create a new paragraph node w/ the previous node’s attrs and insert it at the current from position but that would make two paragraphs once splitBlock is called with the marks getting removed. I can’t use setNodeMarkup because the node doesn’t exist until splitBlock is called.
Does anyone have an idea on how I can do this or has done this before? Still learning more about ProseMirror.
first, store the paragraph’ mark and then split the paragraph, and at last use setStoredMarks with you stored marks before within one transaction, like:
const storedMark = $from.nodeBefore.marks; // you may need to filter some marks like link, etc
tr.split(pos, 1, pType);
tr.setStoredMarks(storedMark);
@Xheldon thanks for your response! Yeah, you can keep the marks with storedMarks, similarly to how splitBlockKeepMarks is written in prosemirror-commands but I’m wondering specifically about a node’s attributes.
A paragraph node will have a set of attributes and a set of marks.
splitNode will be passed a Node, not a NodeType, so you have a type error there. Also, you might want to actually use the attributes of the old node, rather than hard-coding 'left'. And depending on your schema, you’ll also need to look at what kind of node the existing node is, and decide how to split it based on that.
That code works for me (except that bind must be some custom function and I just used keymap to bind the command). TypeScript libraries not supporting pure JavaScript is not a thing.