diff --git a/spec/style-manager-spec.coffee b/spec/style-manager-spec.coffee index 1bf4770b3..7e601bcc1 100644 --- a/spec/style-manager-spec.coffee +++ b/spec/style-manager-spec.coffee @@ -53,14 +53,16 @@ describe "StyleManager", -> expect(addEvents[0].getAttribute('source-path')).toBe '/foo/bar' expect(addEvents[0].textContent).toBe "a {color: yellow;}" - describe "when a group parameter is specified", -> - it "inserts the stylesheet at the end of any existing stylesheets for the same group", -> - manager.addStyleSheet("a {color: red}", group: 'a') - manager.addStyleSheet("a {color: blue}", group: 'b') - manager.addStyleSheet("a {color: green}", group: 'a') + describe "when a priority parameter is specified", -> + it "inserts the style sheet based on the priority", -> + manager.addStyleSheet("a {color: red}", priority: 1) + manager.addStyleSheet("a {color: blue}", priority: 0) + manager.addStyleSheet("a {color: green}", priority: 2) + manager.addStyleSheet("a {color: yellow}", priority: 1) expect(manager.getStyleElements().map (elt) -> elt.textContent).toEqual [ - "a {color: red}" - "a {color: green}" "a {color: blue}" + "a {color: red}" + "a {color: yellow}" + "a {color: green}" ] diff --git a/spec/styles-element-spec.coffee b/spec/styles-element-spec.coffee index 9224c2883..7d2fe722a 100644 --- a/spec/styles-element-spec.coffee +++ b/spec/styles-element-spec.coffee @@ -32,16 +32,18 @@ describe "StylesElement", -> expect(element.children[initialChildCount].textContent).toBe "a {color: blue;}" expect(removedStyleElements).toEqual [addedStyleElements[0]] - it "orders style elements by group", -> + it "orders style elements by priority", -> initialChildCount = element.children.length - atom.styles.addStyleSheet("a {color: red}", group: 'a') - atom.styles.addStyleSheet("a {color: blue}", group: 'b') - atom.styles.addStyleSheet("a {color: green}", group: 'a') + atom.styles.addStyleSheet("a {color: red}", priority: 1) + atom.styles.addStyleSheet("a {color: blue}", priority: 0) + atom.styles.addStyleSheet("a {color: green}", priority: 2) + atom.styles.addStyleSheet("a {color: yellow}", priority: 1) - expect(element.children[initialChildCount].textContent).toBe "a {color: red}" - expect(element.children[initialChildCount + 1].textContent).toBe "a {color: green}" - expect(element.children[initialChildCount + 2].textContent).toBe "a {color: blue}" + expect(element.children[initialChildCount].textContent).toBe "a {color: blue}" + expect(element.children[initialChildCount + 1].textContent).toBe "a {color: red}" + expect(element.children[initialChildCount + 2].textContent).toBe "a {color: yellow}" + expect(element.children[initialChildCount + 3].textContent).toBe "a {color: green}" it "updates existing style nodes when style elements are updated", -> initialChildCount = element.children.length diff --git a/spec/theme-manager-spec.coffee b/spec/theme-manager-spec.coffee index 1d322f7d8..fc9624f93 100644 --- a/spec/theme-manager-spec.coffee +++ b/spec/theme-manager-spec.coffee @@ -96,8 +96,8 @@ describe "ThemeManager", -> runs -> reloadHandler.reset() - expect($('style[group=theme]')).toHaveLength 2 - expect($('style[group=theme]:eq(0)').attr('source-path')).toMatch /atom-dark-ui/ + expect($('style[priority=1]')).toHaveLength 2 + expect($('style[priority=1]:eq(0)').attr('source-path')).toMatch /atom-dark-ui/ atom.config.set('core.themes', ['atom-light-ui', 'atom-dark-ui']) waitsFor -> @@ -105,9 +105,9 @@ describe "ThemeManager", -> runs -> reloadHandler.reset() - expect($('style[group=theme]')).toHaveLength 2 - expect($('style[group=theme]:eq(0)').attr('source-path')).toMatch /atom-dark-ui/ - expect($('style[group=theme]:eq(1)').attr('source-path')).toMatch /atom-light-ui/ + expect($('style[priority=1]')).toHaveLength 2 + expect($('style[priority=1]:eq(0)').attr('source-path')).toMatch /atom-dark-ui/ + expect($('style[priority=1]:eq(1)').attr('source-path')).toMatch /atom-light-ui/ atom.config.set('core.themes', []) waitsFor -> @@ -115,7 +115,7 @@ describe "ThemeManager", -> runs -> reloadHandler.reset() - expect($('style[group=theme]')).toHaveLength 2 + expect($('style[priority=1]')).toHaveLength 2 # atom-dark-ui has an directory path, the syntax one doesn't atom.config.set('core.themes', ['theme-with-index-less', 'atom-dark-ui']) @@ -123,7 +123,7 @@ describe "ThemeManager", -> reloadHandler.callCount == 1 runs -> - expect($('style[group=theme]')).toHaveLength 2 + expect($('style[priority=1]')).toHaveLength 2 importPaths = themeManager.getImportPaths() expect(importPaths.length).toBe 1 expect(importPaths[0]).toContain 'atom-dark-ui' diff --git a/src/package.coffee b/src/package.coffee index 7bb81d481..8ef160972 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -109,7 +109,7 @@ class Package getType: -> 'atom' - getStylesheetType: -> 'bundled' + getStyleSheetPriority: -> 0 load: -> @measure 'loadTime', => @@ -175,8 +175,9 @@ class Package activateStylesheets: -> return if @stylesheetsActivated - group = @getStylesheetType() @stylesheetDisposables = new CompositeDisposable + + priority = @getStyleSheetPriority() for [sourcePath, source] in @stylesheets if match = path.basename(sourcePath).match(/[^.]*\.([^.]*)\./) context = match[1] @@ -185,7 +186,7 @@ class Package else context = undefined - @stylesheetDisposables.add(atom.styles.addStyleSheet(source, {sourcePath, group, context})) + @stylesheetDisposables.add(atom.styles.addStyleSheet(source, {sourcePath, priority, context})) @stylesheetsActivated = true activateResources: -> diff --git a/src/style-manager.coffee b/src/style-manager.coffee index f9d6aa373..c891223b9 100644 --- a/src/style-manager.coffee +++ b/src/style-manager.coffee @@ -92,7 +92,7 @@ class StyleManager addStyleSheet: (source, params) -> sourcePath = params?.sourcePath context = params?.context - group = params?.group + priority = params?.priority if sourcePath? and styleElement = @styleElementsBySourcePath[sourcePath] updated = true @@ -106,9 +106,9 @@ class StyleManager styleElement.context = context styleElement.setAttribute('context', context) - if group? - styleElement.group = group - styleElement.setAttribute('group', group) + if priority? + styleElement.priority = priority + styleElement.setAttribute('priority', priority) styleElement.textContent = source @@ -120,14 +120,14 @@ class StyleManager new Disposable => @removeStyleElement(styleElement) addStyleElement: (styleElement) -> - {sourcePath, group} = styleElement + {sourcePath, priority} = styleElement - if group? + if priority? for existingElement, index in @styleElements - if existingElement.group is group - insertIndex = index + 1 - else - break if insertIndex? + if existingElement.priority > priority + insertIndex = index + break + insertIndex ?= @styleElements.length @styleElements.splice(insertIndex, 0, styleElement) diff --git a/src/styles-element.coffee b/src/styles-element.coffee index c4fe264f4..d333a2e45 100644 --- a/src/styles-element.coffee +++ b/src/styles-element.coffee @@ -53,13 +53,14 @@ class StylesElement extends HTMLElement styleElementClone = styleElement.cloneNode(true) styleElementClone.sourcePath = styleElement.sourcePath styleElementClone.context = styleElement.context + styleElementClone.priority = styleElement.priority @styleElementClonesByOriginalElement.set(styleElement, styleElementClone) - group = styleElement.getAttribute('group') - if group? + priority = styleElement.priority + if priority? for child in @children - if child.getAttribute('group') is group and child.nextSibling?.getAttribute('group') isnt group - insertBefore = child.nextSibling + if child.priority > priority + insertBefore = child break @insertBefore(styleElementClone, insertBefore) diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index dd60b99e6..0379066ca 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -219,28 +219,30 @@ class ThemeManager # # Returns a {Disposable} on which `.dispose()` can be called to remove the # required stylesheet. - requireStylesheet: (stylesheetPath, type='bundled') -> + requireStylesheet: (stylesheetPath) -> if fullPath = @resolveStylesheet(stylesheetPath) content = @loadStylesheet(fullPath) - @applyStylesheet(fullPath, content, type) + @applyStylesheet(fullPath, content) else throw new Error("Could not find a file at path '#{stylesheetPath}'") unwatchUserStylesheet: -> @userStylesheetFile?.off() @userStylesheetFile = null - @removeStylesheet(@userStylesheetPath) if @userStylesheetPath? + @userStyleSheetDisposable?.dispose() + @userStyleSheetDisposable = null loadUserStylesheet: -> @unwatchUserStylesheet() + userStylesheetPath = atom.styles.getUserStyleSheetPath() return unless fs.isFileSync(userStylesheetPath) - @userStylesheetPath = userStylesheetPath @userStylesheetFile = new File(userStylesheetPath) @userStylesheetFile.on 'contents-changed moved removed', => @loadUserStylesheet() userStylesheetContents = @loadStylesheet(userStylesheetPath, true) - @applyStylesheet(userStylesheetPath, userStylesheetContents, 'userTheme') + + @userStyleSheetDisposable = atom.styles.addStyleSheet(userStylesheetContents, sourcePath: userStylesheetPath, priority: 2) loadBaseStylesheets: -> @requireStylesheet('../static/bootstrap') @@ -291,8 +293,8 @@ class ThemeManager removeStylesheet: (stylesheetPath) -> @styleSheetDisposablesBySourcePath[stylesheetPath]?.dispose() - applyStylesheet: (path, text, type='bundled') -> - @styleSheetDisposablesBySourcePath[path] = atom.styles.addStyleSheet(text, sourcePath: path, group: type) + applyStylesheet: (path, text) -> + @styleSheetDisposablesBySourcePath[path] = atom.styles.addStyleSheet(text, sourcePath: path) stringToId: (string) -> string.replace(/\\/g, '/') diff --git a/src/theme-package.coffee b/src/theme-package.coffee index 3c03d818f..e1b87e783 100644 --- a/src/theme-package.coffee +++ b/src/theme-package.coffee @@ -5,7 +5,7 @@ module.exports = class ThemePackage extends Package getType: -> 'theme' - getStylesheetType: -> 'theme' + getStyleSheetPriority: -> 1 enable: -> atom.config.unshiftAtKeyPath('core.themes', @name)