From 8d9d4175650f5ff52e5c3551b5706f8005acd81b Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Sun, 13 Mar 2016 16:09:45 -0400 Subject: [PATCH] hide cursor/etc when focus leaves iframe --- editor.coffee | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/editor.coffee b/editor.coffee index 8bc72ef..a2af4a5 100644 --- a/editor.coffee +++ b/editor.coffee @@ -18,6 +18,11 @@ overlay_padding = 10 timeout = (ms, cb) -> return setTimeout cb, ms +next_frame = (cb) -> + if (window.requestAnimationFrame?) + window.requestAnimationFrame cb + else + timeout 16, cb # xml 1.0 spec, chromium and firefox accept these, plus lots of unicode chars valid_attr_regex = new RegExp '^[a-zA-Z_:][-a-zA-Z0-9_:.]*$' @@ -593,6 +598,7 @@ class PeachHTML5Editor @cursor = null @cursor_el = null @cursor_visible = false + @poll_for_blur_timeout = null @iframe_offset = null opt_fragment = @options.fragment ? true @parser_opts = {} @@ -637,14 +643,19 @@ class PeachHTML5Editor init: -> # called by @iframe's onload (or timeout on firefox) @idoc = @iframe.contentDocument @overlay.onclick = (e) => + @have_focus() return event_return e, @onclick e @overlay.ondoubleclick = (e) => + @have_focus() return event_return e, @ondoubleclick e @outer_idoc.body.onkeyup = (e) => + @have_focus() return event_return e, @onkeyup e @outer_idoc.body.onkeydown = (e) => + @have_focus() return event_return e, @onkeydown e @outer_idoc.body.onkeypress = (e) => + @have_focus() return event_return e, @onkeypress e if @options.stylesheet # TODO test this @@ -938,6 +949,21 @@ class PeachHTML5Editor if prev_in_flow_is_block or parent_flags.block ret += "\n#{indent.substr 4}" return ret + onblur: -> + @kill_cursor() + have_focus: -> + @editor_is_focused = true + @poll_for_blur() + poll_for_blur: -> + return if @poll_for_blur_timeout? # already polling + @poll_for_blur_timeout = timeout 150, => + next_frame => # pause polling when browser knows we're not active/visible/etc. + @poll_for_blur_timeout = null + if document.activeElement is @outer_iframe + @poll_for_blur() + else + @editor_is_focused = false + @onblur() window.peach_html5_editor = (args...) -> return new PeachHTML5Editor args... -- 1.7.10.4