Skip to content

Commit

Permalink
refactor: add more types to parent block
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Oct 13, 2023
1 parent 1e801f0 commit 52d0919
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
39 changes: 23 additions & 16 deletions lib/block/base/parent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const debug = logger("parent:");
abstract class Parent extends TreeNode {
// Used to store icon, checkbox(span) etc. these blocks are not in children properties in json state.
public attachments: LinkedList<Parent> = new LinkedList();
public children: LinkedList<Parent | Content> = new LinkedList();
public children: LinkedList<TreeNode> = new LinkedList();
public prev: Parent | null = null;
public next: Parent | null = null;

Expand Down Expand Up @@ -74,10 +74,10 @@ abstract class Parent extends TreeNode {
* Return the length of children.
*/
length() {
return this.reduce((acc, _) => acc + 1, 0);
return this.reduce((acc: number) => acc + 1, 0);
}

offset(node) {
offset(node: TreeNode) {
return this.children.offset(node);
}

Expand All @@ -96,7 +96,7 @@ abstract class Parent extends TreeNode {
args.forEach((node) => {
node.parent = this;
const { domNode } = node;
this.domNode!.appendChild(domNode);
this.domNode!.appendChild(domNode!);
});

this.children.append(...args);
Expand All @@ -115,30 +115,37 @@ abstract class Parent extends TreeNode {
* This method will only be used when initialization.
* @param {...any} nodes attachment blocks
*/
appendAttachment(...nodes) {
appendAttachment(...nodes: Parent[]) {
nodes.forEach((node) => {
node.parent = this;
const { domNode } = node;
this.domNode!.appendChild(domNode);
this.domNode!.appendChild(domNode!);
});

this.attachments.append(...nodes);
}

forEachAt(index: number, length: number = this.length(), callback) {
forEachAt(
index: number,
length: number = this.length(),
callback: (cur: TreeNode, i: number) => void
) {
return this.children.forEachAt(index, length, callback);
}

forEach(callback) {
forEach(callback: (cur: TreeNode, i: number) => void) {
return this.children.forEach(callback);
}

map(callback) {
map<M>(callback: (cur: TreeNode, i: number) => M): M[] {
return this.children.map(callback);
}

reduce(callback, initialValue) {
return this.children.reduce<number>(callback, initialValue);
reduce<M>(
callback: (memo: M, cur: TreeNode, i: number) => M,
initialValue: M
): M {
return this.children.reduce<M>(callback, initialValue);
}

/**
Expand All @@ -159,11 +166,11 @@ abstract class Parent extends TreeNode {
return block;
}

insertBefore(newNode, refNode?, source = "user") {
insertBefore(newNode: Parent, refNode: Parent | null = null, source = "user") {
newNode.parent = this;
this.children.insertBefore(newNode, refNode);
this.domNode.insertBefore(
newNode.domNode,
this.domNode!.insertBefore(
newNode.domNode!,
refNode ? refNode.domNode : null
);

Expand All @@ -177,8 +184,8 @@ abstract class Parent extends TreeNode {
return newNode;
}

insertAfter(newNode, refNode, source = "user") {
this.insertBefore(newNode, refNode.next, source);
insertAfter(newNode: Parent, refNode: Parent | null = null, source = "user") {
this.insertBefore(newNode, refNode ? refNode.next : null, source);

return newNode;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Muya {
/**
* [ensureContainerDiv ensure container element is div]
*/
function getContainer(originContainer, options) {
function getContainer(originContainer: HTMLElement, options: MuyaOptions) {
const { spellcheckEnabled, hideQuickInsertHint } = options;
const newContainer = document.createElement("div");
const attrs = originContainer.attributes;
Expand Down

0 comments on commit 52d0919

Please sign in to comment.