@quenty/selectionutils 1.0.1-canary.38bdfe3.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## 1.0.1-canary.38bdfe3.0 (2022-09-07)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add SelectionUtils ([bb2b8ea](https://github.com/Quenty/NevermoreEngine/commit/bb2b8eab89c1c9d68ca0c5f8efc728c27ff1130f))
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2014-2022 Quenty
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 all
13
+ 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 THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ ## SelectionUtils
2
+ <div align="center">
3
+ <a href="http://quenty.github.io/NevermoreEngine/">
4
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
5
+ </a>
6
+ <a href="https://discord.gg/mhtGUS8">
7
+ <img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
8
+ </a>
9
+ <a href="https://github.com/Quenty/NevermoreEngine/actions">
10
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
11
+ </a>
12
+ </div>
13
+
14
+ Utility methods around Selection service. Useful for plugins and stories.
15
+
16
+ <div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/SelectionUtils">View docs →</a></div>
17
+
18
+ ## Installation
19
+ ```
20
+ npm install @quenty/selectionutils --save
21
+ ```
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "selectionutils",
3
+ "tree": {
4
+ "$path": "src"
5
+ }
6
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@quenty/selectionutils",
3
+ "version": "1.0.1-canary.38bdfe3.0",
4
+ "description": "Utility methods around Selection service. Useful for plugins and stories.",
5
+ "keywords": [
6
+ "Roblox",
7
+ "Nevermore",
8
+ "Lua",
9
+ "Selection"
10
+ ],
11
+ "bugs": {
12
+ "url": "https://github.com/Quenty/NevermoreEngine/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/Quenty/NevermoreEngine.git",
17
+ "directory": "src/selectionutils/"
18
+ },
19
+ "funding": {
20
+ "type": "patreon",
21
+ "url": "https://www.patreon.com/quenty"
22
+ },
23
+ "license": "MIT",
24
+ "contributors": [
25
+ "Quenty"
26
+ ],
27
+ "dependencies": {
28
+ "@quenty/baseobject": "5.1.1",
29
+ "@quenty/brio": "7.1.1-canary.38bdfe3.0",
30
+ "@quenty/loader": "5.0.1",
31
+ "@quenty/maid": "2.4.0",
32
+ "@quenty/rx": "6.1.1-canary.38bdfe3.0",
33
+ "@quenty/signal": "2.3.0",
34
+ "@quenty/table": "3.1.0",
35
+ "@quenty/valueobject": "6.1.1-canary.38bdfe3.0"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "gitHead": "38bdfe3721bddf1d272628b1104b1d8e0abaf24f"
41
+ }
@@ -0,0 +1,189 @@
1
+ --[=[
2
+ @class RxSelectionUtils
3
+ ]=]
4
+
5
+ local Selection = game:GetService("Selection")
6
+
7
+ local require = require(script.Parent.loader).load(script)
8
+
9
+ local Observable = require("Observable")
10
+ local Maid = require("Maid")
11
+ local Brio = require("Brio")
12
+ local ValueObject = require("ValueObject")
13
+ local RxBrioUtils = require("RxBrioUtils")
14
+ local Set = require("Set")
15
+
16
+ local RxSelectionUtils = {}
17
+
18
+ --[=[
19
+ Observes first selection in the selection list which is of a class
20
+
21
+ ```lua
22
+ RxSelectionUtils.observeFirstSelectionWhichIsA("BasePart"):Subscribe(function(part)
23
+ print("part", part)
24
+ end)
25
+ ```
26
+
27
+ @param className string
28
+ @return Observable<Instance?>
29
+ ]=]
30
+ function RxSelectionUtils.observeFirstSelectionWhichIsA(className)
31
+ assert(type(className) == "string", "Bad className")
32
+
33
+ return RxSelectionUtils.observeFirstSelection(function(inst)
34
+ return inst:IsA(className)
35
+ end)
36
+ end
37
+
38
+ --[=[
39
+ Observes first selection in the selection list which is an "Adornee"
40
+
41
+ @return Observable<Instance?>
42
+ ]=]
43
+ function RxSelectionUtils.observeFirstAdornee()
44
+ return RxSelectionUtils.observeFirstSelection(function(inst)
45
+ return inst:IsA("BasePart") or inst:IsA("Model")
46
+ end)
47
+ end
48
+
49
+ --[=[
50
+ Observes selection in which are an "Adornee"
51
+
52
+ @return Observable<Brio<Instance>>
53
+ ]=]
54
+ function RxSelectionUtils.observeAdorneesBrio()
55
+ return RxSelectionUtils.observeSelectionItemsBrio():Pipe({
56
+ RxBrioUtils.where(function(inst)
57
+ return inst:IsA("BasePart") or inst:IsA("Model")
58
+ end)
59
+ })
60
+ end
61
+
62
+
63
+ --[=[
64
+ Observes first selection which meets condition
65
+
66
+ ```lua
67
+ RxSelectionUtils.observeFirstSelection(function(instance)
68
+ return instance:IsA("BasePart")
69
+ end):Subscribe(function(part)
70
+ print("part", part)
71
+ end)
72
+ ```
73
+
74
+ @param where callback
75
+ @return Observable<Instance?>
76
+ ]=]
77
+ function RxSelectionUtils.observeFirstSelection(where)
78
+ assert(type(where) == "function", "Bad where")
79
+
80
+ return Observable.new(function(sub)
81
+ local maid = Maid.new()
82
+
83
+ local current = ValueObject.new(nil)
84
+ maid:GiveTask(current)
85
+
86
+ local function handleSelectionChanged()
87
+ for _, item in pairs(Selection:Get()) do
88
+ if where(item) then
89
+ current.Value = item
90
+ return
91
+ end
92
+ end
93
+
94
+ current.Value = nil
95
+ end
96
+
97
+ maid:GiveTask(Selection.SelectionChanged:Connect(handleSelectionChanged))
98
+ handleSelectionChanged()
99
+
100
+ maid:GiveTask(current:Observe():Subscribe(function(value)
101
+ task.spawn(function()
102
+ sub:Fire(value)
103
+ end)
104
+ end))
105
+
106
+ return maid
107
+ end)
108
+ end
109
+
110
+ --[=[
111
+ Observes first selection which meets condition
112
+
113
+ @param where callback
114
+ @return Observable<Brio<Instance>>
115
+ ]=]
116
+ function RxSelectionUtils.observeFirstSelectionBrio(where)
117
+ assert(type(where) == "function", "Bad where")
118
+
119
+ return RxSelectionUtils.observeFirstSelection(where):Pipe({
120
+ RxBrioUtils.toBrio();
121
+ RxBrioUtils.onlyLastBrioSurvives();
122
+ RxBrioUtils.where(function(value)
123
+ return value ~= nil
124
+ end)
125
+ })
126
+ end
127
+
128
+
129
+ --[=[
130
+ Observes the current selection table.
131
+
132
+ @return Observable<{ Instance }>
133
+ ]=]
134
+ function RxSelectionUtils.observeSelectionList()
135
+ return Observable.new(function(sub)
136
+ local maid = Maid.new()
137
+
138
+ local current = Selection:Get()
139
+
140
+ maid:GiveTask(Selection.SelectionChanged:Connect(function()
141
+ sub:Fire(Selection:Get())
142
+ end))
143
+ sub:Fire(current)
144
+
145
+ return maid
146
+ end)
147
+ end
148
+
149
+ --[=[
150
+ Observes selection items by brio. De-duplicates changed events.
151
+
152
+ @return Observable<Brio<Instance>>
153
+ ]=]
154
+ function RxSelectionUtils.observeSelectionItemsBrio()
155
+ return Observable.new(function(sub)
156
+ local maid = Maid.new()
157
+
158
+ local lastSet = {}
159
+
160
+ local function handleSelectionChanged()
161
+ local currentSet = Set.fromList(Selection:Get())
162
+ local toRemoveSet = Set.difference(lastSet, currentSet)
163
+ local toAddSet = Set.difference(currentSet, lastSet)
164
+ lastSet = currentSet
165
+
166
+ -- Remove first
167
+ for toRemove, _ in pairs(toRemoveSet) do
168
+ maid[toRemove] = nil
169
+ end
170
+
171
+ -- Then add
172
+ for toAdd, _ in pairs(toAddSet) do
173
+ local brio = Brio.new(toAdd)
174
+ maid[toAdd] = brio
175
+
176
+ task.spawn(function()
177
+ sub:Fire(brio)
178
+ end)
179
+ end
180
+ end
181
+
182
+ maid:GiveTask(Selection.SelectionChanged:Connect(handleSelectionChanged))
183
+ handleSelectionChanged()
184
+
185
+ return maid
186
+ end)
187
+ end
188
+
189
+ return RxSelectionUtils
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }