@elementor/editor-v1-adapters 0.8.2 → 0.8.4
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 +14 -0
- package/dist/index.d.mts +17 -3
- package/dist/index.d.ts +17 -3
- package/dist/index.js +49 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +48 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -5
- package/src/dispatchers/dispatchers.ts +1 -1
- package/src/dispatchers/utils.ts +1 -1
- package/src/hooks/use-is-preview-mode.ts +2 -2
- package/src/hooks/use-is-route-active.ts +2 -2
- package/src/hooks/use-listen-to.ts +2 -1
- package/src/hooks/use-route-status.ts +1 -1
- package/src/index.ts +2 -0
- package/src/listeners/event-creators.ts +1 -1
- package/src/listeners/listeners.ts +7 -7
- package/src/listeners/utils.ts +1 -1
- package/src/readers/index.ts +1 -1
- package/src/undoable/get-history-manager.ts +39 -0
- package/src/undoable/index.ts +1 -0
- package/src/undoable/undoable.ts +64 -0
- package/LICENSE +0 -674
package/src/readers/index.ts
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createError } from '@elementor/utils';
|
|
2
|
+
|
|
3
|
+
export type HistoryItem = {
|
|
4
|
+
title: string;
|
|
5
|
+
subTitle: string;
|
|
6
|
+
type: string;
|
|
7
|
+
restore: ( item: HistoryItem, isRedo: boolean ) => void;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type AddHistoryItem = ( item: HistoryItem ) => void;
|
|
11
|
+
|
|
12
|
+
export type WindowWithHistoryManager = Window & {
|
|
13
|
+
elementor?: {
|
|
14
|
+
documents?: {
|
|
15
|
+
getCurrent?: () => {
|
|
16
|
+
history?: {
|
|
17
|
+
addItem: AddHistoryItem;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const HistoryManagerNotAvailable = createError( {
|
|
25
|
+
code: 'history_manager_not_available',
|
|
26
|
+
message: 'Cannot access History manager.',
|
|
27
|
+
} );
|
|
28
|
+
|
|
29
|
+
export function getHistoryManager() {
|
|
30
|
+
const extendedWindow = window as unknown as WindowWithHistoryManager;
|
|
31
|
+
|
|
32
|
+
const historyManger = extendedWindow.elementor?.documents?.getCurrent?.()?.history;
|
|
33
|
+
|
|
34
|
+
if ( ! historyManger ) {
|
|
35
|
+
throw new HistoryManagerNotAvailable();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return historyManger;
|
|
39
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { undoable } from './undoable';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { getHistoryManager } from './get-history-manager';
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
+
type Payload = Record< string, any > | undefined;
|
|
5
|
+
|
|
6
|
+
type LabelGenerator< TPayload extends Payload, TDoReturn > = (
|
|
7
|
+
payload: TPayload,
|
|
8
|
+
doReturn: Awaited< TDoReturn >
|
|
9
|
+
) => string;
|
|
10
|
+
|
|
11
|
+
type Actions< TPayload extends Payload, TDoReturn, TUndoReturn > = {
|
|
12
|
+
do: ( payload: TPayload ) => TDoReturn;
|
|
13
|
+
undo: ( payload: TPayload, doReturn: Awaited< TDoReturn > ) => TUndoReturn;
|
|
14
|
+
redo?: ( payload: TPayload, doReturn: Awaited< TDoReturn > ) => TDoReturn;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type Options< TPayload extends Payload, TDoReturn > = {
|
|
18
|
+
title: string | LabelGenerator< TPayload, TDoReturn >;
|
|
19
|
+
subtitle?: string | LabelGenerator< TPayload, TDoReturn >;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Action WITHOUT a payload.
|
|
23
|
+
export function undoable< TDoReturn, TUndoReturn >(
|
|
24
|
+
actions: Actions< undefined, TDoReturn, TUndoReturn >,
|
|
25
|
+
options: Options< undefined, TDoReturn >
|
|
26
|
+
): () => Promise< Awaited< TDoReturn > >;
|
|
27
|
+
|
|
28
|
+
// Action WITH a payload.
|
|
29
|
+
export function undoable< TPayload extends NonNullable< Payload >, TDoReturn, TUndoReturn >(
|
|
30
|
+
actions: Actions< TPayload, TDoReturn, TUndoReturn >,
|
|
31
|
+
options: Options< TPayload, TDoReturn >
|
|
32
|
+
): ( payload: TPayload ) => Promise< Awaited< TDoReturn > >;
|
|
33
|
+
|
|
34
|
+
export function undoable< TPayload extends Payload, TDoReturn, TUndoReturn >(
|
|
35
|
+
actions: Actions< TPayload, TDoReturn, TUndoReturn >,
|
|
36
|
+
options: Options< TPayload, TDoReturn >
|
|
37
|
+
): ( payload?: Payload ) => Promise< Awaited< TDoReturn > > {
|
|
38
|
+
actions.redo ??= ( payload ) => actions.do( payload );
|
|
39
|
+
|
|
40
|
+
return async ( payload ): Promise< Awaited< TDoReturn > > => {
|
|
41
|
+
const _payload = payload as TPayload;
|
|
42
|
+
const _actions = actions as Required< Actions< TPayload, TDoReturn, TUndoReturn > >;
|
|
43
|
+
|
|
44
|
+
const history = getHistoryManager();
|
|
45
|
+
|
|
46
|
+
const result = await _actions.do( _payload );
|
|
47
|
+
|
|
48
|
+
history.addItem( {
|
|
49
|
+
title: normalizeToGenerator( options.title )( _payload, result ),
|
|
50
|
+
subTitle: normalizeToGenerator( options.subtitle )( _payload, result ),
|
|
51
|
+
type: '',
|
|
52
|
+
restore: ( _, isRedo ) =>
|
|
53
|
+
isRedo ? _actions.redo( _payload, result ) : _actions.undo( _payload, result ),
|
|
54
|
+
} );
|
|
55
|
+
|
|
56
|
+
return result;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function normalizeToGenerator< TPayload extends Payload, TDoReturn >(
|
|
61
|
+
value: string | undefined | LabelGenerator< TPayload, TDoReturn >
|
|
62
|
+
) {
|
|
63
|
+
return typeof value === 'function' ? value : () => value ?? '';
|
|
64
|
+
}
|