@viewfly/platform-browser 0.5.0 → 0.5.2

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.
@@ -0,0 +1,31 @@
1
+ import { JSXNode, NativeNode } from '@viewfly/core';
2
+ /**
3
+ * 用于创建脱离当前 DOM 树的子节点,常用于弹窗等
4
+ * @param childRender
5
+ * @param host
6
+ * @example
7
+ * ```tsx
8
+ * function App() {
9
+ * const number = createSignal(0)
10
+ *
11
+ * setInterval(() => {
12
+ * number.set(number() + 1)
13
+ * }, 1000)
14
+ *
15
+ * const ModalPortal = function (props) {
16
+ * return createPortal(() => {
17
+ * return <div class="modal">parent data is {props.text}</div>
18
+ * }, document.body)
19
+ * }
20
+ * return () => {
21
+ * return (
22
+ * <div>
23
+ * <div>data is {number()}</div>
24
+ * <ModalPortal text={number()}/>
25
+ * </div>
26
+ * )
27
+ * }
28
+ * }
29
+ * ```
30
+ */
31
+ export declare function createPortal<T extends NativeNode>(childRender: () => JSXNode, host: T): () => null;
@@ -10,6 +10,7 @@ export declare class DomRenderer extends NativeRenderer<HTMLElement, Text> {
10
10
  propMap: Record<string, Record<string, string>>;
11
11
  createElement(name: string, isSvg: boolean): HTMLElement;
12
12
  createTextNode(textContent: string): Text;
13
+ appendChild(parent: HTMLElement, newChild: any): void;
13
14
  prependChild(parent: HTMLElement, newChild: HTMLElement | Text): void;
14
15
  insertAfter(newNode: HTMLElement | Text, ref: HTMLElement | Text): void;
15
16
  remove(node: HTMLElement | Text): void;
@@ -22,5 +23,4 @@ export declare class DomRenderer extends NativeRenderer<HTMLElement, Text> {
22
23
  unListen(node: HTMLElement, type: string, callback: (ev: any) => any): void;
23
24
  syncTextContent(target: Text, content: string): void;
24
25
  private insertBefore;
25
- private appendChild;
26
26
  }
@@ -20,6 +20,7 @@ export declare class HTMLRenderer extends NativeRenderer<VDOMElement, VDomText>
20
20
  createElement(name: string): VDOMElement;
21
21
  createTextNode(textContent: string): VDomText;
22
22
  setProperty(node: VDOMElement, key: string, value: any): void;
23
+ appendChild(parent: VDOMElement, newChild: VDOMElement | VDomText): void;
23
24
  prependChild(parent: VDOMElement, newChild: VDOMElement | VDomText): void;
24
25
  removeProperty(node: VDOMElement, key: string): void;
25
26
  setStyle(target: VDOMElement, key: string, value: any): void;
@@ -1,4 +1,4 @@
1
- import { NativeRenderer, viewfly, makeError, inject, Injector, THROW_IF_NOT_FOUND, InjectFlags, onUnmounted } from '@viewfly/core';
1
+ import { NativeRenderer, viewfly, getCurrentInstance, inject, Component, onPropsChanged, onUpdated, createRenderer } from '@viewfly/core';
2
2
 
3
3
  class DomRenderer extends NativeRenderer {
4
4
  constructor() {
@@ -21,6 +21,9 @@ class DomRenderer extends NativeRenderer {
21
21
  createTextNode(textContent) {
22
22
  return document.createTextNode(textContent);
23
23
  }
24
+ appendChild(parent, newChild) {
25
+ parent.appendChild(newChild);
26
+ }
24
27
  prependChild(parent, newChild) {
25
28
  parent.prepend(newChild);
26
29
  }
@@ -96,9 +99,6 @@ class DomRenderer extends NativeRenderer {
96
99
  insertBefore(newNode, ref) {
97
100
  ref.parentNode.insertBefore(newNode, ref);
98
101
  }
99
- appendChild(parent, newChild) {
100
- parent.appendChild(newChild);
101
- }
102
102
  }
103
103
  DomRenderer.NAMESPACES = {
104
104
  svg: 'http://www.w3.org/2000/svg',
@@ -119,27 +119,50 @@ function createApp(root, config = true) {
119
119
  return viewfly(Object.assign(Object.assign({}, c), { root, nativeRenderer: c.nativeRenderer || new DomRenderer() }));
120
120
  }
121
121
 
122
- const forkErrorFn = makeError('fork');
123
- function fork(root, config = true) {
124
- const c = { autoUpdate: true };
125
- if (typeof config === 'boolean') {
126
- c.autoUpdate = config;
127
- }
128
- else if (typeof config === 'object') {
129
- Object.assign(c, config);
130
- }
131
- let injector;
132
- try {
133
- injector = inject(Injector, THROW_IF_NOT_FOUND, InjectFlags.Default);
134
- }
135
- catch (_a) {
136
- throw forkErrorFn('The fork function can only be called synchronously within a component.');
137
- }
138
- const app = viewfly(Object.assign(Object.assign({}, c), { root, context: injector, nativeRenderer: new DomRenderer() }));
139
- onUnmounted(() => {
140
- app.destroy();
122
+ /**
123
+ * 用于创建脱离当前 DOM 树的子节点,常用于弹窗等
124
+ * @param childRender
125
+ * @param host
126
+ * @example
127
+ * ```tsx
128
+ * function App() {
129
+ * const number = createSignal(0)
130
+ *
131
+ * setInterval(() => {
132
+ * number.set(number() + 1)
133
+ * }, 1000)
134
+ *
135
+ * const ModalPortal = function (props) {
136
+ * return createPortal(() => {
137
+ * return <div class="modal">parent data is {props.text}</div>
138
+ * }, document.body)
139
+ * }
140
+ * return () => {
141
+ * return (
142
+ * <div>
143
+ * <div>data is {number()}</div>
144
+ * <ModalPortal text={number()}/>
145
+ * </div>
146
+ * )
147
+ * }
148
+ * }
149
+ * ```
150
+ */
151
+ function createPortal(childRender, host) {
152
+ const instance = getCurrentInstance();
153
+ const nativeRenderer = inject(NativeRenderer);
154
+ const component = new Component(instance, () => childRender, {});
155
+ onPropsChanged(() => {
156
+ component.markAsDirtied();
157
+ });
158
+ onUpdated(() => {
159
+ instance.$$view.atom.child = component.$$view.atom;
141
160
  });
142
- return app;
161
+ const render = createRenderer(component, nativeRenderer);
162
+ return function () {
163
+ render(host);
164
+ return null;
165
+ };
143
166
  }
144
167
 
145
168
  class VDOMElement {
@@ -171,6 +194,10 @@ class HTMLRenderer extends NativeRenderer {
171
194
  setProperty(node, key, value) {
172
195
  node.props.set(key, value);
173
196
  }
197
+ appendChild(parent, newChild) {
198
+ parent.children.push(newChild);
199
+ newChild.parent = parent;
200
+ }
174
201
  prependChild(parent, newChild) {
175
202
  parent.children.unshift(newChild);
176
203
  newChild.parent = parent;
@@ -309,4 +336,4 @@ OutputTranslator.simpleXSSFilter = {
309
336
  }
310
337
  };
311
338
 
312
- export { DomRenderer, HTMLRenderer, OutputTranslator, VDOMElement, VDomText, createApp, fork };
339
+ export { DomRenderer, HTMLRenderer, OutputTranslator, VDOMElement, VDomText, createApp, createPortal };
package/bundles/index.js CHANGED
@@ -23,6 +23,9 @@ class DomRenderer extends core.NativeRenderer {
23
23
  createTextNode(textContent) {
24
24
  return document.createTextNode(textContent);
25
25
  }
26
+ appendChild(parent, newChild) {
27
+ parent.appendChild(newChild);
28
+ }
26
29
  prependChild(parent, newChild) {
27
30
  parent.prepend(newChild);
28
31
  }
@@ -98,9 +101,6 @@ class DomRenderer extends core.NativeRenderer {
98
101
  insertBefore(newNode, ref) {
99
102
  ref.parentNode.insertBefore(newNode, ref);
100
103
  }
101
- appendChild(parent, newChild) {
102
- parent.appendChild(newChild);
103
- }
104
104
  }
105
105
  DomRenderer.NAMESPACES = {
106
106
  svg: 'http://www.w3.org/2000/svg',
@@ -121,27 +121,50 @@ function createApp(root, config = true) {
121
121
  return core.viewfly(Object.assign(Object.assign({}, c), { root, nativeRenderer: c.nativeRenderer || new DomRenderer() }));
122
122
  }
123
123
 
124
- const forkErrorFn = core.makeError('fork');
125
- function fork(root, config = true) {
126
- const c = { autoUpdate: true };
127
- if (typeof config === 'boolean') {
128
- c.autoUpdate = config;
129
- }
130
- else if (typeof config === 'object') {
131
- Object.assign(c, config);
132
- }
133
- let injector;
134
- try {
135
- injector = core.inject(core.Injector, core.THROW_IF_NOT_FOUND, core.InjectFlags.Default);
136
- }
137
- catch (_a) {
138
- throw forkErrorFn('The fork function can only be called synchronously within a component.');
139
- }
140
- const app = core.viewfly(Object.assign(Object.assign({}, c), { root, context: injector, nativeRenderer: new DomRenderer() }));
141
- core.onUnmounted(() => {
142
- app.destroy();
124
+ /**
125
+ * 用于创建脱离当前 DOM 树的子节点,常用于弹窗等
126
+ * @param childRender
127
+ * @param host
128
+ * @example
129
+ * ```tsx
130
+ * function App() {
131
+ * const number = createSignal(0)
132
+ *
133
+ * setInterval(() => {
134
+ * number.set(number() + 1)
135
+ * }, 1000)
136
+ *
137
+ * const ModalPortal = function (props) {
138
+ * return createPortal(() => {
139
+ * return <div class="modal">parent data is {props.text}</div>
140
+ * }, document.body)
141
+ * }
142
+ * return () => {
143
+ * return (
144
+ * <div>
145
+ * <div>data is {number()}</div>
146
+ * <ModalPortal text={number()}/>
147
+ * </div>
148
+ * )
149
+ * }
150
+ * }
151
+ * ```
152
+ */
153
+ function createPortal(childRender, host) {
154
+ const instance = core.getCurrentInstance();
155
+ const nativeRenderer = core.inject(core.NativeRenderer);
156
+ const component = new core.Component(instance, () => childRender, {});
157
+ core.onPropsChanged(() => {
158
+ component.markAsDirtied();
159
+ });
160
+ core.onUpdated(() => {
161
+ instance.$$view.atom.child = component.$$view.atom;
143
162
  });
144
- return app;
163
+ const render = core.createRenderer(component, nativeRenderer);
164
+ return function () {
165
+ render(host);
166
+ return null;
167
+ };
145
168
  }
146
169
 
147
170
  class VDOMElement {
@@ -173,6 +196,10 @@ class HTMLRenderer extends core.NativeRenderer {
173
196
  setProperty(node, key, value) {
174
197
  node.props.set(key, value);
175
198
  }
199
+ appendChild(parent, newChild) {
200
+ parent.children.push(newChild);
201
+ newChild.parent = parent;
202
+ }
176
203
  prependChild(parent, newChild) {
177
204
  parent.children.unshift(newChild);
178
205
  newChild.parent = parent;
@@ -317,4 +344,4 @@ exports.OutputTranslator = OutputTranslator;
317
344
  exports.VDOMElement = VDOMElement;
318
345
  exports.VDomText = VDomText;
319
346
  exports.createApp = createApp;
320
- exports.fork = fork;
347
+ exports.createPortal = createPortal;
@@ -1,5 +1,5 @@
1
1
  export * from './create-app';
2
- export * from './fork';
2
+ export * from './create-portal';
3
3
  export * from './html-renderer';
4
4
  export * from './dom-renderer';
5
5
  export * from './jsx-dom';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/platform-browser",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "This project is used to enable the Viewfly framework to run in a browser.",
5
5
  "main": "./bundles/index.js",
6
6
  "module": "./bundles/index.esm.js",
@@ -12,7 +12,7 @@
12
12
  "license": "MIT",
13
13
  "keywords": [],
14
14
  "dependencies": {
15
- "@viewfly/core": "^0.5.0",
15
+ "@viewfly/core": "^0.5.1",
16
16
  "csstype": "^3.1.2"
17
17
  },
18
18
  "devDependencies": {
@@ -33,5 +33,5 @@
33
33
  "bugs": {
34
34
  "url": "https://github.com/viewfly/viewfly.git/issues"
35
35
  },
36
- "gitHead": "75a746eb22f41295157d079e8557154d5fa50e01"
36
+ "gitHead": "0e5643bc49cc0aad94f3a21ba430d5ac926a7a10"
37
37
  }
package/bundles/fork.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { JSXNode, Application, Config } from '@viewfly/core';
2
- export declare function fork(root: JSXNode, autoUpdate?: boolean): Application;
3
- export declare function fork(root: JSXNode, config?: Omit<Config, 'nativeRenderer' | 'root'>): Application;