|
| 1 | +import { DOMSerializer } from 'prosemirror-model'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Get all sections in the editor document. |
| 5 | + * This function traverses the document and collects all nodes of the specified section type. |
| 6 | + * @param {Editor} editor - The editor instance to search within. |
| 7 | + * @returns {Array} An array of objects containing the node and its position in the document |
| 8 | + */ |
| 9 | +const getAllSections = (editor) => { |
| 10 | + if (!editor) return []; |
| 11 | + const type = editor.schema.nodes.documentSection; |
| 12 | + if (!type) return []; |
| 13 | + |
| 14 | + const sections = []; |
| 15 | + const { state } = editor; |
| 16 | + state.doc.descendants((node, pos) => { |
| 17 | + if (node.type.name === type.name) { |
| 18 | + sections.push({ node, pos }); |
| 19 | + } |
| 20 | + }); |
| 21 | + return sections; |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Export all sections to HTML format. |
| 26 | + * This function retrieves all sections in the editor and converts them to HTML. |
| 27 | + * @param {Editor} editor - The editor instance containing the sections. |
| 28 | + * @returns {Array} An array of objects containing section details and their HTML representation. |
| 29 | + */ |
| 30 | +export const exportSectionsToHTML = (editor) => { |
| 31 | + const sections = getAllSections(editor); |
| 32 | + |
| 33 | + const processedSections = new Set(); |
| 34 | + const result = []; |
| 35 | + sections.forEach(({ node }) => { |
| 36 | + const { attrs } = node; |
| 37 | + const { id, title, description } = attrs; |
| 38 | + if (processedSections.has(id)) return; |
| 39 | + processedSections.add(id); |
| 40 | + |
| 41 | + const html = getHTMLFromNode(node, editor); |
| 42 | + result.push({ |
| 43 | + id, |
| 44 | + title, |
| 45 | + description, |
| 46 | + html, |
| 47 | + }); |
| 48 | + }); |
| 49 | + return result; |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * Get HTML representation of a ProseMirror node. |
| 54 | + * @param {Node} node - The ProseMirror node to convert. |
| 55 | + * @param {Editor} editor - The editor instance used for serialization. |
| 56 | + * @returns {String} The HTML representation of the node's content. |
| 57 | + */ |
| 58 | +const getHTMLFromNode = (node, editor) => { |
| 59 | + const tempDocument = document.implementation.createHTMLDocument(); |
| 60 | + const container = tempDocument.createElement('div'); |
| 61 | + const fragment = DOMSerializer.fromSchema(editor.schema).serializeFragment(node.content); |
| 62 | + container.appendChild(fragment); |
| 63 | + let html = container.innerHTML; |
| 64 | + return html; |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Export all sections to JSON format. |
| 69 | + * This function retrieves all sections in the editor and converts them to JSON. |
| 70 | + * @param {Editor} editor - The editor instance containing the sections. |
| 71 | + * @returns {Array} An array of objects containing section details and their JSON representation. |
| 72 | + */ |
| 73 | +export const exportSectionsToJSON = (editor) => { |
| 74 | + const sections = getAllSections(editor); |
| 75 | + const processedSections = new Set(); |
| 76 | + const result = []; |
| 77 | + sections.forEach(({ node }) => { |
| 78 | + const { attrs } = node; |
| 79 | + const { id, title, description } = attrs; |
| 80 | + if (processedSections.has(id)) return; |
| 81 | + processedSections.add(id); |
| 82 | + |
| 83 | + result.push({ |
| 84 | + id, |
| 85 | + title, |
| 86 | + description, |
| 87 | + content: node.toJSON(), |
| 88 | + }); |
| 89 | + }); |
| 90 | + return result; |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * Get a linked section editor by its ID. |
| 95 | + * This function creates a child editor for a specific section, allowing for editing of that section's content. |
| 96 | + * @param {String} id - The ID of the section to link to. |
| 97 | + * @param {Object} options - Options for the child editor. |
| 98 | + * @param {Editor} editor - The parent editor instance. |
| 99 | + * @returns {Editor|null} The child editor instance for the linked section, or null if the section is not found. |
| 100 | + */ |
| 101 | +export const getLinkedSectionEditor = (id, options, editor) => { |
| 102 | + const sections = getAllSections(editor); |
| 103 | + const section = sections.find((s) => s.node.attrs.id === id); |
| 104 | + if (!section) return null; |
| 105 | + |
| 106 | + const child = editor.createChildEditor({ |
| 107 | + ...options, |
| 108 | + onUpdate: ({ editor: childEditor, transaction }) => { |
| 109 | + const isFromtLinkedParent = transaction.getMeta('fromLinkedParent'); |
| 110 | + if (isFromtLinkedParent) return; // Prevent feedback loop |
| 111 | + |
| 112 | + // 1. Get updated content from child editor |
| 113 | + const updatedContent = childEditor.state.doc.content; |
| 114 | + |
| 115 | + // 2. Find the section node and its position in the parent |
| 116 | + const sectionNode = getAllSections(editor)?.find((s) => s.node.attrs.id === id); |
| 117 | + if (!sectionNode) return; |
| 118 | + |
| 119 | + const { pos, node } = sectionNode; |
| 120 | + |
| 121 | + // 3. Create a new node with the same type and attrs, but updated content |
| 122 | + const newNode = node.type.create(node.attrs, updatedContent, node.marks); |
| 123 | + |
| 124 | + // 4. Replace the old node with the new node in the parent editor |
| 125 | + const tr = editor.state.tr.replaceWith(pos, pos + node.nodeSize, newNode); |
| 126 | + tr.setMeta('fromLinkedChild', true); // Prevent feedback loop |
| 127 | + editor.view.dispatch(tr); |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + editor.on('update', ({ transaction }) => { |
| 132 | + const isFromLinkedChild = transaction.getMeta('fromLinkedChild'); |
| 133 | + if (isFromLinkedChild) return; // Prevent feedback loop |
| 134 | + |
| 135 | + const sectionNode = getAllSections(editor)?.find((s) => s.node.attrs.id === id); |
| 136 | + if (!sectionNode) return; |
| 137 | + |
| 138 | + // Only update if content is actually different |
| 139 | + const sectionContent = sectionNode.node.content; |
| 140 | + |
| 141 | + const json = { |
| 142 | + type: 'doc', |
| 143 | + content: sectionContent.content.map((node) => node.toJSON()), |
| 144 | + }; |
| 145 | + |
| 146 | + const childTr = child.state.tr; |
| 147 | + childTr.setMeta('fromLinkedParent', true); // Prevent feedback loop |
| 148 | + childTr.replaceWith(0, child.state.doc.content.size, child.schema.nodeFromJSON(json)); |
| 149 | + child.view.dispatch(childTr); |
| 150 | + }); |
| 151 | + |
| 152 | + return child; |
| 153 | +}; |
| 154 | + |
| 155 | +/** |
| 156 | + * SectionHelpers provides utility functions for working with sections in the editor. |
| 157 | + * It includes methods to retrieve all sections and manage section-related data. |
| 158 | + */ |
| 159 | +export const SectionHelpers = { |
| 160 | + getAllSections, |
| 161 | + exportSectionsToHTML, |
| 162 | + exportSectionsToJSON, |
| 163 | + getLinkedSectionEditor, |
| 164 | +}; |
0 commit comments