@elementor/editor-global-classes 0.20.4 → 0.21.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 +45 -0
- package/dist/index.js +145 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +154 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -11
- package/src/components/class-manager/class-manager-panel.tsx +27 -5
- package/src/components/class-manager/global-classes-list.tsx +19 -1
- package/src/components/class-manager/save-changes-dialog.tsx +13 -5
- package/src/global-classes-styles-provider.ts +3 -1
- package/src/init.ts +7 -2
- package/src/store.ts +39 -0
- package/src/utils/snapshot-history.ts +73 -0
package/src/store.ts
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
|
|
16
16
|
import type { ApiContext } from './api';
|
|
17
17
|
import { GlobalClassNotFoundError } from './errors';
|
|
18
|
+
import { SnapshotHistory } from './utils/snapshot-history';
|
|
18
19
|
|
|
19
20
|
export type GlobalClasses = {
|
|
20
21
|
items: Record< StyleDefinitionID, StyleDefinition >;
|
|
@@ -30,6 +31,8 @@ type GlobalClassesState = {
|
|
|
30
31
|
isDirty: boolean;
|
|
31
32
|
};
|
|
32
33
|
|
|
34
|
+
const localHistory = SnapshotHistory.get< GlobalClasses >( 'global-classes' );
|
|
35
|
+
|
|
33
36
|
const initialState: GlobalClassesState = {
|
|
34
37
|
data: { items: {}, order: [] },
|
|
35
38
|
initialData: {
|
|
@@ -65,6 +68,7 @@ export const slice = createSlice( {
|
|
|
65
68
|
},
|
|
66
69
|
|
|
67
70
|
add( state, { payload }: PayloadAction< StyleDefinition > ) {
|
|
71
|
+
localHistory.next( state.data );
|
|
68
72
|
state.data.items[ payload.id ] = payload;
|
|
69
73
|
state.data.order.unshift( payload.id );
|
|
70
74
|
|
|
@@ -72,6 +76,7 @@ export const slice = createSlice( {
|
|
|
72
76
|
},
|
|
73
77
|
|
|
74
78
|
delete( state, { payload }: PayloadAction< StyleDefinitionID > ) {
|
|
79
|
+
localHistory.next( state.data );
|
|
75
80
|
state.data.items = Object.fromEntries(
|
|
76
81
|
Object.entries( state.data.items ).filter( ( [ id ] ) => id !== payload )
|
|
77
82
|
);
|
|
@@ -82,12 +87,14 @@ export const slice = createSlice( {
|
|
|
82
87
|
},
|
|
83
88
|
|
|
84
89
|
setOrder( state, { payload }: PayloadAction< StyleDefinitionID[] > ) {
|
|
90
|
+
localHistory.next( state.data );
|
|
85
91
|
state.data.order = payload;
|
|
86
92
|
|
|
87
93
|
state.isDirty = true;
|
|
88
94
|
},
|
|
89
95
|
|
|
90
96
|
update( state, { payload }: PayloadAction< { style: UpdateActionPayload } > ) {
|
|
97
|
+
localHistory.next( state.data );
|
|
91
98
|
const style = state.data.items[ payload.style.id ];
|
|
92
99
|
|
|
93
100
|
const mergedData = {
|
|
@@ -111,6 +118,7 @@ export const slice = createSlice( {
|
|
|
111
118
|
if ( ! style ) {
|
|
112
119
|
throw new GlobalClassNotFoundError( { context: { styleId: payload.id } } );
|
|
113
120
|
}
|
|
121
|
+
localHistory.next( state.data );
|
|
114
122
|
|
|
115
123
|
const variant = getVariantByMeta( style, payload.meta );
|
|
116
124
|
|
|
@@ -130,6 +138,7 @@ export const slice = createSlice( {
|
|
|
130
138
|
|
|
131
139
|
reset( state, { payload: { context } }: PayloadAction< { context: ApiContext } > ) {
|
|
132
140
|
if ( context === 'frontend' ) {
|
|
141
|
+
localHistory.reset();
|
|
133
142
|
state.initialData.frontend = state.data;
|
|
134
143
|
|
|
135
144
|
state.isDirty = false;
|
|
@@ -137,6 +146,36 @@ export const slice = createSlice( {
|
|
|
137
146
|
|
|
138
147
|
state.initialData.preview = state.data;
|
|
139
148
|
},
|
|
149
|
+
|
|
150
|
+
undo( state ) {
|
|
151
|
+
if ( localHistory.isLast() ) {
|
|
152
|
+
localHistory.next( state.data ); // store current before undo
|
|
153
|
+
}
|
|
154
|
+
const data = localHistory.prev();
|
|
155
|
+
if ( data ) {
|
|
156
|
+
state.data = data;
|
|
157
|
+
state.isDirty = true;
|
|
158
|
+
} else {
|
|
159
|
+
state.data = state.initialData.preview;
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
resetToInitialState( state, { payload: { context } }: PayloadAction< { context: ApiContext } > ) {
|
|
164
|
+
localHistory.reset();
|
|
165
|
+
state.data = state.initialData[ context ];
|
|
166
|
+
state.isDirty = false;
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
redo( state ) {
|
|
170
|
+
const data = localHistory.next();
|
|
171
|
+
if ( localHistory.isLast() ) {
|
|
172
|
+
localHistory.prev();
|
|
173
|
+
}
|
|
174
|
+
if ( data ) {
|
|
175
|
+
state.data = data;
|
|
176
|
+
state.isDirty = true;
|
|
177
|
+
}
|
|
178
|
+
},
|
|
140
179
|
},
|
|
141
180
|
} );
|
|
142
181
|
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
type Link< T > = {
|
|
2
|
+
prev: Link< T > | null;
|
|
3
|
+
next: Link< T > | null;
|
|
4
|
+
value: T;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
function createLink< T >( { value, next, prev }: { value: T; prev?: Link< T >; next?: Link< T > } ): Link< T > {
|
|
8
|
+
return {
|
|
9
|
+
value,
|
|
10
|
+
prev: prev || null,
|
|
11
|
+
next: next || null,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class SnapshotHistory< T > {
|
|
16
|
+
private static registry: Record< string, SnapshotHistory< unknown > > = {};
|
|
17
|
+
|
|
18
|
+
public static get< K >( namespace: string ): SnapshotHistory< K > {
|
|
19
|
+
if ( ! SnapshotHistory.registry[ namespace ] ) {
|
|
20
|
+
SnapshotHistory.registry[ namespace ] = new SnapshotHistory( namespace );
|
|
21
|
+
}
|
|
22
|
+
return SnapshotHistory.registry[ namespace ] as SnapshotHistory< K >;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private first: Link< T > | null = null;
|
|
26
|
+
private current: Link< T > | null = null;
|
|
27
|
+
|
|
28
|
+
private constructor( public readonly namespace: string ) {}
|
|
29
|
+
|
|
30
|
+
private transform( item: T ): T {
|
|
31
|
+
return JSON.parse( JSON.stringify( item ) );
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public reset(): void {
|
|
35
|
+
this.first = this.current = null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public prev(): T | null {
|
|
39
|
+
if ( ! this.current || this.current === this.first ) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
this.current = this.current.prev;
|
|
43
|
+
return this.current?.value || null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public isLast(): boolean {
|
|
47
|
+
return ! this.current || ! this.current.next;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public next( value?: T ): T | null {
|
|
51
|
+
if ( value ) {
|
|
52
|
+
if ( ! this.current ) {
|
|
53
|
+
this.first = createLink( { value: this.transform( value ) } );
|
|
54
|
+
this.current = this.first;
|
|
55
|
+
return this.current.value;
|
|
56
|
+
}
|
|
57
|
+
const nextLink = createLink( {
|
|
58
|
+
value: this.transform( value ),
|
|
59
|
+
prev: this.current,
|
|
60
|
+
} );
|
|
61
|
+
this.current.next = nextLink;
|
|
62
|
+
this.current = nextLink;
|
|
63
|
+
return this.current.value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// No value skip to next without setting any
|
|
67
|
+
if ( ! this.current || ! this.current.next ) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
this.current = this.current.next;
|
|
71
|
+
return this.current.value;
|
|
72
|
+
}
|
|
73
|
+
}
|