@lerx/promise-modal 0.9.1 → 0.10.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/README.md
CHANGED
|
@@ -35,10 +35,6 @@ npm install @lerx/promise-modal
|
|
|
35
35
|
|
|
36
36
|
## Compatibility
|
|
37
37
|
|
|
38
|
-
`@lerx/promise-modal` is built with ECMAScript 2022 (ES2022) syntax.
|
|
39
|
-
|
|
40
|
-
If you're using a JavaScript environment that doesn't support ES2022, you'll need to include this package in your transpilation process.
|
|
41
|
-
|
|
42
38
|
**Supported environments:**
|
|
43
39
|
|
|
44
40
|
- Node.js 16.11.0 or later
|
|
@@ -2,15 +2,22 @@ import type { Fn } from '../@aileron/declare';
|
|
|
2
2
|
import type { ModalNode } from '../core';
|
|
3
3
|
import type { Modal } from '../types';
|
|
4
4
|
export declare class ModalManager {
|
|
5
|
-
|
|
5
|
+
private static __anchor__;
|
|
6
|
+
private static __scope__;
|
|
7
|
+
private static __hash__;
|
|
8
|
+
private static __styleManager__;
|
|
9
|
+
private static __styleSheetDefinition__;
|
|
6
10
|
static anchor(options?: {
|
|
7
11
|
tag?: string;
|
|
8
12
|
prefix?: string;
|
|
9
13
|
root?: HTMLElement;
|
|
10
14
|
}): HTMLElement;
|
|
11
15
|
static get anchored(): boolean;
|
|
16
|
+
private static __prerenderList__;
|
|
12
17
|
static get prerender(): Modal[];
|
|
18
|
+
private static __openHandler__;
|
|
13
19
|
static set openHandler(handler: Fn<[Modal], ModalNode>);
|
|
20
|
+
private static __refreshHandler__?;
|
|
14
21
|
static set refreshHandler(handler: Fn<[], void>);
|
|
15
22
|
static refresh(): void;
|
|
16
23
|
static defineStyleSheet(styleId: string, css: string): void;
|
|
@@ -3,7 +3,6 @@ import type { Fn } from '../../../@aileron/declare';
|
|
|
3
3
|
import type { BackgroundComponent, BaseModal, ForegroundComponent, ManagedEntity, ModalBackground } from '../../../types';
|
|
4
4
|
type AbstractNodeProps<T, B> = BaseModal<T, B> & ManagedEntity;
|
|
5
5
|
export declare abstract class AbstractNode<T, B> {
|
|
6
|
-
#private;
|
|
7
6
|
readonly id: number;
|
|
8
7
|
readonly group?: string;
|
|
9
8
|
readonly initiator: string;
|
|
@@ -16,9 +15,13 @@ export declare abstract class AbstractNode<T, B> {
|
|
|
16
15
|
readonly closeOnBackdropClick: boolean;
|
|
17
16
|
readonly ForegroundComponent?: ForegroundComponent;
|
|
18
17
|
readonly BackgroundComponent?: BackgroundComponent;
|
|
18
|
+
private __alive__;
|
|
19
19
|
get alive(): boolean;
|
|
20
|
+
private __visible__;
|
|
20
21
|
get visible(): boolean;
|
|
22
|
+
private __handleResolve__?;
|
|
21
23
|
set handleResolve(handleResolve: Fn<[result: T | null]>);
|
|
24
|
+
private __listeners__;
|
|
22
25
|
constructor({ id, initiator, group, title, subtitle, background, dimmed, duration, manualDestroy, closeOnBackdropClick, handleResolve, ForegroundComponent, BackgroundComponent, }: AbstractNodeProps<T, B>);
|
|
23
26
|
abstract onClose(): void;
|
|
24
27
|
abstract onConfirm(): void;
|
|
@@ -4,7 +4,6 @@ import type { FooterOptions, ManagedEntity, PromptContentProps, PromptFooterRend
|
|
|
4
4
|
import { AbstractNode } from './AbstractNode';
|
|
5
5
|
type PromptNodeProps<T, B> = PromptModal<T, B> & ManagedEntity;
|
|
6
6
|
export declare class PromptNode<T = any, B = any> extends AbstractNode<T, B> {
|
|
7
|
-
#private;
|
|
8
7
|
readonly type: 'prompt';
|
|
9
8
|
readonly content?: ReactNode | ComponentType<PromptContentProps>;
|
|
10
9
|
readonly defaultValue: T | undefined;
|
|
@@ -12,6 +11,7 @@ export declare class PromptNode<T = any, B = any> extends AbstractNode<T, B> {
|
|
|
12
11
|
readonly disabled?: Fn<[value: T], boolean>;
|
|
13
12
|
readonly returnOnCancel?: boolean;
|
|
14
13
|
readonly footer?: PromptFooterRender<T> | FooterOptions | false;
|
|
14
|
+
private __value__;
|
|
15
15
|
constructor({ id, group, initiator, type, title, subtitle, content, defaultValue, Input, disabled, returnOnCancel, footer, background, dimmed, duration, manualDestroy, closeOnBackdropClick, handleResolve, ForegroundComponent, BackgroundComponent, }: PromptNodeProps<T, B>);
|
|
16
16
|
onChange(value: T): void;
|
|
17
17
|
onConfirm(): void;
|
package/dist/index.cjs
CHANGED
|
@@ -15,66 +15,65 @@ const hoc = require('@winglet/react-utils/hoc');
|
|
|
15
15
|
const render = require('@winglet/react-utils/render');
|
|
16
16
|
|
|
17
17
|
class ModalManager {
|
|
18
|
-
static #anchor = null;
|
|
19
|
-
static #scope = `promise-modal-${lib.getRandomString(36)}`;
|
|
20
|
-
static #hash = hash.polynomialHash(ModalManager.#scope);
|
|
21
|
-
static #styleManager = styleManager.styleManagerFactory(ModalManager.#scope);
|
|
22
|
-
static #styleSheetDefinition = new Map();
|
|
23
18
|
static anchor(options) {
|
|
24
|
-
if (ModalManager
|
|
25
|
-
return ModalManager
|
|
19
|
+
if (ModalManager.__anchor__ !== null)
|
|
20
|
+
return ModalManager.__anchor__;
|
|
26
21
|
const { tag = 'div', prefix = 'promise-modal', root = document.body, } = options || {};
|
|
27
22
|
const node = document.createElement(tag);
|
|
28
23
|
node.id = `${prefix}-${lib.getRandomString(36)}`;
|
|
29
|
-
node.className = ModalManager
|
|
24
|
+
node.className = ModalManager.__scope__;
|
|
30
25
|
root.appendChild(node);
|
|
31
|
-
ModalManager
|
|
26
|
+
ModalManager.__anchor__ = node;
|
|
32
27
|
return node;
|
|
33
28
|
}
|
|
34
29
|
static get anchored() {
|
|
35
|
-
return ModalManager
|
|
30
|
+
return ModalManager.__anchor__ !== null;
|
|
36
31
|
}
|
|
37
|
-
static #prerenderList = [];
|
|
38
32
|
static get prerender() {
|
|
39
|
-
return ModalManager
|
|
33
|
+
return ModalManager.__prerenderList__;
|
|
40
34
|
}
|
|
41
|
-
static #openHandler = ((modal) => {
|
|
42
|
-
ModalManager.#prerenderList.push(modal);
|
|
43
|
-
});
|
|
44
35
|
static set openHandler(handler) {
|
|
45
|
-
ModalManager
|
|
46
|
-
ModalManager
|
|
36
|
+
ModalManager.__openHandler__ = handler;
|
|
37
|
+
ModalManager.__prerenderList__ = [];
|
|
47
38
|
}
|
|
48
|
-
static #refreshHandler;
|
|
49
39
|
static set refreshHandler(handler) {
|
|
50
|
-
ModalManager
|
|
40
|
+
ModalManager.__refreshHandler__ = handler;
|
|
51
41
|
}
|
|
52
42
|
static refresh() {
|
|
53
|
-
ModalManager
|
|
43
|
+
ModalManager.__refreshHandler__?.();
|
|
54
44
|
}
|
|
55
45
|
static defineStyleSheet(styleId, css) {
|
|
56
|
-
ModalManager
|
|
46
|
+
ModalManager.__styleSheetDefinition__.set(styleId, util.compressCss(css));
|
|
57
47
|
}
|
|
58
48
|
static applyStyleSheet() {
|
|
59
|
-
for (const [styleId, css] of ModalManager
|
|
60
|
-
ModalManager
|
|
49
|
+
for (const [styleId, css] of ModalManager.__styleSheetDefinition__)
|
|
50
|
+
ModalManager.__styleManager__(styleId, css, true);
|
|
61
51
|
}
|
|
62
52
|
static getHashedClassNames(styleId) {
|
|
63
|
-
return `${styleId}-${ModalManager
|
|
53
|
+
return `${styleId}-${ModalManager.__hash__}`;
|
|
64
54
|
}
|
|
65
55
|
static reset() {
|
|
66
|
-
ModalManager
|
|
67
|
-
ModalManager
|
|
68
|
-
ModalManager
|
|
69
|
-
ModalManager
|
|
56
|
+
ModalManager.__anchor__ = null;
|
|
57
|
+
ModalManager.__prerenderList__ = [];
|
|
58
|
+
ModalManager.__openHandler__ = ((modal) => {
|
|
59
|
+
ModalManager.__prerenderList__.push(modal);
|
|
70
60
|
});
|
|
71
|
-
ModalManager
|
|
72
|
-
styleManager.destroyScope(ModalManager
|
|
61
|
+
ModalManager.__refreshHandler__ = undefined;
|
|
62
|
+
styleManager.destroyScope(ModalManager.__scope__);
|
|
73
63
|
}
|
|
74
64
|
static open(modal) {
|
|
75
|
-
return ModalManager
|
|
65
|
+
return ModalManager.__openHandler__(modal);
|
|
76
66
|
}
|
|
77
67
|
}
|
|
68
|
+
ModalManager.__anchor__ = null;
|
|
69
|
+
ModalManager.__scope__ = `promise-modal-${lib.getRandomString(36)}`;
|
|
70
|
+
ModalManager.__hash__ = hash.polynomialHash(ModalManager.__scope__);
|
|
71
|
+
ModalManager.__styleManager__ = styleManager.styleManagerFactory(ModalManager.__scope__);
|
|
72
|
+
ModalManager.__styleSheetDefinition__ = new Map();
|
|
73
|
+
ModalManager.__prerenderList__ = [];
|
|
74
|
+
ModalManager.__openHandler__ = ((modal) => {
|
|
75
|
+
ModalManager.__prerenderList__.push(modal);
|
|
76
|
+
});
|
|
78
77
|
|
|
79
78
|
const closeModal = (modalNode, refresh = true) => {
|
|
80
79
|
if (modalNode.visible === false)
|
|
@@ -173,32 +172,17 @@ const confirm = (args) => confirmHandler(args).promiseHandler;
|
|
|
173
172
|
const prompt = (args) => promptHandler(args).promiseHandler;
|
|
174
173
|
|
|
175
174
|
class AbstractNode {
|
|
176
|
-
id;
|
|
177
|
-
group;
|
|
178
|
-
initiator;
|
|
179
|
-
title;
|
|
180
|
-
subtitle;
|
|
181
|
-
background;
|
|
182
|
-
dimmed;
|
|
183
|
-
duration;
|
|
184
|
-
manualDestroy;
|
|
185
|
-
closeOnBackdropClick;
|
|
186
|
-
ForegroundComponent;
|
|
187
|
-
BackgroundComponent;
|
|
188
|
-
#alive;
|
|
189
175
|
get alive() {
|
|
190
|
-
return this
|
|
176
|
+
return this.__alive__;
|
|
191
177
|
}
|
|
192
|
-
#visible;
|
|
193
178
|
get visible() {
|
|
194
|
-
return this
|
|
179
|
+
return this.__visible__;
|
|
195
180
|
}
|
|
196
|
-
#handleResolve;
|
|
197
181
|
set handleResolve(handleResolve) {
|
|
198
|
-
this
|
|
182
|
+
this.__handleResolve__ = handleResolve;
|
|
199
183
|
}
|
|
200
|
-
#listeners = new Set();
|
|
201
184
|
constructor({ id, initiator, group, title, subtitle, background, dimmed = true, duration = 0, manualDestroy = false, closeOnBackdropClick = true, handleResolve, ForegroundComponent, BackgroundComponent, }) {
|
|
185
|
+
this.__listeners__ = new Set();
|
|
202
186
|
this.id = id;
|
|
203
187
|
this.group = group;
|
|
204
188
|
this.initiator = initiator;
|
|
@@ -211,48 +195,44 @@ class AbstractNode {
|
|
|
211
195
|
this.closeOnBackdropClick = closeOnBackdropClick;
|
|
212
196
|
this.ForegroundComponent = ForegroundComponent;
|
|
213
197
|
this.BackgroundComponent = BackgroundComponent;
|
|
214
|
-
this
|
|
215
|
-
this
|
|
216
|
-
this
|
|
198
|
+
this.__alive__ = true;
|
|
199
|
+
this.__visible__ = true;
|
|
200
|
+
this.__handleResolve__ = handleResolve;
|
|
217
201
|
}
|
|
218
202
|
onResolve(result) {
|
|
219
|
-
this
|
|
203
|
+
this.__handleResolve__?.(result);
|
|
220
204
|
}
|
|
221
205
|
subscribe(listener) {
|
|
222
|
-
this
|
|
206
|
+
this.__listeners__.add(listener);
|
|
223
207
|
return () => {
|
|
224
|
-
this
|
|
208
|
+
this.__listeners__.delete(listener);
|
|
225
209
|
};
|
|
226
210
|
}
|
|
227
211
|
publish() {
|
|
228
|
-
for (const listener of this
|
|
212
|
+
for (const listener of this.__listeners__)
|
|
229
213
|
listener();
|
|
230
214
|
}
|
|
231
215
|
onDestroy() {
|
|
232
|
-
const needPublish = this
|
|
233
|
-
this
|
|
216
|
+
const needPublish = this.__alive__ === true;
|
|
217
|
+
this.__alive__ = false;
|
|
234
218
|
if (this.manualDestroy && needPublish)
|
|
235
219
|
this.publish();
|
|
236
220
|
}
|
|
237
221
|
onShow() {
|
|
238
|
-
const needPublish = this
|
|
239
|
-
this
|
|
222
|
+
const needPublish = this.__visible__ === false;
|
|
223
|
+
this.__visible__ = true;
|
|
240
224
|
if (needPublish)
|
|
241
225
|
this.publish();
|
|
242
226
|
}
|
|
243
227
|
onHide() {
|
|
244
|
-
const needPublish = this
|
|
245
|
-
this
|
|
228
|
+
const needPublish = this.__visible__ === true;
|
|
229
|
+
this.__visible__ = false;
|
|
246
230
|
if (needPublish)
|
|
247
231
|
this.publish();
|
|
248
232
|
}
|
|
249
233
|
}
|
|
250
234
|
|
|
251
235
|
class AlertNode extends AbstractNode {
|
|
252
|
-
type;
|
|
253
|
-
subtype;
|
|
254
|
-
content;
|
|
255
|
-
footer;
|
|
256
236
|
constructor({ id, group, initiator, type, subtype, title, subtitle, content, footer, background, dimmed, duration, manualDestroy, closeOnBackdropClick, handleResolve, ForegroundComponent, BackgroundComponent, }) {
|
|
257
237
|
super({
|
|
258
238
|
id,
|
|
@@ -283,10 +263,6 @@ class AlertNode extends AbstractNode {
|
|
|
283
263
|
}
|
|
284
264
|
|
|
285
265
|
class ConfirmNode extends AbstractNode {
|
|
286
|
-
type;
|
|
287
|
-
subtype;
|
|
288
|
-
content;
|
|
289
|
-
footer;
|
|
290
266
|
constructor({ id, group, initiator, type, subtype, title, subtitle, content, footer, background, dimmed, duration, manualDestroy, closeOnBackdropClick, handleResolve, ForegroundComponent, BackgroundComponent, }) {
|
|
291
267
|
super({
|
|
292
268
|
id,
|
|
@@ -317,14 +293,6 @@ class ConfirmNode extends AbstractNode {
|
|
|
317
293
|
}
|
|
318
294
|
|
|
319
295
|
class PromptNode extends AbstractNode {
|
|
320
|
-
type;
|
|
321
|
-
content;
|
|
322
|
-
defaultValue;
|
|
323
|
-
Input;
|
|
324
|
-
disabled;
|
|
325
|
-
returnOnCancel;
|
|
326
|
-
footer;
|
|
327
|
-
#value;
|
|
328
296
|
constructor({ id, group, initiator, type, title, subtitle, content, defaultValue, Input, disabled, returnOnCancel, footer, background, dimmed, duration, manualDestroy, closeOnBackdropClick, handleResolve, ForegroundComponent, BackgroundComponent, }) {
|
|
329
297
|
super({
|
|
330
298
|
id,
|
|
@@ -345,20 +313,20 @@ class PromptNode extends AbstractNode {
|
|
|
345
313
|
this.content = content;
|
|
346
314
|
this.Input = Input;
|
|
347
315
|
this.defaultValue = defaultValue;
|
|
348
|
-
this
|
|
316
|
+
this.__value__ = defaultValue;
|
|
349
317
|
this.disabled = disabled;
|
|
350
318
|
this.returnOnCancel = returnOnCancel;
|
|
351
319
|
this.footer = footer;
|
|
352
320
|
}
|
|
353
321
|
onChange(value) {
|
|
354
|
-
this
|
|
322
|
+
this.__value__ = value;
|
|
355
323
|
}
|
|
356
324
|
onConfirm() {
|
|
357
|
-
this.onResolve(this
|
|
325
|
+
this.onResolve(this.__value__ ?? null);
|
|
358
326
|
}
|
|
359
327
|
onClose() {
|
|
360
328
|
if (this.returnOnCancel)
|
|
361
|
-
this.onResolve(this
|
|
329
|
+
this.onResolve(this.__value__ ?? null);
|
|
362
330
|
else
|
|
363
331
|
this.onResolve(null);
|
|
364
332
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -13,66 +13,65 @@ import { withErrorBoundary } from '@winglet/react-utils/hoc';
|
|
|
13
13
|
import { renderComponent } from '@winglet/react-utils/render';
|
|
14
14
|
|
|
15
15
|
class ModalManager {
|
|
16
|
-
static #anchor = null;
|
|
17
|
-
static #scope = `promise-modal-${getRandomString(36)}`;
|
|
18
|
-
static #hash = polynomialHash(ModalManager.#scope);
|
|
19
|
-
static #styleManager = styleManagerFactory(ModalManager.#scope);
|
|
20
|
-
static #styleSheetDefinition = new Map();
|
|
21
16
|
static anchor(options) {
|
|
22
|
-
if (ModalManager
|
|
23
|
-
return ModalManager
|
|
17
|
+
if (ModalManager.__anchor__ !== null)
|
|
18
|
+
return ModalManager.__anchor__;
|
|
24
19
|
const { tag = 'div', prefix = 'promise-modal', root = document.body, } = options || {};
|
|
25
20
|
const node = document.createElement(tag);
|
|
26
21
|
node.id = `${prefix}-${getRandomString(36)}`;
|
|
27
|
-
node.className = ModalManager
|
|
22
|
+
node.className = ModalManager.__scope__;
|
|
28
23
|
root.appendChild(node);
|
|
29
|
-
ModalManager
|
|
24
|
+
ModalManager.__anchor__ = node;
|
|
30
25
|
return node;
|
|
31
26
|
}
|
|
32
27
|
static get anchored() {
|
|
33
|
-
return ModalManager
|
|
28
|
+
return ModalManager.__anchor__ !== null;
|
|
34
29
|
}
|
|
35
|
-
static #prerenderList = [];
|
|
36
30
|
static get prerender() {
|
|
37
|
-
return ModalManager
|
|
31
|
+
return ModalManager.__prerenderList__;
|
|
38
32
|
}
|
|
39
|
-
static #openHandler = ((modal) => {
|
|
40
|
-
ModalManager.#prerenderList.push(modal);
|
|
41
|
-
});
|
|
42
33
|
static set openHandler(handler) {
|
|
43
|
-
ModalManager
|
|
44
|
-
ModalManager
|
|
34
|
+
ModalManager.__openHandler__ = handler;
|
|
35
|
+
ModalManager.__prerenderList__ = [];
|
|
45
36
|
}
|
|
46
|
-
static #refreshHandler;
|
|
47
37
|
static set refreshHandler(handler) {
|
|
48
|
-
ModalManager
|
|
38
|
+
ModalManager.__refreshHandler__ = handler;
|
|
49
39
|
}
|
|
50
40
|
static refresh() {
|
|
51
|
-
ModalManager
|
|
41
|
+
ModalManager.__refreshHandler__?.();
|
|
52
42
|
}
|
|
53
43
|
static defineStyleSheet(styleId, css) {
|
|
54
|
-
ModalManager
|
|
44
|
+
ModalManager.__styleSheetDefinition__.set(styleId, compressCss(css));
|
|
55
45
|
}
|
|
56
46
|
static applyStyleSheet() {
|
|
57
|
-
for (const [styleId, css] of ModalManager
|
|
58
|
-
ModalManager
|
|
47
|
+
for (const [styleId, css] of ModalManager.__styleSheetDefinition__)
|
|
48
|
+
ModalManager.__styleManager__(styleId, css, true);
|
|
59
49
|
}
|
|
60
50
|
static getHashedClassNames(styleId) {
|
|
61
|
-
return `${styleId}-${ModalManager
|
|
51
|
+
return `${styleId}-${ModalManager.__hash__}`;
|
|
62
52
|
}
|
|
63
53
|
static reset() {
|
|
64
|
-
ModalManager
|
|
65
|
-
ModalManager
|
|
66
|
-
ModalManager
|
|
67
|
-
ModalManager
|
|
54
|
+
ModalManager.__anchor__ = null;
|
|
55
|
+
ModalManager.__prerenderList__ = [];
|
|
56
|
+
ModalManager.__openHandler__ = ((modal) => {
|
|
57
|
+
ModalManager.__prerenderList__.push(modal);
|
|
68
58
|
});
|
|
69
|
-
ModalManager
|
|
70
|
-
destroyScope(ModalManager
|
|
59
|
+
ModalManager.__refreshHandler__ = undefined;
|
|
60
|
+
destroyScope(ModalManager.__scope__);
|
|
71
61
|
}
|
|
72
62
|
static open(modal) {
|
|
73
|
-
return ModalManager
|
|
63
|
+
return ModalManager.__openHandler__(modal);
|
|
74
64
|
}
|
|
75
65
|
}
|
|
66
|
+
ModalManager.__anchor__ = null;
|
|
67
|
+
ModalManager.__scope__ = `promise-modal-${getRandomString(36)}`;
|
|
68
|
+
ModalManager.__hash__ = polynomialHash(ModalManager.__scope__);
|
|
69
|
+
ModalManager.__styleManager__ = styleManagerFactory(ModalManager.__scope__);
|
|
70
|
+
ModalManager.__styleSheetDefinition__ = new Map();
|
|
71
|
+
ModalManager.__prerenderList__ = [];
|
|
72
|
+
ModalManager.__openHandler__ = ((modal) => {
|
|
73
|
+
ModalManager.__prerenderList__.push(modal);
|
|
74
|
+
});
|
|
76
75
|
|
|
77
76
|
const closeModal = (modalNode, refresh = true) => {
|
|
78
77
|
if (modalNode.visible === false)
|
|
@@ -171,32 +170,17 @@ const confirm = (args) => confirmHandler(args).promiseHandler;
|
|
|
171
170
|
const prompt = (args) => promptHandler(args).promiseHandler;
|
|
172
171
|
|
|
173
172
|
class AbstractNode {
|
|
174
|
-
id;
|
|
175
|
-
group;
|
|
176
|
-
initiator;
|
|
177
|
-
title;
|
|
178
|
-
subtitle;
|
|
179
|
-
background;
|
|
180
|
-
dimmed;
|
|
181
|
-
duration;
|
|
182
|
-
manualDestroy;
|
|
183
|
-
closeOnBackdropClick;
|
|
184
|
-
ForegroundComponent;
|
|
185
|
-
BackgroundComponent;
|
|
186
|
-
#alive;
|
|
187
173
|
get alive() {
|
|
188
|
-
return this
|
|
174
|
+
return this.__alive__;
|
|
189
175
|
}
|
|
190
|
-
#visible;
|
|
191
176
|
get visible() {
|
|
192
|
-
return this
|
|
177
|
+
return this.__visible__;
|
|
193
178
|
}
|
|
194
|
-
#handleResolve;
|
|
195
179
|
set handleResolve(handleResolve) {
|
|
196
|
-
this
|
|
180
|
+
this.__handleResolve__ = handleResolve;
|
|
197
181
|
}
|
|
198
|
-
#listeners = new Set();
|
|
199
182
|
constructor({ id, initiator, group, title, subtitle, background, dimmed = true, duration = 0, manualDestroy = false, closeOnBackdropClick = true, handleResolve, ForegroundComponent, BackgroundComponent, }) {
|
|
183
|
+
this.__listeners__ = new Set();
|
|
200
184
|
this.id = id;
|
|
201
185
|
this.group = group;
|
|
202
186
|
this.initiator = initiator;
|
|
@@ -209,48 +193,44 @@ class AbstractNode {
|
|
|
209
193
|
this.closeOnBackdropClick = closeOnBackdropClick;
|
|
210
194
|
this.ForegroundComponent = ForegroundComponent;
|
|
211
195
|
this.BackgroundComponent = BackgroundComponent;
|
|
212
|
-
this
|
|
213
|
-
this
|
|
214
|
-
this
|
|
196
|
+
this.__alive__ = true;
|
|
197
|
+
this.__visible__ = true;
|
|
198
|
+
this.__handleResolve__ = handleResolve;
|
|
215
199
|
}
|
|
216
200
|
onResolve(result) {
|
|
217
|
-
this
|
|
201
|
+
this.__handleResolve__?.(result);
|
|
218
202
|
}
|
|
219
203
|
subscribe(listener) {
|
|
220
|
-
this
|
|
204
|
+
this.__listeners__.add(listener);
|
|
221
205
|
return () => {
|
|
222
|
-
this
|
|
206
|
+
this.__listeners__.delete(listener);
|
|
223
207
|
};
|
|
224
208
|
}
|
|
225
209
|
publish() {
|
|
226
|
-
for (const listener of this
|
|
210
|
+
for (const listener of this.__listeners__)
|
|
227
211
|
listener();
|
|
228
212
|
}
|
|
229
213
|
onDestroy() {
|
|
230
|
-
const needPublish = this
|
|
231
|
-
this
|
|
214
|
+
const needPublish = this.__alive__ === true;
|
|
215
|
+
this.__alive__ = false;
|
|
232
216
|
if (this.manualDestroy && needPublish)
|
|
233
217
|
this.publish();
|
|
234
218
|
}
|
|
235
219
|
onShow() {
|
|
236
|
-
const needPublish = this
|
|
237
|
-
this
|
|
220
|
+
const needPublish = this.__visible__ === false;
|
|
221
|
+
this.__visible__ = true;
|
|
238
222
|
if (needPublish)
|
|
239
223
|
this.publish();
|
|
240
224
|
}
|
|
241
225
|
onHide() {
|
|
242
|
-
const needPublish = this
|
|
243
|
-
this
|
|
226
|
+
const needPublish = this.__visible__ === true;
|
|
227
|
+
this.__visible__ = false;
|
|
244
228
|
if (needPublish)
|
|
245
229
|
this.publish();
|
|
246
230
|
}
|
|
247
231
|
}
|
|
248
232
|
|
|
249
233
|
class AlertNode extends AbstractNode {
|
|
250
|
-
type;
|
|
251
|
-
subtype;
|
|
252
|
-
content;
|
|
253
|
-
footer;
|
|
254
234
|
constructor({ id, group, initiator, type, subtype, title, subtitle, content, footer, background, dimmed, duration, manualDestroy, closeOnBackdropClick, handleResolve, ForegroundComponent, BackgroundComponent, }) {
|
|
255
235
|
super({
|
|
256
236
|
id,
|
|
@@ -281,10 +261,6 @@ class AlertNode extends AbstractNode {
|
|
|
281
261
|
}
|
|
282
262
|
|
|
283
263
|
class ConfirmNode extends AbstractNode {
|
|
284
|
-
type;
|
|
285
|
-
subtype;
|
|
286
|
-
content;
|
|
287
|
-
footer;
|
|
288
264
|
constructor({ id, group, initiator, type, subtype, title, subtitle, content, footer, background, dimmed, duration, manualDestroy, closeOnBackdropClick, handleResolve, ForegroundComponent, BackgroundComponent, }) {
|
|
289
265
|
super({
|
|
290
266
|
id,
|
|
@@ -315,14 +291,6 @@ class ConfirmNode extends AbstractNode {
|
|
|
315
291
|
}
|
|
316
292
|
|
|
317
293
|
class PromptNode extends AbstractNode {
|
|
318
|
-
type;
|
|
319
|
-
content;
|
|
320
|
-
defaultValue;
|
|
321
|
-
Input;
|
|
322
|
-
disabled;
|
|
323
|
-
returnOnCancel;
|
|
324
|
-
footer;
|
|
325
|
-
#value;
|
|
326
294
|
constructor({ id, group, initiator, type, title, subtitle, content, defaultValue, Input, disabled, returnOnCancel, footer, background, dimmed, duration, manualDestroy, closeOnBackdropClick, handleResolve, ForegroundComponent, BackgroundComponent, }) {
|
|
327
295
|
super({
|
|
328
296
|
id,
|
|
@@ -343,20 +311,20 @@ class PromptNode extends AbstractNode {
|
|
|
343
311
|
this.content = content;
|
|
344
312
|
this.Input = Input;
|
|
345
313
|
this.defaultValue = defaultValue;
|
|
346
|
-
this
|
|
314
|
+
this.__value__ = defaultValue;
|
|
347
315
|
this.disabled = disabled;
|
|
348
316
|
this.returnOnCancel = returnOnCancel;
|
|
349
317
|
this.footer = footer;
|
|
350
318
|
}
|
|
351
319
|
onChange(value) {
|
|
352
|
-
this
|
|
320
|
+
this.__value__ = value;
|
|
353
321
|
}
|
|
354
322
|
onConfirm() {
|
|
355
|
-
this.onResolve(this
|
|
323
|
+
this.onResolve(this.__value__ ?? null);
|
|
356
324
|
}
|
|
357
325
|
onClose() {
|
|
358
326
|
if (this.returnOnCancel)
|
|
359
|
-
this.onResolve(this
|
|
327
|
+
this.onResolve(this.__value__ ?? null);
|
|
360
328
|
else
|
|
361
329
|
this.onResolve(null);
|
|
362
330
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lerx/promise-modal",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Universal React modal utility that can be used outside React components with promise-based results for alert, confirm, and prompt modals",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"type": "module",
|
|
34
34
|
"exports": {
|
|
35
35
|
".": {
|
|
36
|
-
"source": "./src/index.ts",
|
|
37
36
|
"types": "./dist/index.d.ts",
|
|
37
|
+
"source": "./src/index.ts",
|
|
38
38
|
"import": "./dist/index.mjs",
|
|
39
39
|
"require": "./dist/index.cjs"
|
|
40
40
|
}
|
|
@@ -48,9 +48,9 @@
|
|
|
48
48
|
],
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "rollup -c && yarn build:types",
|
|
51
|
-
"build-storybook": "storybook build",
|
|
52
51
|
"build:publish:npm": "yarn build && yarn publish:npm",
|
|
53
52
|
"build:types": "tsc -p ./tsconfig.declarations.json && tsc-alias -p ./tsconfig.declarations.json",
|
|
53
|
+
"build-storybook": "storybook build",
|
|
54
54
|
"format": "prettier --write \"src/**/*.{ts,tsx}\"",
|
|
55
55
|
"lint": "eslint \"src/**/*.{ts,tsx}\"",
|
|
56
56
|
"make-dependency-graph": "npx depcruise src --config .dependency-cruiser.js --output-type dot > dependency-graph.dot && dot -Tpng dependency-graph.dot -o dependency-graph.png",
|
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
"version:patch": "yarn version patch"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@winglet/common-utils": "^0.
|
|
68
|
-
"@winglet/react-utils": "^0.
|
|
69
|
-
"@winglet/style-utils": "^0.
|
|
67
|
+
"@winglet/common-utils": "^0.10.0",
|
|
68
|
+
"@winglet/react-utils": "^0.10.0",
|
|
69
|
+
"@winglet/style-utils": "^0.10.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@types/react": "^19.0.0",
|