I’ve been trying to write a command for a while now but I’m apparently not smart enough to figure it out myself. I’m hoping the generous experts in this forum could give me some guidance.
Basically what I want to do is cut out anything selected, split any inline node as well as the wrapping block node one level up, and insert another block node wrapping the selected content in that split. I’ve tried several different transactions but none have worked so far.
This is a simplified schema I’m using, omitting extra unnecessary content like marks and attributes.
let schema = new Schema({
nodes: {
doc: {
content: 'question'
},
question: {
content: 'block+',
...
},
paragraph: {
content: 'inline*',
group: 'block',
...
},
math_display: {
group: 'block math',
content: 'text*',
atom: true,
code: true,
},
text: {
group: 'inline'
}
},
})
This is an example starting state of the document. Notice in the innermost text node I have positioned the selection $from and $to
const INITIAL_STATE = {
type: 'doc',
content: [
{
type: 'question',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Some $from text $to here'
},
]
},
]
}
]
}
And finally this is how I would like the state to end up.
const FINAL_STATE = {
type: 'doc',
content: [
{
type: 'question',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Some '
},
]
},
{
type: 'math_display',
content: [
{
type: 'text',
text: '$from text $to'
}
]
},
{
type: 'paragraph',
content: [
{
type: 'text',
text: ' here'
},
]
},
]
}
]
}
I’ve tried different combinations of using split, replaceSelectionWith, replaceWith, setBlockType… I’ve tried a lot of things but haven’t been able to get things to work right. I think part of the problem I’m having is that methods like replaceSelectionWith attempt to insert a math_display node into a paragraph node, which is not a valid operation.