@solidjs/universal 2.0.0-experimental.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/LICENSE +21 -0
- package/README.md +122 -0
- package/dist/dev.cjs +214 -0
- package/dist/dev.js +239 -0
- package/dist/universal.cjs +214 -0
- package/dist/universal.js +239 -0
- package/package.json +53 -0
- package/types/index.d.ts +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-2023 Ryan Carniato
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Solid Universal
|
|
2
|
+
|
|
3
|
+
This contains the means to create the runtime for a custom renderer for Solid. This can enable using Solid to render to different platforms like native mobile and desktop, canvas or WebGL, or even the terminal. It relies on custom compilation from `babel-preset-solid` and exporting the result of `createRenderer` at a referenceable location.
|
|
4
|
+
|
|
5
|
+
## Example
|
|
6
|
+
|
|
7
|
+
### Babel
|
|
8
|
+
|
|
9
|
+
To use a custom renderer available in the (fictional) `solid-custom-dom` package you'd configure your babelrc as:
|
|
10
|
+
```json
|
|
11
|
+
{
|
|
12
|
+
"presets": [
|
|
13
|
+
[
|
|
14
|
+
"babel-preset-solid",
|
|
15
|
+
{
|
|
16
|
+
"moduleName": "solid-custom-dom",
|
|
17
|
+
"generate": "universal"
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
]
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Vite
|
|
25
|
+
|
|
26
|
+
To use a custom renderer available in the (fictional) `solid-custom-dom` package you'd configure your vite config as:
|
|
27
|
+
```js
|
|
28
|
+
import { defineConfig } from 'vite';
|
|
29
|
+
import solidPlugin from 'vite-plugin-solid';
|
|
30
|
+
|
|
31
|
+
export default defineConfig({
|
|
32
|
+
plugins: [solidPlugin({
|
|
33
|
+
solid: {
|
|
34
|
+
moduleName: "solid-custom-dom",
|
|
35
|
+
generate: 'universal'
|
|
36
|
+
}
|
|
37
|
+
})]
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
To create a custom renderer you must implement certain methods and export (as named exports) the results. You may also want to forward `solid-js` control flow to allow them to be auto imported as well.
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
// example custom dom renderer
|
|
45
|
+
import { createRenderer } from "solid-js/universal";
|
|
46
|
+
|
|
47
|
+
const PROPERTIES = new Set(["className", "textContent"]);
|
|
48
|
+
|
|
49
|
+
export const {
|
|
50
|
+
render,
|
|
51
|
+
effect,
|
|
52
|
+
memo,
|
|
53
|
+
createComponent,
|
|
54
|
+
createElement,
|
|
55
|
+
createTextNode,
|
|
56
|
+
insertNode,
|
|
57
|
+
insert,
|
|
58
|
+
spread,
|
|
59
|
+
setProp,
|
|
60
|
+
mergeProps,
|
|
61
|
+
use
|
|
62
|
+
} = createRenderer({
|
|
63
|
+
createElement(string) {
|
|
64
|
+
return document.createElement(string);
|
|
65
|
+
},
|
|
66
|
+
createTextNode(value) {
|
|
67
|
+
return document.createTextNode(value);
|
|
68
|
+
},
|
|
69
|
+
replaceText(textNode, value) {
|
|
70
|
+
textNode.data = value;
|
|
71
|
+
},
|
|
72
|
+
setProperty(node, name, value) {
|
|
73
|
+
if (name === "style") Object.assign(node.style, value);
|
|
74
|
+
else if (name.startsWith("on")) node[name.toLowerCase()] = value;
|
|
75
|
+
else if (PROPERTIES.has(name)) node[name] = value;
|
|
76
|
+
else node.setAttribute(name, value);
|
|
77
|
+
},
|
|
78
|
+
insertNode(parent, node, anchor) {
|
|
79
|
+
parent.insertBefore(node, anchor);
|
|
80
|
+
},
|
|
81
|
+
isTextNode(node) {
|
|
82
|
+
return node.type === 3;
|
|
83
|
+
},
|
|
84
|
+
removeNode(parent, node) {
|
|
85
|
+
parent.removeChild(node);
|
|
86
|
+
},
|
|
87
|
+
getParentNode(node) {
|
|
88
|
+
return node.parentNode;
|
|
89
|
+
},
|
|
90
|
+
getFirstChild(node) {
|
|
91
|
+
return node.firstChild;
|
|
92
|
+
},
|
|
93
|
+
getNextSibling(node) {
|
|
94
|
+
return node.nextSibling;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Forward Solid control flow
|
|
99
|
+
export {
|
|
100
|
+
For,
|
|
101
|
+
Show,
|
|
102
|
+
Suspense,
|
|
103
|
+
SuspenseList,
|
|
104
|
+
Switch,
|
|
105
|
+
Match,
|
|
106
|
+
Index,
|
|
107
|
+
ErrorBoundary
|
|
108
|
+
} from "solid-js";
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Then to consume:
|
|
112
|
+
```js
|
|
113
|
+
import { render } from "solid-custom-dom";
|
|
114
|
+
|
|
115
|
+
function App() {
|
|
116
|
+
// the skies the limits
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
render(() => <App />, mountNode)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
> Note: For TypeScript support of non-standard JSX you will need to provide your own types at a jsx-runtime entry on your package so that it can be set as the `jsxImportSource`. If mixing and matching different JSX implementations you will need use the per file pragmas.
|
package/dist/dev.cjs
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var solidJs = require('solid-js');
|
|
4
|
+
|
|
5
|
+
function createRenderer({
|
|
6
|
+
createElement,
|
|
7
|
+
createTextNode,
|
|
8
|
+
isTextNode,
|
|
9
|
+
replaceText,
|
|
10
|
+
insertNode,
|
|
11
|
+
removeNode,
|
|
12
|
+
setProperty,
|
|
13
|
+
getParentNode,
|
|
14
|
+
getFirstChild,
|
|
15
|
+
getNextSibling
|
|
16
|
+
}) {
|
|
17
|
+
function insert(parent, accessor, marker, initial) {
|
|
18
|
+
const multi = marker !== undefined;
|
|
19
|
+
if (multi && !initial) initial = [];
|
|
20
|
+
if (typeof accessor !== "function") {
|
|
21
|
+
accessor = normalize(accessor, multi, true);
|
|
22
|
+
if (typeof accessor !== "function") return insertExpression(parent, accessor, initial, marker);
|
|
23
|
+
}
|
|
24
|
+
solidJs.createRenderEffect(() => normalize(accessor, multi), (value, current) => insertExpression(parent, value, current, marker), initial);
|
|
25
|
+
}
|
|
26
|
+
function insertExpression(parent, value, current, marker) {
|
|
27
|
+
if (value === current) return;
|
|
28
|
+
const t = typeof value,
|
|
29
|
+
multi = marker !== undefined;
|
|
30
|
+
if (t === "string" || t === "number") {
|
|
31
|
+
const tc = typeof current;
|
|
32
|
+
if (tc === "string" || tc === "number") {
|
|
33
|
+
replaceText(getFirstChild(parent), value);
|
|
34
|
+
} else {
|
|
35
|
+
cleanChildren(parent, current, marker, createTextNode(value));
|
|
36
|
+
}
|
|
37
|
+
} else if (value == null) {
|
|
38
|
+
cleanChildren(parent, current, marker);
|
|
39
|
+
} else if (Array.isArray(value)) {
|
|
40
|
+
if (value.length === 0) {
|
|
41
|
+
cleanChildren(parent, current, marker);
|
|
42
|
+
} else {
|
|
43
|
+
if (Array.isArray(current)) {
|
|
44
|
+
if (current.length === 0) {
|
|
45
|
+
appendNodes(parent, value, marker);
|
|
46
|
+
} else reconcileArrays(parent, current, value);
|
|
47
|
+
} else if (current == null) {
|
|
48
|
+
appendNodes(parent, value);
|
|
49
|
+
} else {
|
|
50
|
+
reconcileArrays(parent, multi && current || [getFirstChild(parent)], value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
if (Array.isArray(current)) {
|
|
55
|
+
cleanChildren(parent, current, multi ? marker : null, value);
|
|
56
|
+
} else if (current == null || !getFirstChild(parent)) {
|
|
57
|
+
insertNode(parent, value);
|
|
58
|
+
} else replaceNode(parent, value, getFirstChild(parent));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function normalize(value, multi, doNotUnwrap) {
|
|
62
|
+
value = solidJs.flatten(value, {
|
|
63
|
+
skipNonRendered: true,
|
|
64
|
+
doNotUnwrap
|
|
65
|
+
});
|
|
66
|
+
if (doNotUnwrap && typeof value === "function") return value;
|
|
67
|
+
if (multi && value != null && !Array.isArray(value)) value = [value];
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
for (let i = 0, len = value.length; i < len; i++) {
|
|
70
|
+
const item = value[i],
|
|
71
|
+
t = typeof item;
|
|
72
|
+
if (t === "string" || t === "number") value[i] = document.createTextNode(item);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
function reconcileArrays(parentNode, a, b) {
|
|
78
|
+
let bLength = b.length,
|
|
79
|
+
aEnd = a.length,
|
|
80
|
+
bEnd = bLength,
|
|
81
|
+
aStart = 0,
|
|
82
|
+
bStart = 0,
|
|
83
|
+
after = getNextSibling(a[aEnd - 1]),
|
|
84
|
+
map = null;
|
|
85
|
+
while (aStart < aEnd || bStart < bEnd) {
|
|
86
|
+
if (a[aStart] === b[bStart]) {
|
|
87
|
+
aStart++;
|
|
88
|
+
bStart++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
while (a[aEnd - 1] === b[bEnd - 1]) {
|
|
92
|
+
aEnd--;
|
|
93
|
+
bEnd--;
|
|
94
|
+
}
|
|
95
|
+
if (aEnd === aStart) {
|
|
96
|
+
const node = bEnd < bLength ? bStart ? getNextSibling(b[bStart - 1]) : b[bEnd - bStart] : after;
|
|
97
|
+
while (bStart < bEnd) insertNode(parentNode, b[bStart++], node);
|
|
98
|
+
} else if (bEnd === bStart) {
|
|
99
|
+
while (aStart < aEnd) {
|
|
100
|
+
if (!map || !map.has(a[aStart])) removeNode(parentNode, a[aStart]);
|
|
101
|
+
aStart++;
|
|
102
|
+
}
|
|
103
|
+
} else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
|
|
104
|
+
const node = getNextSibling(a[--aEnd]);
|
|
105
|
+
insertNode(parentNode, b[bStart++], getNextSibling(a[aStart++]));
|
|
106
|
+
insertNode(parentNode, b[--bEnd], node);
|
|
107
|
+
a[aEnd] = b[bEnd];
|
|
108
|
+
} else {
|
|
109
|
+
if (!map) {
|
|
110
|
+
map = new Map();
|
|
111
|
+
let i = bStart;
|
|
112
|
+
while (i < bEnd) map.set(b[i], i++);
|
|
113
|
+
}
|
|
114
|
+
const index = map.get(a[aStart]);
|
|
115
|
+
if (index != null) {
|
|
116
|
+
if (bStart < index && index < bEnd) {
|
|
117
|
+
let i = aStart,
|
|
118
|
+
sequence = 1,
|
|
119
|
+
t;
|
|
120
|
+
while (++i < aEnd && i < bEnd) {
|
|
121
|
+
if ((t = map.get(a[i])) == null || t !== index + sequence) break;
|
|
122
|
+
sequence++;
|
|
123
|
+
}
|
|
124
|
+
if (sequence > index - bStart) {
|
|
125
|
+
const node = a[aStart];
|
|
126
|
+
while (bStart < index) insertNode(parentNode, b[bStart++], node);
|
|
127
|
+
} else replaceNode(parentNode, b[bStart++], a[aStart++]);
|
|
128
|
+
} else aStart++;
|
|
129
|
+
} else removeNode(parentNode, a[aStart++]);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function cleanChildren(parent, current, marker, replacement) {
|
|
134
|
+
if (marker === undefined) {
|
|
135
|
+
let removed;
|
|
136
|
+
while (removed = getFirstChild(parent)) removeNode(parent, removed);
|
|
137
|
+
replacement && insertNode(parent, replacement);
|
|
138
|
+
return "";
|
|
139
|
+
}
|
|
140
|
+
if (current.length) {
|
|
141
|
+
let inserted = false;
|
|
142
|
+
for (let i = current.length - 1; i >= 0; i--) {
|
|
143
|
+
const el = current[i];
|
|
144
|
+
if (replacement !== el) {
|
|
145
|
+
const isParent = getParentNode(el) === parent;
|
|
146
|
+
if (replacement && !inserted && !i) isParent ? replaceNode(parent, replacement, el) : insertNode(parent, replacement, marker);else isParent && removeNode(parent, el);
|
|
147
|
+
} else inserted = true;
|
|
148
|
+
}
|
|
149
|
+
} else if (replacement) insertNode(parent, replacement, marker);
|
|
150
|
+
}
|
|
151
|
+
function appendNodes(parent, array, marker) {
|
|
152
|
+
for (let i = 0, len = array.length; i < len; i++) insertNode(parent, array[i], marker);
|
|
153
|
+
}
|
|
154
|
+
function replaceNode(parent, newNode, oldNode) {
|
|
155
|
+
insertNode(parent, newNode, oldNode);
|
|
156
|
+
removeNode(parent, oldNode);
|
|
157
|
+
}
|
|
158
|
+
function spread(node, props, skipChildren) {
|
|
159
|
+
const prevProps = {};
|
|
160
|
+
props || (props = {});
|
|
161
|
+
if (!skipChildren) {
|
|
162
|
+
solidJs.createRenderEffect(() => normalize(props.children), value => {
|
|
163
|
+
insertExpression(node, value, prevProps.children);
|
|
164
|
+
prevProps.children = value;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
solidJs.createRenderEffect(() => props.ref && props.ref(node), () => ({}));
|
|
168
|
+
solidJs.createRenderEffect(() => {
|
|
169
|
+
const newProps = {};
|
|
170
|
+
for (const prop in props) {
|
|
171
|
+
if (prop === "children" || prop === "ref") continue;
|
|
172
|
+
const value = props[prop];
|
|
173
|
+
if (value === prevProps[prop]) continue;
|
|
174
|
+
newProps[prop] = value;
|
|
175
|
+
}
|
|
176
|
+
return newProps;
|
|
177
|
+
}, props => {
|
|
178
|
+
for (const prop in props) {
|
|
179
|
+
const value = props[prop];
|
|
180
|
+
setProperty(node, prop, value, prevProps[prop]);
|
|
181
|
+
prevProps[prop] = value;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return prevProps;
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
render(code, element) {
|
|
188
|
+
let disposer;
|
|
189
|
+
solidJs.createRoot(dispose => {
|
|
190
|
+
disposer = dispose;
|
|
191
|
+
insert(element, code());
|
|
192
|
+
});
|
|
193
|
+
return disposer;
|
|
194
|
+
},
|
|
195
|
+
insert,
|
|
196
|
+
spread,
|
|
197
|
+
createElement,
|
|
198
|
+
createTextNode,
|
|
199
|
+
insertNode,
|
|
200
|
+
setProp(node, name, value, prev) {
|
|
201
|
+
setProperty(node, name, value, prev);
|
|
202
|
+
return value;
|
|
203
|
+
},
|
|
204
|
+
mergeProps: solidJs.merge,
|
|
205
|
+
effect: solidJs.createRenderEffect,
|
|
206
|
+
memo: solidJs.createMemo,
|
|
207
|
+
createComponent: solidJs.createComponent,
|
|
208
|
+
use(fn, element, arg) {
|
|
209
|
+
return solidJs.untrack(() => fn(element, arg));
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
exports.createRenderer = createRenderer;
|
package/dist/dev.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRoot,
|
|
3
|
+
merge,
|
|
4
|
+
createRenderEffect,
|
|
5
|
+
createMemo,
|
|
6
|
+
createComponent,
|
|
7
|
+
untrack,
|
|
8
|
+
flatten
|
|
9
|
+
} from "solid-js";
|
|
10
|
+
|
|
11
|
+
function createRenderer({
|
|
12
|
+
createElement,
|
|
13
|
+
createTextNode,
|
|
14
|
+
isTextNode,
|
|
15
|
+
replaceText,
|
|
16
|
+
insertNode,
|
|
17
|
+
removeNode,
|
|
18
|
+
setProperty,
|
|
19
|
+
getParentNode,
|
|
20
|
+
getFirstChild,
|
|
21
|
+
getNextSibling
|
|
22
|
+
}) {
|
|
23
|
+
function insert(parent, accessor, marker, initial) {
|
|
24
|
+
const multi = marker !== undefined;
|
|
25
|
+
if (multi && !initial) initial = [];
|
|
26
|
+
if (typeof accessor !== "function") {
|
|
27
|
+
accessor = normalize(accessor, multi, true);
|
|
28
|
+
if (typeof accessor !== "function")
|
|
29
|
+
return insertExpression(parent, accessor, initial, marker);
|
|
30
|
+
}
|
|
31
|
+
createRenderEffect(
|
|
32
|
+
() => normalize(accessor, multi),
|
|
33
|
+
(value, current) => insertExpression(parent, value, current, marker),
|
|
34
|
+
initial
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
function insertExpression(parent, value, current, marker) {
|
|
38
|
+
if (value === current) return;
|
|
39
|
+
const t = typeof value,
|
|
40
|
+
multi = marker !== undefined;
|
|
41
|
+
if (t === "string" || t === "number") {
|
|
42
|
+
const tc = typeof current;
|
|
43
|
+
if (tc === "string" || tc === "number") {
|
|
44
|
+
replaceText(getFirstChild(parent), value);
|
|
45
|
+
} else {
|
|
46
|
+
cleanChildren(parent, current, marker, createTextNode(value));
|
|
47
|
+
}
|
|
48
|
+
} else if (value == null) {
|
|
49
|
+
cleanChildren(parent, current, marker);
|
|
50
|
+
} else if (Array.isArray(value)) {
|
|
51
|
+
if (value.length === 0) {
|
|
52
|
+
cleanChildren(parent, current, marker);
|
|
53
|
+
} else {
|
|
54
|
+
if (Array.isArray(current)) {
|
|
55
|
+
if (current.length === 0) {
|
|
56
|
+
appendNodes(parent, value, marker);
|
|
57
|
+
} else reconcileArrays(parent, current, value);
|
|
58
|
+
} else if (current == null) {
|
|
59
|
+
appendNodes(parent, value);
|
|
60
|
+
} else {
|
|
61
|
+
reconcileArrays(parent, (multi && current) || [getFirstChild(parent)], value);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
if (Array.isArray(current)) {
|
|
66
|
+
cleanChildren(parent, current, multi ? marker : null, value);
|
|
67
|
+
} else if (current == null || !getFirstChild(parent)) {
|
|
68
|
+
insertNode(parent, value);
|
|
69
|
+
} else replaceNode(parent, value, getFirstChild(parent));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function normalize(value, multi, doNotUnwrap) {
|
|
73
|
+
value = flatten(value, {
|
|
74
|
+
skipNonRendered: true,
|
|
75
|
+
doNotUnwrap
|
|
76
|
+
});
|
|
77
|
+
if (doNotUnwrap && typeof value === "function") return value;
|
|
78
|
+
if (multi && value != null && !Array.isArray(value)) value = [value];
|
|
79
|
+
if (Array.isArray(value)) {
|
|
80
|
+
for (let i = 0, len = value.length; i < len; i++) {
|
|
81
|
+
const item = value[i],
|
|
82
|
+
t = typeof item;
|
|
83
|
+
if (t === "string" || t === "number") value[i] = document.createTextNode(item);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
function reconcileArrays(parentNode, a, b) {
|
|
89
|
+
let bLength = b.length,
|
|
90
|
+
aEnd = a.length,
|
|
91
|
+
bEnd = bLength,
|
|
92
|
+
aStart = 0,
|
|
93
|
+
bStart = 0,
|
|
94
|
+
after = getNextSibling(a[aEnd - 1]),
|
|
95
|
+
map = null;
|
|
96
|
+
while (aStart < aEnd || bStart < bEnd) {
|
|
97
|
+
if (a[aStart] === b[bStart]) {
|
|
98
|
+
aStart++;
|
|
99
|
+
bStart++;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
while (a[aEnd - 1] === b[bEnd - 1]) {
|
|
103
|
+
aEnd--;
|
|
104
|
+
bEnd--;
|
|
105
|
+
}
|
|
106
|
+
if (aEnd === aStart) {
|
|
107
|
+
const node =
|
|
108
|
+
bEnd < bLength ? (bStart ? getNextSibling(b[bStart - 1]) : b[bEnd - bStart]) : after;
|
|
109
|
+
while (bStart < bEnd) insertNode(parentNode, b[bStart++], node);
|
|
110
|
+
} else if (bEnd === bStart) {
|
|
111
|
+
while (aStart < aEnd) {
|
|
112
|
+
if (!map || !map.has(a[aStart])) removeNode(parentNode, a[aStart]);
|
|
113
|
+
aStart++;
|
|
114
|
+
}
|
|
115
|
+
} else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
|
|
116
|
+
const node = getNextSibling(a[--aEnd]);
|
|
117
|
+
insertNode(parentNode, b[bStart++], getNextSibling(a[aStart++]));
|
|
118
|
+
insertNode(parentNode, b[--bEnd], node);
|
|
119
|
+
a[aEnd] = b[bEnd];
|
|
120
|
+
} else {
|
|
121
|
+
if (!map) {
|
|
122
|
+
map = new Map();
|
|
123
|
+
let i = bStart;
|
|
124
|
+
while (i < bEnd) map.set(b[i], i++);
|
|
125
|
+
}
|
|
126
|
+
const index = map.get(a[aStart]);
|
|
127
|
+
if (index != null) {
|
|
128
|
+
if (bStart < index && index < bEnd) {
|
|
129
|
+
let i = aStart,
|
|
130
|
+
sequence = 1,
|
|
131
|
+
t;
|
|
132
|
+
while (++i < aEnd && i < bEnd) {
|
|
133
|
+
if ((t = map.get(a[i])) == null || t !== index + sequence) break;
|
|
134
|
+
sequence++;
|
|
135
|
+
}
|
|
136
|
+
if (sequence > index - bStart) {
|
|
137
|
+
const node = a[aStart];
|
|
138
|
+
while (bStart < index) insertNode(parentNode, b[bStart++], node);
|
|
139
|
+
} else replaceNode(parentNode, b[bStart++], a[aStart++]);
|
|
140
|
+
} else aStart++;
|
|
141
|
+
} else removeNode(parentNode, a[aStart++]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function cleanChildren(parent, current, marker, replacement) {
|
|
146
|
+
if (marker === undefined) {
|
|
147
|
+
let removed;
|
|
148
|
+
while ((removed = getFirstChild(parent))) removeNode(parent, removed);
|
|
149
|
+
replacement && insertNode(parent, replacement);
|
|
150
|
+
return "";
|
|
151
|
+
}
|
|
152
|
+
if (current.length) {
|
|
153
|
+
let inserted = false;
|
|
154
|
+
for (let i = current.length - 1; i >= 0; i--) {
|
|
155
|
+
const el = current[i];
|
|
156
|
+
if (replacement !== el) {
|
|
157
|
+
const isParent = getParentNode(el) === parent;
|
|
158
|
+
if (replacement && !inserted && !i)
|
|
159
|
+
isParent
|
|
160
|
+
? replaceNode(parent, replacement, el)
|
|
161
|
+
: insertNode(parent, replacement, marker);
|
|
162
|
+
else isParent && removeNode(parent, el);
|
|
163
|
+
} else inserted = true;
|
|
164
|
+
}
|
|
165
|
+
} else if (replacement) insertNode(parent, replacement, marker);
|
|
166
|
+
}
|
|
167
|
+
function appendNodes(parent, array, marker) {
|
|
168
|
+
for (let i = 0, len = array.length; i < len; i++) insertNode(parent, array[i], marker);
|
|
169
|
+
}
|
|
170
|
+
function replaceNode(parent, newNode, oldNode) {
|
|
171
|
+
insertNode(parent, newNode, oldNode);
|
|
172
|
+
removeNode(parent, oldNode);
|
|
173
|
+
}
|
|
174
|
+
function spread(node, props, skipChildren) {
|
|
175
|
+
const prevProps = {};
|
|
176
|
+
props || (props = {});
|
|
177
|
+
if (!skipChildren) {
|
|
178
|
+
createRenderEffect(
|
|
179
|
+
() => normalize(props.children),
|
|
180
|
+
value => {
|
|
181
|
+
insertExpression(node, value, prevProps.children);
|
|
182
|
+
prevProps.children = value;
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
createRenderEffect(
|
|
187
|
+
() => props.ref && props.ref(node),
|
|
188
|
+
() => ({})
|
|
189
|
+
);
|
|
190
|
+
createRenderEffect(
|
|
191
|
+
() => {
|
|
192
|
+
const newProps = {};
|
|
193
|
+
for (const prop in props) {
|
|
194
|
+
if (prop === "children" || prop === "ref") continue;
|
|
195
|
+
const value = props[prop];
|
|
196
|
+
if (value === prevProps[prop]) continue;
|
|
197
|
+
newProps[prop] = value;
|
|
198
|
+
}
|
|
199
|
+
return newProps;
|
|
200
|
+
},
|
|
201
|
+
props => {
|
|
202
|
+
for (const prop in props) {
|
|
203
|
+
const value = props[prop];
|
|
204
|
+
setProperty(node, prop, value, prevProps[prop]);
|
|
205
|
+
prevProps[prop] = value;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
return prevProps;
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
render(code, element) {
|
|
213
|
+
let disposer;
|
|
214
|
+
createRoot(dispose => {
|
|
215
|
+
disposer = dispose;
|
|
216
|
+
insert(element, code());
|
|
217
|
+
});
|
|
218
|
+
return disposer;
|
|
219
|
+
},
|
|
220
|
+
insert,
|
|
221
|
+
spread,
|
|
222
|
+
createElement,
|
|
223
|
+
createTextNode,
|
|
224
|
+
insertNode,
|
|
225
|
+
setProp(node, name, value, prev) {
|
|
226
|
+
setProperty(node, name, value, prev);
|
|
227
|
+
return value;
|
|
228
|
+
},
|
|
229
|
+
mergeProps: merge,
|
|
230
|
+
effect: createRenderEffect,
|
|
231
|
+
memo: createMemo,
|
|
232
|
+
createComponent,
|
|
233
|
+
use(fn, element, arg) {
|
|
234
|
+
return untrack(() => fn(element, arg));
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export { createRenderer };
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var solidJs = require('solid-js');
|
|
4
|
+
|
|
5
|
+
function createRenderer({
|
|
6
|
+
createElement,
|
|
7
|
+
createTextNode,
|
|
8
|
+
isTextNode,
|
|
9
|
+
replaceText,
|
|
10
|
+
insertNode,
|
|
11
|
+
removeNode,
|
|
12
|
+
setProperty,
|
|
13
|
+
getParentNode,
|
|
14
|
+
getFirstChild,
|
|
15
|
+
getNextSibling
|
|
16
|
+
}) {
|
|
17
|
+
function insert(parent, accessor, marker, initial) {
|
|
18
|
+
const multi = marker !== undefined;
|
|
19
|
+
if (multi && !initial) initial = [];
|
|
20
|
+
if (typeof accessor !== "function") {
|
|
21
|
+
accessor = normalize(accessor, multi, true);
|
|
22
|
+
if (typeof accessor !== "function") return insertExpression(parent, accessor, initial, marker);
|
|
23
|
+
}
|
|
24
|
+
solidJs.createRenderEffect(() => normalize(accessor, multi), (value, current) => insertExpression(parent, value, current, marker), initial);
|
|
25
|
+
}
|
|
26
|
+
function insertExpression(parent, value, current, marker) {
|
|
27
|
+
if (value === current) return;
|
|
28
|
+
const t = typeof value,
|
|
29
|
+
multi = marker !== undefined;
|
|
30
|
+
if (t === "string" || t === "number") {
|
|
31
|
+
const tc = typeof current;
|
|
32
|
+
if (tc === "string" || tc === "number") {
|
|
33
|
+
replaceText(getFirstChild(parent), value);
|
|
34
|
+
} else {
|
|
35
|
+
cleanChildren(parent, current, marker, createTextNode(value));
|
|
36
|
+
}
|
|
37
|
+
} else if (value == null) {
|
|
38
|
+
cleanChildren(parent, current, marker);
|
|
39
|
+
} else if (Array.isArray(value)) {
|
|
40
|
+
if (value.length === 0) {
|
|
41
|
+
cleanChildren(parent, current, marker);
|
|
42
|
+
} else {
|
|
43
|
+
if (Array.isArray(current)) {
|
|
44
|
+
if (current.length === 0) {
|
|
45
|
+
appendNodes(parent, value, marker);
|
|
46
|
+
} else reconcileArrays(parent, current, value);
|
|
47
|
+
} else if (current == null) {
|
|
48
|
+
appendNodes(parent, value);
|
|
49
|
+
} else {
|
|
50
|
+
reconcileArrays(parent, multi && current || [getFirstChild(parent)], value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
if (Array.isArray(current)) {
|
|
55
|
+
cleanChildren(parent, current, multi ? marker : null, value);
|
|
56
|
+
} else if (current == null || !getFirstChild(parent)) {
|
|
57
|
+
insertNode(parent, value);
|
|
58
|
+
} else replaceNode(parent, value, getFirstChild(parent));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function normalize(value, multi, doNotUnwrap) {
|
|
62
|
+
value = solidJs.flatten(value, {
|
|
63
|
+
skipNonRendered: true,
|
|
64
|
+
doNotUnwrap
|
|
65
|
+
});
|
|
66
|
+
if (doNotUnwrap && typeof value === "function") return value;
|
|
67
|
+
if (multi && value != null && !Array.isArray(value)) value = [value];
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
for (let i = 0, len = value.length; i < len; i++) {
|
|
70
|
+
const item = value[i],
|
|
71
|
+
t = typeof item;
|
|
72
|
+
if (t === "string" || t === "number") value[i] = document.createTextNode(item);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
function reconcileArrays(parentNode, a, b) {
|
|
78
|
+
let bLength = b.length,
|
|
79
|
+
aEnd = a.length,
|
|
80
|
+
bEnd = bLength,
|
|
81
|
+
aStart = 0,
|
|
82
|
+
bStart = 0,
|
|
83
|
+
after = getNextSibling(a[aEnd - 1]),
|
|
84
|
+
map = null;
|
|
85
|
+
while (aStart < aEnd || bStart < bEnd) {
|
|
86
|
+
if (a[aStart] === b[bStart]) {
|
|
87
|
+
aStart++;
|
|
88
|
+
bStart++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
while (a[aEnd - 1] === b[bEnd - 1]) {
|
|
92
|
+
aEnd--;
|
|
93
|
+
bEnd--;
|
|
94
|
+
}
|
|
95
|
+
if (aEnd === aStart) {
|
|
96
|
+
const node = bEnd < bLength ? bStart ? getNextSibling(b[bStart - 1]) : b[bEnd - bStart] : after;
|
|
97
|
+
while (bStart < bEnd) insertNode(parentNode, b[bStart++], node);
|
|
98
|
+
} else if (bEnd === bStart) {
|
|
99
|
+
while (aStart < aEnd) {
|
|
100
|
+
if (!map || !map.has(a[aStart])) removeNode(parentNode, a[aStart]);
|
|
101
|
+
aStart++;
|
|
102
|
+
}
|
|
103
|
+
} else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
|
|
104
|
+
const node = getNextSibling(a[--aEnd]);
|
|
105
|
+
insertNode(parentNode, b[bStart++], getNextSibling(a[aStart++]));
|
|
106
|
+
insertNode(parentNode, b[--bEnd], node);
|
|
107
|
+
a[aEnd] = b[bEnd];
|
|
108
|
+
} else {
|
|
109
|
+
if (!map) {
|
|
110
|
+
map = new Map();
|
|
111
|
+
let i = bStart;
|
|
112
|
+
while (i < bEnd) map.set(b[i], i++);
|
|
113
|
+
}
|
|
114
|
+
const index = map.get(a[aStart]);
|
|
115
|
+
if (index != null) {
|
|
116
|
+
if (bStart < index && index < bEnd) {
|
|
117
|
+
let i = aStart,
|
|
118
|
+
sequence = 1,
|
|
119
|
+
t;
|
|
120
|
+
while (++i < aEnd && i < bEnd) {
|
|
121
|
+
if ((t = map.get(a[i])) == null || t !== index + sequence) break;
|
|
122
|
+
sequence++;
|
|
123
|
+
}
|
|
124
|
+
if (sequence > index - bStart) {
|
|
125
|
+
const node = a[aStart];
|
|
126
|
+
while (bStart < index) insertNode(parentNode, b[bStart++], node);
|
|
127
|
+
} else replaceNode(parentNode, b[bStart++], a[aStart++]);
|
|
128
|
+
} else aStart++;
|
|
129
|
+
} else removeNode(parentNode, a[aStart++]);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function cleanChildren(parent, current, marker, replacement) {
|
|
134
|
+
if (marker === undefined) {
|
|
135
|
+
let removed;
|
|
136
|
+
while (removed = getFirstChild(parent)) removeNode(parent, removed);
|
|
137
|
+
replacement && insertNode(parent, replacement);
|
|
138
|
+
return "";
|
|
139
|
+
}
|
|
140
|
+
if (current.length) {
|
|
141
|
+
let inserted = false;
|
|
142
|
+
for (let i = current.length - 1; i >= 0; i--) {
|
|
143
|
+
const el = current[i];
|
|
144
|
+
if (replacement !== el) {
|
|
145
|
+
const isParent = getParentNode(el) === parent;
|
|
146
|
+
if (replacement && !inserted && !i) isParent ? replaceNode(parent, replacement, el) : insertNode(parent, replacement, marker);else isParent && removeNode(parent, el);
|
|
147
|
+
} else inserted = true;
|
|
148
|
+
}
|
|
149
|
+
} else if (replacement) insertNode(parent, replacement, marker);
|
|
150
|
+
}
|
|
151
|
+
function appendNodes(parent, array, marker) {
|
|
152
|
+
for (let i = 0, len = array.length; i < len; i++) insertNode(parent, array[i], marker);
|
|
153
|
+
}
|
|
154
|
+
function replaceNode(parent, newNode, oldNode) {
|
|
155
|
+
insertNode(parent, newNode, oldNode);
|
|
156
|
+
removeNode(parent, oldNode);
|
|
157
|
+
}
|
|
158
|
+
function spread(node, props, skipChildren) {
|
|
159
|
+
const prevProps = {};
|
|
160
|
+
props || (props = {});
|
|
161
|
+
if (!skipChildren) {
|
|
162
|
+
solidJs.createRenderEffect(() => normalize(props.children), value => {
|
|
163
|
+
insertExpression(node, value, prevProps.children);
|
|
164
|
+
prevProps.children = value;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
solidJs.createRenderEffect(() => props.ref && props.ref(node), () => ({}));
|
|
168
|
+
solidJs.createRenderEffect(() => {
|
|
169
|
+
const newProps = {};
|
|
170
|
+
for (const prop in props) {
|
|
171
|
+
if (prop === "children" || prop === "ref") continue;
|
|
172
|
+
const value = props[prop];
|
|
173
|
+
if (value === prevProps[prop]) continue;
|
|
174
|
+
newProps[prop] = value;
|
|
175
|
+
}
|
|
176
|
+
return newProps;
|
|
177
|
+
}, props => {
|
|
178
|
+
for (const prop in props) {
|
|
179
|
+
const value = props[prop];
|
|
180
|
+
setProperty(node, prop, value, prevProps[prop]);
|
|
181
|
+
prevProps[prop] = value;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return prevProps;
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
render(code, element) {
|
|
188
|
+
let disposer;
|
|
189
|
+
solidJs.createRoot(dispose => {
|
|
190
|
+
disposer = dispose;
|
|
191
|
+
insert(element, code());
|
|
192
|
+
});
|
|
193
|
+
return disposer;
|
|
194
|
+
},
|
|
195
|
+
insert,
|
|
196
|
+
spread,
|
|
197
|
+
createElement,
|
|
198
|
+
createTextNode,
|
|
199
|
+
insertNode,
|
|
200
|
+
setProp(node, name, value, prev) {
|
|
201
|
+
setProperty(node, name, value, prev);
|
|
202
|
+
return value;
|
|
203
|
+
},
|
|
204
|
+
mergeProps: solidJs.merge,
|
|
205
|
+
effect: solidJs.createRenderEffect,
|
|
206
|
+
memo: solidJs.createMemo,
|
|
207
|
+
createComponent: solidJs.createComponent,
|
|
208
|
+
use(fn, element, arg) {
|
|
209
|
+
return solidJs.untrack(() => fn(element, arg));
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
exports.createRenderer = createRenderer;
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRoot,
|
|
3
|
+
merge,
|
|
4
|
+
createRenderEffect,
|
|
5
|
+
createMemo,
|
|
6
|
+
createComponent,
|
|
7
|
+
untrack,
|
|
8
|
+
flatten
|
|
9
|
+
} from "solid-js";
|
|
10
|
+
|
|
11
|
+
function createRenderer({
|
|
12
|
+
createElement,
|
|
13
|
+
createTextNode,
|
|
14
|
+
isTextNode,
|
|
15
|
+
replaceText,
|
|
16
|
+
insertNode,
|
|
17
|
+
removeNode,
|
|
18
|
+
setProperty,
|
|
19
|
+
getParentNode,
|
|
20
|
+
getFirstChild,
|
|
21
|
+
getNextSibling
|
|
22
|
+
}) {
|
|
23
|
+
function insert(parent, accessor, marker, initial) {
|
|
24
|
+
const multi = marker !== undefined;
|
|
25
|
+
if (multi && !initial) initial = [];
|
|
26
|
+
if (typeof accessor !== "function") {
|
|
27
|
+
accessor = normalize(accessor, multi, true);
|
|
28
|
+
if (typeof accessor !== "function")
|
|
29
|
+
return insertExpression(parent, accessor, initial, marker);
|
|
30
|
+
}
|
|
31
|
+
createRenderEffect(
|
|
32
|
+
() => normalize(accessor, multi),
|
|
33
|
+
(value, current) => insertExpression(parent, value, current, marker),
|
|
34
|
+
initial
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
function insertExpression(parent, value, current, marker) {
|
|
38
|
+
if (value === current) return;
|
|
39
|
+
const t = typeof value,
|
|
40
|
+
multi = marker !== undefined;
|
|
41
|
+
if (t === "string" || t === "number") {
|
|
42
|
+
const tc = typeof current;
|
|
43
|
+
if (tc === "string" || tc === "number") {
|
|
44
|
+
replaceText(getFirstChild(parent), value);
|
|
45
|
+
} else {
|
|
46
|
+
cleanChildren(parent, current, marker, createTextNode(value));
|
|
47
|
+
}
|
|
48
|
+
} else if (value == null) {
|
|
49
|
+
cleanChildren(parent, current, marker);
|
|
50
|
+
} else if (Array.isArray(value)) {
|
|
51
|
+
if (value.length === 0) {
|
|
52
|
+
cleanChildren(parent, current, marker);
|
|
53
|
+
} else {
|
|
54
|
+
if (Array.isArray(current)) {
|
|
55
|
+
if (current.length === 0) {
|
|
56
|
+
appendNodes(parent, value, marker);
|
|
57
|
+
} else reconcileArrays(parent, current, value);
|
|
58
|
+
} else if (current == null) {
|
|
59
|
+
appendNodes(parent, value);
|
|
60
|
+
} else {
|
|
61
|
+
reconcileArrays(parent, (multi && current) || [getFirstChild(parent)], value);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
if (Array.isArray(current)) {
|
|
66
|
+
cleanChildren(parent, current, multi ? marker : null, value);
|
|
67
|
+
} else if (current == null || !getFirstChild(parent)) {
|
|
68
|
+
insertNode(parent, value);
|
|
69
|
+
} else replaceNode(parent, value, getFirstChild(parent));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function normalize(value, multi, doNotUnwrap) {
|
|
73
|
+
value = flatten(value, {
|
|
74
|
+
skipNonRendered: true,
|
|
75
|
+
doNotUnwrap
|
|
76
|
+
});
|
|
77
|
+
if (doNotUnwrap && typeof value === "function") return value;
|
|
78
|
+
if (multi && value != null && !Array.isArray(value)) value = [value];
|
|
79
|
+
if (Array.isArray(value)) {
|
|
80
|
+
for (let i = 0, len = value.length; i < len; i++) {
|
|
81
|
+
const item = value[i],
|
|
82
|
+
t = typeof item;
|
|
83
|
+
if (t === "string" || t === "number") value[i] = document.createTextNode(item);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
function reconcileArrays(parentNode, a, b) {
|
|
89
|
+
let bLength = b.length,
|
|
90
|
+
aEnd = a.length,
|
|
91
|
+
bEnd = bLength,
|
|
92
|
+
aStart = 0,
|
|
93
|
+
bStart = 0,
|
|
94
|
+
after = getNextSibling(a[aEnd - 1]),
|
|
95
|
+
map = null;
|
|
96
|
+
while (aStart < aEnd || bStart < bEnd) {
|
|
97
|
+
if (a[aStart] === b[bStart]) {
|
|
98
|
+
aStart++;
|
|
99
|
+
bStart++;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
while (a[aEnd - 1] === b[bEnd - 1]) {
|
|
103
|
+
aEnd--;
|
|
104
|
+
bEnd--;
|
|
105
|
+
}
|
|
106
|
+
if (aEnd === aStart) {
|
|
107
|
+
const node =
|
|
108
|
+
bEnd < bLength ? (bStart ? getNextSibling(b[bStart - 1]) : b[bEnd - bStart]) : after;
|
|
109
|
+
while (bStart < bEnd) insertNode(parentNode, b[bStart++], node);
|
|
110
|
+
} else if (bEnd === bStart) {
|
|
111
|
+
while (aStart < aEnd) {
|
|
112
|
+
if (!map || !map.has(a[aStart])) removeNode(parentNode, a[aStart]);
|
|
113
|
+
aStart++;
|
|
114
|
+
}
|
|
115
|
+
} else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
|
|
116
|
+
const node = getNextSibling(a[--aEnd]);
|
|
117
|
+
insertNode(parentNode, b[bStart++], getNextSibling(a[aStart++]));
|
|
118
|
+
insertNode(parentNode, b[--bEnd], node);
|
|
119
|
+
a[aEnd] = b[bEnd];
|
|
120
|
+
} else {
|
|
121
|
+
if (!map) {
|
|
122
|
+
map = new Map();
|
|
123
|
+
let i = bStart;
|
|
124
|
+
while (i < bEnd) map.set(b[i], i++);
|
|
125
|
+
}
|
|
126
|
+
const index = map.get(a[aStart]);
|
|
127
|
+
if (index != null) {
|
|
128
|
+
if (bStart < index && index < bEnd) {
|
|
129
|
+
let i = aStart,
|
|
130
|
+
sequence = 1,
|
|
131
|
+
t;
|
|
132
|
+
while (++i < aEnd && i < bEnd) {
|
|
133
|
+
if ((t = map.get(a[i])) == null || t !== index + sequence) break;
|
|
134
|
+
sequence++;
|
|
135
|
+
}
|
|
136
|
+
if (sequence > index - bStart) {
|
|
137
|
+
const node = a[aStart];
|
|
138
|
+
while (bStart < index) insertNode(parentNode, b[bStart++], node);
|
|
139
|
+
} else replaceNode(parentNode, b[bStart++], a[aStart++]);
|
|
140
|
+
} else aStart++;
|
|
141
|
+
} else removeNode(parentNode, a[aStart++]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function cleanChildren(parent, current, marker, replacement) {
|
|
146
|
+
if (marker === undefined) {
|
|
147
|
+
let removed;
|
|
148
|
+
while ((removed = getFirstChild(parent))) removeNode(parent, removed);
|
|
149
|
+
replacement && insertNode(parent, replacement);
|
|
150
|
+
return "";
|
|
151
|
+
}
|
|
152
|
+
if (current.length) {
|
|
153
|
+
let inserted = false;
|
|
154
|
+
for (let i = current.length - 1; i >= 0; i--) {
|
|
155
|
+
const el = current[i];
|
|
156
|
+
if (replacement !== el) {
|
|
157
|
+
const isParent = getParentNode(el) === parent;
|
|
158
|
+
if (replacement && !inserted && !i)
|
|
159
|
+
isParent
|
|
160
|
+
? replaceNode(parent, replacement, el)
|
|
161
|
+
: insertNode(parent, replacement, marker);
|
|
162
|
+
else isParent && removeNode(parent, el);
|
|
163
|
+
} else inserted = true;
|
|
164
|
+
}
|
|
165
|
+
} else if (replacement) insertNode(parent, replacement, marker);
|
|
166
|
+
}
|
|
167
|
+
function appendNodes(parent, array, marker) {
|
|
168
|
+
for (let i = 0, len = array.length; i < len; i++) insertNode(parent, array[i], marker);
|
|
169
|
+
}
|
|
170
|
+
function replaceNode(parent, newNode, oldNode) {
|
|
171
|
+
insertNode(parent, newNode, oldNode);
|
|
172
|
+
removeNode(parent, oldNode);
|
|
173
|
+
}
|
|
174
|
+
function spread(node, props, skipChildren) {
|
|
175
|
+
const prevProps = {};
|
|
176
|
+
props || (props = {});
|
|
177
|
+
if (!skipChildren) {
|
|
178
|
+
createRenderEffect(
|
|
179
|
+
() => normalize(props.children),
|
|
180
|
+
value => {
|
|
181
|
+
insertExpression(node, value, prevProps.children);
|
|
182
|
+
prevProps.children = value;
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
createRenderEffect(
|
|
187
|
+
() => props.ref && props.ref(node),
|
|
188
|
+
() => ({})
|
|
189
|
+
);
|
|
190
|
+
createRenderEffect(
|
|
191
|
+
() => {
|
|
192
|
+
const newProps = {};
|
|
193
|
+
for (const prop in props) {
|
|
194
|
+
if (prop === "children" || prop === "ref") continue;
|
|
195
|
+
const value = props[prop];
|
|
196
|
+
if (value === prevProps[prop]) continue;
|
|
197
|
+
newProps[prop] = value;
|
|
198
|
+
}
|
|
199
|
+
return newProps;
|
|
200
|
+
},
|
|
201
|
+
props => {
|
|
202
|
+
for (const prop in props) {
|
|
203
|
+
const value = props[prop];
|
|
204
|
+
setProperty(node, prop, value, prevProps[prop]);
|
|
205
|
+
prevProps[prop] = value;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
return prevProps;
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
render(code, element) {
|
|
213
|
+
let disposer;
|
|
214
|
+
createRoot(dispose => {
|
|
215
|
+
disposer = dispose;
|
|
216
|
+
insert(element, code());
|
|
217
|
+
});
|
|
218
|
+
return disposer;
|
|
219
|
+
},
|
|
220
|
+
insert,
|
|
221
|
+
spread,
|
|
222
|
+
createElement,
|
|
223
|
+
createTextNode,
|
|
224
|
+
insertNode,
|
|
225
|
+
setProp(node, name, value, prev) {
|
|
226
|
+
setProperty(node, name, value, prev);
|
|
227
|
+
return value;
|
|
228
|
+
},
|
|
229
|
+
mergeProps: merge,
|
|
230
|
+
effect: createRenderEffect,
|
|
231
|
+
memo: createMemo,
|
|
232
|
+
createComponent,
|
|
233
|
+
use(fn, element, arg) {
|
|
234
|
+
return untrack(() => fn(element, arg));
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export { createRenderer };
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@solidjs/universal",
|
|
3
|
+
"description": "Solid's universal runtime for creating custom renderers",
|
|
4
|
+
"version": "2.0.0-experimental.0",
|
|
5
|
+
"author": "Ryan Carniato",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://solidjs.com",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/solidjs/solid-universal"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/universal.cjs",
|
|
16
|
+
"module": "./dist/universal.js",
|
|
17
|
+
"types": "./types/index.d.ts",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"types",
|
|
23
|
+
"package.json"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"development": {
|
|
28
|
+
"types": "./types/index.d.ts",
|
|
29
|
+
"import": "./dist/dev.js",
|
|
30
|
+
"require": "./dist/dev.cjs"
|
|
31
|
+
},
|
|
32
|
+
"types": "./types/index.d.ts",
|
|
33
|
+
"import": "./dist/universal.js",
|
|
34
|
+
"require": "./dist/universal.cjs"
|
|
35
|
+
},
|
|
36
|
+
"./dist/*": "./dist/*",
|
|
37
|
+
"./types/*": "./types/*"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"solid-js": "^2.0.0-experimental.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"solid-js": "2.0.0-experimental.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "npm-run-all -nl build:*",
|
|
47
|
+
"build:clean": "rimraf dist/ coverage/",
|
|
48
|
+
"build:js": "rollup -c",
|
|
49
|
+
"types": "npm-run-all -nl types:*",
|
|
50
|
+
"types:clean": "rimraf types/",
|
|
51
|
+
"types:universal": "tsc --project ./tsconfig.json && ncp ../../node_modules/dom-expressions/src/universal.d.ts ./types/index.d.ts"
|
|
52
|
+
}
|
|
53
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface RendererOptions<NodeType> {
|
|
2
|
+
createElement(tag: string): NodeType;
|
|
3
|
+
createTextNode(value: string): NodeType;
|
|
4
|
+
replaceText(textNode: NodeType, value: string): void;
|
|
5
|
+
isTextNode(node: NodeType): boolean;
|
|
6
|
+
setProperty<T>(node: NodeType, name: string, value: T, prev?: T): void;
|
|
7
|
+
insertNode(parent: NodeType, node: NodeType, anchor?: NodeType): void;
|
|
8
|
+
removeNode(parent: NodeType, node: NodeType): void;
|
|
9
|
+
getParentNode(node: NodeType): NodeType | undefined;
|
|
10
|
+
getFirstChild(node: NodeType): NodeType | undefined;
|
|
11
|
+
getNextSibling(node: NodeType): NodeType | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Renderer<NodeType> {
|
|
15
|
+
render(code: () => NodeType, node: NodeType): () => void;
|
|
16
|
+
effect<T>(fn: (prev?: T) => T, effect: (value: T, prev?: T) => void, init?: T): void;
|
|
17
|
+
memo<T>(fn: () => T, equal: boolean): () => T;
|
|
18
|
+
createComponent<T>(Comp: (props: T) => NodeType, props: T): NodeType;
|
|
19
|
+
createElement(tag: string): NodeType;
|
|
20
|
+
createTextNode(value: string): NodeType;
|
|
21
|
+
insertNode(parent: NodeType, node: NodeType, anchor?: NodeType): void;
|
|
22
|
+
insert<T>(parent: any, accessor: (() => T) | T, marker?: any | null): NodeType;
|
|
23
|
+
spread<T>(node: any, accessor: (() => T) | T, skipChildren?: Boolean): void;
|
|
24
|
+
setProp<T>(node: NodeType, name: string, value: T, prev?: T): T;
|
|
25
|
+
mergeProps(...sources: unknown[]): unknown;
|
|
26
|
+
use<A, T>(fn: (element: NodeType, arg: A) => T, element: NodeType, arg: A): T;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createRenderer<NodeType>(options: RendererOptions<NodeType>): Renderer<NodeType>;
|