Initialize Node with String

Hi,

Is there any way to get the selection set at the first pos in the Code Mirror node so that I can use tr,insertText()? Or can I pass a string when I am using createAndFill()? Basically, my codeMirror node is nested within another node, and I want to initialize that with some code before creating the parent node. Would appreciate any help

    let nodeType = schema.nodes.search;
    let codeType = nodeType.contentExpr.elements[0].nodeTypes[0];
    let a = codeType.createAndFill();
    let b = nodeType.createAndFill({viewType: 0}, a);

This is my current code. I want to the codeType node to have some content, which I am passing as a string. I understand that createAndFill() takes in a fragment or a node, so I wanted to know how I can manipulate that to take a string, or convert a string to a fragment

Thanks

If you know what the content of a node should be, you probably want create rather than createAndFill

I could use create instead, but the problem would be the same.

I could create a text node first, create the codeMirror node using that, and then create my node, using the initialized node. However, how can I create the node, just from a string? Or convert the string to a fragment?

Yeah, that’s what you do. There’s no built-in convenience methods for that, but it’s pretty easy to write them if you are writing lots of code like this.

Cool. So if I get it straight, I have to do the following:

  1. Create Text Node using the string that I am passing
  2. Use the text node to create codeBlock
  3. Use the codeBlock to create my required node

I am still a bit uncertain as to how I can execute step 1. Since I would have the same problem of creating a node using a string. I tried using tr.insertText, however, I could not get the selection to get inside the codeMirror node by default.

Sorry for troubling you so much @marijn, but this has been bugging me for some time now:P

Got it. schema,text() helps. Thanks @marijn :slight_smile: