Add window.requireStylesheet

This allows you to synchronously load a stylesheet into the document's
head whenever it is needed.
This commit is contained in:
Nathan Sobo
2012-01-16 20:03:48 -08:00
parent 30faf9b043
commit f4aa8daa52
3 changed files with 27 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
require 'window'
$ = require 'jquery'
fs = require 'fs'
describe "Window", ->
describe "keybindings", ->
@@ -15,6 +15,23 @@ describe "Window", ->
$(window).trigger 'close'
expect(window.close).toHaveBeenCalled()
describe "requireStylesheet(path)", ->
it "synchronously loads the stylesheet at the given path and installs a style tag for it in the head", ->
$('head style').remove()
expect($('head style').length).toBe 0
requireStylesheet('atom.css')
expect($('head style').length).toBe 1
styleElt = $('head style')
fullPath = require.resolve('atom.css')
expect(styleElt.attr('path')).toBe fullPath
expect(styleElt.text()).toBe fs.read(fullPath)
requireStylesheet('atom.css')
expect($('head style').length).toBe 1
describe "bindMenuItem(path, keyPattern, action)", ->
it "causes the given menu item to be added to the menu when the window is focused and removed when it is blurred", ->
addedPaths = []

View File

@@ -3,11 +3,13 @@ $ = require 'jquery'
_ = require 'underscore'
Native = require 'native'
BindingSet = require 'binding-set'
require 'window'
afterEach ->
(new Native).resetMainMenu()
atom.globalKeymap.reset()
$('#jasmine-content').empty()
$('head style[path]').remove()
window.atom = new (require 'app')

View File

@@ -1,6 +1,7 @@
fs = require 'fs'
_ = require 'underscore'
$ = require 'jquery'
fs = require 'fs'
RootView = require 'root-view'
@@ -25,6 +26,12 @@ windowAdditions =
$(window).unbind('focus')
$(window).unbind('blur')
requireStylesheet: (path) ->
fullPath = require.resolve(path)
content = fs.read(fullPath)
return if $("head style[path='#{fullPath}']").length
$('head').append "<style path='#{fullPath}'>#{content}</style>"
bindMenuItems: ->
# we want to integrate this better with keybindings
# @bindMenuItem "File > Save", "meta+s", => @rootView.editor.save()