Load stylesheets from package stylesheets directory

This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-01-02 16:40:03 -08:00
parent bcde77dd0e
commit c765ec80a1
4 changed files with 29 additions and 4 deletions

View File

@@ -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

View File

@@ -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
[]

View File

@@ -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 "<style id='#{id}'>#{text}</style>"
reload: ->