soccerloway / quill-better-table

Module for better table in Quill, more useful features are supported.
MIT License
319 stars 121 forks source link

Clicking "insert table" when it's inside another table breaks the table #5

Closed glebtv closed 5 years ago

glebtv commented 5 years ago

Like this: DeepinScreenshot_select-area_20190520115114

glebtv commented 5 years ago

Also inserting headers is broken

soccerloway commented 5 years ago

Hi, @glebtv It was really a problem. quill-better-table can not support inserting other Block blots into table cells except TableCellLine now. And also can not support other Container blots in table cell. To support these, I think it might need to modify these relative build-in blots. It will have an effect on others. If you really need the supports ?

soccerloway commented 5 years ago

@glebtv In my product, I disabled inserting table when the cursor is in table cell line.

MuhammedAlkhudiry commented 5 years ago

I fixed this issue by making the editor insert the new table after the existed table:

    if (isCaretInTable()) {
        const caretContainer = window.getSelection().getRangeAt(0).endContainer;
        let nextElement = caretContainer.closest('.quill-better-table-wrapper').nextElementSibling;
        setRangeIn(nextElement);
        quill.insertText(quill.getSelection(), '\n');
    }

    // tableModule.insertTable(1, 2);
});

const isCaretInTable = () => {
    let caretContainer = window.getSelection().getRangeAt(0).endContainer;

    if (!caretContainer.dataset) return false;
    else return caretContainer.dataset.row;
};

const setRangeIn  = (element) => {
        let range = document.createRange();
        let sel = window.getSelection();
        range.setStart(element, 0);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
    }

this code checks if text caret in a table, if so move the caret after the table then insert there. I'm not sure if this solution satisfies you, but It's a solution.