@plohoj/html-editor 0.0.5
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 +21 -0
- package/README.md +123 -0
- package/dist/index.js +315 -0
- package/dist/index.js.map +1 -0
- package/package.json +24 -0
- package/src/index.ts +15 -0
- package/src/observable/await-element.ts +32 -0
- package/src/observable/observe-mutation.ts +12 -0
- package/src/observable/observe-query-selector-all.ts +96 -0
- package/src/observable/observe-query-selector.ts +104 -0
- package/src/observable/url-change.ts +38 -0
- package/src/operators/click-element.ts +20 -0
- package/src/operators/merge-map-added-elements.ts +77 -0
- package/src/operators/merge-map-string-toggle.ts +36 -0
- package/src/operators/remove-element.ts +9 -0
- package/src/operators/set-input-value.ts +23 -0
- package/src/utils/find-recursively.ts +53 -0
- package/src/utils/random-from-array.ts +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Alexandr Plokhih
|
|
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,123 @@
|
|
|
1
|
+
# HTML editor
|
|
2
|
+
**`html-editor`** it's a tool for helping modification html elements
|
|
3
|
+
|
|
4
|
+
## Table of contents
|
|
5
|
+
* [Observables](#observables)
|
|
6
|
+
* [observeElementMutation](#observe-element-mutation)
|
|
7
|
+
* [observeQuerySelector](#observe-query-selector)
|
|
8
|
+
* [observeQuerySelectorAll](#observe-query-selector-all)
|
|
9
|
+
* [awaitElement](#await-element)
|
|
10
|
+
* [awaitRandomElement](#await-random-element)
|
|
11
|
+
* [urlChange$](#url-change)
|
|
12
|
+
* [Operators](#operators)
|
|
13
|
+
* [mergeMapAddedElements](#merge-map-added-elements)
|
|
14
|
+
* [mergeMapStringToggle](#merge-map-string-toggle)
|
|
15
|
+
|
|
16
|
+
# <a name="observables"></a> Observables
|
|
17
|
+
## <a name="observe-element-mutation"></a> `observeElementMutation`
|
|
18
|
+
Converts the callback of the MutationObserver class to an Rx event stream.
|
|
19
|
+
|
|
20
|
+
Example:
|
|
21
|
+
```ts
|
|
22
|
+
observeElementMutation(
|
|
23
|
+
document.querySelector('#my-element'),
|
|
24
|
+
{ attributeFilter: ['data-my-data'] },
|
|
25
|
+
).subscribe(console.log);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## <a name="observe-query-selector"></a> `observeQuerySelector`
|
|
29
|
+
Returns change (addition and deletion) of element that match selectors, like an Rx stream.
|
|
30
|
+
|
|
31
|
+
Example:
|
|
32
|
+
```ts
|
|
33
|
+
observeQuerySelector(
|
|
34
|
+
'.my-child',
|
|
35
|
+
{
|
|
36
|
+
parent: document.querySelector('#my-parent'),
|
|
37
|
+
has: '.my-sub-child',
|
|
38
|
+
filter: element => element.classList.contains('.my-child-modifier'),
|
|
39
|
+
}
|
|
40
|
+
).subscribe(console.log);
|
|
41
|
+
```
|
|
42
|
+
Example log:
|
|
43
|
+
```ts
|
|
44
|
+
{added: Element, target: Element, removed: undefined};
|
|
45
|
+
{added: undefined, target: undefined, removed: Element};
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## <a name="observe-query-selector-all"></a> `observeQuerySelectorAll`
|
|
49
|
+
Returns changes (additions and deletions) of elements that match selectors, like an Rx stream.
|
|
50
|
+
|
|
51
|
+
Example:
|
|
52
|
+
```ts
|
|
53
|
+
observeQuerySelectorAll(
|
|
54
|
+
'.my-child',
|
|
55
|
+
{
|
|
56
|
+
parent: document.querySelector('#my-parent'),
|
|
57
|
+
has: '.my-sub-child',
|
|
58
|
+
filter: element => element.classList.contains('.my-child-modifier'),
|
|
59
|
+
}
|
|
60
|
+
).subscribe(console.log);
|
|
61
|
+
```
|
|
62
|
+
Example log:
|
|
63
|
+
```ts
|
|
64
|
+
{added: [Element], target: [Element, Element], removed: []};
|
|
65
|
+
{added: [], target: [Element], removed: [Element]};
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## <a name="await-element"></a> `awaitElement`
|
|
69
|
+
Awaiting only one element to match the selector and returns it as an Rx stream. The stream ends immediately after one element is found / added.
|
|
70
|
+
|
|
71
|
+
Example:
|
|
72
|
+
```ts
|
|
73
|
+
awaitElement('#my-element')
|
|
74
|
+
.subscribe(console.log);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## <a name="await-random-element"></a> `awaitRandomElement`
|
|
78
|
+
Awaiting Expects at least one element to match the selector and returns it as an Rx stream. If there are more than 1 elements, it will return a random one. The stream ends immediately after the elements are found / added.
|
|
79
|
+
|
|
80
|
+
Example:
|
|
81
|
+
```ts
|
|
82
|
+
awaitRandomElement('.my-element')
|
|
83
|
+
.subscribe(console.log);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## <a name="url-change"></a> `urlChange$`
|
|
87
|
+
Emit new location url when the URL is changes
|
|
88
|
+
|
|
89
|
+
Example:
|
|
90
|
+
```ts
|
|
91
|
+
urlChange$.subscribe(console.log);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
# <a name="operators"></a> Operators
|
|
95
|
+
|
|
96
|
+
## <a name="merge-map-added-elements"></a> `mergeMapAddedElements`
|
|
97
|
+
Conversion operator to a new stream for each new added element
|
|
98
|
+
|
|
99
|
+
Example:
|
|
100
|
+
```ts
|
|
101
|
+
observeQuerySelectorAll('.my-button')
|
|
102
|
+
.pipe(
|
|
103
|
+
mergeMapAddedElements(
|
|
104
|
+
element => fromEvent(element, 'click'),
|
|
105
|
+
{ isTakeUntilRemoved: true }
|
|
106
|
+
)
|
|
107
|
+
).subscribe(console.log);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## <a name="merge-map-string-toggle"></a> `mergeMapStringToggle`
|
|
111
|
+
The operator creates a separate stream when the source string is validated.
|
|
112
|
+
|
|
113
|
+
Example:
|
|
114
|
+
```ts
|
|
115
|
+
urlChange$
|
|
116
|
+
.pipe(
|
|
117
|
+
mergeMapStringToggle(
|
|
118
|
+
/my-url-segment/,
|
|
119
|
+
() => observeQuerySelectorAll('.my-element'),
|
|
120
|
+
{ isTakeUntilToggle: true },
|
|
121
|
+
)
|
|
122
|
+
).subscribe(console.log);
|
|
123
|
+
```
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { throttleTime, mergeMap, distinctUntilChanged, switchMap, filter, map, take, shareReplay, tap, connect, takeUntil } from 'rxjs/operators';
|
|
2
|
+
import { Observable, concat, defer, EMPTY, of, pipe, merge, from } from 'rxjs';
|
|
3
|
+
|
|
4
|
+
function randomFromArray(array, from = 0, to = array.length) {
|
|
5
|
+
if (to < 0) {
|
|
6
|
+
to = array.length + to;
|
|
7
|
+
}
|
|
8
|
+
return array[Math.floor(Math.random() * (to - from)) + from];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Converts the callback of the MutationObserver class to an Rx event stream
|
|
13
|
+
*/
|
|
14
|
+
function observeElementMutation(node, options) {
|
|
15
|
+
return new Observable(subscriber => {
|
|
16
|
+
const mutationObserver = new MutationObserver(mutation => subscriber.next(mutation));
|
|
17
|
+
mutationObserver.observe(node, options);
|
|
18
|
+
return () => mutationObserver.disconnect();
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Returns change (addition and deletion) of element that match selectors, like an Rx stream.
|
|
24
|
+
*/
|
|
25
|
+
function observeQuerySelector(query, options = {}) {
|
|
26
|
+
const { parent = document.documentElement, asRemovedWhen } = options;
|
|
27
|
+
let targetElement;
|
|
28
|
+
function checkChanges() {
|
|
29
|
+
const querySelectedElements = parent.querySelectorAll(query);
|
|
30
|
+
let filteredSelectedElement;
|
|
31
|
+
const changes = {};
|
|
32
|
+
for (const querySelectedElement of querySelectedElements) {
|
|
33
|
+
if (options.has && !querySelectedElement.querySelector(options.has)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (options.filter && !options.filter(querySelectedElement)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
filteredSelectedElement = querySelectedElement;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
if (filteredSelectedElement === targetElement) {
|
|
43
|
+
return EMPTY;
|
|
44
|
+
}
|
|
45
|
+
if (targetElement) {
|
|
46
|
+
changes.removed = targetElement;
|
|
47
|
+
}
|
|
48
|
+
if (filteredSelectedElement) {
|
|
49
|
+
changes.added = filteredSelectedElement;
|
|
50
|
+
}
|
|
51
|
+
changes.target = filteredSelectedElement;
|
|
52
|
+
targetElement = filteredSelectedElement;
|
|
53
|
+
return of(changes);
|
|
54
|
+
}
|
|
55
|
+
const observeQuerySelector$ = concat(defer(() => checkChanges()), observeElementMutation(parent, { subtree: true, childList: true }).pipe(throttleTime(0, undefined, { leading: true, trailing: true }), mergeMap(checkChanges)));
|
|
56
|
+
if (asRemovedWhen) {
|
|
57
|
+
const removedObserver$ = defer(() => {
|
|
58
|
+
if (!targetElement) {
|
|
59
|
+
return EMPTY;
|
|
60
|
+
}
|
|
61
|
+
const changes = {
|
|
62
|
+
removed: targetElement,
|
|
63
|
+
};
|
|
64
|
+
targetElement = undefined;
|
|
65
|
+
return of(changes);
|
|
66
|
+
});
|
|
67
|
+
const observeQuerySelectorWithRemovedWhen$ = asRemovedWhen.pipe(distinctUntilChanged(), switchMap(asRemoved => asRemoved ? removedObserver$ : observeQuerySelector$));
|
|
68
|
+
return observeQuerySelectorWithRemovedWhen$;
|
|
69
|
+
}
|
|
70
|
+
return observeQuerySelector$;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Returns changes (additions and deletions) of elements that match selectors, like an Rx stream. */
|
|
74
|
+
function observeQuerySelectorAll(query, options = {}) {
|
|
75
|
+
const { parent = document.documentElement, asRemovedWhen } = options;
|
|
76
|
+
const targetElements = new Set();
|
|
77
|
+
function checkChanges() {
|
|
78
|
+
const addedElements = new Set();
|
|
79
|
+
const targetElementsDiff = new Set(targetElements);
|
|
80
|
+
const querySelectedElements = new Set(parent.querySelectorAll(query));
|
|
81
|
+
for (const querySelectedElement of querySelectedElements) {
|
|
82
|
+
if (options.has && !querySelectedElement.querySelector(options.has)) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (options.filter && !options.filter(querySelectedElement)) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (targetElementsDiff.has(querySelectedElement)) {
|
|
89
|
+
targetElementsDiff.delete(querySelectedElement);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
addedElements.add(querySelectedElement);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// No changes
|
|
96
|
+
if (addedElements.size === 0 && targetElementsDiff.size === 0) {
|
|
97
|
+
return EMPTY;
|
|
98
|
+
}
|
|
99
|
+
for (const removedElement of targetElementsDiff) {
|
|
100
|
+
targetElements.delete(removedElement);
|
|
101
|
+
}
|
|
102
|
+
for (const addedElement of addedElements) {
|
|
103
|
+
targetElements.add(addedElement);
|
|
104
|
+
}
|
|
105
|
+
const changes = {
|
|
106
|
+
target: [...targetElements.values()],
|
|
107
|
+
added: [...addedElements.values()],
|
|
108
|
+
removed: [...targetElementsDiff.values()],
|
|
109
|
+
};
|
|
110
|
+
return of(changes);
|
|
111
|
+
}
|
|
112
|
+
let observeQuerySelectorAll$ = concat(defer(() => checkChanges()), observeElementMutation(parent, { subtree: true, childList: true }).pipe(throttleTime(0, undefined, { leading: true, trailing: true }), mergeMap(checkChanges)));
|
|
113
|
+
if (asRemovedWhen) {
|
|
114
|
+
const removedObserver$ = defer(() => {
|
|
115
|
+
if (targetElements.size === 0) {
|
|
116
|
+
return EMPTY;
|
|
117
|
+
}
|
|
118
|
+
const changes = {
|
|
119
|
+
target: [],
|
|
120
|
+
added: [],
|
|
121
|
+
removed: [...targetElements.values()],
|
|
122
|
+
};
|
|
123
|
+
targetElements.clear();
|
|
124
|
+
return of(changes);
|
|
125
|
+
});
|
|
126
|
+
const observeQuerySelectorAllWithRemovedWhen$ = asRemovedWhen.pipe(distinctUntilChanged(), switchMap(asRemoved => asRemoved ? removedObserver$ : observeQuerySelectorAll$));
|
|
127
|
+
return observeQuerySelectorAllWithRemovedWhen$;
|
|
128
|
+
}
|
|
129
|
+
return observeQuerySelectorAll$;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Awaiting only one element to match the selector and returns it as an Rx stream.
|
|
134
|
+
* The stream ends immediately after one element is found / added.
|
|
135
|
+
*/
|
|
136
|
+
function awaitElement(query) {
|
|
137
|
+
return observeQuerySelector(query)
|
|
138
|
+
.pipe(filter(changes => !!changes.target), map(changes => changes.target), take(1));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Awaiting Expects at least one element to match the selector and returns it as an Rx stream.
|
|
142
|
+
* If there are more than 1 elements,
|
|
143
|
+
* it will return a random one. The stream ends immediately after the elements are found / added.
|
|
144
|
+
*/
|
|
145
|
+
function awaitRandomElement(query) {
|
|
146
|
+
return observeQuerySelectorAll(query)
|
|
147
|
+
.pipe(filter(changes => !!changes.target), map(changes => randomFromArray(changes.target)), take(1));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let pushStateSubscriber$;
|
|
151
|
+
function injectPushStateHandler() {
|
|
152
|
+
const pushState = history.pushState;
|
|
153
|
+
history.pushState = function (...args) {
|
|
154
|
+
pushState.apply(this, args);
|
|
155
|
+
pushStateSubscriber$ === null || pushStateSubscriber$ === void 0 ? void 0 : pushStateSubscriber$.next(location.href);
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Emit new location url when the URL is changes
|
|
160
|
+
*/
|
|
161
|
+
const urlChange$ = new Observable(subscriber$ => {
|
|
162
|
+
function updateURL() {
|
|
163
|
+
subscriber$.next(location.href);
|
|
164
|
+
}
|
|
165
|
+
window.addEventListener('hashchange', updateURL);
|
|
166
|
+
window.addEventListener('popstate', updateURL);
|
|
167
|
+
pushStateSubscriber$ = subscriber$;
|
|
168
|
+
subscriber$.next(location.href);
|
|
169
|
+
injectPushStateHandler();
|
|
170
|
+
return () => {
|
|
171
|
+
pushStateSubscriber$ = undefined;
|
|
172
|
+
window.removeEventListener('hashchange', updateURL);
|
|
173
|
+
window.removeEventListener('popstate', updateURL);
|
|
174
|
+
};
|
|
175
|
+
}).pipe(distinctUntilChanged(), shareReplay(1));
|
|
176
|
+
|
|
177
|
+
function clickElementImmediately(element) {
|
|
178
|
+
element.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
179
|
+
console.log(`Click: `, element);
|
|
180
|
+
}
|
|
181
|
+
function clickElement(element) {
|
|
182
|
+
if (element) {
|
|
183
|
+
clickElementImmediately(element);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
return pipe(tap((element) => clickElementImmediately(element)));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function assuredArray(values) {
|
|
191
|
+
if (values instanceof Array) {
|
|
192
|
+
return values;
|
|
193
|
+
}
|
|
194
|
+
if (values) {
|
|
195
|
+
return [values];
|
|
196
|
+
}
|
|
197
|
+
return [];
|
|
198
|
+
}
|
|
199
|
+
/** Conversion operator to a new stream for each new added element */
|
|
200
|
+
function mergeMapAddedElements(project, options) {
|
|
201
|
+
if (!(options === null || options === void 0 ? void 0 : options.isTakeUntilRemoved)) {
|
|
202
|
+
return source$ => source$.pipe(mergeMap(changes => {
|
|
203
|
+
let added = assuredArray(changes.added);
|
|
204
|
+
if (added.length === 0) {
|
|
205
|
+
return EMPTY;
|
|
206
|
+
}
|
|
207
|
+
let addedObservers = added.map(project);
|
|
208
|
+
return merge(...addedObservers);
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
return source$ => source$.pipe(connect(connectedSource$ => connectedSource$.pipe(mergeMap(changes => {
|
|
212
|
+
let added = assuredArray(changes.added);
|
|
213
|
+
if (added.length === 0) {
|
|
214
|
+
return EMPTY;
|
|
215
|
+
}
|
|
216
|
+
let addedObservers = added.map(addedElement => from(project(addedElement)).pipe(takeUntil(connectedSource$.pipe(filter(changes => {
|
|
217
|
+
if (changes.removed instanceof Array) {
|
|
218
|
+
return changes.removed.indexOf(addedElement) != -1;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
return changes.removed === addedElement;
|
|
222
|
+
}
|
|
223
|
+
})))));
|
|
224
|
+
return merge(...addedObservers);
|
|
225
|
+
}))));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function removeElement() {
|
|
229
|
+
return pipe(tap((element) => {
|
|
230
|
+
element.remove();
|
|
231
|
+
console.log(`Remove: `, element);
|
|
232
|
+
}));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function setInputValueImmediately(element, value) {
|
|
236
|
+
element.value = value;
|
|
237
|
+
element.focus();
|
|
238
|
+
element.dispatchEvent(new Event('input', { target: element }));
|
|
239
|
+
console.log(`Set value: `, { element, value });
|
|
240
|
+
}
|
|
241
|
+
function setInputValue(elementOrValue, value) {
|
|
242
|
+
if (typeof elementOrValue === 'string') {
|
|
243
|
+
return pipe(tap((element) => setInputValueImmediately(element, elementOrValue)));
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
setInputValueImmediately(elementOrValue, value);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The operator creates a separate stream when the source string is validated. */
|
|
251
|
+
function mergeMapStringToggle(condition, project, options) {
|
|
252
|
+
const mapConditionFn = typeof condition === 'function' ? condition : (url) => condition.test(url);
|
|
253
|
+
let urlMatchToggler;
|
|
254
|
+
if (typeof project === 'function') {
|
|
255
|
+
urlMatchToggler = (isUrlMatch) => isUrlMatch ? from(project()) : EMPTY;
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
urlMatchToggler = (isUrlMatch) => isUrlMatch ? from(project) : EMPTY;
|
|
259
|
+
}
|
|
260
|
+
const mergeOperator = (options === null || options === void 0 ? void 0 : options.isTakeUntilToggle)
|
|
261
|
+
? switchMap(urlMatchToggler)
|
|
262
|
+
: mergeMap(urlMatchToggler);
|
|
263
|
+
return pipe(map(mapConditionFn), distinctUntilChanged(), mergeOperator);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function findRecursively(obj, mather) {
|
|
267
|
+
const seen = new Set();
|
|
268
|
+
const searched = {};
|
|
269
|
+
const needFind = new Set([[undefined, obj]]);
|
|
270
|
+
function addToCheck(path, value) {
|
|
271
|
+
if (seen.has(value)) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
needFind.add([path, value]);
|
|
275
|
+
seen.add(value);
|
|
276
|
+
}
|
|
277
|
+
while (true) {
|
|
278
|
+
const iterator = needFind.values().next();
|
|
279
|
+
if (iterator.done) {
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
const path = iterator.value[0];
|
|
283
|
+
const iteratedObj = iterator.value[1];
|
|
284
|
+
needFind.delete(iterator.value);
|
|
285
|
+
if (!iteratedObj) {
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (iteratedObj instanceof Array) {
|
|
289
|
+
for (const [index, value] of iteratedObj.entries()) {
|
|
290
|
+
if (value)
|
|
291
|
+
addToCheck(`${path || ''}[${index}]`, value);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
else if (typeof iteratedObj === "object") {
|
|
295
|
+
for (const key of Object.keys(iteratedObj)) {
|
|
296
|
+
const value = iteratedObj[key];
|
|
297
|
+
const keyPath = path ? `${path}.${key}` : key;
|
|
298
|
+
if (mather(key, value, keyPath)) {
|
|
299
|
+
searched[keyPath] = value;
|
|
300
|
+
}
|
|
301
|
+
addToCheck(keyPath, value);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (Object.keys(searched).length === 0) {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
return searched;
|
|
309
|
+
}
|
|
310
|
+
function findRecursivelyPropertyName(obj, propertyName) {
|
|
311
|
+
return findRecursively(obj, matchedPropertyName => matchedPropertyName === propertyName);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export { awaitElement, awaitRandomElement, clickElement, findRecursively, findRecursivelyPropertyName, mergeMapAddedElements, mergeMapStringToggle, observeElementMutation, observeQuerySelector, observeQuerySelectorAll, randomFromArray, removeElement, setInputValue, urlChange$ };
|
|
315
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@plohoj/html-editor",
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "html-editor it's a tool for helping modification html elements",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/plohoj/html-editor.git"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "rollup --config"
|
|
11
|
+
},
|
|
12
|
+
"author": "Plokhikh Alexandr",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"types": "./src/index.ts",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"rxjs": "^7.0.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"rollup": "^2.60.2",
|
|
21
|
+
"rollup-plugin-typescript2": "^0.31.1",
|
|
22
|
+
"typescript": "^4.5.2"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Observable
|
|
2
|
+
export { awaitElement, awaitRandomElement } from "./observable/await-element";
|
|
3
|
+
export { observeElementMutation } from "./observable/observe-mutation";
|
|
4
|
+
export { observeQuerySelector } from "./observable/observe-query-selector";
|
|
5
|
+
export { observeQuerySelectorAll } from "./observable/observe-query-selector-all";
|
|
6
|
+
export { urlChange$ } from "./observable/url-change";
|
|
7
|
+
// Operators
|
|
8
|
+
export { clickElement } from "./operators/click-element";
|
|
9
|
+
export { mergeMapAddedElements } from "./operators/merge-map-added-elements";
|
|
10
|
+
export { removeElement } from "./operators/remove-element";
|
|
11
|
+
export { setInputValue } from "./operators/set-input-value";
|
|
12
|
+
export { mergeMapStringToggle } from "./operators/merge-map-string-toggle";
|
|
13
|
+
// Utils
|
|
14
|
+
export { findRecursively, findRecursivelyPropertyName, RecursivelyFindMather } from "./utils/find-recursively";
|
|
15
|
+
export { randomFromArray } from "./utils/random-from-array";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
import { filter, map, take } from "rxjs/operators";
|
|
3
|
+
import { randomFromArray } from "../utils/random-from-array";
|
|
4
|
+
import { observeQuerySelector } from "./observe-query-selector";
|
|
5
|
+
import { observeQuerySelectorAll } from "./observe-query-selector-all";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Awaiting only one element to match the selector and returns it as an Rx stream.
|
|
9
|
+
* The stream ends immediately after one element is found / added.
|
|
10
|
+
*/
|
|
11
|
+
export function awaitElement<T extends Element = Element>(query: string): Observable<T> {
|
|
12
|
+
return observeQuerySelector<T>(query)
|
|
13
|
+
.pipe(
|
|
14
|
+
filter(changes => !!changes.target),
|
|
15
|
+
map(changes => changes.target!),
|
|
16
|
+
take(1),
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Awaiting Expects at least one element to match the selector and returns it as an Rx stream.
|
|
22
|
+
* If there are more than 1 elements,
|
|
23
|
+
* it will return a random one. The stream ends immediately after the elements are found / added.
|
|
24
|
+
*/
|
|
25
|
+
export function awaitRandomElement<T extends Element = Element>(query: string): Observable<T> {
|
|
26
|
+
return observeQuerySelectorAll<T>(query)
|
|
27
|
+
.pipe(
|
|
28
|
+
filter(changes => !!changes.target),
|
|
29
|
+
map(changes => randomFromArray(changes.target)),
|
|
30
|
+
take(1),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts the callback of the MutationObserver class to an Rx event stream
|
|
5
|
+
*/
|
|
6
|
+
export function observeElementMutation<T extends Node>(node: T, options?: MutationObserverInit): Observable<MutationRecord[]> {
|
|
7
|
+
return new Observable<MutationRecord[]>(subscriber => {
|
|
8
|
+
const mutationObserver = new MutationObserver(mutation => subscriber.next(mutation));
|
|
9
|
+
mutationObserver.observe(node, options);
|
|
10
|
+
return () => mutationObserver.disconnect();
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
|
|
2
|
+
import { concat, defer, EMPTY, Observable, of } from "rxjs";
|
|
3
|
+
import { distinctUntilChanged, mergeMap, switchMap, throttleTime } from "rxjs/operators";
|
|
4
|
+
import { observeElementMutation } from "./observe-mutation";
|
|
5
|
+
import { IObserveQuerySelectorOptions } from "./observe-query-selector";
|
|
6
|
+
|
|
7
|
+
export interface IObservedElementsChanges<T extends Element = Element> {
|
|
8
|
+
/** All elements that satisfy the filtering condition. */
|
|
9
|
+
target: T[];
|
|
10
|
+
/** New elements that have been added since the last emit. */
|
|
11
|
+
added: T[];
|
|
12
|
+
/** Elements that have been removed since the last emit. */
|
|
13
|
+
removed: T[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Returns changes (additions and deletions) of elements that match selectors, like an Rx stream. */
|
|
17
|
+
export function observeQuerySelectorAll<T extends Element = Element>(
|
|
18
|
+
query: string,
|
|
19
|
+
options: IObserveQuerySelectorOptions = {},
|
|
20
|
+
): Observable<IObservedElementsChanges<T>> {
|
|
21
|
+
const { parent = document.documentElement, asRemovedWhen } = options;
|
|
22
|
+
const targetElements = new Set<T>();
|
|
23
|
+
|
|
24
|
+
function checkChanges(): Observable<IObservedElementsChanges<T>> {
|
|
25
|
+
const addedElements = new Set<T>();
|
|
26
|
+
const targetElementsDiff = new Set(targetElements);
|
|
27
|
+
const querySelectedElements = new Set(parent.querySelectorAll<T>(query));
|
|
28
|
+
|
|
29
|
+
for (const querySelectedElement of querySelectedElements) {
|
|
30
|
+
if (options.has && !querySelectedElement.querySelector(options.has)) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (options.filter && !options.filter(querySelectedElement)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (targetElementsDiff.has(querySelectedElement)) {
|
|
38
|
+
targetElementsDiff.delete(querySelectedElement);
|
|
39
|
+
} else {
|
|
40
|
+
addedElements.add(querySelectedElement);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// No changes
|
|
45
|
+
if (addedElements.size === 0 && targetElementsDiff.size === 0) {
|
|
46
|
+
return EMPTY;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (const removedElement of targetElementsDiff) {
|
|
50
|
+
targetElements.delete(removedElement);
|
|
51
|
+
}
|
|
52
|
+
for (const addedElement of addedElements) {
|
|
53
|
+
targetElements.add(addedElement);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const changes: IObservedElementsChanges<T> = {
|
|
57
|
+
target: [...targetElements.values()],
|
|
58
|
+
added: [...addedElements.values()],
|
|
59
|
+
removed: [...targetElementsDiff.values()],
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return of(changes);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let observeQuerySelectorAll$ = concat(
|
|
66
|
+
defer(() => checkChanges()),
|
|
67
|
+
observeElementMutation(parent, {subtree: true, childList: true}).pipe(
|
|
68
|
+
throttleTime(0, undefined, {leading: true, trailing: true}),
|
|
69
|
+
mergeMap(checkChanges)
|
|
70
|
+
),
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
if (asRemovedWhen) {
|
|
74
|
+
const removedObserver$ = defer(() => {
|
|
75
|
+
if (targetElements.size === 0) {
|
|
76
|
+
return EMPTY;
|
|
77
|
+
}
|
|
78
|
+
const changes: IObservedElementsChanges<T> = {
|
|
79
|
+
target: [],
|
|
80
|
+
added: [],
|
|
81
|
+
removed: [...targetElements.values()],
|
|
82
|
+
};
|
|
83
|
+
targetElements.clear();
|
|
84
|
+
return of(changes);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const observeQuerySelectorAllWithRemovedWhen$ = asRemovedWhen.pipe(
|
|
88
|
+
distinctUntilChanged(),
|
|
89
|
+
switchMap(asRemoved => asRemoved ? removedObserver$ : observeQuerySelectorAll$),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return observeQuerySelectorAllWithRemovedWhen$;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return observeQuerySelectorAll$;
|
|
96
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
|
|
2
|
+
import { concat, defer, EMPTY, Observable, of } from "rxjs";
|
|
3
|
+
import { distinctUntilChanged, mergeMap, switchMap, throttleTime } from "rxjs/operators";
|
|
4
|
+
import { observeElementMutation } from "./observe-mutation";
|
|
5
|
+
|
|
6
|
+
export interface IObserveQuerySelectorOptions<T extends Element = Element> {
|
|
7
|
+
/**
|
|
8
|
+
* The parent element within which changes are tracked.
|
|
9
|
+
* @default document.documentElement
|
|
10
|
+
*/
|
|
11
|
+
parent?: T;
|
|
12
|
+
/** Checks if the added element has any child elements that match the selectors. */
|
|
13
|
+
has?: string;
|
|
14
|
+
/** Custom validation of each item */
|
|
15
|
+
filter?: (element: T) => boolean;
|
|
16
|
+
/**
|
|
17
|
+
* When the `asRemovedWhen` parameter emits a` true` value,
|
|
18
|
+
* all currently added items will be returned as removed.
|
|
19
|
+
* When the `asRemovedWhen` parameter emits a `false` value,
|
|
20
|
+
* the search will resume and all items will again be returned as added.
|
|
21
|
+
*/
|
|
22
|
+
asRemovedWhen?: Observable<Boolean>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface IObserveElementChange<T extends Element = Element> {
|
|
26
|
+
/** Element that satisfy the filtering condition. */
|
|
27
|
+
target?: T;
|
|
28
|
+
/** New element that have been added since the last emit. */
|
|
29
|
+
added?: T;
|
|
30
|
+
/** Element that have been removed since the last emit. */
|
|
31
|
+
removed?: T;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns change (addition and deletion) of element that match selectors, like an Rx stream.
|
|
36
|
+
*/
|
|
37
|
+
export function observeQuerySelector<T extends Element = Element>(
|
|
38
|
+
query: string,
|
|
39
|
+
options: IObserveQuerySelectorOptions = {},
|
|
40
|
+
): Observable<IObserveElementChange<T>> {
|
|
41
|
+
const { parent = document.documentElement, asRemovedWhen } = options;
|
|
42
|
+
let targetElement: T | undefined;
|
|
43
|
+
|
|
44
|
+
function checkChanges(): Observable<IObserveElementChange<T>> {
|
|
45
|
+
const querySelectedElements: NodeListOf<T> = parent.querySelectorAll<T>(query);
|
|
46
|
+
let filteredSelectedElement: T | undefined;
|
|
47
|
+
const changes: IObserveElementChange<T> = {};
|
|
48
|
+
|
|
49
|
+
for (const querySelectedElement of querySelectedElements) {
|
|
50
|
+
if (options.has && !querySelectedElement.querySelector(options.has)) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (options.filter && !options.filter(querySelectedElement)) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
filteredSelectedElement = querySelectedElement;
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (filteredSelectedElement === targetElement) {
|
|
62
|
+
return EMPTY;
|
|
63
|
+
}
|
|
64
|
+
if (targetElement) {
|
|
65
|
+
changes.removed = targetElement;
|
|
66
|
+
}
|
|
67
|
+
if (filteredSelectedElement) {
|
|
68
|
+
changes.added = filteredSelectedElement;
|
|
69
|
+
}
|
|
70
|
+
changes.target = filteredSelectedElement;
|
|
71
|
+
targetElement = filteredSelectedElement;
|
|
72
|
+
return of(changes);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const observeQuerySelector$ = concat(
|
|
76
|
+
defer(() => checkChanges()),
|
|
77
|
+
observeElementMutation(parent, {subtree: true, childList: true}).pipe(
|
|
78
|
+
throttleTime(0, undefined, {leading: true, trailing: true}),
|
|
79
|
+
mergeMap(checkChanges),
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
if (asRemovedWhen) {
|
|
84
|
+
const removedObserver$ = defer(() => {
|
|
85
|
+
if (!targetElement) {
|
|
86
|
+
return EMPTY;
|
|
87
|
+
}
|
|
88
|
+
const changes: IObserveElementChange<T> = {
|
|
89
|
+
removed: targetElement,
|
|
90
|
+
};
|
|
91
|
+
targetElement = undefined;
|
|
92
|
+
return of(changes);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const observeQuerySelectorWithRemovedWhen$ = asRemovedWhen.pipe(
|
|
96
|
+
distinctUntilChanged(),
|
|
97
|
+
switchMap(asRemoved => asRemoved ? removedObserver$ : observeQuerySelector$),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return observeQuerySelectorWithRemovedWhen$;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return observeQuerySelector$;
|
|
104
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Observable, Subscriber } from "rxjs";
|
|
2
|
+
import { distinctUntilChanged, shareReplay } from "rxjs/operators";
|
|
3
|
+
|
|
4
|
+
let pushStateSubscriber$: Subscriber<string> | undefined;
|
|
5
|
+
let isPushStateWasInjected = false;
|
|
6
|
+
|
|
7
|
+
function injectPushStateHandler(): void {
|
|
8
|
+
if (isPushStateWasInjected) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const pushState = history.pushState;
|
|
12
|
+
history.pushState = function (...args) {
|
|
13
|
+
pushState.apply(this, args);
|
|
14
|
+
pushStateSubscriber$?.next(location.href);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Emit new location url when the URL is changes
|
|
20
|
+
*/
|
|
21
|
+
export const urlChange$ = new Observable<string>(subscriber$ => {
|
|
22
|
+
function updateURL(): void {
|
|
23
|
+
subscriber$.next(location.href)
|
|
24
|
+
}
|
|
25
|
+
window.addEventListener('hashchange', updateURL);
|
|
26
|
+
window.addEventListener('popstate', updateURL);
|
|
27
|
+
pushStateSubscriber$ = subscriber$;
|
|
28
|
+
subscriber$.next(location.href);
|
|
29
|
+
injectPushStateHandler();
|
|
30
|
+
return () => {
|
|
31
|
+
pushStateSubscriber$ = undefined;
|
|
32
|
+
window.removeEventListener('hashchange', updateURL);
|
|
33
|
+
window.removeEventListener('popstate', updateURL);
|
|
34
|
+
}
|
|
35
|
+
}).pipe(
|
|
36
|
+
distinctUntilChanged(),
|
|
37
|
+
shareReplay(1),
|
|
38
|
+
);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MonoTypeOperatorFunction, pipe } from "rxjs";
|
|
2
|
+
import { tap } from "rxjs/operators";
|
|
3
|
+
|
|
4
|
+
function clickElementImmediately(element: Element): void {
|
|
5
|
+
element.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
6
|
+
console.log(`Click: `, element);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export function clickElement<T extends Element>(): MonoTypeOperatorFunction<T>;
|
|
11
|
+
export function clickElement<T extends Element>(element: T): void;
|
|
12
|
+
export function clickElement<T extends Element>(element?: T): MonoTypeOperatorFunction<T> | void {
|
|
13
|
+
if (element) {
|
|
14
|
+
clickElementImmediately(element);
|
|
15
|
+
} else {
|
|
16
|
+
return pipe(
|
|
17
|
+
tap((element: T) => clickElementImmediately(element)),
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { EMPTY, from, merge, ObservableInput, ObservedValueOf, OperatorFunction } from "rxjs";
|
|
2
|
+
import { connect, filter, mergeMap, takeUntil } from "rxjs/operators";
|
|
3
|
+
import { IObserveElementChange } from "../observable/observe-query-selector";
|
|
4
|
+
import { IObservedElementsChanges } from "../observable/observe-query-selector-all";
|
|
5
|
+
|
|
6
|
+
export interface IMapElementChangeOptions {
|
|
7
|
+
/**
|
|
8
|
+
* If the `isTakeUntilRemoved` parameter is equal to the `true` value,
|
|
9
|
+
* then each thread will be interrupted after the element is removed.
|
|
10
|
+
* Only the stream that belongs to the deleted element is interrupted.
|
|
11
|
+
*
|
|
12
|
+
* If the `isTakeUntilRemoved` parameter is equal to the `false` value,
|
|
13
|
+
* then the converted streams will not be interrupted.
|
|
14
|
+
*/
|
|
15
|
+
isTakeUntilRemoved?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function assuredArray<T>(values?: T | T[]): T[] {
|
|
19
|
+
if (values instanceof Array) {
|
|
20
|
+
return values
|
|
21
|
+
}
|
|
22
|
+
if (values) {
|
|
23
|
+
return [values];
|
|
24
|
+
}
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Conversion operator to a new stream for each new added element */
|
|
29
|
+
export function mergeMapAddedElements<T extends Element, O extends ObservableInput<any>>(
|
|
30
|
+
project: (element: T) => O,
|
|
31
|
+
options?: IMapElementChangeOptions,
|
|
32
|
+
): OperatorFunction<
|
|
33
|
+
IObservedElementsChanges<T> | IObserveElementChange<T>,
|
|
34
|
+
ObservedValueOf<O>
|
|
35
|
+
> {
|
|
36
|
+
if (!options?.isTakeUntilRemoved) {
|
|
37
|
+
return source$ => source$.pipe(
|
|
38
|
+
mergeMap(changes => {
|
|
39
|
+
let added = assuredArray(changes.added);
|
|
40
|
+
if (added.length === 0) {
|
|
41
|
+
return EMPTY;
|
|
42
|
+
}
|
|
43
|
+
let addedObservers = added.map(project);
|
|
44
|
+
return merge(...addedObservers);
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return source$ => source$.pipe(
|
|
49
|
+
connect(connectedSource$ => connectedSource$.pipe(
|
|
50
|
+
mergeMap(changes => {
|
|
51
|
+
let added = assuredArray(changes.added);
|
|
52
|
+
if (added.length === 0) {
|
|
53
|
+
return EMPTY;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let addedObservers = added.map(addedElement =>
|
|
57
|
+
from(
|
|
58
|
+
project(addedElement)
|
|
59
|
+
).pipe(
|
|
60
|
+
takeUntil(
|
|
61
|
+
connectedSource$.pipe(
|
|
62
|
+
filter(changes => {
|
|
63
|
+
if (changes.removed instanceof Array) {
|
|
64
|
+
return changes.removed.indexOf(addedElement) != -1;
|
|
65
|
+
} else {
|
|
66
|
+
return changes.removed === addedElement;
|
|
67
|
+
}
|
|
68
|
+
}),
|
|
69
|
+
),
|
|
70
|
+
),
|
|
71
|
+
) as O,
|
|
72
|
+
);
|
|
73
|
+
return merge(...addedObservers);
|
|
74
|
+
})
|
|
75
|
+
)),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { EMPTY, from, Observable, ObservableInput, ObservedValueOf, OperatorFunction, pipe } from "rxjs";
|
|
2
|
+
import { distinctUntilChanged, map, mergeMap, switchMap } from "rxjs/operators";
|
|
3
|
+
|
|
4
|
+
export interface IMergeMapStringToggleOptions {
|
|
5
|
+
/**
|
|
6
|
+
* If the `isTakeUntilToggle` parameter is equal to the `true` value,
|
|
7
|
+
* the stream will be interrupted as soon as the source string fails validation.
|
|
8
|
+
*
|
|
9
|
+
* If the `isTakeUntilToggle` parameter is equal to the `false` value, the stream will never be interrupted.
|
|
10
|
+
*/
|
|
11
|
+
isTakeUntilToggle?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The operator creates a separate stream when the source string is validated. */
|
|
15
|
+
export function mergeMapStringToggle<O extends ObservableInput<any>>(
|
|
16
|
+
condition: RegExp | ((url: string) => boolean),
|
|
17
|
+
project: (() => O) | O,
|
|
18
|
+
options?: IMergeMapStringToggleOptions,
|
|
19
|
+
): OperatorFunction<string, ObservedValueOf<O>> {
|
|
20
|
+
const mapConditionFn = typeof condition === 'function' ? condition : (url: string) => condition.test(url);
|
|
21
|
+
let urlMatchToggler: (isUrlMatch: Boolean) => Observable<ObservedValueOf<O>>;
|
|
22
|
+
if (typeof project === 'function') {
|
|
23
|
+
urlMatchToggler = (isUrlMatch: Boolean) => isUrlMatch ? from(project()) : EMPTY
|
|
24
|
+
} else {
|
|
25
|
+
urlMatchToggler = (isUrlMatch: Boolean) => isUrlMatch ? from(project) : EMPTY
|
|
26
|
+
}
|
|
27
|
+
const mergeOperator = options?.isTakeUntilToggle
|
|
28
|
+
? switchMap(urlMatchToggler)
|
|
29
|
+
: mergeMap(urlMatchToggler);
|
|
30
|
+
|
|
31
|
+
return pipe(
|
|
32
|
+
map(mapConditionFn),
|
|
33
|
+
distinctUntilChanged(),
|
|
34
|
+
mergeOperator
|
|
35
|
+
)
|
|
36
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { MonoTypeOperatorFunction, pipe } from "rxjs";
|
|
2
|
+
import { tap } from "rxjs/operators";
|
|
3
|
+
|
|
4
|
+
export function removeElement<T extends Element>(): MonoTypeOperatorFunction<T> {
|
|
5
|
+
return pipe(tap((element) => {
|
|
6
|
+
element.remove();
|
|
7
|
+
console.log(`Remove: `, element);
|
|
8
|
+
}));
|
|
9
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MonoTypeOperatorFunction, pipe } from "rxjs";
|
|
2
|
+
import { tap } from "rxjs/operators";
|
|
3
|
+
|
|
4
|
+
function setInputValueImmediately(element: HTMLInputElement, value: string): void {
|
|
5
|
+
element.value = value;
|
|
6
|
+
element.focus();
|
|
7
|
+
element.dispatchEvent(new Event('input', <EventInit>{ target: element }));
|
|
8
|
+
console.log(`Set value: `, { element, value });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function setInputValue(value: string): MonoTypeOperatorFunction<HTMLInputElement>;
|
|
12
|
+
export function setInputValue(element: HTMLInputElement, value: string): void;
|
|
13
|
+
export function setInputValue(
|
|
14
|
+
elementOrValue: HTMLInputElement | string, value?: string
|
|
15
|
+
): MonoTypeOperatorFunction<HTMLInputElement> | void {
|
|
16
|
+
if (typeof elementOrValue === 'string') {
|
|
17
|
+
return pipe(
|
|
18
|
+
tap((element: HTMLInputElement) => setInputValueImmediately(element, elementOrValue))
|
|
19
|
+
);
|
|
20
|
+
} else {
|
|
21
|
+
setInputValueImmediately(elementOrValue, value!);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type RecursivelyFindMather = (propertyName: string, value: unknown, path: string) => boolean;
|
|
2
|
+
|
|
3
|
+
export function findRecursively(obj: unknown, mather: RecursivelyFindMather): Record<string, unknown> | null {
|
|
4
|
+
const seen = new Set<unknown>();
|
|
5
|
+
const searched: Record<string, unknown> = {};
|
|
6
|
+
const needFind = new Set<[path: string | undefined, value: unknown]>([[undefined, obj]]);
|
|
7
|
+
|
|
8
|
+
function addToCheck(path: string, value: unknown) {
|
|
9
|
+
if (seen.has(value) ) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
needFind.add([path, value]);
|
|
13
|
+
seen.add(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
while (true) {
|
|
17
|
+
const iterator = needFind.values().next();
|
|
18
|
+
if (iterator.done) {
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
const path = iterator.value[0];
|
|
22
|
+
const iteratedObj = iterator.value[1];
|
|
23
|
+
needFind.delete(iterator.value);
|
|
24
|
+
|
|
25
|
+
if (!iteratedObj) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (iteratedObj instanceof Array) {
|
|
29
|
+
for (const [index, value] of (iteratedObj as unknown[]).entries()) {
|
|
30
|
+
if (value )
|
|
31
|
+
addToCheck(`${path || ''}[${index}]`, value);
|
|
32
|
+
}
|
|
33
|
+
} else if (typeof iteratedObj === "object") {
|
|
34
|
+
for (const key of Object.keys(iteratedObj as object)) {
|
|
35
|
+
const value: unknown = (iteratedObj as any)[key];
|
|
36
|
+
const keyPath = path ? `${path}.${key}` : key;
|
|
37
|
+
if (mather(key, value, keyPath)) {
|
|
38
|
+
searched[keyPath] = value;
|
|
39
|
+
}
|
|
40
|
+
addToCheck(keyPath, value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (Object.keys(searched).length === 0) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return searched;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function findRecursivelyPropertyName(obj: unknown, propertyName: string): Record<string, unknown> | null {
|
|
52
|
+
return findRecursively(obj, matchedPropertyName => matchedPropertyName === propertyName);
|
|
53
|
+
}
|