aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/elements/Style.js51
-rw-r--r--src/main.js1
-rw-r--r--src/utils/utils.js7
3 files changed, 59 insertions, 0 deletions
diff --git a/src/elements/Style.js b/src/elements/Style.js
new file mode 100644
index 0000000..5c25893
--- /dev/null
+++ b/src/elements/Style.js
@@ -0,0 +1,51 @@
+import { nodeOrNew } from '../utils/adopter.js'
+import { registerMethods } from '../utils/methods.js'
+import { unCamelCase } from '../utils/utils.js'
+import Element from './Element.js'
+
+function cssRule (selector, rule) {
+ if (!selector) return ''
+ if (!rule) return selector
+
+ var ret = selector + '{'
+
+ for (var i in rule) {
+ ret += unCamelCase(i) + ':' + rule[i] + ';'
+ }
+
+ ret += '}'
+
+ return ret
+}
+
+export default class Style extends Element {
+ constructor (node) {
+ super(nodeOrNew('style', node), Style)
+ }
+
+ words (w) {
+ this.node.textContent += (w || '')
+ return this
+ }
+
+ font (name, src, params = {}) {
+ return this.rule('@font-face', {
+ fontFamily: name,
+ src: src,
+ ...params
+ })
+ }
+
+ rule (selector, obj) {
+ return this.words(cssRule(selector, obj))
+ }
+}
+
+registerMethods('Element', {
+ style (selector, obj) {
+ return this.put(new Style()).rule(selector, obj)
+ },
+ fontface (name, src, params) {
+ return this.put(new Style()).font(name, src, params)
+ }
+})
diff --git a/src/main.js b/src/main.js
index 278e8fd..8a7fd96 100644
--- a/src/main.js
+++ b/src/main.js
@@ -105,6 +105,7 @@ export { default as Polyline } from './elements/Polyline.js'
export { default as Rect } from './elements/Rect.js'
export { default as Shape } from './elements/Shape.js'
export { default as Stop } from './elements/Stop.js'
+export { default as Style } from './elements/Style.js'
export { default as Symbol } from './elements/Symbol.js'
export { default as Text } from './elements/Text.js'
export { default as TextPath } from './elements/TextPath.js'
diff --git a/src/utils/utils.js b/src/utils/utils.js
index e3c9111..64c0eed 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -41,6 +41,13 @@ export function camelCase (s) {
})
}
+// Convert camel cased string to string seperated
+export function unCamelCase (s) {
+ return s.replace(/([A-Z])/g, function (m, g) {
+ return '-' + g.toLowerCase()
+ })
+}
+
// Capitalize first letter of a string
export function capitalize (s) {
return s.charAt(0).toUpperCase() + s.slice(1)