@elementor/editor-documents 0.11.4 → 0.11.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/CHANGELOG.md +16 -105
- package/dist/index.js +41 -59
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -59
- package/dist/index.mjs.map +1 -1
- package/package.json +42 -42
- package/src/hooks/__tests__/use-active-document-actions.test.ts +3 -11
- package/src/hooks/__tests__/use-active-document.test.tsx +1 -1
- package/src/hooks/__tests__/use-host-document.test.tsx +15 -11
- package/src/hooks/__tests__/use-sync-document-title.test.ts +42 -32
- package/src/hooks/use-sync-document-title.ts +2 -5
- package/src/index.ts +1 -3
- package/src/store/index.ts +11 -11
- package/src/store/selectors.ts +5 -13
- package/src/sync/__tests__/sync-store.test.ts +105 -108
- package/src/sync/__tests__/test-utils.ts +23 -19
- package/src/sync/sync-store.ts +52 -71
- package/src/sync/utils.ts +3 -2
- package/src/types.ts +58 -58
|
@@ -12,12 +12,12 @@ import {
|
|
|
12
12
|
import { selectActiveDocument } from '../../store/selectors';
|
|
13
13
|
import { getV1DocumentPermalink, getV1DocumentsExitTo } from '../utils';
|
|
14
14
|
|
|
15
|
-
type WindowWithOptionalElementor = Omit<ExtendedWindow, 'elementor'> & {
|
|
16
|
-
elementor?: ExtendedWindow['elementor'];
|
|
17
|
-
}
|
|
15
|
+
type WindowWithOptionalElementor = Omit< ExtendedWindow, 'elementor' > & {
|
|
16
|
+
elementor?: ExtendedWindow[ 'elementor' ];
|
|
17
|
+
};
|
|
18
18
|
|
|
19
19
|
describe( '@elementor/editor-documents - Sync Store', () => {
|
|
20
|
-
let store: Store<SliceState<typeof slice
|
|
20
|
+
let store: Store< SliceState< typeof slice > >;
|
|
21
21
|
|
|
22
22
|
beforeEach( () => {
|
|
23
23
|
jest.useFakeTimers();
|
|
@@ -34,10 +34,7 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
34
34
|
|
|
35
35
|
it( 'should sync documents on V1 load', () => {
|
|
36
36
|
// Arrange.
|
|
37
|
-
mockV1DocumentsManager( [
|
|
38
|
-
makeMockV1Document( { id: 1 } ),
|
|
39
|
-
makeMockV1Document( { id: 2 } ),
|
|
40
|
-
], 'this_post' );
|
|
37
|
+
mockV1DocumentsManager( [ makeMockV1Document( { id: 1 } ), makeMockV1Document( { id: 2 } ) ], 'this_post' );
|
|
41
38
|
|
|
42
39
|
// Act.
|
|
43
40
|
dispatchV1ReadyEvent();
|
|
@@ -45,7 +42,7 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
45
42
|
// Assert.
|
|
46
43
|
const storeState = store.getState();
|
|
47
44
|
|
|
48
|
-
expect( storeState.documents.entities ).toEqual<Record<number, Document
|
|
45
|
+
expect( storeState.documents.entities ).toEqual< Record< number, Document > >( {
|
|
49
46
|
1: {
|
|
50
47
|
id: 1,
|
|
51
48
|
title: 'Document 1',
|
|
@@ -112,10 +109,7 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
112
109
|
},
|
|
113
110
|
] )( 'should sync active document on $type', ( { dispatchEvent } ) => {
|
|
114
111
|
// Arrange.
|
|
115
|
-
mockV1DocumentsManager( [
|
|
116
|
-
makeMockV1Document( { id: 1 } ),
|
|
117
|
-
makeMockV1Document( { id: 2 } ),
|
|
118
|
-
], 'this_post', 2 );
|
|
112
|
+
mockV1DocumentsManager( [ makeMockV1Document( { id: 1 } ), makeMockV1Document( { id: 2 } ) ], 'this_post', 2 );
|
|
119
113
|
|
|
120
114
|
// Act.
|
|
121
115
|
dispatchEvent();
|
|
@@ -123,7 +117,7 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
123
117
|
// Assert.
|
|
124
118
|
const currentDocument = selectActiveDocument( store.getState() );
|
|
125
119
|
|
|
126
|
-
expect( currentDocument ).toEqual<Document>( {
|
|
120
|
+
expect( currentDocument ).toEqual< Document >( {
|
|
127
121
|
id: 2,
|
|
128
122
|
title: 'Document 2',
|
|
129
123
|
type: {
|
|
@@ -160,30 +154,27 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
160
154
|
openAsHost: false,
|
|
161
155
|
expectedHost: 1,
|
|
162
156
|
},
|
|
163
|
-
] )(
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
157
|
+
] )(
|
|
158
|
+
'should sync host document when a new host is opened { openAsHost: $openAsHost }',
|
|
159
|
+
( { openAsHost, expectedHost } ) => {
|
|
160
|
+
// Arrange.
|
|
161
|
+
const mockDocument1 = makeMockV1Document( { id: 1 } );
|
|
162
|
+
const mockDocument2 = makeMockV1Document( { id: 2 } );
|
|
167
163
|
|
|
168
|
-
|
|
169
|
-
mockDocument1,
|
|
170
|
-
mockDocument2,
|
|
171
|
-
], 'this_post', 1, 1 );
|
|
164
|
+
mockV1DocumentsManager( [ mockDocument1, mockDocument2 ], 'this_post', 1, 1 );
|
|
172
165
|
|
|
173
|
-
|
|
174
|
-
|
|
166
|
+
// Populate the documents state.
|
|
167
|
+
dispatchV1ReadyEvent();
|
|
175
168
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
mockDocument1,
|
|
179
|
-
mockDocument2,
|
|
180
|
-
], 'this_post', 2, openAsHost ? 2 : 1 );
|
|
169
|
+
// Act - Mock a host document change.
|
|
170
|
+
mockV1DocumentsManager( [ mockDocument1, mockDocument2 ], 'this_post', 2, openAsHost ? 2 : 1 );
|
|
181
171
|
|
|
182
|
-
|
|
172
|
+
dispatchCommandAfter( 'editor/documents/open' );
|
|
183
173
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
174
|
+
// Assert.
|
|
175
|
+
expect( store.getState().documents.hostId ).toBe( expectedHost );
|
|
176
|
+
}
|
|
177
|
+
);
|
|
187
178
|
|
|
188
179
|
it( 'should sync saving state of a document on V1 load', () => {
|
|
189
180
|
// Arrange.
|
|
@@ -208,9 +199,7 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
208
199
|
|
|
209
200
|
it( 'should sync saving state of a document on save', () => {
|
|
210
201
|
// Arrange.
|
|
211
|
-
mockV1DocumentsManager( [
|
|
212
|
-
makeMockV1Document(),
|
|
213
|
-
] );
|
|
202
|
+
mockV1DocumentsManager( [ makeMockV1Document() ] );
|
|
214
203
|
|
|
215
204
|
// Populate the documents state.
|
|
216
205
|
dispatchV1ReadyEvent();
|
|
@@ -235,9 +224,7 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
235
224
|
|
|
236
225
|
it( 'should sync draft saving state of a document on save', () => {
|
|
237
226
|
// Arrange.
|
|
238
|
-
mockV1DocumentsManager( [
|
|
239
|
-
makeMockV1Document(),
|
|
240
|
-
] );
|
|
227
|
+
mockV1DocumentsManager( [ makeMockV1Document() ] );
|
|
241
228
|
|
|
242
229
|
// Populate the documents state.
|
|
243
230
|
dispatchV1ReadyEvent();
|
|
@@ -268,15 +255,17 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
268
255
|
// Arrange.
|
|
269
256
|
const mockDocument = makeMockV1Document( { id: 1 } );
|
|
270
257
|
|
|
271
|
-
mockV1DocumentsManager( [
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
258
|
+
mockV1DocumentsManager( [
|
|
259
|
+
{
|
|
260
|
+
...mockDocument,
|
|
261
|
+
config: {
|
|
262
|
+
...mockDocument.config,
|
|
263
|
+
revisions: {
|
|
264
|
+
current_id: 2,
|
|
265
|
+
},
|
|
277
266
|
},
|
|
278
267
|
},
|
|
279
|
-
|
|
268
|
+
] );
|
|
280
269
|
|
|
281
270
|
// Act.
|
|
282
271
|
dispatchV1ReadyEvent();
|
|
@@ -289,9 +278,7 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
289
278
|
// Arrange.
|
|
290
279
|
const mockDocument = makeMockV1Document();
|
|
291
280
|
|
|
292
|
-
mockV1DocumentsManager( [
|
|
293
|
-
mockDocument,
|
|
294
|
-
] );
|
|
281
|
+
mockV1DocumentsManager( [ mockDocument ] );
|
|
295
282
|
|
|
296
283
|
// Populate the documents state.
|
|
297
284
|
dispatchV1ReadyEvent();
|
|
@@ -373,30 +360,38 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
373
360
|
|
|
374
361
|
it( 'should update the document when finish saving', () => {
|
|
375
362
|
// Arrange.
|
|
376
|
-
mockV1DocumentsManager(
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
363
|
+
mockV1DocumentsManager(
|
|
364
|
+
[
|
|
365
|
+
makeMockV1Document( {
|
|
366
|
+
id: 1,
|
|
367
|
+
status: 'draft',
|
|
368
|
+
title: 'test',
|
|
369
|
+
} ),
|
|
370
|
+
],
|
|
371
|
+
'this_post'
|
|
372
|
+
);
|
|
383
373
|
|
|
384
374
|
// Populate the documents state.
|
|
385
375
|
dispatchV1ReadyEvent();
|
|
386
376
|
|
|
387
377
|
// Mock a change.
|
|
388
|
-
mockV1DocumentsManager(
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
378
|
+
mockV1DocumentsManager(
|
|
379
|
+
[
|
|
380
|
+
makeMockV1Document( {
|
|
381
|
+
id: 1,
|
|
382
|
+
status: 'publish',
|
|
383
|
+
title: 'test title changed',
|
|
384
|
+
} ),
|
|
385
|
+
],
|
|
386
|
+
'dashboard'
|
|
387
|
+
);
|
|
395
388
|
|
|
396
389
|
// Assert.
|
|
397
390
|
expect( selectActiveDocument( store.getState() )?.title ).toBe( 'test' );
|
|
398
391
|
expect( selectActiveDocument( store.getState() )?.status.value ).toBe( 'draft' );
|
|
399
|
-
expect( selectActiveDocument( store.getState() )?.links.platformEdit ).toBe(
|
|
392
|
+
expect( selectActiveDocument( store.getState() )?.links.platformEdit ).toBe(
|
|
393
|
+
'https://localhost/wp-admin/post.php?post=1&action=edit'
|
|
394
|
+
);
|
|
400
395
|
|
|
401
396
|
// Act.
|
|
402
397
|
dispatchCommandAfter( 'document/save/save' );
|
|
@@ -407,55 +402,57 @@ describe( '@elementor/editor-documents - Sync Store', () => {
|
|
|
407
402
|
expect( selectActiveDocument( store.getState() )?.links.platformEdit ).toBe( 'https://localhost/wp-admin/' );
|
|
408
403
|
} );
|
|
409
404
|
|
|
410
|
-
it.each( [
|
|
411
|
-
'
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
const mockDocument = makeMockV1Document( { id: 1 } );
|
|
417
|
-
mockV1DocumentsManager( [
|
|
418
|
-
mockDocument,
|
|
419
|
-
], exitTo );
|
|
405
|
+
it.each( [ 'dashboard', 'this_post', 'all_posts' ] as ExitTo[] )(
|
|
406
|
+
'should sync active document $ExitTo',
|
|
407
|
+
( exitTo ) => {
|
|
408
|
+
// Arrange.
|
|
409
|
+
const mockDocument = makeMockV1Document( { id: 1 } );
|
|
410
|
+
mockV1DocumentsManager( [ mockDocument ], exitTo );
|
|
420
411
|
|
|
421
|
-
|
|
422
|
-
|
|
412
|
+
// Populate the documents state.
|
|
413
|
+
dispatchV1ReadyEvent();
|
|
423
414
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
415
|
+
// Assert.
|
|
416
|
+
const currentDocument = selectActiveDocument( store.getState() );
|
|
417
|
+
const platformEdit = getV1DocumentsExitTo( mockDocument );
|
|
418
|
+
const permalink = getV1DocumentPermalink( mockDocument );
|
|
428
419
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
420
|
+
expect( currentDocument ).toEqual< Document >( {
|
|
421
|
+
id: 1,
|
|
422
|
+
title: 'Document 1',
|
|
423
|
+
type: {
|
|
424
|
+
value: 'wp-page',
|
|
425
|
+
label: 'WP-PAGE',
|
|
426
|
+
},
|
|
427
|
+
links: {
|
|
428
|
+
platformEdit,
|
|
429
|
+
permalink,
|
|
430
|
+
},
|
|
431
|
+
status: {
|
|
432
|
+
value: 'publish',
|
|
433
|
+
label: 'PUBLISH',
|
|
434
|
+
},
|
|
435
|
+
isDirty: false,
|
|
436
|
+
isSaving: false,
|
|
437
|
+
isSavingDraft: false,
|
|
438
|
+
userCan: {
|
|
439
|
+
publish: true,
|
|
440
|
+
},
|
|
441
|
+
permissions: {
|
|
442
|
+
allowAddingWidgets: true,
|
|
443
|
+
showCopyAndShare: false,
|
|
444
|
+
},
|
|
445
|
+
} );
|
|
446
|
+
}
|
|
447
|
+
);
|
|
456
448
|
} );
|
|
457
449
|
|
|
458
|
-
function mockV1DocumentsManager(
|
|
450
|
+
function mockV1DocumentsManager(
|
|
451
|
+
documentsArray: V1Document[],
|
|
452
|
+
exitTo: ExitTo = 'this_post',
|
|
453
|
+
current = 1,
|
|
454
|
+
initial = 1
|
|
455
|
+
) {
|
|
459
456
|
( window as unknown as WindowWithOptionalElementor ).elementor = {
|
|
460
457
|
getPreferences: () => exitTo,
|
|
461
458
|
documents: makeDocumentsManager( documentsArray, current, initial ),
|
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { V1Document } from '../../types';
|
|
2
2
|
|
|
3
3
|
export function dispatchCommandBefore( command: string, args: object = {} ) {
|
|
4
|
-
window.dispatchEvent(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
window.dispatchEvent(
|
|
5
|
+
new CustomEvent( 'elementor/commands/run/before', {
|
|
6
|
+
detail: {
|
|
7
|
+
command,
|
|
8
|
+
args,
|
|
9
|
+
},
|
|
10
|
+
} )
|
|
11
|
+
);
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export function dispatchCommandAfter( command: string, args: object = {} ) {
|
|
13
|
-
window.dispatchEvent(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
window.dispatchEvent(
|
|
16
|
+
new CustomEvent( 'elementor/commands/run/after', {
|
|
17
|
+
detail: {
|
|
18
|
+
command,
|
|
19
|
+
args,
|
|
20
|
+
},
|
|
21
|
+
} )
|
|
22
|
+
);
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
export function dispatchWindowEvent( event: string ) {
|
|
@@ -27,7 +31,7 @@ export function dispatchV1ReadyEvent() {
|
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
export function makeDocumentsManager( documentsArray: V1Document[], current = 1, initial = current ) {
|
|
30
|
-
const documents = documentsArray.reduce( ( acc: Record<number, V1Document>, document ) => {
|
|
34
|
+
const documents = documentsArray.reduce( ( acc: Record< number, V1Document >, document ) => {
|
|
31
35
|
acc[ document.id ] = document;
|
|
32
36
|
|
|
33
37
|
return acc;
|
|
@@ -53,10 +57,10 @@ export function makeMockV1Document( {
|
|
|
53
57
|
status = 'publish',
|
|
54
58
|
type = 'wp-page',
|
|
55
59
|
}: {
|
|
56
|
-
id?: number
|
|
57
|
-
status?: string
|
|
58
|
-
title?: string
|
|
59
|
-
type?: string
|
|
60
|
+
id?: number;
|
|
61
|
+
status?: string;
|
|
62
|
+
title?: string;
|
|
63
|
+
type?: string;
|
|
60
64
|
} = {} ): V1Document {
|
|
61
65
|
return {
|
|
62
66
|
id,
|
|
@@ -97,10 +101,10 @@ export function makeMockV1Document( {
|
|
|
97
101
|
}
|
|
98
102
|
|
|
99
103
|
// Mock Backbone's settings model.
|
|
100
|
-
function makeV1Settings<T extends object>( settings: T ) {
|
|
104
|
+
function makeV1Settings< T extends object >( settings: T ) {
|
|
101
105
|
return {
|
|
102
106
|
get( key: keyof T ) {
|
|
103
107
|
return settings[ key ];
|
|
104
108
|
},
|
|
105
|
-
} as V1Document['container']['settings'];
|
|
109
|
+
} as V1Document[ 'container' ][ 'settings' ];
|
|
106
110
|
}
|
package/src/sync/sync-store.ts
CHANGED
|
@@ -23,50 +23,48 @@ export function syncStore() {
|
|
|
23
23
|
function syncInitialization() {
|
|
24
24
|
const { init } = slice.actions;
|
|
25
25
|
|
|
26
|
-
listenTo(
|
|
27
|
-
|
|
28
|
-
() => {
|
|
29
|
-
const documentsManager = getV1DocumentsManager();
|
|
26
|
+
listenTo( v1ReadyEvent(), () => {
|
|
27
|
+
const documentsManager = getV1DocumentsManager();
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
const entities = Object.entries( documentsManager.documents ).reduce(
|
|
30
|
+
( acc: Record< string, Document >, [ id, document ] ) => {
|
|
31
|
+
acc[ id ] = normalizeV1Document( document );
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
return acc;
|
|
34
|
+
},
|
|
35
|
+
{}
|
|
36
|
+
);
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
__dispatch(
|
|
39
|
+
init( {
|
|
39
40
|
entities,
|
|
40
41
|
hostId: documentsManager.getInitialId(),
|
|
41
42
|
activeId: documentsManager.getCurrentId(),
|
|
42
|
-
} )
|
|
43
|
-
|
|
44
|
-
);
|
|
43
|
+
} )
|
|
44
|
+
);
|
|
45
|
+
} );
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
function syncActiveDocument() {
|
|
48
49
|
const { activateDocument, setAsHost } = slice.actions;
|
|
49
50
|
|
|
50
|
-
listenTo(
|
|
51
|
-
|
|
52
|
-
()
|
|
53
|
-
const documentsManager = getV1DocumentsManager();
|
|
54
|
-
const currentDocument = normalizeV1Document( documentsManager.getCurrent() );
|
|
51
|
+
listenTo( commandEndEvent( 'editor/documents/open' ), () => {
|
|
52
|
+
const documentsManager = getV1DocumentsManager();
|
|
53
|
+
const currentDocument = normalizeV1Document( documentsManager.getCurrent() );
|
|
55
54
|
|
|
56
|
-
|
|
55
|
+
__dispatch( activateDocument( currentDocument ) );
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
57
|
+
if ( documentsManager.getInitialId() === currentDocument.id ) {
|
|
58
|
+
__dispatch( setAsHost( currentDocument.id ) );
|
|
61
59
|
}
|
|
62
|
-
);
|
|
60
|
+
} );
|
|
63
61
|
}
|
|
64
62
|
|
|
65
63
|
function syncOnDocumentSave() {
|
|
66
64
|
const { startSaving, endSaving, startSavingDraft, endSavingDraft } = slice.actions;
|
|
67
65
|
|
|
68
66
|
const isDraft = ( e: ListenerEvent ) => {
|
|
69
|
-
const event = e as CommandEvent<{ status: string }>;
|
|
67
|
+
const event = e as CommandEvent< { status: string } >;
|
|
70
68
|
|
|
71
69
|
/**
|
|
72
70
|
* @see https://github.com/elementor/elementor/blob/5f815d40a/assets/dev/js/editor/document/save/hooks/ui/save/before.js#L18-L22
|
|
@@ -74,39 +72,31 @@ function syncOnDocumentSave() {
|
|
|
74
72
|
return event.args?.status === 'autosave';
|
|
75
73
|
};
|
|
76
74
|
|
|
77
|
-
listenTo(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
__dispatch( startSavingDraft() );
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
__dispatch( startSaving() );
|
|
75
|
+
listenTo( commandStartEvent( 'document/save/save' ), ( e ) => {
|
|
76
|
+
if ( isDraft( e ) ) {
|
|
77
|
+
__dispatch( startSavingDraft() );
|
|
78
|
+
return;
|
|
86
79
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
} else {
|
|
99
|
-
__dispatch( endSaving( activeDocument ) );
|
|
100
|
-
}
|
|
80
|
+
|
|
81
|
+
__dispatch( startSaving() );
|
|
82
|
+
} );
|
|
83
|
+
|
|
84
|
+
listenTo( commandEndEvent( 'document/save/save' ), ( e ) => {
|
|
85
|
+
const activeDocument = normalizeV1Document( getV1DocumentsManager().getCurrent() );
|
|
86
|
+
|
|
87
|
+
if ( isDraft( e ) ) {
|
|
88
|
+
__dispatch( endSavingDraft( activeDocument ) );
|
|
89
|
+
} else {
|
|
90
|
+
__dispatch( endSaving( activeDocument ) );
|
|
101
91
|
}
|
|
102
|
-
);
|
|
92
|
+
} );
|
|
103
93
|
}
|
|
104
94
|
|
|
105
95
|
function syncOnTitleChange() {
|
|
106
96
|
const { updateActiveDocument } = slice.actions;
|
|
107
97
|
|
|
108
98
|
const updateTitle = debounce( ( e: ListenerEvent ) => {
|
|
109
|
-
const event = e as CommandEvent<{ settings: { post_title?: string } }>;
|
|
99
|
+
const event = e as CommandEvent< { settings: { post_title?: string } } >;
|
|
110
100
|
|
|
111
101
|
if ( ! ( 'post_title' in event.args?.settings ) ) {
|
|
112
102
|
return;
|
|
@@ -118,17 +108,14 @@ function syncOnTitleChange() {
|
|
|
118
108
|
__dispatch( updateActiveDocument( { title: newTitle } ) );
|
|
119
109
|
}, 400 );
|
|
120
110
|
|
|
121
|
-
listenTo(
|
|
122
|
-
commandEndEvent( 'document/elements/settings' ),
|
|
123
|
-
updateTitle
|
|
124
|
-
);
|
|
111
|
+
listenTo( commandEndEvent( 'document/elements/settings' ), updateTitle );
|
|
125
112
|
}
|
|
126
113
|
|
|
127
114
|
function syncOnExitToChange() {
|
|
128
115
|
const { updateActiveDocument } = slice.actions;
|
|
129
116
|
|
|
130
117
|
const updateExitTo = debounce( ( e: ListenerEvent ) => {
|
|
131
|
-
const event = e as CommandEvent<{ settings: { exit_to?: string } }>;
|
|
118
|
+
const event = e as CommandEvent< { settings: { exit_to?: string } } >;
|
|
132
119
|
|
|
133
120
|
if ( ! ( 'exit_to' in event.args?.settings ) ) {
|
|
134
121
|
return;
|
|
@@ -141,32 +128,26 @@ function syncOnExitToChange() {
|
|
|
141
128
|
__dispatch( updateActiveDocument( { links: { platformEdit: newExitTo, permalink } } ) );
|
|
142
129
|
}, 400 );
|
|
143
130
|
|
|
144
|
-
listenTo(
|
|
145
|
-
commandEndEvent( 'document/elements/settings' ),
|
|
146
|
-
updateExitTo
|
|
147
|
-
);
|
|
131
|
+
listenTo( commandEndEvent( 'document/elements/settings' ), updateExitTo );
|
|
148
132
|
}
|
|
149
133
|
|
|
150
134
|
function syncOnDocumentChange() {
|
|
151
135
|
const { markAsDirty, markAsPristine } = slice.actions;
|
|
152
136
|
|
|
153
|
-
listenTo(
|
|
154
|
-
|
|
155
|
-
() => {
|
|
156
|
-
const currentDocument = getV1DocumentsManager().getCurrent();
|
|
157
|
-
|
|
158
|
-
if ( currentDocument.editor.isChanged ) {
|
|
159
|
-
__dispatch( markAsDirty() );
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
137
|
+
listenTo( commandEndEvent( 'document/save/set-is-modified' ), () => {
|
|
138
|
+
const currentDocument = getV1DocumentsManager().getCurrent();
|
|
162
139
|
|
|
163
|
-
|
|
140
|
+
if ( currentDocument.editor.isChanged ) {
|
|
141
|
+
__dispatch( markAsDirty() );
|
|
142
|
+
return;
|
|
164
143
|
}
|
|
165
|
-
|
|
144
|
+
|
|
145
|
+
__dispatch( markAsPristine() );
|
|
146
|
+
} );
|
|
166
147
|
}
|
|
167
148
|
|
|
168
|
-
function debounce<TArgs extends unknown[]>( fn: ( ...args: TArgs ) => unknown, timeout: number ) {
|
|
169
|
-
let timer: ReturnType<typeof setTimeout>;
|
|
149
|
+
function debounce< TArgs extends unknown[] >( fn: ( ...args: TArgs ) => unknown, timeout: number ) {
|
|
150
|
+
let timer: ReturnType< typeof setTimeout >;
|
|
170
151
|
|
|
171
152
|
return ( ...args: TArgs ) => {
|
|
172
153
|
clearTimeout( timer );
|
package/src/sync/utils.ts
CHANGED
|
@@ -11,7 +11,8 @@ export function getV1DocumentsManager() {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function getV1DocumentsExitTo( documentData: V1Document ) {
|
|
14
|
-
const exitPreference =
|
|
14
|
+
const exitPreference =
|
|
15
|
+
( window as unknown as ExtendedWindow ).elementor?.getPreferences?.( 'exit_to' ) || 'this_post';
|
|
15
16
|
|
|
16
17
|
switch ( exitPreference ) {
|
|
17
18
|
case 'dashboard':
|
|
@@ -26,7 +27,7 @@ export function getV1DocumentsExitTo( documentData: V1Document ) {
|
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
export function getV1DocumentShowCopyAndShare( documentData: V1Document|null ) {
|
|
30
|
+
export function getV1DocumentShowCopyAndShare( documentData: V1Document | null ) {
|
|
30
31
|
return documentData?.config?.panel?.show_copy_and_share ?? false;
|
|
31
32
|
}
|
|
32
33
|
|