feat: 增加 node replaceWith 方法

This commit is contained in:
力皓 2020-06-18 14:46:39 +08:00
parent 5adff447c1
commit d44f95b643
2 changed files with 36 additions and 6 deletions

View File

@ -158,6 +158,13 @@ export class NodeChildren {
return this.children.indexOf(node);
}
/**
*
*/
splice(start: number, deleteCount: number, node: Node): Node[] {
return this.children.splice(start, deleteCount, node);
}
/**
*
*/

View File

@ -175,9 +175,9 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
if (!autoruns || autoruns.length < 1) {
return;
}
this.autoruns = autoruns.map(item => {
this.autoruns = autoruns.map((item) => {
return autorun(() => {
item.autorun(this.props.get(item.name, true) as any)
item.autorun(this.props.get(item.name, true) as any);
}, true);
});
}
@ -384,9 +384,33 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
// todo
}
replaceWith(schema: Schema, migrate = true) {
replaceWith(schema: Schema, migrate = false) {
// reuse the same id? or replaceSelection
//
schema = Object.assign({}, migrate ? this.export() : {}, schema);
return this.parent?.replaceChild(this, schema);
}
/**
*
*
* @param {Node} node
* @param {object} data
*/
replaceChild(node: Node, data: any) {
if (this.children?.has(node)) {
const selected = this.document.selection.has(node.id);
delete data.id;
const newNode = this.document.createNode(data);
this.insertBefore(newNode, node);
node.remove();
if (selected) {
this.document.selection.select(newNode.id);
}
}
return node;
}
getProp(path: string, stash = true): Prop | null {
@ -442,7 +466,6 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
return this.parent.children.indexOf(this);
}
/**
*
*/
@ -607,7 +630,7 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
if (this.isParental()) {
this.children.purge();
}
this.autoruns?.forEach(dispose => dispose());
this.autoruns?.forEach((dispose) => dispose());
this.props.purge();
this.document.internalRemoveAndPurgeNode(this);
}