Add StyleElement

This will be used to handle stylesheet rendering when we move management
of loading stylesheets to the StyleManager instead of the theme manager.
This sets us up for being able to render specific stylesheets in shadow
roots in addition to just having global stylesheets.
This commit is contained in:
Nathan Sobo
2014-10-13 17:31:58 -06:00
parent 62a43c6fb9
commit 79598aaae9
5 changed files with 72 additions and 0 deletions

View File

@@ -74,6 +74,7 @@ beforeEach ->
atom.workspace = new Workspace()
atom.keymaps.keyBindings = _.clone(keyBindingsToRestore)
atom.commands.restoreSnapshot(commandsToRestore)
atom.styles.clear()
window.resetTimeouts()
atom.packages.packageStates = {}

View File

@@ -0,0 +1,35 @@
StylesElement = require '../src/styles-element'
StyleManager = require '../src/style-manager'
describe "StylesElement", ->
element = null
beforeEach ->
element = new StylesElement
document.querySelector('#jasmine-content').appendChild(element)
it "renders a style tag for all currently active stylesheets in the style manager", ->
expect(element.children.length).toBe 0
disposable1 = atom.styles.addStyleSheet("a {color: red;}")
expect(element.children.length).toBe 1
expect(element.children[0].textContent).toBe "a {color: red;}"
disposable2 = atom.styles.addStyleSheet("a {color: blue;}")
expect(element.children.length).toBe 2
expect(element.children[1].textContent).toBe "a {color: blue;}"
disposable1.dispose()
expect(element.children.length).toBe 1
expect(element.children[0].textContent).toBe "a {color: blue;}"
it "orders style elements by group", ->
expect(element.children.length).toBe 0
atom.styles.addStyleSheet("a {color: red}", group: 'a')
atom.styles.addStyleSheet("a {color: blue}", group: 'b')
atom.styles.addStyleSheet("a {color: green}", group: 'a')
expect(element.children[0].textContent).toBe "a {color: red}"
expect(element.children[1].textContent).toBe "a {color: green}"
expect(element.children[2].textContent).toBe "a {color: blue}"

View File

@@ -182,6 +182,7 @@ class Atom extends Model
Clipboard = require './clipboard'
Syntax = require './syntax'
ThemeManager = require './theme-manager'
StyleManager = require './style-manager'
ContextMenuManager = require './context-menu-manager'
MenuManager = require './menu-manager'
{devMode, safeMode, resourcePath} = @getLoadSettings()
@@ -202,6 +203,7 @@ class Atom extends Model
@commands = new CommandRegistry
@packages = new PackageManager({devMode, configDirPath, resourcePath, safeMode})
@themes = new ThemeManager({packageManager: @packages, configDirPath, resourcePath, safeMode})
@styles = new StyleManager({resourcePath})
@contextMenu = new ContextMenuManager({resourcePath, devMode})
@menu = new MenuManager({resourcePath})
@clipboard = new Clipboard()

View File

@@ -61,3 +61,7 @@ class StyleManager
sourcePath = params?.sourcePath
delete @styleElementsBySourcePath[sourcePath] if sourcePath?
@emitter.emit 'did-remove-style-element', styleElement
clear: ->
@styleElements = []
@styleElementsBySourcePath = {}

30
src/styles-element.coffee Normal file
View File

@@ -0,0 +1,30 @@
{CompositeDisposable} = require 'event-kit'
class StylesElement extends HTMLElement
attachedCallback: ->
@subscriptions = new CompositeDisposable
@styleElementClonesByOriginalElement = new WeakMap
@subscriptions.add atom.styles.observeStyleElements(@styleElementAdded.bind(this))
@subscriptions.add atom.styles.onDidRemoveStyleElement(@styleElementRemoved.bind(this))
styleElementAdded: (styleElement) ->
{group} = styleElement
styleElementClone = styleElement.cloneNode(true)
styleElementClone.group = group
@styleElementClonesByOriginalElement.set(styleElement, styleElementClone)
if group?
for child in @children
if child.group is group and child.nextSibling?.group isnt group
insertBefore = child.nextSibling
break
@insertBefore(styleElementClone, insertBefore)
styleElementRemoved: (styleElement) ->
@styleElementClonesByOriginalElement.get(styleElement).remove()
detachedCallback: ->
@subscriptions.dispose()
module.exports = StylesElement = document.registerElement 'atom-styles', prototype: StylesElement.prototype