@elementor/editor-variables 0.8.0 → 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/CHANGELOG.md +31 -0
- package/dist/index.js +304 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +304 -26
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -4
- package/src/api.ts +33 -0
- package/src/components/color-variable-creation.tsx +6 -1
- package/src/components/color-variables-selection.tsx +4 -4
- package/src/components/font-variables-selection.tsx +4 -4
- package/src/controls/color-variables-selection-control.tsx +1 -1
- package/src/controls/font-variables-selection-control.tsx +1 -1
- package/src/create-style-variables-repository.ts +50 -0
- package/src/hooks/use-prop-variables.ts +25 -31
- package/src/init.ts +11 -1
- package/src/renderers/style-variables-renderer.tsx +56 -0
- package/src/service.ts +154 -0
- package/src/storage.ts +74 -0
- package/src/style-variables-repository.ts +3 -0
- package/src/sync/get-canvas-iframe-document.ts +7 -0
- package/src/sync/types.ts +5 -0
- package/src/types.ts +12 -2
package/src/service.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { apiClient } from './api';
|
|
2
|
+
import { OP_RW, Storage, type TVariable, type TVariablesList } from './storage';
|
|
3
|
+
|
|
4
|
+
const storage = new Storage();
|
|
5
|
+
|
|
6
|
+
export const service = {
|
|
7
|
+
variables: (): TVariablesList => {
|
|
8
|
+
return storage.load();
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
init: () => {
|
|
12
|
+
service.load();
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
load: () => {
|
|
16
|
+
return apiClient
|
|
17
|
+
.list()
|
|
18
|
+
.then( ( response ) => {
|
|
19
|
+
const { success, data: payload } = response.data;
|
|
20
|
+
|
|
21
|
+
if ( ! success ) {
|
|
22
|
+
throw new Error( 'Unexpected response from server' );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return payload;
|
|
26
|
+
} )
|
|
27
|
+
.then( ( data ) => {
|
|
28
|
+
const { variables, watermark } = data;
|
|
29
|
+
|
|
30
|
+
storage.fill( variables, watermark );
|
|
31
|
+
|
|
32
|
+
return variables;
|
|
33
|
+
} );
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
create: ( { type, label, value }: TVariable ) => {
|
|
37
|
+
return apiClient
|
|
38
|
+
.create( type, label, value )
|
|
39
|
+
.then( ( response ) => {
|
|
40
|
+
const { success, data: payload } = response.data;
|
|
41
|
+
|
|
42
|
+
if ( ! success ) {
|
|
43
|
+
throw new Error( 'Unexpected response from server' );
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return payload;
|
|
47
|
+
} )
|
|
48
|
+
.then( ( data ) => {
|
|
49
|
+
const { variable, watermark } = data;
|
|
50
|
+
|
|
51
|
+
handleWatermark( OP_RW, watermark );
|
|
52
|
+
|
|
53
|
+
const { id: variableId, ...createdVariable } = variable;
|
|
54
|
+
|
|
55
|
+
storage.add( variableId, createdVariable );
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
id: variableId,
|
|
59
|
+
variable: createdVariable,
|
|
60
|
+
};
|
|
61
|
+
} );
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
update: ( id: string, { label, value }: TVariable ) => {
|
|
65
|
+
return apiClient
|
|
66
|
+
.update( id, label, value )
|
|
67
|
+
.then( ( response ) => {
|
|
68
|
+
const { success, data: payload } = response.data;
|
|
69
|
+
|
|
70
|
+
if ( ! success ) {
|
|
71
|
+
throw new Error( 'Unexpected response from server' );
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return payload;
|
|
75
|
+
} )
|
|
76
|
+
.then( ( data ) => {
|
|
77
|
+
const { variable, watermark } = data;
|
|
78
|
+
|
|
79
|
+
handleWatermark( OP_RW, watermark );
|
|
80
|
+
|
|
81
|
+
const { id: variableId, ...updatedVariable } = variable;
|
|
82
|
+
|
|
83
|
+
storage.update( variableId, updatedVariable );
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
id: variableId,
|
|
87
|
+
variable: updatedVariable,
|
|
88
|
+
};
|
|
89
|
+
} );
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
delete: ( id: string ) => {
|
|
93
|
+
return apiClient
|
|
94
|
+
.delete( id )
|
|
95
|
+
.then( ( response ) => {
|
|
96
|
+
const { success, data: payload } = response.data;
|
|
97
|
+
|
|
98
|
+
if ( ! success ) {
|
|
99
|
+
throw new Error( 'Unexpected response from server' );
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return payload;
|
|
103
|
+
} )
|
|
104
|
+
.then( ( data ) => {
|
|
105
|
+
const { variable, watermark } = data;
|
|
106
|
+
|
|
107
|
+
handleWatermark( OP_RW, watermark );
|
|
108
|
+
|
|
109
|
+
const { id: variableId, ...deletedVariable } = variable;
|
|
110
|
+
|
|
111
|
+
storage.update( variableId, deletedVariable );
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
id: variableId,
|
|
115
|
+
variable: deletedVariable,
|
|
116
|
+
};
|
|
117
|
+
} );
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
restore: ( id: string ) => {
|
|
121
|
+
return apiClient
|
|
122
|
+
.restore( id )
|
|
123
|
+
.then( ( response ) => {
|
|
124
|
+
const { success, data: payload } = response.data;
|
|
125
|
+
|
|
126
|
+
if ( ! success ) {
|
|
127
|
+
throw new Error( 'Unexpected response from server' );
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return payload;
|
|
131
|
+
} )
|
|
132
|
+
.then( ( data ) => {
|
|
133
|
+
const { variable, watermark } = data;
|
|
134
|
+
|
|
135
|
+
handleWatermark( OP_RW, watermark );
|
|
136
|
+
|
|
137
|
+
const { id: variableId, ...restoredVariable } = variable;
|
|
138
|
+
|
|
139
|
+
storage.update( variableId, restoredVariable );
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
id: variableId,
|
|
143
|
+
variable: restoredVariable,
|
|
144
|
+
};
|
|
145
|
+
} );
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const handleWatermark = ( operation: string, newWatermark: number ) => {
|
|
150
|
+
if ( storage.watermarkDiff( operation, newWatermark ) ) {
|
|
151
|
+
setTimeout( () => service.load(), 500 );
|
|
152
|
+
}
|
|
153
|
+
storage.watermark( newWatermark );
|
|
154
|
+
};
|
package/src/storage.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export type TVariable = {
|
|
2
|
+
type: string;
|
|
3
|
+
label: string;
|
|
4
|
+
value: string;
|
|
5
|
+
deleted?: boolean;
|
|
6
|
+
deleted_at?: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type TVariablesList = Record< string, TVariable >;
|
|
10
|
+
|
|
11
|
+
const STORAGE_KEY = 'elementor-global-variables';
|
|
12
|
+
const STORAGE_WATERMARK_KEY = 'elementor-global-variables-watermark';
|
|
13
|
+
|
|
14
|
+
export const OP_RW = 'RW';
|
|
15
|
+
const OP_RO = 'RO';
|
|
16
|
+
|
|
17
|
+
export class Storage {
|
|
18
|
+
state: {
|
|
19
|
+
watermark: number;
|
|
20
|
+
variables: TVariablesList;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
constructor() {
|
|
24
|
+
this.state = {
|
|
25
|
+
watermark: -1,
|
|
26
|
+
variables: {},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
load() {
|
|
31
|
+
this.state.watermark = parseInt( localStorage.getItem( STORAGE_WATERMARK_KEY ) || '-1' );
|
|
32
|
+
this.state.variables = JSON.parse( localStorage.getItem( STORAGE_KEY ) || '{}' ) as TVariablesList;
|
|
33
|
+
return this.state.variables;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fill( variables: TVariablesList, watermark: number ) {
|
|
37
|
+
this.state.watermark = watermark;
|
|
38
|
+
this.state.variables = variables;
|
|
39
|
+
|
|
40
|
+
localStorage.setItem( STORAGE_WATERMARK_KEY, this.state.watermark.toString() );
|
|
41
|
+
localStorage.setItem( STORAGE_KEY, JSON.stringify( this.state.variables ) );
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
add( id: string, variable: TVariable ) {
|
|
45
|
+
this.load();
|
|
46
|
+
this.state.variables[ id ] = variable;
|
|
47
|
+
localStorage.setItem( STORAGE_KEY, JSON.stringify( this.state.variables ) );
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
update( id: string, variable: TVariable ) {
|
|
51
|
+
this.load();
|
|
52
|
+
this.state.variables[ id ] = variable;
|
|
53
|
+
localStorage.setItem( STORAGE_KEY, JSON.stringify( this.state.variables ) );
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
watermark( watermark: number ) {
|
|
57
|
+
this.state.watermark = watermark;
|
|
58
|
+
localStorage.setItem( STORAGE_WATERMARK_KEY, this.state.watermark.toString() );
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
watermarkDiff( operation: string, newWatermark: number ) {
|
|
62
|
+
const diff = newWatermark - this.state.watermark;
|
|
63
|
+
|
|
64
|
+
if ( OP_RW === operation ) {
|
|
65
|
+
return 1 !== diff;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if ( OP_RO === operation ) {
|
|
69
|
+
return 0 !== diff;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
+
export type VariableKey = string;
|
|
2
|
+
|
|
3
|
+
export type VariableValue = string;
|
|
4
|
+
|
|
1
5
|
export type Variable = {
|
|
2
|
-
value:
|
|
6
|
+
value: VariableValue;
|
|
3
7
|
label: string;
|
|
4
|
-
|
|
8
|
+
type: string;
|
|
5
9
|
};
|
|
10
|
+
|
|
11
|
+
export type Variables = {
|
|
12
|
+
[ key: VariableKey ]: Variable;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type StyleVariables = Record< VariableKey, VariableValue >;
|