@thi.ng/rdom 0.6.9 → 0.7.4
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/CHANGELOG.md +50 -32
- package/README.md +16 -7
- package/checks.d.ts +3 -0
- package/checks.js +2 -0
- package/compile.d.ts +1 -1
- package/compile.js +8 -6
- package/component.d.ts +1 -1
- package/component.js +2 -2
- package/dom.d.ts +2 -2
- package/dom.js +15 -7
- package/event.js +1 -1
- package/index.d.ts +15 -14
- package/index.js +15 -14
- package/klist.d.ts +2 -2
- package/klist.js +4 -4
- package/list.d.ts +2 -2
- package/list.js +3 -3
- package/object.d.ts +3 -3
- package/object.js +3 -3
- package/package.json +80 -29
- package/promise.d.ts +2 -2
- package/promise.js +1 -1
- package/replace.d.ts +45 -0
- package/replace.js +62 -0
- package/scheduler.d.ts +1 -1
- package/sub.d.ts +3 -2
- package/sub.js +9 -8
- package/switch.d.ts +3 -3
- package/switch.js +5 -5
- package/wrap.d.ts +1 -1
- package/wrap.js +2 -2
- package/lib/index.js +0 -753
- package/lib/index.js.map +0 -1
- package/lib/index.umd.js +0 -1
- package/lib/index.umd.js.map +0 -1
- package/utils.d.ts +0 -5
- package/utils.js +0 -3
package/replace.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { $compile } from "./compile.js";
|
|
2
|
+
import { Component } from "./component.js";
|
|
3
|
+
import { $sub } from "./sub.js";
|
|
4
|
+
import { $wrapText } from "./wrap.js";
|
|
5
|
+
/**
|
|
6
|
+
* Similar to {@link $refresh}, but more basic/simple. Takes a reactive value
|
|
7
|
+
* `src` and wraps it in a {@link $sub} component using an inner
|
|
8
|
+
* {@link Replace}, which then passes the value to {@link $compile} for each
|
|
9
|
+
* change and replaces the result in the target DOM. If the value evaluates to
|
|
10
|
+
* `null`ish, the previously mounted component will be unmounted and stays so
|
|
11
|
+
* until the value becomes non-null again.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* If the reactive value is null-ish when the wrapper component is first
|
|
15
|
+
* mounted, a hidden dummy `<span>` element will be created instead. This is to
|
|
16
|
+
* ensure the general {@link IComponent.mount} contract will not be broken. The
|
|
17
|
+
* dummy element will later be removed/replaced as soon as the reactive value
|
|
18
|
+
* becomes non-null.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { $compile, $replace } from "@thi.ng/rdom";
|
|
23
|
+
* import { fromInterval } from "@thi.ng/rstream";
|
|
24
|
+
*
|
|
25
|
+
* // reactive counter component
|
|
26
|
+
* const counter = fromInterval(16).map((x) => [
|
|
27
|
+
* "div",
|
|
28
|
+
* { style: { "font-size": `${(x % 100) + 10}px` } },
|
|
29
|
+
* x,
|
|
30
|
+
* ]);
|
|
31
|
+
*
|
|
32
|
+
* $compile($replace(counter)).mount(document.body);
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @param src
|
|
36
|
+
*/
|
|
37
|
+
export const $replace = (src) => $sub(src, new Replace());
|
|
38
|
+
export class Replace extends Component {
|
|
39
|
+
async mount(parent, index, val) {
|
|
40
|
+
this.parent = parent;
|
|
41
|
+
this.index = index;
|
|
42
|
+
await this.update(val);
|
|
43
|
+
if (!this.inner) {
|
|
44
|
+
this.inner = $wrapText("span", { hidden: true });
|
|
45
|
+
await this.inner.mount(parent, index);
|
|
46
|
+
}
|
|
47
|
+
return this.inner.el;
|
|
48
|
+
}
|
|
49
|
+
async unmount() {
|
|
50
|
+
this.inner && (await this.inner.unmount());
|
|
51
|
+
this.parent = undefined;
|
|
52
|
+
this.inner = undefined;
|
|
53
|
+
}
|
|
54
|
+
async update(val) {
|
|
55
|
+
this.inner && (await this.inner.unmount());
|
|
56
|
+
this.inner = undefined;
|
|
57
|
+
if (val != null) {
|
|
58
|
+
this.inner = $compile(val);
|
|
59
|
+
this.inner && (await this.inner.mount(this.parent, this.index));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
package/scheduler.d.ts
CHANGED
package/sub.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Fn2, Path } from "@thi.ng/api";
|
|
2
|
-
import { ISubscribable
|
|
3
|
-
import
|
|
2
|
+
import type { ISubscribable } from "@thi.ng/rstream";
|
|
3
|
+
import { Subscription } from "@thi.ng/rstream/subscription";
|
|
4
|
+
import type { IComponent, IMountWithState, NumOrElement } from "./api.js";
|
|
4
5
|
/**
|
|
5
6
|
* Takes an {@link @thi.ng/rstream#ISubscribable} and creates a simple component
|
|
6
7
|
* wrapper for its reactively produced values.
|
package/sub.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { isString } from "@thi.ng/checks";
|
|
2
|
-
import { defSetterUnsafe } from "@thi.ng/paths";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { isString } from "@thi.ng/checks/is-string";
|
|
2
|
+
import { defSetterUnsafe } from "@thi.ng/paths/setter";
|
|
3
|
+
import { __nextID } from "@thi.ng/rstream/idgen";
|
|
4
|
+
import { Subscription } from "@thi.ng/rstream/subscription";
|
|
5
|
+
import { $attribs } from "./dom.js";
|
|
6
|
+
import { SCHEDULER } from "./scheduler.js";
|
|
7
|
+
import { $wrapText } from "./wrap.js";
|
|
7
8
|
export function $sub(src, tag, attribs) {
|
|
8
9
|
return (src.subscribe(new $Sub(isString(tag) ? $wrapText(tag, attribs) : tag)));
|
|
9
10
|
}
|
|
10
11
|
export class $Sub extends Subscription {
|
|
11
12
|
constructor(inner) {
|
|
12
|
-
super(undefined, { id: `rdom$sub-${
|
|
13
|
+
super(undefined, { id: `rdom$sub-${__nextID()}` });
|
|
13
14
|
this.inner = inner;
|
|
14
15
|
}
|
|
15
16
|
async mount(parent, index = -1) {
|
|
@@ -30,7 +31,7 @@ export class $Sub extends Subscription {
|
|
|
30
31
|
}
|
|
31
32
|
export class $SubA extends Subscription {
|
|
32
33
|
constructor(comp, path) {
|
|
33
|
-
super(undefined, { id: `rdom$sub-${
|
|
34
|
+
super(undefined, { id: `rdom$sub-${__nextID()}` });
|
|
34
35
|
this.comp = comp;
|
|
35
36
|
this.attr = {};
|
|
36
37
|
this.setter = defSetterUnsafe(path);
|
package/switch.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Fn, NumOrString } from "@thi.ng/api";
|
|
1
|
+
import type { Fn, NumOrString } from "@thi.ng/api";
|
|
2
2
|
import type { ISubscribable } from "@thi.ng/rstream";
|
|
3
|
-
import type { IComponent, IMountWithState, NumOrElement } from "./api";
|
|
4
|
-
import { Component } from "./component";
|
|
3
|
+
import type { IComponent, IMountWithState, NumOrElement } from "./api.js";
|
|
4
|
+
import { Component } from "./component.js";
|
|
5
5
|
/**
|
|
6
6
|
* Reactive component wrapper to dynamically switch/replace itself with one of
|
|
7
7
|
* the given components depending on subscribed value.
|
package/switch.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { assert } from "@thi.ng/
|
|
2
|
-
import { $compile } from "./compile";
|
|
3
|
-
import { Component } from "./component";
|
|
4
|
-
import { $sub } from "./sub";
|
|
5
|
-
import { $wrapText } from "./wrap";
|
|
1
|
+
import { assert } from "@thi.ng/errors/assert";
|
|
2
|
+
import { $compile } from "./compile.js";
|
|
3
|
+
import { Component } from "./component.js";
|
|
4
|
+
import { $sub } from "./sub.js";
|
|
5
|
+
import { $wrapText } from "./wrap.js";
|
|
6
6
|
/**
|
|
7
7
|
* Reactive component wrapper to dynamically switch/replace itself with one of
|
|
8
8
|
* the given components depending on subscribed value.
|
package/wrap.d.ts
CHANGED
package/wrap.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { $el, $html, $remove, $text } from "./dom";
|
|
2
|
-
import { SCHEDULER } from "./scheduler";
|
|
1
|
+
import { $el, $html, $remove, $text } from "./dom.js";
|
|
2
|
+
import { SCHEDULER } from "./scheduler.js";
|
|
3
3
|
const wrapper = (update) => (tag, attribs, body) => ({
|
|
4
4
|
el: undefined,
|
|
5
5
|
async mount(parent, index, state) {
|