Skip to content

Commit

Permalink
fix: fix selectCompletedChilds
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Jan 1, 2023
1 parent 8d0ed9b commit 1274030
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 23 deletions.
18 changes: 18 additions & 0 deletions packages/helper/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
"roots": [
"<rootDir>",
],
"transform": {
"^.+\\.tsx?$": "ts-jest",
},
"testMatch": ["<rootDir>/test/**/*.spec.ts"],
// "testRegex": "spec\\.ts$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node",
],
};
9 changes: 6 additions & 3 deletions packages/helper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moveable/helper",
"version": "0.1.1",
"version": "0.1.2",
"description": "Helper for demo of Moveable",
"main": "./dist/helper.cjs.js",
"module": "./dist/helper.esm.js",
Expand All @@ -13,7 +13,8 @@
"lint": "eslint ./src/ --ext .ts,.tsx",
"start": "rollup -c -w",
"build": "npm run lint && rollup -c && npm run declaration && print-sizes ./dist ",
"declaration": "rm -rf declaration && tsc -p tsconfig.declaration.json"
"declaration": "rm -rf declaration && tsc -p tsconfig.declaration.json",
"test": "jest --watchAll"
},
"keywords": [
"moveable",
Expand Down Expand Up @@ -58,6 +59,7 @@
"@types/node": "^14.6.0",
"@types/react": "^16.9.17",
"@types/react-dom": "^16.9.4",
"@types/jest": "^24.0.13",
"@typescript-eslint/eslint-plugin": "^3.9.1",
"@typescript-eslint/parser": "^3.9.1",
"babel-loader": "^8.0.6",
Expand All @@ -66,7 +68,8 @@
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-react": "^7.22.0",
"gh-pages": "^2.1.1",
"jest": "^29.0.3",
"jest": "^24.8.0",
"ts-jest": "^24.0.2",
"keycon": "^1.0.0",
"print-sizes": "0.0.4",
"pvu": "^0.6.1",
Expand Down
36 changes: 30 additions & 6 deletions packages/helper/src/GroupManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-cond-assign */
import { deepFlat, isArray } from "@daybrush/utils";
import { GroupArrayChild, GroupSingleChild } from "./groups";
import { GroupChild, TargetGroupsObject, TargetGroupsType, TargetList } from "./types";
Expand Down Expand Up @@ -186,13 +187,15 @@ export class GroupManager extends GroupArrayChild {
});

added.forEach(element => {
const pureChild = this.findNextPureChild(element, startSelected);
const parentGroup = this._findParentGroup(element, startSelected);

if (pureChild) {
nextTargets.push(pureChild.toTargetGroups());
} else {
nextTargets.push(element);
const nextChild = parentGroup.findContainedChild(element);

if (nextChild?.type === "group") {
nextTargets.push(nextChild.toTargetGroups());
return;
}
nextTargets.push(element);
});
return toTargetList(this.toChilds(nextTargets));
}
Expand Down Expand Up @@ -251,7 +254,6 @@ export class GroupManager extends GroupArrayChild {
public toChilds(targets: TargetGroupsType): GroupChild[] {
const childs: GroupChild[] = [];


targets.forEach(target => {
if (isArray(target)) {
const arrayChild = this.findArrayChild(target);
Expand Down Expand Up @@ -298,4 +300,26 @@ export class GroupManager extends GroupArrayChild {

return value;
}
protected _findParentGroup(
element: HTMLElement | SVGElement,
range: Array<HTMLElement | SVGElement>,
) {
if (!range.length) {
return this;
}
const single = this.map.get(element);

if (!single) {
return this;
}
let parent: GroupArrayChild | undefined = single.parent;

while (parent) {
if (range.some(element => parent!.contains(element))) {
return parent;
}
parent = parent.parent;
}
return this;
}
}
60 changes: 47 additions & 13 deletions packages/helper/src/groups.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, deepFlat } from "@daybrush/utils";
import { isArray, deepFlat, find } from "@daybrush/utils";
import { GroupChild, TargetGroupsType } from "./types";

export class Child {
Expand Down Expand Up @@ -53,6 +53,30 @@ export class GroupArrayChild extends Child {
public has(target: HTMLElement | SVGElement) {
return this.map.has(target);
}
public contains(element: HTMLElement | SVGElement): boolean {
if (this.has(element)) {
return true;
}
return this.value.some(child => {
if (child.type === "group") {
return child.contains(element);
} else {
return false;
}
});
}
public findContainedChild(element: HTMLElement | SVGElement) {
return find(this.value, child => {
if (child.type === "single") {
return child.value === element;
} else {
return child.contains(element);
}
});
}
/**
* Exact group containing targets
*/
public findExactChild(target: TargetGroupsType[0]): GroupChild | undefined {
const map = this.map;

Expand All @@ -70,7 +94,7 @@ export class GroupArrayChild extends Child {
let parent: GroupArrayChild | undefined = single.parent;

while (parent) {
if (parent.map.size === length) {
if (parent.map.size >= length) {
return parent;
}
parent = parent.parent;
Expand Down Expand Up @@ -127,6 +151,7 @@ export class GroupArrayChild extends Child {
return nextChild;
}


const nextGroupChild = this.findExactChild(child);

if (!nextGroupChild) {
Expand Down Expand Up @@ -161,6 +186,9 @@ export class GroupArrayChild extends Child {
}
return null;
}
/**
* Finds a group that does not overlap within the range and includes the target.
*/
public findPureChild(
target: HTMLElement | SVGElement,
range: Array<HTMLElement | SVGElement>,
Expand Down Expand Up @@ -189,9 +217,7 @@ export class GroupArrayChild extends Child {
target: HTMLElement | SVGElement,
range: Array<HTMLElement | SVGElement>,
): GroupArrayChild | null {
const nextChild = this.findNextChild(
target,
);
const nextChild = this.findNextChild(target);

if (nextChild) {
return nextChild.findPureChild(target, range);
Expand All @@ -209,25 +235,33 @@ export class GroupArrayChild extends Child {
}
public findArrayChild(targets: TargetGroupsType): GroupArrayChild | null {
const {
map,
value,
} = this;

let result = false;

if (this.type !== "root") {
result = targets.every(target => {
if (isArray(target)) {
return value.some(child => {
return child.type === "group" && child.findArrayChild(target);
});
result = value.every(child => {
if (child.type === "single") {
return targets.some(target => child.value === target);
} else {
return map.get(target);
return targets.some(target => {
return isArray(target) && child.findArrayChild(target);
});
}
});
// result = targets.every(target => {
// if (isArray(target)) {
// return value.some(child => {
// return child.type === "group" && child.findArrayChild(target);
// });
// } else {
// return map.get(target);
// }
// });
}

if (result) {
if (result && targets.length === value.length) {
return this;
} else {
let childResult: GroupArrayChild | null = null;
Expand Down
24 changes: 24 additions & 0 deletions packages/helper/test/unit/GroupManager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createElements } from "./utils";
import { GroupManager } from "../../src/";

describe("test GroupManager", () => {
describe("test groups [[0, 1], 2], 3", () => {
const elements = createElements(4);
const manager = new GroupManager([
[[elements[0], elements[1]], elements[2]],
], elements);

it("selectCompletedChilds [0, 1]", () => {
const list = manager.selectCompletedChilds([], [elements[1]], []);

expect(list.flatten()).toStrictEqual([elements[0], elements[1], elements[2]]);
});
it("selectCompletedChilds 2 => [0, 1]", () => {
// element[2]가 선택된 상태에서 elements[0]을 선택
const list = manager.selectCompletedChilds([elements[2]], [elements[0]], [elements[2]]);

expect(list.flatten()).toStrictEqual([elements[0], elements[1]]);
});
});

});
13 changes: 13 additions & 0 deletions packages/helper/test/unit/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function createElements(count: number) {
const elements: HTMLElement[] = [];

for (let i = 0; i < count; ++i) {
const div = document.createElement("div");
div.innerHTML = `${i}`;

(div as any).eid = i;
elements.push(div);
}

return elements;
}
3 changes: 2 additions & 1 deletion packages/helper/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"sourceMap": true,
"module": "esnext",
"target": "es5",

"experimentalDecorators": true,
"skipLibCheck": true,
"moduleResolution": "node",
Expand All @@ -20,7 +21,7 @@
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"isolatedModules": false,
"noEmit": true,
"baseUrl": "."
},
Expand Down

0 comments on commit 1274030

Please sign in to comment.