a tool for shared writing and social publishing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

flatten html

+28 -1
+28 -1
components/TextBlock/useHandlePaste.ts
··· 29 29 if (textHTML) { 30 30 let xml = new DOMParser().parseFromString(textHTML, "text/html"); 31 31 let currentPosition = propsRef.current.position; 32 - let children = [...xml.body.children]; 32 + let children = flattenHTMLToTextBlocks(xml.body); 33 33 if (!children.find((c) => ["P", "H1", "H2", "H3"].includes(c.tagName))) 34 34 return; 35 35 if (children.length === 1) return false; ··· 211 211 } 212 212 }, 10); 213 213 }; 214 + 215 + function flattenHTMLToTextBlocks(element: HTMLElement): HTMLElement[] { 216 + // Function to recursively collect HTML from nodes 217 + function collectHTML(node: Node, htmlBlocks: HTMLElement[]): void { 218 + console.log(node); 219 + if (node.nodeType === Node.ELEMENT_NODE) { 220 + const elementNode = node as HTMLElement; 221 + // Collect outer HTML for paragraph-like elements 222 + if ( 223 + ["P", "H1", "H2", "H3", "H4", "H5", "H6", "LI"].includes( 224 + elementNode.tagName, 225 + ) 226 + ) { 227 + htmlBlocks.push(elementNode); 228 + } else { 229 + // Recursively collect HTML from child nodes 230 + for (let child of node.childNodes) { 231 + collectHTML(child, htmlBlocks); 232 + } 233 + } 234 + } 235 + } 236 + 237 + const htmlBlocks: HTMLElement[] = []; 238 + collectHTML(element, htmlBlocks); 239 + return htmlBlocks; 240 + }