{"size": 1076, "ext": "js", "max_stars_repo_path": "lib/commands/unwrapList.js", "max_stars_repo_name": "joefitter/slate-edit-list", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/commands/unwrapList.js", "max_issues_repo_name": "joefitter/slate-edit-list", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/commands/unwrapList.js", "max_forks_repo_name": "joefitter/slate-edit-list", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.5897435897, "max_line_length": 79, "alphanum_fraction": 0.5985130112}
// @flow
import { type Editor } from 'slate';
import type Options from '../options';

/**
 * Unwrap items at range from their list.
 */
function unwrapList(opts: Options, editor: Editor): Editor {
    const items = editor.getItemsAtRange();

    if (items.isEmpty()) {
        return editor;
    }

    return editor.withoutNormalizing(() => {
        // Unwrap the items from their list
        items.forEach(item => editor.unwrapNodeByKey(item.key));

        // Parent of the list of the items
        const firstItem = items.first();
        const parent = editor.value.document.getParent(firstItem.key);

        let index = parent.nodes.findIndex(node => node.key === firstItem.key);

        // Unwrap the items' children
        items.forEach(item => {
            item.nodes.forEach(node => {
                editor.moveNodeByKey(node.key, parent.key, index);
                index += 1;
            });
        });

        // Finally, remove the now empty items
        items.forEach(item => editor.removeNodeByKey(item.key));
    });
}

export default unwrapList;
