@leafer/selector 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer/selector
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@leafer/selector",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer/selector",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "files": ["src"],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/leaferjs/leafer.git"
12
+ },
13
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/selector",
14
+ "bugs": "https://github.com/leaferjs/leafer/issues",
15
+ "keywords": [
16
+ "leafer",
17
+ "leaferjs"
18
+ ],
19
+ "dependencies": {
20
+ "@leafer/event": "1.0.0-alpha.1",
21
+ "@leafer/math": "1.0.0-alpha.1",
22
+ "@leafer/list": "1.0.0-alpha.1"
23
+ },
24
+ "devDependencies": {
25
+ "@leafer/interface": "1.0.0-alpha.1"
26
+ }
27
+ }
@@ -0,0 +1,131 @@
1
+ import { ILeaf, ILeafList, IPointData, IRadiusPointData, ISelectPathResult, ISelectPathOptions, ISelector } from '@leafer/interface'
2
+ import { BoundsHelper } from '@leafer/math'
3
+ import { LeafList } from '@leafer/list'
4
+
5
+ const { hitRadiusPoint } = BoundsHelper
6
+
7
+
8
+ export class PathFinder {
9
+
10
+ protected target: ILeaf
11
+ protected selector: ISelector
12
+
13
+ protected leaf?: ILeaf
14
+ protected throughPath: ILeafList
15
+ protected exclude: ILeafList
16
+
17
+ protected point: IRadiusPointData
18
+ protected isStop: boolean
19
+
20
+ constructor(target: ILeaf, selector: ISelector) {
21
+ this.target = target
22
+ this.selector = selector
23
+ }
24
+
25
+ public getHitPointPath(hitPoint: IPointData, hitRadius: number, options?: ISelectPathOptions): ISelectPathResult {
26
+ const through = options ? options.through : false
27
+ this.exclude = options ? options.exclude : undefined
28
+
29
+ this.isStop = false
30
+ this.point = { x: hitPoint.x, y: hitPoint.y, radiusX: hitRadius, radiusY: hitRadius }
31
+
32
+ // path
33
+ this.eachFind(this.target.children)
34
+
35
+ const { leaf } = this
36
+ const { defaultPath } = this.selector
37
+ const path = this.getPath(leaf)
38
+ path.pushList(defaultPath.list)
39
+
40
+ let result: ISelectPathResult
41
+
42
+ // throughPath
43
+ if (through) {
44
+ const throughPath = this.throughPath = new LeafList()
45
+ this.eachThroughFind(this.target.children)
46
+ throughPath.pushList(defaultPath.list)
47
+ result = { path, leaf, throughPath }
48
+ } else {
49
+ result = { path, leaf }
50
+ }
51
+
52
+ this.clear()
53
+
54
+ return result
55
+
56
+ }
57
+
58
+ public getPath(leaf: ILeaf): LeafList {
59
+ const list: LeafList = new LeafList()
60
+ while (leaf) {
61
+ list.push(leaf)
62
+ leaf = leaf.parent
63
+ }
64
+ return list
65
+ }
66
+
67
+ protected eachThroughFind(children: Array<ILeaf>): void {
68
+ let child: ILeaf
69
+ const { point } = this, len = children.length
70
+ for (let i = len - 1; i > -1; i--) {
71
+ child = children[i]
72
+ if (child.__interactionOff) continue
73
+
74
+ if (hitRadiusPoint(child.__world, point)) {
75
+ if (child.__isBranch) {
76
+ child.__childrenInteractionOff || this.eachThroughFind(child.children)
77
+ }
78
+
79
+ if (this.exclude && this.exclude.has(child)) continue
80
+ if (child.__hitWorld(point)) this.throughPath.push(child)
81
+ }
82
+
83
+ }
84
+ }
85
+
86
+ protected eachFind(children: Array<ILeaf>): void {
87
+ let child: ILeaf
88
+ const { point } = this, len = children.length
89
+ for (let i = len - 1; i > -1; i--) {
90
+ child = children[i]
91
+ if (child.__interactionOff) continue
92
+
93
+ if (hitRadiusPoint(child.__world, point)) {
94
+ if (child.__isBranch) {
95
+
96
+ child.__childrenInteractionOff || this.eachFind(child.children)
97
+
98
+ if (child.__isBranchLeaf) { // 填充了背景色的Group, 如画板/Frame元素
99
+ if (!this.isStop) this.hitChild(child, point)
100
+ }
101
+
102
+ } else {
103
+ this.hitChild(child, point)
104
+ }
105
+ }
106
+
107
+ if (this.isStop) break
108
+ }
109
+ }
110
+
111
+ protected hitChild(child: ILeaf, point: IRadiusPointData): void {
112
+ if (this.exclude && this.exclude.has(child)) return
113
+ if (child.__hitWorld(point)) {
114
+ this.leaf = child
115
+ this.isStop = true
116
+ }
117
+ }
118
+
119
+ protected clear(): void {
120
+ this.point = undefined
121
+ this.leaf = undefined
122
+ this.throughPath = undefined
123
+ this.exclude = undefined
124
+ }
125
+
126
+ public destroy(): void {
127
+ this.target = undefined
128
+ this.selector = undefined
129
+ }
130
+
131
+ }
@@ -0,0 +1,145 @@
1
+ import { ILeaf, ILeafArrayMap, ILeafMap, ISelector, ISelectPathResult, ISelectPathOptions, IPointData, ILeafList, IEventListenerId } from '@leafer/interface'
2
+ import { ChildEvent } from '@leafer/event'
3
+ import { LeafList } from '@leafer/list'
4
+
5
+ import { PathFinder } from './PathFinder'
6
+
7
+
8
+ interface IFind {
9
+ (leaf: ILeaf): boolean
10
+ }
11
+
12
+
13
+ export class Selector implements ISelector {
14
+
15
+ public target: ILeaf
16
+ protected pathFinder: PathFinder
17
+
18
+ public defaultPath: ILeafList
19
+
20
+ protected eventIds: IEventListenerId[]
21
+
22
+ protected innerIdList: ILeafMap = {}
23
+ protected idList: ILeafMap = {}
24
+ protected classNameList: ILeafArrayMap = {}
25
+ protected tagNameList: ILeafArrayMap = {}
26
+
27
+ constructor(target: ILeaf) {
28
+ this.target = target
29
+ this.defaultPath = new LeafList(target)
30
+ this.pathFinder = new PathFinder(target, this)
31
+ this.listenEvents()
32
+ }
33
+
34
+ protected listenEvents(): void {
35
+ this.eventIds = [
36
+ this.target.on__(ChildEvent.REMOVE, this.onRemoveChild, this)
37
+ ]
38
+ }
39
+
40
+ protected removeListenEvents(): void {
41
+ this.target.off__(this.eventIds)
42
+ }
43
+
44
+ protected onRemoveChild(event: ChildEvent): void {
45
+ const target = event.target as ILeaf
46
+ if (this.idList[target.id]) this.idList[target.id] = undefined
47
+ if (this.innerIdList[target.id]) this.innerIdList[target.innerId] = undefined
48
+ }
49
+
50
+ public getHitPointPath(hitPoint: IPointData, hitRadius: number, options?: ISelectPathOptions): ISelectPathResult {
51
+ return this.pathFinder.getHitPointPath(hitPoint, hitRadius, options)
52
+ }
53
+
54
+
55
+ public find(name: number | string, branch?: ILeaf): ILeaf | ILeaf[] {
56
+ if (typeof name === 'number') {
57
+ return this.getByInnerId(name, branch)
58
+ } else if (name.startsWith('#')) {
59
+ return this.getById(name.substring(1), branch)
60
+ } else if (name.startsWith('.')) {
61
+ return this.getByClassName(name.substring(1), branch)
62
+ } else {
63
+ return this.getByTagName(name, branch)
64
+ }
65
+ }
66
+
67
+ public getByInnerId(name: number, branch?: ILeaf): ILeaf {
68
+ let cache = this.innerIdList[name]
69
+ if (cache) return cache
70
+ if (!branch) branch = this.target
71
+ let find: ILeaf
72
+ this.loopFind(branch, (leaf) => {
73
+ if (leaf.innerId === name) {
74
+ find = leaf
75
+ this.innerIdList[name] = find
76
+ return true
77
+ } else {
78
+ return false
79
+ }
80
+ })
81
+ return find
82
+ }
83
+
84
+ public getById(name: string, branch?: ILeaf): ILeaf {
85
+ let cache = this.idList[name]
86
+ if (cache) return cache
87
+ if (!branch) branch = this.target
88
+ let find: ILeaf
89
+ this.loopFind(branch, (leaf) => {
90
+ if (leaf.id === name) {
91
+ find = leaf
92
+ this.idList[name] = find
93
+ return true
94
+ } else {
95
+ return false
96
+ }
97
+ })
98
+ return find
99
+ }
100
+
101
+ public getByClassName(name: string, branch?: ILeaf): ILeaf[] {
102
+ if (!branch) branch = this.target
103
+ let find: Array<ILeaf | ILeaf> = []
104
+ this.loopFind(branch, (leaf) => {
105
+ if (leaf.className === name) find.push(leaf)
106
+ return false
107
+ })
108
+ return find
109
+ }
110
+
111
+ public getByTagName(name: string, branch?: ILeaf): ILeaf[] {
112
+ if (!branch) branch = this.target
113
+ let find: Array<ILeaf | ILeaf> = []
114
+ this.loopFind(branch, (leaf) => {
115
+ if (leaf.tag === name) find.push(leaf)
116
+ return false
117
+ })
118
+ return find
119
+ }
120
+
121
+ protected loopFind(branch: ILeaf, find: IFind): void {
122
+ if (find(branch)) return
123
+ const { children } = branch
124
+ for (let i = 0, len = children.length; i < len; i++) {
125
+ branch = children[i] as ILeaf
126
+ if (find(branch)) return
127
+ if (branch.__isBranch) this.loopFind(branch, find)
128
+ }
129
+ }
130
+
131
+ public destroy(): void {
132
+ if (this.target) {
133
+ this.removeListenEvents()
134
+ this.pathFinder.destroy()
135
+
136
+ this.target = undefined
137
+ this.pathFinder = undefined
138
+ this.innerIdList = undefined
139
+ this.idList = undefined
140
+ this.classNameList = undefined
141
+ this.tagNameList = undefined
142
+ }
143
+ }
144
+
145
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { Selector } from './Selector'