From c765ec80a1c266a4f557167bcd009cdeb6799a7d Mon Sep 17 00:00:00 2001 From: Kevin Sawicki & Nathan Sobo Date: Wed, 2 Jan 2013 16:40:03 -0800 Subject: [PATCH] Load stylesheets from package stylesheets directory --- spec/app/atom-spec.coffee | 12 +++++++++++- .../package-with-module/stylesheets/styles.css | 0 src/app/atom-package.coffee | 14 +++++++++++++- src/app/window.coffee | 7 +++++-- 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/packages/package-with-module/stylesheets/styles.css diff --git a/spec/app/atom-spec.coffee b/spec/app/atom-spec.coffee index e40224d61..b4474c3fa 100644 --- a/spec/app/atom-spec.coffee +++ b/spec/app/atom-spec.coffee @@ -2,13 +2,23 @@ RootView = require 'root-view' describe "the `atom` global", -> describe ".loadPackage(name)", -> - extension = null + [extension, stylesheetPath] = [] beforeEach -> rootView = new RootView extension = require "package-with-module" + stylesheetPath = require.resolve("fixtures/packages/package-with-module/stylesheets/styles.css") + + afterEach -> + removeStylesheet(stylesheetPath) it "requires and activates the package's main module if it exists", -> spyOn(rootView, 'activatePackage').andCallThrough() atom.loadPackage("package-with-module") expect(rootView.activatePackage).toHaveBeenCalledWith(extension) + + it "loads stylesheets associated with the package", -> + stylesheetPath = require.resolve("fixtures/packages/package-with-module/stylesheets/styles.css") + expect(stylesheetElementForId(stylesheetPath).length).toBe 0 + atom.loadPackage("package-with-module") + expect(stylesheetElementForId(stylesheetPath).length).toBe 1 diff --git a/spec/fixtures/packages/package-with-module/stylesheets/styles.css b/spec/fixtures/packages/package-with-module/stylesheets/styles.css new file mode 100644 index 000000000..e69de29bb diff --git a/src/app/atom-package.coffee b/src/app/atom-package.coffee index e4e51efa6..9c3dd071d 100644 --- a/src/app/atom-package.coffee +++ b/src/app/atom-package.coffee @@ -10,8 +10,9 @@ class AtomPackage extends Package load: -> try - rootView.activatePackage(@module) @loadKeymaps() + @loadStylesheets() + rootView.activatePackage(@module) catch e console.error "Failed to load package named '#{@name}'", e.stack @@ -25,3 +26,14 @@ class AtomPackage extends Package fs.list keymapsDirPath else [] + + loadStylesheets: -> + for stylesheetPath in @getStylesheetPaths() + requireStylesheet(stylesheetPath) + + getStylesheetPaths: -> + stylesheetDirPath = fs.join(@path, 'stylesheets') + if fs.exists stylesheetDirPath + fs.list stylesheetDirPath + else + [] diff --git a/src/app/window.coffee b/src/app/window.coffee index 94fccad2d..7e306f44f 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -61,6 +61,9 @@ windowAdditions = @_handleKeyEvent = (e) => @keymap.handleKeyEvent(e) $(document).on 'keydown', @_handleKeyEvent + stylesheetElementForId: (id) -> + $("head style[id='#{id}']") + requireStylesheet: (path) -> unless fullPath = require.resolve(path) throw new Error("Could not find a file at path '#{path}'") @@ -69,10 +72,10 @@ windowAdditions = removeStylesheet: (path) -> unless fullPath = require.resolve(path) throw new Error("Could not find a file at path '#{path}'") - $("head style[id='#{fullPath}']").remove() + window.stylesheetElementForId(fullPath).remove() applyStylesheet: (id, text) -> - unless $("head style[id='#{id}']").length + unless window.stylesheetElementForId(id).length $('head').append "" reload: ->