@viewfly/platform-browser 2.1.0 → 2.2.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/README.md +34 -37
- package/dist/create-app.d.ts +17 -0
- package/dist/create-portal.d.ts +35 -0
- package/dist/dom-renderer.d.ts +28 -0
- package/dist/html-renderer.d.ts +57 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.esm.js +354 -0
- package/dist/index.js +362 -0
- package/{bundles/index.d.ts → dist/jsx-dom.d.ts} +73 -212
- package/package.json +25 -18
- package/bundles/index.esm.js +0 -488
- package/bundles/index.js +0 -497
- package/rollup-d.config.ts +0 -14
package/dist/index.js
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _viewfly_core = require("@viewfly/core");
|
|
3
|
+
//#region src/dom-renderer.ts
|
|
4
|
+
var DomRenderer = class DomRenderer extends _viewfly_core.NativeRenderer {
|
|
5
|
+
static NAMESPACES = {
|
|
6
|
+
svg: "http://www.w3.org/2000/svg",
|
|
7
|
+
html: "http://www.w3.org/1999/xhtml",
|
|
8
|
+
xml: "http://www.w3.org/XML/1998/namespace",
|
|
9
|
+
xlink: "http://www.w3.org/1999/xlink",
|
|
10
|
+
xmlns: "http://www.w3.org/2000/xmlns/",
|
|
11
|
+
mathml: "http://www.w3.org/1998/Math/MathML"
|
|
12
|
+
};
|
|
13
|
+
propMap = {
|
|
14
|
+
INPUT: { readonly: "readOnly" },
|
|
15
|
+
TEXTAREA: { readonly: "readOnly" }
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* IDL 属性赋 `''` 会被转成数字 0(如 maxLength/minLength),无法表示「未设置」。
|
|
19
|
+
* 这些键在移除时应删掉对应 content attribute。
|
|
20
|
+
*/
|
|
21
|
+
static REMOVE_VIA_ATTRIBUTE = {
|
|
22
|
+
maxLength: "maxlength",
|
|
23
|
+
minLength: "minlength",
|
|
24
|
+
size: "size",
|
|
25
|
+
cols: "cols",
|
|
26
|
+
rows: "rows",
|
|
27
|
+
tabIndex: "tabindex"
|
|
28
|
+
};
|
|
29
|
+
createElement(name, namespace) {
|
|
30
|
+
const ns = namespace && DomRenderer.NAMESPACES[namespace];
|
|
31
|
+
if (ns) return document.createElementNS(ns, name);
|
|
32
|
+
return document.createElement(name);
|
|
33
|
+
}
|
|
34
|
+
createTextNode(textContent) {
|
|
35
|
+
return document.createTextNode(textContent);
|
|
36
|
+
}
|
|
37
|
+
appendChild(parent, newChild) {
|
|
38
|
+
parent.appendChild(newChild);
|
|
39
|
+
}
|
|
40
|
+
prependChild(parent, newChild) {
|
|
41
|
+
parent.prepend(newChild);
|
|
42
|
+
}
|
|
43
|
+
insertAfter(newNode, ref) {
|
|
44
|
+
if (ref.nextSibling) this.insertBefore(newNode, ref.nextSibling);
|
|
45
|
+
else if (ref.parentNode) this.appendChild(ref.parentNode, newNode);
|
|
46
|
+
else console.warn(`Element "${ref instanceof Text ? ref.textContent : ref.tagName}" was accidentally deleted, and viewfly is unable to update the current view`);
|
|
47
|
+
}
|
|
48
|
+
remove(node) {
|
|
49
|
+
node.remove();
|
|
50
|
+
}
|
|
51
|
+
cleanChildren(node) {
|
|
52
|
+
node.textContent = "";
|
|
53
|
+
}
|
|
54
|
+
setProperty(node, key, value, namespace) {
|
|
55
|
+
if (value == null) {
|
|
56
|
+
this.removeProperty(node, key, namespace);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (namespace) {
|
|
60
|
+
if (key.startsWith("xlink:")) {
|
|
61
|
+
const ns = key.substring(6);
|
|
62
|
+
node.setAttributeNS(ns, key, String(value));
|
|
63
|
+
} else node.setAttribute(key, String(value));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const map = this.propMap[node.tagName];
|
|
67
|
+
if (map) key = map[key] || key;
|
|
68
|
+
if (key in node) {
|
|
69
|
+
if (map && document.activeElement === node && key === "value") return;
|
|
70
|
+
node[key] = value;
|
|
71
|
+
} else node.setAttribute(key, value);
|
|
72
|
+
}
|
|
73
|
+
removeProperty(node, key, namespace) {
|
|
74
|
+
if (namespace) {
|
|
75
|
+
if (key.startsWith("xlink:")) {
|
|
76
|
+
const ns = key.substring(6);
|
|
77
|
+
node.removeAttributeNS(ns, key.substring(6));
|
|
78
|
+
} else node.removeAttribute(key);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const map = this.propMap[node.tagName];
|
|
82
|
+
const resolvedKey = map ? map[key] || key : key;
|
|
83
|
+
const attrName = DomRenderer.REMOVE_VIA_ATTRIBUTE[resolvedKey];
|
|
84
|
+
if (attrName) {
|
|
85
|
+
node.removeAttribute(attrName);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (resolvedKey in node) node[resolvedKey] = "";
|
|
89
|
+
else node.removeAttribute(key);
|
|
90
|
+
}
|
|
91
|
+
setClass(target, className) {
|
|
92
|
+
target.setAttribute("class", className);
|
|
93
|
+
}
|
|
94
|
+
setStyle(target, key, value) {
|
|
95
|
+
if (key.startsWith("--")) {
|
|
96
|
+
target.style.setProperty(key, value);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
target.style[key] = value ?? "";
|
|
100
|
+
}
|
|
101
|
+
removeStyle(target, key) {
|
|
102
|
+
if (key.startsWith("--")) {
|
|
103
|
+
target.style.removeProperty(key);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
target.style[key] = "";
|
|
107
|
+
}
|
|
108
|
+
listen(node, type, callback) {
|
|
109
|
+
const normalizedType = this.normalizedEventType(type);
|
|
110
|
+
node.addEventListener(normalizedType, callback);
|
|
111
|
+
}
|
|
112
|
+
unListen(node, type, callback) {
|
|
113
|
+
const normalizedType = this.normalizedEventType(type);
|
|
114
|
+
node.removeEventListener(normalizedType, callback);
|
|
115
|
+
}
|
|
116
|
+
syncTextContent(target, content) {
|
|
117
|
+
target.textContent = content;
|
|
118
|
+
}
|
|
119
|
+
getNameSpace(type, namespace) {
|
|
120
|
+
if (namespace === "svg") {
|
|
121
|
+
if (type === "foreignObject") return;
|
|
122
|
+
return namespace;
|
|
123
|
+
}
|
|
124
|
+
if (type === "svg") return type;
|
|
125
|
+
if (type === "math") return "mathml";
|
|
126
|
+
return namespace;
|
|
127
|
+
}
|
|
128
|
+
normalizedEventType(type) {
|
|
129
|
+
return type.substring(2).toLowerCase();
|
|
130
|
+
}
|
|
131
|
+
insertBefore(newNode, ref) {
|
|
132
|
+
if (ref.parentNode) ref.parentNode.insertBefore(newNode, ref);
|
|
133
|
+
else console.warn(`Element "${ref instanceof Text ? ref.textContent : ref.tagName}" was accidentally deleted, and viewfly is unable to update the current view`);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/create-app.ts
|
|
138
|
+
function createApp(root, config = true) {
|
|
139
|
+
const c = { autoUpdate: true };
|
|
140
|
+
if (typeof config === "boolean") c.autoUpdate = config;
|
|
141
|
+
else if (typeof config === "object") Object.assign(c, config);
|
|
142
|
+
return (0, _viewfly_core.viewfly)({
|
|
143
|
+
...c,
|
|
144
|
+
root,
|
|
145
|
+
nativeRenderer: c.nativeRenderer || new DomRenderer()
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/create-portal.ts
|
|
150
|
+
/**
|
|
151
|
+
* 用于创建脱离当前 DOM 树的子节点,常用于弹窗等
|
|
152
|
+
* @deprecated 即将弃用,请使用 @viewfly/core 模块的 Portal 组件实现
|
|
153
|
+
* @param childRender
|
|
154
|
+
* @param host
|
|
155
|
+
* @example
|
|
156
|
+
* ```tsx
|
|
157
|
+
* function App() {
|
|
158
|
+
* const number = createSignal(0)
|
|
159
|
+
*
|
|
160
|
+
* setInterval(() => {
|
|
161
|
+
* number.set(number() + 1)
|
|
162
|
+
* }, 1000)
|
|
163
|
+
*
|
|
164
|
+
* const ModalPortal = function (props) {
|
|
165
|
+
* return createPortal(() => {
|
|
166
|
+
* return <div class="modal">parent data is {props.text}</div>
|
|
167
|
+
* }, document.body)
|
|
168
|
+
* }
|
|
169
|
+
* return () => {
|
|
170
|
+
* return (
|
|
171
|
+
* <div>
|
|
172
|
+
* <div>data is {number()}</div>
|
|
173
|
+
* <ModalPortal text={number()}/>
|
|
174
|
+
* </div>
|
|
175
|
+
* )
|
|
176
|
+
* }
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
function createPortal(childRender, host) {
|
|
181
|
+
return {
|
|
182
|
+
$portalHost: host,
|
|
183
|
+
$render: childRender
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region src/html-renderer.ts
|
|
188
|
+
var VDOMNode = class {
|
|
189
|
+
parent = null;
|
|
190
|
+
remove() {
|
|
191
|
+
if (this.parent) {
|
|
192
|
+
const i = this.parent.children.indexOf(this);
|
|
193
|
+
if (i > -1) this.parent.children.splice(i, 1);
|
|
194
|
+
}
|
|
195
|
+
this.parent = null;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
var VDOMElement = class extends VDOMNode {
|
|
199
|
+
props = /* @__PURE__ */ new Map();
|
|
200
|
+
children = [];
|
|
201
|
+
style = /* @__PURE__ */ new Map();
|
|
202
|
+
className = "";
|
|
203
|
+
constructor(name) {
|
|
204
|
+
super();
|
|
205
|
+
this.name = name;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
var VDOMText = class extends VDOMNode {
|
|
209
|
+
constructor(text) {
|
|
210
|
+
super();
|
|
211
|
+
this.text = text;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
/**
|
|
215
|
+
* 用于生成模拟轻量 DOM 节点的渲染器
|
|
216
|
+
*/
|
|
217
|
+
var HTMLRenderer = class extends _viewfly_core.NativeRenderer {
|
|
218
|
+
createElement(name) {
|
|
219
|
+
return new VDOMElement(name);
|
|
220
|
+
}
|
|
221
|
+
createTextNode(textContent) {
|
|
222
|
+
return new VDOMText(textContent);
|
|
223
|
+
}
|
|
224
|
+
setProperty(node, key, value) {
|
|
225
|
+
node.props.set(key, value);
|
|
226
|
+
}
|
|
227
|
+
appendChild(parent, newChild) {
|
|
228
|
+
newChild.remove();
|
|
229
|
+
parent.children.push(newChild);
|
|
230
|
+
newChild.parent = parent;
|
|
231
|
+
}
|
|
232
|
+
prependChild(parent, newChild) {
|
|
233
|
+
newChild.remove();
|
|
234
|
+
parent.children.unshift(newChild);
|
|
235
|
+
newChild.parent = parent;
|
|
236
|
+
}
|
|
237
|
+
removeProperty(node, key) {
|
|
238
|
+
node.props.delete(key);
|
|
239
|
+
}
|
|
240
|
+
setStyle(target, key, value) {
|
|
241
|
+
target.style.set(key, value);
|
|
242
|
+
}
|
|
243
|
+
removeStyle(target, key) {
|
|
244
|
+
target.style.delete(key);
|
|
245
|
+
}
|
|
246
|
+
setClass(target, value) {
|
|
247
|
+
target.className = value;
|
|
248
|
+
}
|
|
249
|
+
listen() {}
|
|
250
|
+
unListen() {}
|
|
251
|
+
remove(node) {
|
|
252
|
+
node.remove();
|
|
253
|
+
}
|
|
254
|
+
cleanChildren(node) {
|
|
255
|
+
node.children.forEach((i) => i.parent = null);
|
|
256
|
+
node.children = [];
|
|
257
|
+
}
|
|
258
|
+
syncTextContent(target, content) {
|
|
259
|
+
target.text = content;
|
|
260
|
+
}
|
|
261
|
+
insertAfter(newNode, ref) {
|
|
262
|
+
newNode.remove();
|
|
263
|
+
const parent = ref.parent;
|
|
264
|
+
if (parent) {
|
|
265
|
+
const i = parent.children.indexOf(ref);
|
|
266
|
+
if (i > -1) {
|
|
267
|
+
newNode.parent = parent;
|
|
268
|
+
parent.children.splice(i + 1, 0, newNode);
|
|
269
|
+
}
|
|
270
|
+
} else console.warn(`Element "${ref instanceof VDOMText ? ref.text : ref.name}" was accidentally deleted, and viewfly is unable to update the current view`);
|
|
271
|
+
}
|
|
272
|
+
getNameSpace() {}
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* 轻量 DOM 转换为 HTML 字符串的转换器
|
|
276
|
+
*/
|
|
277
|
+
var OutputTranslator = class OutputTranslator {
|
|
278
|
+
static singleTags = "area,base,br,col,embed,hr,img,input,link,meta,source,track,wbr".split(",");
|
|
279
|
+
static simpleXSSFilter = {
|
|
280
|
+
text(text) {
|
|
281
|
+
return text.replace(/[><&]/g, (str) => {
|
|
282
|
+
return {
|
|
283
|
+
"<": "<",
|
|
284
|
+
">": ">",
|
|
285
|
+
"&": "&"
|
|
286
|
+
}[str];
|
|
287
|
+
});
|
|
288
|
+
},
|
|
289
|
+
attrName(text) {
|
|
290
|
+
return text.replace(/[><"'&]/g, (str) => {
|
|
291
|
+
return {
|
|
292
|
+
"<": "<",
|
|
293
|
+
">": ">",
|
|
294
|
+
"\"": """,
|
|
295
|
+
"'": "'",
|
|
296
|
+
"&": "&"
|
|
297
|
+
}[str];
|
|
298
|
+
});
|
|
299
|
+
},
|
|
300
|
+
attrValue(text) {
|
|
301
|
+
return text.replace(/["']/g, (str) => {
|
|
302
|
+
return {
|
|
303
|
+
"\"": """,
|
|
304
|
+
"'": "'"
|
|
305
|
+
}[str];
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
singleTagTest = new RegExp(`^(${OutputTranslator.singleTags.join("|")})$`, "i");
|
|
310
|
+
/**
|
|
311
|
+
* 将虚拟 DOM 转换为 HTML 字符串的方法
|
|
312
|
+
* @param vDom 虚拟 DOM 节点
|
|
313
|
+
*/
|
|
314
|
+
transform(vDom) {
|
|
315
|
+
return vDom.children.map((child) => {
|
|
316
|
+
return this.vDomToHTMLString(child);
|
|
317
|
+
}).join("");
|
|
318
|
+
}
|
|
319
|
+
vDomToHTMLString(vDom) {
|
|
320
|
+
const xssFilter = OutputTranslator.simpleXSSFilter;
|
|
321
|
+
if (vDom instanceof VDOMText) return this.replaceEmpty(xssFilter.text(vDom.text), " ");
|
|
322
|
+
const styles = Array.from(vDom.style.keys()).filter((key) => {
|
|
323
|
+
const v = vDom.style.get(key);
|
|
324
|
+
return !(v === void 0 || v === null || v === "");
|
|
325
|
+
}).map((key) => {
|
|
326
|
+
const k = key.replace(/(?=[A-Z])/g, "-").toLowerCase();
|
|
327
|
+
return xssFilter.attrValue(`${k}:${vDom.style.get(key)}`);
|
|
328
|
+
}).join(";");
|
|
329
|
+
const attrs = Array.from(vDom.props.keys()).filter((key) => key !== "ref" && vDom.props.get(key) !== false).map((k) => {
|
|
330
|
+
const key = xssFilter.attrName(k);
|
|
331
|
+
const value = vDom.props.get(k);
|
|
332
|
+
return value === true && /^\w+$/.test(key) ? `${key}` : `${key}="${xssFilter.attrValue(`${value}`)}"`;
|
|
333
|
+
});
|
|
334
|
+
if (styles) attrs.push(`style="${styles}"`);
|
|
335
|
+
if (vDom.className) attrs.push(`class="${xssFilter.attrValue(vDom.className)}"`);
|
|
336
|
+
let attrStr = attrs.join(" ");
|
|
337
|
+
attrStr = attrStr ? " " + attrStr : "";
|
|
338
|
+
if (this.singleTagTest.test(vDom.name)) return `<${vDom.name}${attrStr}>`;
|
|
339
|
+
const childHTML = vDom.children.map((child) => {
|
|
340
|
+
return this.vDomToHTMLString(child);
|
|
341
|
+
}).join("");
|
|
342
|
+
return [
|
|
343
|
+
`<${vDom.name}${attrStr}>`,
|
|
344
|
+
childHTML,
|
|
345
|
+
`</${vDom.name}>`
|
|
346
|
+
].join("");
|
|
347
|
+
}
|
|
348
|
+
replaceEmpty(s, target) {
|
|
349
|
+
return s.replace(/\s\s+/g, (str) => {
|
|
350
|
+
return " " + Array.from({ length: str.length - 1 }).fill(target).join("");
|
|
351
|
+
}).replace(/^\s|\s$/g, target);
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
//#endregion
|
|
355
|
+
exports.DomRenderer = DomRenderer;
|
|
356
|
+
exports.HTMLRenderer = HTMLRenderer;
|
|
357
|
+
exports.OutputTranslator = OutputTranslator;
|
|
358
|
+
exports.VDOMElement = VDOMElement;
|
|
359
|
+
exports.VDOMNode = VDOMNode;
|
|
360
|
+
exports.VDOMText = VDOMText;
|
|
361
|
+
exports.createApp = createApp;
|
|
362
|
+
exports.createPortal = createPortal;
|