Skip to content

Commit

Permalink
feat(rdom): update $tree() span handling, update $moveTo()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 30, 2020
1 parent 9dddb77 commit 6d90187
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/rdom/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
$clear,
$el,
$html,
$move,
$moveTo,
$remove,
$style,
$text,
Expand Down Expand Up @@ -78,6 +78,6 @@ export abstract class Component<T = any> implements IComponent<T> {
}

$moveTo(newParent: Element, el = this.el!, idx?: NumOrElement) {
$move(el, newParent, idx);
$moveTo(newParent, el, idx);
}
}
26 changes: 21 additions & 5 deletions packages/rdom/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { illegalArgs } from "@thi.ng/errors";
import {
ATTRIB_JOIN_DELIMS,
mergeClasses,
NO_SPANS,
RE_TAG,
SVG_NS,
SVG_TAGS,
Expand All @@ -29,8 +30,9 @@ import { isComponent } from "./utils";
* - {@link IDeref} instance (must resolve to another supported type in
* this list)
* - `["div#id.class", {...attribs}, ...children]`
* - `[IComponent, ...mountargs]`
* - `[function, ...args]`
* - ES6 iterator of the above (as children only!)
* - ES6 iterable of the above (for child values only!)
*
* Any other values will be cast to strings and added as spans to
* current `parent`.
Expand All @@ -46,17 +48,30 @@ export const $tree = async (
): Promise<any> => {
if (isArray(tree)) {
const tag = tree[0];
// [tag, attribs, ...body]
if (isString(tag)) {
parent = $el(tag, tree[1], null, parent, idx);
const n = tree.length;
const { 1: attribs, 2: body } = tree;
if (n === 3 && (isString(body) || isNumber(body))) {
// emmet-free base tag
const tmp = /^\w+/.exec(tag);
if (tmp && NO_SPANS[tmp[0]]) {
// don't wrap single body in <span> here
parent = $el(tag, attribs, body, parent, idx);
return parent;
}
}
parent = $el(tag, attribs, null, parent, idx);
for (let i = 2; i < n; i++) {
$tree(tree[i], parent);
}
return parent;
}
// [icomponent, ...args]
if (isComponent(tag)) {
return tag.mount(parent, idx, ...tree.slice(1));
}
// [fn, ...args]
if (isFunction(tag)) {
return $tree(tag.apply(null, tree.slice(1)), parent);
}
Expand All @@ -72,6 +87,7 @@ export const $tree = async (
for (let t of tree) {
$tree(t, parent);
}
return;
}
return tree != null
? $el("span", null, tree, <HTMLElement>parent, idx)
Expand Down Expand Up @@ -139,11 +155,11 @@ export const $addChild = (
: parent.insertBefore(child, idx);
};

export const $remove = (el: Element) => el.remove(); //el.parentNode!.removeChild(el);
export const $remove = (el: Element) => el.remove();

export const $move = (
el: Element,
export const $moveTo = (
newParent: Element,
el: Element,
idx: NumOrElement = -1
) => {
$remove(el);
Expand Down
4 changes: 2 additions & 2 deletions packages/rdom/src/klist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ISubscribable } from "@thi.ng/rstream";
import type { IComponent, IMountWithState, NumOrElement } from "./api";
import { $compile } from "./compile";
import { Component } from "./component";
import { $move } from "./dom";
import { $moveTo } from "./dom";
import { $sub } from "./sub";

interface KListItem {
Expand Down Expand Up @@ -83,7 +83,7 @@ export class KList<T> extends Component<T[]> implements IMountWithState<T[]> {

const insert = async (item: KListItem) => {
if (cache!.has(item.k)) {
$move(item.v.el!, parent!, next);
$moveTo(parent!, item.v.el!, next);
next = item.v.el!;
} else {
cache!.set(item.k, item);
Expand Down

0 comments on commit 6d90187

Please sign in to comment.