cellery 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cells.js +35 -53
- package/lib/compat.js +18 -0
- package/lib/template.js +14 -4
- package/package.json +1 -1
package/lib/cells.js
CHANGED
|
@@ -11,8 +11,10 @@ class Cell {
|
|
|
11
11
|
this.alignment = opts.alignment
|
|
12
12
|
this.decoration = opts.decoration
|
|
13
13
|
this.size = opts.size
|
|
14
|
-
this.
|
|
14
|
+
this.events = opts.events
|
|
15
15
|
this.style = opts.style
|
|
16
|
+
|
|
17
|
+
this._eventsRegistered = false
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
static Styled(styledOpts = {}) {
|
|
@@ -39,14 +41,21 @@ class Cell {
|
|
|
39
41
|
|
|
40
42
|
render(opts = {}) {
|
|
41
43
|
const cell = this._render()
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
|
|
45
|
+
if (!this.cellery) {
|
|
46
|
+
this.cellery = Cell.cellery
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// @todo explore - 'ready' style first time setup?
|
|
50
|
+
if (this.events?.length && this.id && !this._eventsRegistered) {
|
|
51
|
+
this._eventsRegistered = true
|
|
52
|
+
this.cellery.pub({ event: 'register', id: this.id, targets: this.events })
|
|
44
53
|
}
|
|
45
54
|
|
|
46
55
|
this.cellery.pub({
|
|
47
56
|
event: 'render',
|
|
48
57
|
id: cell.id,
|
|
49
|
-
content: this.cellery.adapter
|
|
58
|
+
content: this.cellery.adapter?.render(cell),
|
|
50
59
|
...opts
|
|
51
60
|
})
|
|
52
61
|
}
|
|
@@ -59,17 +68,25 @@ class Cell {
|
|
|
59
68
|
})
|
|
60
69
|
}
|
|
61
70
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
for (const c of this.children) {
|
|
65
|
-
c.register(cellery)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
71
|
+
// @deprecated
|
|
72
|
+
register(cellery) {}
|
|
68
73
|
}
|
|
69
74
|
|
|
70
75
|
class Style {
|
|
71
76
|
constructor(opts = {}) {
|
|
72
77
|
this.content = parse(opts.children?.join('\n'))
|
|
78
|
+
|
|
79
|
+
// setup data-cellery-cell for namespaced matching
|
|
80
|
+
const cellNames = new Set(Object.keys(opts.cells))
|
|
81
|
+
|
|
82
|
+
walk(this.content, {
|
|
83
|
+
visit: 'TypeSelector',
|
|
84
|
+
enter(node) {
|
|
85
|
+
if (cellNames.has(node.name)) {
|
|
86
|
+
node.name = `[data-cellery-cell="${node.name}"]`
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
})
|
|
73
90
|
}
|
|
74
91
|
|
|
75
92
|
findPropertyOfCell(name, property) {
|
|
@@ -104,40 +121,12 @@ class Style {
|
|
|
104
121
|
const str = generate(selector)
|
|
105
122
|
if (!str.startsWith(target)) return
|
|
106
123
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
for (let i = 0; i < items.length; i++) {
|
|
110
|
-
const node = items[i]
|
|
111
|
-
if (node.type === 'PseudoClassSelector' || node.type === 'PseudoElementSelector') {
|
|
112
|
-
selector.children.insertData({ type: 'IdSelector', name: parent }, items[i])
|
|
113
|
-
inserted = true
|
|
114
|
-
break
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (!inserted) {
|
|
118
|
-
selector.children.appendData({ type: 'IdSelector', name: parent })
|
|
119
|
-
}
|
|
124
|
+
selector.children.prependData({ type: 'Combinator', name: ' ' })
|
|
125
|
+
selector.children.prependData({ type: 'IdSelector', name: parent })
|
|
120
126
|
})
|
|
121
127
|
}
|
|
122
128
|
})
|
|
123
129
|
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
class StyleHTML extends Style {
|
|
127
|
-
constructor(opts = {}) {
|
|
128
|
-
super(opts)
|
|
129
|
-
|
|
130
|
-
const cellNames = new Set(Object.keys(opts.cells))
|
|
131
|
-
|
|
132
|
-
walk(this.content, {
|
|
133
|
-
visit: 'TypeSelector',
|
|
134
|
-
enter(node) {
|
|
135
|
-
if (cellNames.has(node.name)) {
|
|
136
|
-
node.name = `[data-cellery-cell="${node.name}"]`
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
}
|
|
141
130
|
|
|
142
131
|
toCSS() {
|
|
143
132
|
return generate(this.content)
|
|
@@ -160,9 +149,9 @@ class Container extends Cell {
|
|
|
160
149
|
}
|
|
161
150
|
}
|
|
162
151
|
|
|
163
|
-
class
|
|
152
|
+
class Fragment extends Cell {
|
|
164
153
|
constructor(opts = {}) {
|
|
165
|
-
super({ ...opts
|
|
154
|
+
super({ ...opts })
|
|
166
155
|
}
|
|
167
156
|
}
|
|
168
157
|
|
|
@@ -170,12 +159,8 @@ class Text extends Cell {
|
|
|
170
159
|
constructor(opts = {}) {
|
|
171
160
|
super(opts)
|
|
172
161
|
this.value = opts.value || this.children.filter((c) => typeof c === 'string').join('') || ''
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
class Paragraph extends Cell {
|
|
177
|
-
constructor(opts = {}) {
|
|
178
|
-
super(opts)
|
|
162
|
+
this.paragraph = !!opts.paragraph
|
|
163
|
+
this.heading = opts.heading
|
|
179
164
|
}
|
|
180
165
|
}
|
|
181
166
|
|
|
@@ -191,11 +176,8 @@ class Input extends Cell {
|
|
|
191
176
|
module.exports = {
|
|
192
177
|
Cell,
|
|
193
178
|
Container,
|
|
194
|
-
|
|
179
|
+
Fragment,
|
|
195
180
|
Text,
|
|
196
|
-
Paragraph,
|
|
197
181
|
Input,
|
|
198
|
-
|
|
199
|
-
Style,
|
|
200
|
-
StyleHTML
|
|
182
|
+
Style
|
|
201
183
|
}
|
package/lib/compat.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const { Text, Input, Container, Fragment, Style } = require('./cells')
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
'': Fragment,
|
|
5
|
+
h1: Text.Styled({ heading: 1 }),
|
|
6
|
+
h2: Text.Styled({ heading: 2 }),
|
|
7
|
+
h3: Text.Styled({ heading: 3 }),
|
|
8
|
+
h4: Text.Styled({ heading: 4 }),
|
|
9
|
+
h5: Text.Styled({ heading: 5 }),
|
|
10
|
+
h6: Text.Styled({ heading: 6 }),
|
|
11
|
+
div: Container,
|
|
12
|
+
input: Input.Styled({ multiline: false }),
|
|
13
|
+
textbox: Input.Styled({ multiline: true }),
|
|
14
|
+
span: Text,
|
|
15
|
+
button: Text.Styled({ events: ['click'] }),
|
|
16
|
+
p: Text.Styled({ paragraph: true }),
|
|
17
|
+
style: Style
|
|
18
|
+
}
|
package/lib/template.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
const { Cell, Container,
|
|
1
|
+
const { Cell, Container, Fragment, Text, Paragraph, Input, Style } = require('./cells')
|
|
2
|
+
const compat = require('./compat')
|
|
2
3
|
|
|
3
4
|
const SENTINEL = '\x00'
|
|
4
5
|
|
|
5
|
-
const registry = {
|
|
6
|
+
const registry = {
|
|
7
|
+
...compat,
|
|
8
|
+
Cell,
|
|
9
|
+
Container,
|
|
10
|
+
Fragment,
|
|
11
|
+
Text,
|
|
12
|
+
Paragraph,
|
|
13
|
+
Input,
|
|
14
|
+
Style
|
|
15
|
+
}
|
|
6
16
|
|
|
7
17
|
function register(cells) {
|
|
8
18
|
Object.assign(registry, cells)
|
|
@@ -146,7 +156,7 @@ function parse(input, slots) {
|
|
|
146
156
|
while (!eof() && !/[\s>\/]/.test(peek())) val += advance()
|
|
147
157
|
}
|
|
148
158
|
|
|
149
|
-
attrs[name] = resolveValue(val)
|
|
159
|
+
attrs[name] = name === 'events' ? resolveValue(val).split(',') : resolveValue(val)
|
|
150
160
|
} else {
|
|
151
161
|
attrs[name] = true
|
|
152
162
|
}
|
|
@@ -181,7 +191,7 @@ function build(node, slots) {
|
|
|
181
191
|
...node.attrs,
|
|
182
192
|
children,
|
|
183
193
|
style,
|
|
184
|
-
cells: node.tag.
|
|
194
|
+
cells: node.tag.toLowerCase() === 'style' ? registry : undefined
|
|
185
195
|
})
|
|
186
196
|
}
|
|
187
197
|
|