Skip to content

Commit 39e7481

Browse files
Refactor slot with underscore
1 parent bb55fb4 commit 39e7481

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

examples/dist/demo.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
<div class="row">
4949
<div class="col-lg-8">
5050
<h1 class="display-1 fw-bold mb-4">Demo</h1>
51-
<div class="wrapper">
52-
<div class="demo my-demo" something="alocal">
51+
<div class="wrapper my-demo" something="alocal">
52+
<div class="demo">
5353
<h1>anObjectDefault</h1>
5454
<p>
5555
<strong>first</strong>
@@ -157,6 +157,8 @@ <h1>anArray</h1>
157157
<hr>
158158
<h1>aComputed</h1>
159159
<p>Yes</p>
160+
My filled slot
161+
Hello
160162
</div>
161163
</div>
162164
</div>

examples/src/components/demo.html

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
}
4848
],
4949
anArray: ['first', 'second'],
50-
aComputed: locals.aComputed === true ? 'Yes' : 'No',
50+
aComputed: locals.aComputed === true ? 'Yes' : 'No'
5151
};
5252
</script>
5353
<div class="wrapper">
54-
<div class="demo" attributes>
54+
<div class="demo">
5555
<h1>anObjectDefault</h1>
5656
<each loop="o,i in anObjectDefault">
5757
<p><strong>{{ i }}</strong>: {{ o }}</p>
@@ -95,5 +95,9 @@ <h1>anArray</h1>
9595

9696
<h1>aComputed</h1>
9797
<p>{{ aComputed }}</p>
98+
99+
<slot:name>
100+
{{ $slots.name.locals.aslotlocal }}
101+
</slot:name>
98102
</div>
99103
</div>

examples/src/pages/demo.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ <h1 class="display-1 fw-bold mb-4">Demo</h1>
1919
merge:locals='{ "something": "alocal", "anArray": [ "ten" ] }'
2020

2121
class="my-demo"
22-
></x-demo>
22+
>
23+
<fill:name aslotlocal="Hello" prepend>My filled slot</fill:name>
24+
</x-demo>
2325
</div>
2426
</div>
2527
</x-page>

src/slots.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
const {match} = require('posthtml/lib/api');
44
const {render} = require('posthtml-render');
5+
const {each, omit} = require('underscore');
6+
7+
const separator = ':';
58

69
/**
710
* Set filled slots
@@ -17,15 +20,14 @@ function setFilledSlots(currentNode, filledSlots, {fill}) {
1720
fillNode.attrs = {};
1821
}
1922

20-
const name = fillNode.tag.split(':')[1];
23+
const name = fillNode.tag.split(separator)[1];
2124

22-
/** @var {Object} locals - NOT YET TESTED */
23-
const locals = Object.fromEntries(Object.entries(fillNode.attrs).filter(([attributeName]) => ![name, 'type'].includes(attributeName)));
25+
const locals = omit(fillNode.attrs, [name, 'type', 'append', 'prepend', 'aware']);
2426

2527
if (locals) {
26-
Object.keys(locals).forEach(local => {
28+
each(locals, (value, key, attrs) => {
2729
try {
28-
locals[local] = JSON.parse(locals[local]);
30+
attrs[key] = JSON.parse(value);
2931
} catch {}
3032
});
3133
}
@@ -54,7 +56,7 @@ function setFilledSlots(currentNode, filledSlots, {fill}) {
5456
*/
5557
function processFillContent(tree, filledSlots, {fill}) {
5658
match.call(tree, {tag: fill}, fillNode => {
57-
const name = fillNode.tag.split(':')[1];
59+
const name = fillNode.tag.split(separator)[1];
5860

5961
if (!filledSlots[name]) {
6062
filledSlots[name] = {};
@@ -83,7 +85,7 @@ function processFillContent(tree, filledSlots, {fill}) {
8385
*/
8486
function processSlotContent(tree, filledSlots, {slot}) {
8587
match.call(tree, {tag: slot}, slotNode => {
86-
const name = slotNode.tag.split(':')[1];
88+
const name = slotNode.tag.split(separator)[1];
8789

8890
slotNode.tag = false;
8991

0 commit comments

Comments
 (0)