Skip to content

Indentation within code-blocks lost #13

@sronsiek

Description

@sronsiek

When I paste code into Quill, or use tabs or spaces to indent code, that indentation seems to be lost in deltas, which is a shame.

However it is possible to use the Quill indent formatter to indent. This results in an 'indent: 1' attribute.
In your deltaToIntermediate.js file, this attribute is in a BlockInsert element. But when you combine block-code elements, only the child attributes are preserved - the parent block of all except the first BlockInsert element are discarded, so the indentation attribute is lost.

I was able to fix it as follows (there are probably more elegant ways - but it works):

/**
 * @param blocks {BlockInsert[]}
 */
function mergeAdjacentCodeBlocks(blocks) {
    for (let i = 0; i < blocks.length - 1; i++){
        if (blocks[i].attributes['code-block'] && blocks[i + 1].attributes['code-block']) {

            // SR: If the parent block being merged has attributes other than 'code-block',
            //     merge them into the children
            if ( Object.keys( blocks[i+1].attributes ).length > 1 ) {

                let parentAttrs = Object.assign({}, blocks[i + 1].attributes);
                delete parentAttrs['code-block'];
                blocks[i + 1].children.forEach( function( child ) {
                    Object.assign( child.attributes, parentAttrs );
                });
            }

            blocks[i].children.push(...blocks[i + 1].children);
            blocks.splice(i + 1, 1);
            // Decrement index since the array has been changed
            i--;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions