Skip to content

Commit

Permalink
refactor: remove find method in task list item
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Dec 20, 2023
1 parent c44594e commit da310f9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
5 changes: 5 additions & 0 deletions lib/block/base/treeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ interface IConstructor<T> {

class TreeNode extends LinkedNode<TreeNode> {
public parent: Nullable<Parent> = null;

public domNode: Nullable<HTMLElement> = null;

public tagName: string = '';

public classList: string[] = [];

public attributes: Attributes = {};

public datasets: Datasets = {};

static blockName = 'tree.node';
Expand Down
13 changes: 7 additions & 6 deletions lib/block/gfm/taskListCheckbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const debug = logger('tasklistCheckbox:');
// span has a cursor staggered problem.
class TaskListCheckbox extends TreeNode {
private checked: boolean;

private eventIds: string[] = [];

static blockName = 'task-list-checkbox';
Expand Down Expand Up @@ -98,6 +99,7 @@ class TaskListCheckbox extends TreeNode {
(this.domNode as HTMLInputElement).checked = checked;
}

// No need to trigger the OT operation If the source is `api`.
if (source === 'api') {
taskListItem.meta.checked = checked;
} else {
Expand All @@ -113,13 +115,12 @@ class TaskListCheckbox extends TreeNode {
}
}

remove() {
this.detachDOMEvents();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
remove(_source: string) {
super.remove();
if (this.domNode) {
this.domNode.remove();
}
this.domNode = null;
this.detachDOMEvents();

return this;
}

getState() {
Expand Down
14 changes: 3 additions & 11 deletions lib/block/gfm/taskListItem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TaskListItem extends Parent {
listItem.appendAttachment(
ScrollPage.loadBlock('task-list-checkbox').create(muya, state.meta)
);

listItem.append(
...state.children.map((child) =>
ScrollPage.loadBlock(child.name).create(muya, child)
Expand All @@ -43,8 +44,9 @@ class TaskListItem extends Parent {

set checked(checked) {
const oldCheckStatus = this.meta.checked;
this.meta.checked = checked;

if (checked !== oldCheckStatus) {
this.meta.checked = checked;
const { path } = this;
path.pop();
path.push('meta', 'checked');
Expand All @@ -61,16 +63,6 @@ class TaskListItem extends Parent {
this.createDomNode();
}

find(key: number | string) {
if (typeof key === 'number') {
return super.find(key);
} else if (typeof key === 'string') {
// If key is checked.
// Return taskListCheckbox.
return this.attachments.head;
}
}

getState(): ITaskListItemState {
const state: ITaskListItemState = {
name: 'task-list-item',
Expand Down
4 changes: 2 additions & 2 deletions lib/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class JSONState {
this._emitStateChange();
}

replaceOperation(path: Path, oldValue: TState, newValue: TState) {
const operation = json1.replaceOp(path, oldValue as unknown as Doc, newValue as unknown as Doc)!;
replaceOperation(path: Path, oldValue: Doc, newValue: Doc) {
const operation = json1.replaceOp(path, oldValue, newValue)!;

this.operationCache.push(operation);

Expand Down

0 comments on commit da310f9

Please sign in to comment.