From 072a91dee12e07caf91a693c29fd9f3be5ee177b Mon Sep 17 00:00:00 2001
From: =?utf8?q?Aleksander=20=C5=81ukasz?=
 <aleksander.lukasz@nowoczesnapolska.org.pl>
Date: Fri, 19 Apr 2013 15:49:44 +0200
Subject: [PATCH] Visual editor: handling empty document

---
 editor.css              |  1 +
 modules/data.js         | 17 +++++++++++++++++
 modules/visualEditor.js |  7 ++++++-
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/editor.css b/editor.css
index aa920c8..cf1abc2 100644
--- a/editor.css
+++ b/editor.css
@@ -174,6 +174,7 @@ body {
     border-color: white;
     border-style:solid;
     border-width:1px;
+    min-height:20px;
 }
 
 [wlxml-tag=header] {
diff --git a/modules/data.js b/modules/data.js
index e4b564e..8debbd3 100644
--- a/modules/data.js
+++ b/modules/data.js
@@ -3,6 +3,23 @@ rng.modules.data = function(sandbox) {
     var doc = sandbox.getBootstrappedData().document;
     var document_id = sandbox.getBootstrappedData().document_id;
 
+    
+    if(doc === '') {
+        doc = '<document>\n\
+    <section\n\
+        xmlns="http://nowoczesnapolska.org.pl/sst#"\n\
+        xmlns:xlink="http://www.w3.org/1999/xlink"\n\
+        xmlns:dc="http://purl.org/dc/elements/1.1/"\n\
+        xmlns:dcterms="http://purl.org/dc/terms/"\n\
+    >\n\
+        <metadata>\n\
+        </metadata>\n\
+        <div class="p"></div>\n\
+    </section>\n\
+</document>';
+    }
+    
+    
     function readCookie(name) {
         var nameEQ = escape(name) + "=";
         var ca = document.cookie.split(';');
diff --git a/modules/visualEditor.js b/modules/visualEditor.js
index 73622f9..f3c0a5c 100644
--- a/modules/visualEditor.js
+++ b/modules/visualEditor.js
@@ -107,8 +107,13 @@ rng.modules.visualEditor = function(sandbox) {
             var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
                 return $(this).clone().children().remove().end().text().trim() !== '';
             }).first();
+            var node;
             if(firstNodeWithText.length)
-                this.selectNode($(firstNodeWithText[0]));
+                node = $(firstNodeWithText[0])
+            else {
+                node = this.node.find('[wlxml-class|="p"]')
+            }
+            this.selectNode(node);
         },
         _addMetaRow: function(key, value) {
             var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));
-- 
2.20.1