/** * NormalizeEntries.js * * Released under LGPL License. * Copyright (c) 1999-2018 Ephox Corp. All rights reserved * * License: http://www.tinymce.com/license * Contributing: http://www.tinymce.com/contributing */ import { Entry } from './Entry'; import { Arr, Merger, Option } from '@ephox/katamari'; const assimilateEntry = (adherent: Entry, source: Entry) => { adherent.listType = source.listType; adherent.listAttributes = Merger.merge({}, source.listAttributes); adherent.itemAttributes = Merger.merge({}, source.itemAttributes); }; const normalizeShallow = (outline: Array>, entry: Entry): Array> => { const matchingEntryDepth = entry.depth - 1; outline[matchingEntryDepth].each((matchingEntry) => assimilateEntry(entry, matchingEntry)); const newOutline = outline.slice(0, matchingEntryDepth); newOutline.push(Option.some(entry)); return newOutline; }; const normalizeDeep = (outline: Array>, entry: Entry): Array> => { const newOutline = outline.slice(0); const diff = entry.depth - outline.length; for (let i = 1; i < diff; i++) { newOutline.push(Option.none()); } newOutline.push(Option.some(entry)); return newOutline; }; const normalizeEntries = (entries: Entry[]): void => { Arr.foldl(entries, (outline: Array>, entry) => { return entry.depth > outline.length ? normalizeDeep(outline, entry) : normalizeShallow(outline, entry); }, []); }; export { normalizeEntries };