Browse Source

Support divs inside compose area

Danilo Bargen 8 năm trước cách đây
mục cha
commit
5e5430bb17
1 tập tin đã thay đổi với 28 bổ sung20 xóa
  1. 28 20
      src/directives/compose_area.ts

+ 28 - 20
src/directives/compose_area.ts

@@ -120,28 +120,36 @@ export default [
                 function submitText(): Promise<any> {
                     let text = '';
 
-                    // Extract text
-                    // tslint:disable-next-line: prefer-for-of (see #98)
-                    for (let i = 0; i < composeDiv[0].childNodes.length; i++) {
-                        const node = composeDiv[0].childNodes[i];
-                        switch (node.nodeType) {
-                            case Node.TEXT_NODE:
-                                // Append text, but strip leading and trailing newlines
-                                text += node.nodeValue.replace(/(^[\r\n]*|[\r\n]*$)/g, '');
-                                break;
-                            case Node.ELEMENT_NODE:
-                                const tag = node.tagName.toLowerCase();
-                                if (tag === 'img') {
-                                    text += node.alt;
-                                    break;
-                                } else if (tag === 'br') {
-                                    text += '\n';
+                    // Process a DOM node recursively and extract text.
+                    const visitChildNodes = (parentNode: HTMLElement) => {
+                        // tslint:disable-next-line: prefer-for-of (see #98)
+                        for (let i = 0; i < parentNode.childNodes.length; i++) {
+                            const node = parentNode.childNodes[i] as HTMLElement;
+                            switch (node.nodeType) {
+                                case Node.TEXT_NODE:
+                                    // Append text, but strip leading and trailing newlines
+                                    text += node.nodeValue.replace(/(^[\r\n]*|[\r\n]*$)/g, '');
                                     break;
-                                }
-                            default:
-                                $log.warn(logTag, 'Unhandled node:', node);
+                                case Node.ELEMENT_NODE:
+                                    const tag = node.tagName.toLowerCase();
+                                    if (tag === 'div') {
+                                        visitChildNodes(node);
+                                        break;
+                                    } else if (tag === 'img') {
+                                        text += (node as HTMLImageElement).alt;
+                                        break;
+                                    } else if (tag === 'br') {
+                                        text += '\n';
+                                        break;
+                                    }
+                                default:
+                                    $log.warn(logTag, 'Unhandled node:', node);
+                            }
                         }
-                    }
+                    };
+
+                    // Extract text
+                    visitChildNodes(composeDiv[0]);
 
                     return new Promise((resolve, reject) => {
                         let submitTexts = (strings: string[]) => {