mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-11-10 18:24:09 +03:00
Start on StyleManager
This will take over raw stylesheet management from the theme manager now that it’s becoming more complex with the need to target specific host elements. Instead of actually adding nodes to the head of the document, it will instead simply manage a set of stylesheets we want to apply and leave actual DOM manipulation to <atom-styles> custom elements that can render the set of active stylesheets in the appropriate locations.
This commit is contained in:
parent
b2b4860983
commit
d3371dbcd2
29
spec/style-manager-spec.coffee
Normal file
29
spec/style-manager-spec.coffee
Normal file
@ -0,0 +1,29 @@
|
||||
StyleManager = require '../src/style-manager'
|
||||
|
||||
describe "StyleManager", ->
|
||||
manager = null
|
||||
|
||||
beforeEach ->
|
||||
manager = new StyleManager
|
||||
|
||||
describe "::addStyleSheet(source, params)", ->
|
||||
it "adds a stylesheet based on the given source and returns a disposable allowing it to be removed", ->
|
||||
addEvents = []
|
||||
removeEvents = []
|
||||
manager.onDidAddStyleSheet (event) -> addEvents.push(event)
|
||||
manager.onDidRemoveStyleSheet (event) -> removeEvents.push(event)
|
||||
|
||||
disposable = manager.addStyleSheet("a {color: red;}")
|
||||
|
||||
expect(addEvents.length).toBe 1
|
||||
expect(addEvents[0].styleElement.textContent).toBe "a {color: red;}"
|
||||
|
||||
styleElements = manager.getStyleElements()
|
||||
expect(styleElements.length).toBe 1
|
||||
expect(styleElements[0].textContent).toBe "a {color: red;}"
|
||||
|
||||
disposable.dispose()
|
||||
|
||||
expect(removeEvents.length).toBe 1
|
||||
expect(removeEvents[0].styleElement.textContent).toBe "a {color: red;}"
|
||||
expect(manager.getStyleElements().length).toBe 0
|
30
src/style-manager.coffee
Normal file
30
src/style-manager.coffee
Normal file
@ -0,0 +1,30 @@
|
||||
{Emitter, Disposable} = require 'event-kit'
|
||||
|
||||
module.exports =
|
||||
class StyleManager
|
||||
constructor: ->
|
||||
@emitter = new Emitter
|
||||
@styleElements = []
|
||||
|
||||
onDidAddStyleSheet: (callback) ->
|
||||
@emitter.on 'did-add-style-sheet', callback
|
||||
|
||||
onDidRemoveStyleSheet: (callback) ->
|
||||
@emitter.on 'did-remove-style-sheet', callback
|
||||
|
||||
getStyleElements: ->
|
||||
@styleElements.slice()
|
||||
|
||||
addStyleSheet: (source) ->
|
||||
styleElement = document.createElement('style')
|
||||
styleElement.textContent = source
|
||||
@styleElements.push(styleElement)
|
||||
@emitter.emit 'did-add-style-sheet', {styleElement}
|
||||
|
||||
new Disposable => @removeStyleElement(styleElement)
|
||||
|
||||
removeStyleElement: (styleElement) ->
|
||||
index = @styleElements.indexOf(styleElement)
|
||||
unless index is -1
|
||||
@styleElements.splice(index, 1)
|
||||
@emitter.emit 'did-remove-style-sheet', {styleElement}
|
Loading…
Reference in New Issue
Block a user