From 953f2c61f78d9db2586fa868228e134a991aeca0 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 8 Dec 2014 16:11:13 -0800 Subject: [PATCH 1/4] Add TextEditorElement::getDefaultCharacterWidth --- spec/text-editor-element-spec.coffee | 10 ++++++++++ src/text-editor-element.coffee | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/spec/text-editor-element-spec.coffee b/spec/text-editor-element-spec.coffee index 18143ebb9..47a21020a 100644 --- a/spec/text-editor-element-spec.coffee +++ b/spec/text-editor-element-spec.coffee @@ -124,3 +124,13 @@ describe "TextEditorElement", -> element.getModel().setText("goodbye") expect(window.requestAnimationFrame).not.toHaveBeenCalled() expect(element.shadowRoot.textContent).toContain "goodbye" + + describe "::getDefaultCharacterWidth", -> + it "throws an exception if the editor is not attached", -> + element = new TextEditorElement + expect(-> element.getDefaultCharacterWidth()).toThrow("The editor must be attached to get the default character width") + + it "returns the width of a character in the root scope", -> + element = new TextEditorElement + jasmine.attachToDOM(element) + expect(element.getDefaultCharacterWidth()).toBeGreaterThan(0) diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index 550c491fa..cb0b263f5 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -13,6 +13,7 @@ class TextEditorElement extends HTMLElement model: null componentDescriptor: null component: null + attached: false lineOverdrawMargin: null focusOnAttach: false @@ -57,11 +58,15 @@ class TextEditorElement extends HTMLElement @__spacePenView = new TextEditorView(this) attachedCallback: -> + @attached = true @buildModel() unless @getModel()? @mountComponent() unless @component?.isMounted() @component.checkForVisibilityChange() @focus() if @focusOnAttach + detachedCallback: -> + @attached = false + initialize: (model) -> @setModel(model) this @@ -160,6 +165,10 @@ class TextEditorElement extends HTMLElement isUpdatedSynchronously: -> @updatedSynchronously + getDefaultCharacterWidth: -> + throw new Error("The editor must be attached to get the default character width") unless @attached + @getModel().getDefaultCharWidth() + stopEventPropagation = (commandListeners) -> newCommandListeners = {} for commandName, commandListener of commandListeners From 8f02b21d086d67cb4cbb84d1927c68ba4d60eb64 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 2 Dec 2014 17:02:52 -0800 Subject: [PATCH 2/4] Add TextEditorElement::onDidAttach, ::onDidDetach --- spec/text-editor-element-spec.coffee | 21 +++++++++++++++++++++ src/text-editor-element.coffee | 10 ++++++++++ 2 files changed, 31 insertions(+) diff --git a/spec/text-editor-element-spec.coffee b/spec/text-editor-element-spec.coffee index 47a21020a..205cc9910 100644 --- a/spec/text-editor-element-spec.coffee +++ b/spec/text-editor-element-spec.coffee @@ -104,6 +104,27 @@ describe "TextEditorElement", -> scrollbarWidth = verticalScrollbarNode.offsetWidth - verticalScrollbarNode.clientWidth expect(scrollbarWidth).toEqual(8) + describe "::onDidAttach and ::onDidDetach", -> + it "invokes callbacks when the element is attached and detached", -> + element = new TextEditorElement + + attachedCallback = jasmine.createSpy("attachedCallback") + detachedCallback = jasmine.createSpy("detachedCallback") + + element.onDidAttach(attachedCallback) + element.onDidDetach(detachedCallback) + + jasmine.attachToDOM(element) + + expect(attachedCallback).toHaveBeenCalled() + expect(detachedCallback).not.toHaveBeenCalled() + + attachedCallback.reset() + element.remove() + + expect(attachedCallback).not.toHaveBeenCalled() + expect(detachedCallback).toHaveBeenCalled() + describe "::setUpdatedSynchronously", -> it "controls whether the text editor is updated synchronously", -> spyOn(window, 'requestAnimationFrame').andCallFake (fn) -> fn() diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index cb0b263f5..a15df6462 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -1,3 +1,4 @@ +{Emitter} = require 'event-kit' {View, $, callRemoveHooks} = require 'space-pen' React = require 'react-atom-fork' Path = require 'path' @@ -18,6 +19,7 @@ class TextEditorElement extends HTMLElement focusOnAttach: false createdCallback: -> + @emitter = new Emitter @initializeContent() @createSpacePenShim() @addEventListener 'focus', @focused.bind(this) @@ -63,9 +65,11 @@ class TextEditorElement extends HTMLElement @mountComponent() unless @component?.isMounted() @component.checkForVisibilityChange() @focus() if @focusOnAttach + @emitter.emit("did-attach") detachedCallback: -> @attached = false + @emitter.emit("did-detach") initialize: (model) -> @setModel(model) @@ -169,6 +173,12 @@ class TextEditorElement extends HTMLElement throw new Error("The editor must be attached to get the default character width") unless @attached @getModel().getDefaultCharWidth() + onDidAttach: (callback) -> + @emitter.on("did-attach", callback) + + onDidDetach: (callback) -> + @emitter.on("did-detach", callback) + stopEventPropagation = (commandListeners) -> newCommandListeners = {} for commandName, commandListener of commandListeners From d03cfda6c4775a79aa2fd1b6a2008ef2a3c7bab7 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 8 Dec 2014 16:38:43 -0800 Subject: [PATCH 3/4] :memo: new TextEditorElement methods --- src/text-editor-element.coffee | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index a15df6462..426e450ef 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -169,13 +169,22 @@ class TextEditorElement extends HTMLElement isUpdatedSynchronously: -> @updatedSynchronously + # Extended: get the width of a character of text displayed in this element. + # + # Returns a {Number} of pixels. getDefaultCharacterWidth: -> throw new Error("The editor must be attached to get the default character width") unless @attached @getModel().getDefaultCharWidth() + # Extended: call the given `callback` when the editor is attached to the DOM. + # + # * `callback` {Function} onDidAttach: (callback) -> @emitter.on("did-attach", callback) + # Extended: call the given `callback` when the editor is detached from the DOM. + # + # * `callback` {Function} onDidDetach: (callback) -> @emitter.on("did-detach", callback) From 5c37d208f5d1feddc8bd3e2b2eb3b4875df59d0c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 8 Dec 2014 16:56:46 -0800 Subject: [PATCH 4/4] Don't throw when ::getDefaultCharacterWidth is called while detached --- spec/text-editor-element-spec.coffee | 4 ++-- src/text-editor-element.coffee | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/spec/text-editor-element-spec.coffee b/spec/text-editor-element-spec.coffee index 205cc9910..b5f05f4d9 100644 --- a/spec/text-editor-element-spec.coffee +++ b/spec/text-editor-element-spec.coffee @@ -147,9 +147,9 @@ describe "TextEditorElement", -> expect(element.shadowRoot.textContent).toContain "goodbye" describe "::getDefaultCharacterWidth", -> - it "throws an exception if the editor is not attached", -> + it "returns null before the element is attached", -> element = new TextEditorElement - expect(-> element.getDefaultCharacterWidth()).toThrow("The editor must be attached to get the default character width") + expect(element.getDefaultCharacterWidth()).toBeNull() it "returns the width of a character in the root scope", -> element = new TextEditorElement diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index 426e450ef..47fc3f67a 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -60,7 +60,6 @@ class TextEditorElement extends HTMLElement @__spacePenView = new TextEditorView(this) attachedCallback: -> - @attached = true @buildModel() unless @getModel()? @mountComponent() unless @component?.isMounted() @component.checkForVisibilityChange() @@ -68,7 +67,6 @@ class TextEditorElement extends HTMLElement @emitter.emit("did-attach") detachedCallback: -> - @attached = false @emitter.emit("did-detach") initialize: (model) -> @@ -173,7 +171,6 @@ class TextEditorElement extends HTMLElement # # Returns a {Number} of pixels. getDefaultCharacterWidth: -> - throw new Error("The editor must be attached to get the default character width") unless @attached @getModel().getDefaultCharWidth() # Extended: call the given `callback` when the editor is attached to the DOM.