|
| 1 | +import { DOMParser } from 'prosemirror-model'; |
| 2 | +import { convertEmToPt, sanitizeHtml } from '../../InputRule.js'; |
| 3 | +import { ListHelpers } from '../../helpers/list-numbering-helpers.js'; |
| 4 | +import { createSingleItemList } from '../html/html-helpers.js'; |
| 5 | +import { getLvlTextForGoogleList, googleNumDefMap } from '../../helpers/pasteListHelpers.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Main handler for pasted Google Docs content. |
| 9 | + * |
| 10 | + * @param {string} html The string being pasted |
| 11 | + * @param {Editor} editor The SuperEditor instance |
| 12 | + * @param {Object} view The ProseMirror view |
| 13 | + * @returns |
| 14 | + */ |
| 15 | +export const handleGoogleDocsHtml = (html, editor, view) => { |
| 16 | + // convert lists |
| 17 | + const htmlWithPtSizing = convertEmToPt(html); |
| 18 | + const cleanedHtml = sanitizeHtml(htmlWithPtSizing).innerHTML; |
| 19 | + |
| 20 | + const tempDiv = document.createElement('div'); |
| 21 | + tempDiv.innerHTML = cleanedHtml; |
| 22 | + |
| 23 | + const htmlWithMergedLists = mergeSeparateLists(tempDiv); |
| 24 | + const flattenHtml = flattenListsInHtml(htmlWithMergedLists, editor); |
| 25 | + |
| 26 | + const doc = DOMParser.fromSchema(editor.schema).parse(flattenHtml); |
| 27 | + tempDiv.remove(); |
| 28 | + |
| 29 | + const { dispatch } = editor.view; |
| 30 | + if (!dispatch) return false; |
| 31 | + |
| 32 | + dispatch(view.state.tr.replaceSelectionWith(doc, true)); |
| 33 | + return true; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Flattens lists to ensure each list contains exactly ONE list item. |
| 38 | + */ |
| 39 | +function flattenListsInHtml(container, editor) { |
| 40 | + // Keep processing until all lists are flattened |
| 41 | + let foundList; |
| 42 | + while ((foundList = findListToFlatten(container))) { |
| 43 | + flattenFoundList(foundList, editor); |
| 44 | + } |
| 45 | + |
| 46 | + return container; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Finds lists to be flattened |
| 51 | + */ |
| 52 | +function findListToFlatten(container) { |
| 53 | + // First priority: unprocessed lists |
| 54 | + let list = container.querySelector('ol:not([data-list-id]), ul:not([data-list-id])'); |
| 55 | + if (list) return list; |
| 56 | + |
| 57 | + return null; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Flattens a single list by: |
| 62 | + * 1. Ensuring it has proper data-list-id |
| 63 | + * 2. Splitting multi-item lists into single-item lists |
| 64 | + * 3. Extracting nested lists and processing them recursively |
| 65 | + */ |
| 66 | +function flattenFoundList(listElem, editor) { |
| 67 | + let NodeInterface; |
| 68 | + if (editor.options.mockDocument) { |
| 69 | + const win = editor.options.mockDocument.defaultView; |
| 70 | + NodeInterface = win.Node; |
| 71 | + } else { |
| 72 | + NodeInterface = window.Node; |
| 73 | + } |
| 74 | + |
| 75 | + const tag = listElem.tagName.toLowerCase(); |
| 76 | + const rootListLevel = Number(listElem.children[0].getAttribute('aria-level')); |
| 77 | + const rootListFmt = listElem.children[0].style['list-style-type'] || 'decimal'; |
| 78 | + const start = listElem.getAttribute('start') || 1; |
| 79 | + |
| 80 | + // Google docs list doesn't have numId |
| 81 | + const rootNumId = ListHelpers.getNewListId(editor); |
| 82 | + |
| 83 | + ListHelpers.generateNewListDefinition({ |
| 84 | + numId: rootNumId, |
| 85 | + listType: tag === 'ol' ? 'orderedList' : 'bulletList', |
| 86 | + editor, |
| 87 | + fmt: googleNumDefMap.get(rootListFmt), |
| 88 | + level: (rootListLevel - 1).toString(), |
| 89 | + start, |
| 90 | + text: getLvlTextForGoogleList(rootListFmt, rootListLevel, editor), |
| 91 | + }); |
| 92 | + |
| 93 | + // Create single-item lists for each item |
| 94 | + const newLists = []; |
| 95 | + |
| 96 | + // Get all direct <li> children |
| 97 | + const items = Array.from(listElem.children).filter((c) => c.tagName.toLowerCase() === 'li'); |
| 98 | + |
| 99 | + items.forEach((li) => { |
| 100 | + const level = Number(li.getAttribute('aria-level')) - 1; |
| 101 | + const listLevel = [level + 1]; |
| 102 | + const nestedLists = getNestedLists([li.nextSibling]); |
| 103 | + |
| 104 | + // Create a new single-item list for this li |
| 105 | + const newList = createSingleItemList({ li, tag, rootNumId, level, listLevel, editor, NodeInterface }); |
| 106 | + newLists.push(newList); |
| 107 | + |
| 108 | + nestedLists.forEach((list) => { |
| 109 | + newLists.push(list.cloneNode(true)); |
| 110 | + }); |
| 111 | + if (nestedLists.length && ['OL', 'UL'].includes(li.nextSibling.tagName)) { |
| 112 | + li.nextSibling?.remove(); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + // Replace the original list with the new single-item lists |
| 117 | + const parent = listElem.parentNode; |
| 118 | + const nextSibling = listElem.nextSibling; |
| 119 | + parent.removeChild(listElem); |
| 120 | + |
| 121 | + newLists.forEach((list) => { |
| 122 | + parent.insertBefore(list, nextSibling); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Recursive helper to find all nested lists for the list item |
| 128 | + */ |
| 129 | +function getNestedLists(nodes) { |
| 130 | + let result = []; |
| 131 | + |
| 132 | + const nodesArray = Array.from(nodes).filter((n) => n !== null); |
| 133 | + |
| 134 | + for (let item of nodesArray) { |
| 135 | + if (item.tagName === 'OL' || item.tagName === 'UL') { |
| 136 | + result.push(item); |
| 137 | + result.push(...getNestedLists(item.children)); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return result; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Method that combines separate lists with sequential start attribute into one list |
| 146 | + * Google Docs list items could be presented as separate lists with sequential start attribute |
| 147 | + */ |
| 148 | +function mergeSeparateLists(container) { |
| 149 | + const tempCont = container.cloneNode(true); |
| 150 | + |
| 151 | + const rootLevelLists = Array.from(tempCont.querySelectorAll('ol:not(ol ol):not(ul ol)') || []); |
| 152 | + const mainList = rootLevelLists.find((list) => !list.getAttribute('start')); |
| 153 | + const hasStartAttr = rootLevelLists.some((list) => list.getAttribute('start') !== null); |
| 154 | + |
| 155 | + if (hasStartAttr) { |
| 156 | + const listsWithStartAttr = rootLevelLists.filter((list) => list.getAttribute('start') !== null); |
| 157 | + for (let [index, item] of listsWithStartAttr.entries()) { |
| 158 | + if (item.getAttribute('start') === (index + 2).toString()) { |
| 159 | + mainList.append(...item.childNodes); |
| 160 | + item.remove(); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return tempCont; |
| 166 | +} |
0 commit comments