I am trying to implement Sections inside the Canvas. A section should have a title, a due date and it should be possible to rearrange sections using the arrow-up/arrow-down buttons on the gray right panel.
What would be the right way of moving a section up or down? My idea was the following, say I want to move section 5 down:
- I first copy all the content from section 5, i.e. the section itself and everything until the next section.
- Insert it below section 6.
- And then delete the range where section 5 was before.
private moveSectionDown(sectionNodes: Array<{id: string, pos: number, text: string}>, index: number){
const sectionToMove = sectionNodes[index];
const sectionBelow = sectionNodes[index + 1];
// begining of last section or end of document if section below is the last one.
const sectionBelowEnd = this.isLastSection(sectionNodes, index + 1) ? this.currentCanvasState.doc.content.size - 1 : sectionNodes[index + 2].pos;
const sliceToMove = this.currentCanvasState.doc.cut(sectionToMove.pos, sectionBelow.pos);
const moveTransaction = this.currentCanvasState.tr.insert(sectionBelowEnd, sliceToMove);
this.dispatchTransactionToCanvas(moveTransaction);
// after this delete copied section.
}
var sectionNodes = [
{"id":"section_hhujde40","text":"section 1","pos":1},
{"id":"section_bv9sl5j5","text":"section 5","pos":22},
{"id":"section_rt84rmt6","text":"section 6 ","pos":43}
];
moveSectionDown(sectionNodes, 1);
But this is not working. After the transaction is dispatched the editor remains the same.