JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
don't scroll to top when content height changes
[peach-html5-editor.git] / editor.coffee
index cbbe9c3..0a02396 100644 (file)
@@ -197,6 +197,8 @@ outer_css = (args) ->
        ret += 'body {'
        ret +=     'margin: 0;'
        ret +=     'padding: 0;'
+       ret +=     'color: black;'
+       ret +=     'background: white;'
        ret += '}'
        ret += '#wrap1 {'
        ret +=     "border: #{occupy 1}px solid black;"
@@ -620,6 +622,7 @@ class PeachHTML5Editor
                @cursor_visible = false
                @poll_for_blur_timeout = null
                @iframe_offset = null
+               @iframe_height = null
                opt_fragment = @options.fragment ? true
                @parser_opts = {}
                if opt_fragment
@@ -641,6 +644,7 @@ class PeachHTML5Editor
                        setTimeout (=> @init() unless @inited), 200 # firefox never fires this onload
                        @outer_idoc.body.appendChild(
                                domify @outer_idoc, div: id: 'wrap1', children: [
+                                       domify @outer_idoc, div: style: "position: absolute; top: 0; left: 1px; font-size: 10px", children: [ domify @outer_idoc, text: "Peach HTML5 Editor" ]
                                        @wrap2 = domify @outer_idoc, div: id: 'wrap2', children: [
                                                domify @outer_idoc, div: id: 'wrap3', children: [
                                                        @iframe
@@ -681,8 +685,11 @@ class PeachHTML5Editor
                # so add a <base> tag
                @idoc.head.appendChild domify @idoc, base: href: this_url_sans_path()
                if @options.css_file
-                       # TODO test this
-                       @idoc.head.appendChild domify @idoc, link: rel: 'stylesheet', type: 'text/css', href: @options.css_file
+                       istyle = domify @idoc, link: rel: 'stylesheet', type: 'text/css', href: @options.css_file + '?foo=baz'
+                       istyle.onload = =>
+                               @adjust_iframe_height()
+                       @idoc.head.appendChild istyle
+               @idoc.head.appendChild domify @idoc, style: children: [domify @idoc, text: "body { overflow: hidden; }"]
                @load_html @in_el.value
                @inited = true
                if @options.on_init?
@@ -809,8 +816,15 @@ class PeachHTML5Editor
                @in_el.value = @pretty_html @tree
                @in_el.onchange = =>
                        @load_html @in_el.value
-               @iframe.style.height = "0"
-               @iframe.style.height = "#{@idoc.body.scrollHeight}px"
+               @adjust_iframe_height()
+       adjust_iframe_height: ->
+               h = parseInt(@idoc.body.scrollHeight, 10)
+               if @iframe_height isnt h
+                       @iframe_height = h
+                       s = @wrap2.scrollTop
+                       @iframe.style.height = "0"
+                       @iframe.style.height = "#{h}px"
+                       @wrap2.scrollTop = s
        kill_cursor: -> # remove it, forget where it was
                if @cursor_visible
                        @cursor_el.parentNode.removeChild @cursor_el