Added source formatting
authorAlexander Ebert <ebert@woltlab.com>
Tue, 27 Sep 2016 12:02:22 +0000 (14:02 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 27 Sep 2016 12:02:27 +0000 (14:02 +0200)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSource.js

index 1ef9e054628647fd06713e39c45dd99e24aea9b3..4a091d419f391bc9cf11800f17e1b3321ae1c837 100644 (file)
@@ -31,6 +31,8 @@ $.Redactor.prototype.WoltLabSource = function() {
                                textarea.style.setProperty('height', Math.ceil(height) + 'px', '');
                                textarea.style.setProperty('display', 'block', '');
                                
+                               textarea.value = this.WoltLabSource.format(textarea.value);
+                               
                                textarea.selectionStart = textarea.selectionEnd = textarea.value.length;
                        }).bind(this);
                        
@@ -44,6 +46,26 @@ $.Redactor.prototype.WoltLabSource = function() {
                
                isActive: function () {
                        return (this.$editor[0].style.getPropertyValue('display') === 'none');
+               },
+               
+               format: function (html) {
+                       var blockTags = this.block.tags.join('|').toLowerCase();
+                       blockTags += '|ul|ol|li';
+                       
+                       html = html.replace(new RegExp('<(' + blockTags + ')([^>]*)>\s*', 'g'), '<$1$2>\n\t');
+                       html = html.replace(new RegExp('\s*</(' + blockTags + ')>', 'g'), '\n</$1>');
+                       
+                       html = html.replace(/\s*<li>/g, '\n<li>');
+                       html = html.replace(/<\/li>\s*/g, '</li>\n');
+                       html = html.replace(/<li>([\s\S]*?)<\/li>/g, function(match, content) {
+                               var parts = content.trim().split('\n');
+                               return '<li>\n\t' + parts.join('\n\t') + '\n</li>';
+                       });
+                       
+                       html = html.replace(/\s*<\/p>\s*/g, '\n</p>\n');
+                       html = html.replace(/<p>\s*<br>\s*<\/p>/g, '<p><br></p>');
+                       
+                       return html.replace(/^\s+/, '');
                }
        };
 };