Keep previous paragraph node's attributes when user clicks Enter

Hi,

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.

you can just use storeMarks.

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.

Hello, i want to know if you have a good way to resolve this problem now ? :sob: This problem has caught me

Try the new splitBlockAs command-builder, maybe.

1 Like

wow it’s new , but i don’t konw how to use . I want to keep previous paragraph’s attrs like ‘indent’ and ‘align’ when I hit ‘Enter’

let paragraphType = schema.nodes.paragraph

bind("Enter", splitBlockAs(splitNode(paragraphType,true)))

function splitNode(nodeType , atEnd){
  return {
    type: nodeType,
    attrs: { indent: 'left' ,align: 'left'}
  }
}

but it’s wrong , can you write a use case , thank you very much :pray:

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.

thanks for your reply , I seem to know how to do it :rose:

hey , i’m back. Is this wrong ? I saw an error on the console: image with the code is:

bind("Enter", splitBlockAs(n => ({type: n.type.schema.nodes.heading, attrs: {level: 2}})))

Does it not support pure JavaScript or is there any other error?

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.

I have fixed it now, and the function I want is perfectly realized. Thank you very much !!! :star_struck: