Detect section break in the selection

Hello, could you help me? I need to detect if a "Section Break" is present in the selection. How can I do that?
I already tried using selection.text, but it detects it as \r, the same as when I press Enter in the document.


8 Replies

DS Dhanush Sekar Syncfusion Team April 3, 2025 05:26 PM UTC

Hi Oleksandr,


The start and end offsets of a selection can be obtained using the selection module. The initial index in the offset corresponds to the section index.

ElementHierarchical FormatExplanation
Move selection to ParagraphsectionIndex;blockIndex;offset
Ex: 0;0;0
It moves the cursor to the start of paragraph.

On the selection change event, retrieve the start and end offsets and compare their section indices. A section break exists if the section indices differ; if they are the same, no section break is present within the selection.

Move selection to position in React Document editor | Syncfusion

Please let us know whether this solution meets your requirements


Regards,

Dhanush S



OK Oleksandr Kovalenko April 7, 2025 02:57 PM UTC

Thank you, Dhanush.

I have another question: what should I do if I select only the Section Break?
As you can see in the screenshot, the startOffset is 2;31;0 and the endOffset is 2;32;1


Screenshort:
Image_1097_1744037267467



DS Dhanush Sekar Syncfusion Team April 8, 2025 01:43 PM UTC

Hi Olaksandr,


The provided screenshot has been reviewed, and it has been confirmed that the selection does not span across the section break. The selected paragraph is located within the same section, which means the section index remains unchanged. This is the expected behavior.


Regards,

Dhanush S



OK Oleksandr Kovalenko April 8, 2025 01:57 PM UTC

I understand it, do you have any solutions on how I can catch this?



DS Dhanush Sekar Syncfusion Team April 9, 2025 02:17 PM UTC

Hi Olaksandr,


It's not possible to achieve your requirement.

Could you please share more details about your use case? So, we can check the alternative solution from our end.


Regards,

Dhanush S




OK Oleksandr Kovalenko April 9, 2025 02:54 PM UTC

Sure, I want to prevent remove "Section Break". 

Example of my code to detect if user selects text in two sections. Thanks for helping with this.


 if (code === 'Backspace' || code === 'Delete') {
    const docEditor = args.source;
    const { selection } = docEditor;
    const { startOffset, endOffset } = selection;

    const startSectionIndex = startOffset.split(';')[0];
    const endSectionIndex = endOffset.split(';')[0];



    if (startSectionIndex !== endSectionIndex) {
      wordRootStore.modals.openModal('DeleteSectionBreak');
      eventPreventDefault(args);
    }

  }

But I also need to prevent deletion if the user selects only a section break.



DS Dhanush Sekar Syncfusion Team April 14, 2025 05:13 AM UTC

Hi Olaksandr,


We apologize for the delay.

We have validated your query, and it's impossible to achieve your requirement using Document editor


Regards,

Dhanush S



DS Dhanush Sekar Syncfusion Team April 14, 2025 05:13 AM UTC

Hi Olaksandr,


We apologize for the delay.

We have validated your query, and it's impossible to achieve your requirement using Document editor


Regards,

Dhanush S


Loader.
Up arrow icon