mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-11-11 04:48:44 +03:00
Start on TextMateTheme object
This commit is contained in:
parent
238fecb766
commit
98ef3e4243
49
spec/app/text-mate-theme-spec.coffee
Normal file
49
spec/app/text-mate-theme-spec.coffee
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
fs = require 'fs'
|
||||||
|
plist = require 'plist'
|
||||||
|
TextMateTheme = require 'text-mate-theme'
|
||||||
|
|
||||||
|
describe "TextMateTheme", ->
|
||||||
|
theme = null
|
||||||
|
beforeEach ->
|
||||||
|
twilightPlist = fs.read(require.resolve('Twilight.tmTheme'))
|
||||||
|
plist.parseString twilightPlist, (err, data) ->
|
||||||
|
theme = new TextMateTheme(data[0])
|
||||||
|
|
||||||
|
describe ".getRulesets()", ->
|
||||||
|
rulesets = null
|
||||||
|
|
||||||
|
beforeEach ->
|
||||||
|
rulesets = theme.getRulesets()
|
||||||
|
|
||||||
|
it "returns rulesets representing the theme's global style settings", ->
|
||||||
|
expect(rulesets[0]).toEqual
|
||||||
|
selector: '.editor'
|
||||||
|
properties:
|
||||||
|
'background-color': '#141414'
|
||||||
|
'color': '#F8F8F8'
|
||||||
|
|
||||||
|
expect(rulesets[1]).toEqual
|
||||||
|
selector: '.editor.focused .cursor'
|
||||||
|
properties:
|
||||||
|
'border-color': '#A7A7A7'
|
||||||
|
|
||||||
|
expect(rulesets[2]).toEqual
|
||||||
|
selector: '.editor.focused .selection'
|
||||||
|
properties:
|
||||||
|
'background-color': "rgba(221, 240, 255, 51)"
|
||||||
|
|
||||||
|
it "returns an array of objects representing the theme's scope selectors", ->
|
||||||
|
expect(rulesets[11]).toEqual
|
||||||
|
comment: "Invalid – Deprecated"
|
||||||
|
selector: "invalid--deprecated"
|
||||||
|
properties:
|
||||||
|
'color': "#D2A8A1"
|
||||||
|
'font-style': 'italic'
|
||||||
|
'text-decoration': 'underline'
|
||||||
|
|
||||||
|
expect(rulesets[12]).toEqual
|
||||||
|
comment: "Invalid – Illegal"
|
||||||
|
selector: "invalid--illegal"
|
||||||
|
properties:
|
||||||
|
'color': "#F8F8F8"
|
||||||
|
'background-color': 'rgba(86, 45, 86, 191)'
|
64
src/app/text-mate-theme.coffee
Normal file
64
src/app/text-mate-theme.coffee
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
_ = require 'underscore'
|
||||||
|
|
||||||
|
module.exports =
|
||||||
|
class TextMateTheme
|
||||||
|
constructor: ({@name, settings}) ->
|
||||||
|
@rulesets = []
|
||||||
|
globalSettings = settings[0]
|
||||||
|
@buildGlobalSettingsRulesets(settings[0])
|
||||||
|
@buildScopeSelectorRulesets(settings[1..])
|
||||||
|
|
||||||
|
getRulesets: -> @rulesets
|
||||||
|
|
||||||
|
buildGlobalSettingsRulesets: ({settings}) ->
|
||||||
|
{ background, foreground, caret, selection } = settings
|
||||||
|
|
||||||
|
@rulesets.push
|
||||||
|
selector: '.editor'
|
||||||
|
properties:
|
||||||
|
'background-color': @translateColor(background)
|
||||||
|
'color': @translateColor(foreground)
|
||||||
|
|
||||||
|
@rulesets.push
|
||||||
|
selector: '.editor.focused .cursor'
|
||||||
|
properties:
|
||||||
|
'border-color': @translateColor(caret)
|
||||||
|
|
||||||
|
@rulesets.push
|
||||||
|
selector: '.editor.focused .selection'
|
||||||
|
properties:
|
||||||
|
'background-color': @translateColor(selection)
|
||||||
|
|
||||||
|
buildScopeSelectorRulesets: (scopeSelectorSettings) ->
|
||||||
|
for { name, scope, settings } in scopeSelectorSettings
|
||||||
|
continue unless scope
|
||||||
|
@rulesets.push
|
||||||
|
comment: name
|
||||||
|
selector: @translateScopeSelector(scope)
|
||||||
|
properties: @translateScopeSelectorSettings(settings)
|
||||||
|
|
||||||
|
translateScopeSelector: (textmateScopeSelector) ->
|
||||||
|
textmateScopeSelector.replace('.', '--')
|
||||||
|
|
||||||
|
translateScopeSelectorSettings: ({ foreground, background, fontStyle }) ->
|
||||||
|
properties = {}
|
||||||
|
|
||||||
|
if fontStyle
|
||||||
|
fontStyles = fontStyle.split(/\s+/)
|
||||||
|
properties['font-weight'] = 'bold' if _.contains(fontStyles, 'bold')
|
||||||
|
properties['font-style'] = 'italic' if _.contains(fontStyles, 'italic')
|
||||||
|
properties['text-decoration'] = 'underline' if _.contains(fontStyles, 'underline')
|
||||||
|
|
||||||
|
properties['color'] = @translateColor(foreground) if foreground
|
||||||
|
properties['background-color'] = @translateColor(background) if background
|
||||||
|
properties
|
||||||
|
|
||||||
|
translateColor: (textmateColor) ->
|
||||||
|
if textmateColor.length <= 7
|
||||||
|
textmateColor
|
||||||
|
else
|
||||||
|
r = parseInt(textmateColor[1..2], 16)
|
||||||
|
g = parseInt(textmateColor[3..4], 16)
|
||||||
|
b = parseInt(textmateColor[5..6], 16)
|
||||||
|
a = parseInt(textmateColor[7..8], 16)
|
||||||
|
"rgba(#{r}, #{g}, #{b}, #{a})"
|
Loading…
Reference in New Issue
Block a user