-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic snake addon implementation
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
|
||
var atoms = { | ||
var atoms = exports.atoms = { | ||
d: 'display', | ||
|
||
mar: 'margin', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
|
||
var atoms = require('./atoms').atoms; | ||
|
||
var createSnake = function (renderer, rules) { | ||
rules = rules || {}; | ||
|
||
var defaultRules = renderer.assign({}, atoms, { | ||
bgWhite: function () { | ||
defaultRules.bg.call(this, '#fff'); | ||
}, | ||
|
||
bgBlack: function () { | ||
defaultRules.bg.call(this, '#000'); | ||
}, | ||
}); | ||
|
||
rules = renderer.assign({}, defaultRules, rules); | ||
|
||
var snake = { | ||
start: function () { | ||
var instance = Object.create(snake); | ||
|
||
instance.obj = {}; | ||
instance.toString = function () { | ||
return renderer.cache(instance.obj); | ||
}; | ||
|
||
return instance; | ||
} | ||
}; | ||
|
||
var onRule = function (name) { | ||
var rule = rules[name]; | ||
|
||
if (typeof rule === 'function') { | ||
if (!rule.length) { | ||
Object.defineProperty(snake, name, { | ||
get: function () { | ||
rule.call(this.obj); | ||
return this; | ||
} | ||
}); | ||
} else { | ||
snake[name] = function () { | ||
rule.apply(this.obj, arguments); | ||
return this; | ||
}; | ||
} | ||
} else { | ||
snake[name] = function (value) { | ||
this.obj['' + rule] = value; | ||
return this; | ||
}; | ||
} | ||
}; | ||
|
||
for (var name in rules) onRule(name); | ||
|
||
return snake; | ||
}; | ||
|
||
exports.addon = function (renderer) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
require('./__dev__/warnOnMissingDependencies')('sheet', renderer, ['cache']); | ||
} | ||
|
||
var snake = createSnake(renderer); | ||
|
||
Object.defineProperty(renderer, 's', { | ||
get: function () { | ||
return snake.start(); | ||
} | ||
}); | ||
}; |