@elementor/editor-documents 0.11.3 → 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/src/init.ts CHANGED
@@ -1,9 +1,16 @@
1
1
  import { syncStore } from './sync';
2
2
  import { __registerSlice } from '@elementor/store';
3
3
  import { slice } from './store';
4
+ import { injectIntoLogic } from '@elementor/editor';
5
+ import { LogicHooks } from './components/logic-hooks';
4
6
 
5
7
  export default function init() {
6
8
  initStore();
9
+
10
+ injectIntoLogic( {
11
+ id: 'documents-hooks',
12
+ component: LogicHooks,
13
+ } );
7
14
  }
8
15
 
9
16
  function initStore() {
@@ -2,10 +2,10 @@ import { Document } from '../types';
2
2
  import { __createSlice, PayloadAction } from '@elementor/store';
3
3
 
4
4
  type State = {
5
- entities: Record<Document['id'], Document>,
6
- activeId: Document['id'] | null, // The currently editing document.
7
- hostId: Document['id'] | null, // The document that host all the other documents.
8
- }
5
+ entities: Record< Document[ 'id' ], Document >;
6
+ activeId: Document[ 'id' ] | null; // The currently editing document.
7
+ hostId: Document[ 'id' ] | null; // The document that host all the other documents.
8
+ };
9
9
 
10
10
  const initialState: State = {
11
11
  entities: {},
@@ -13,7 +13,7 @@ const initialState: State = {
13
13
  hostId: null,
14
14
  };
15
15
 
16
- type StateWithActiveId = Omit<State, 'activeId'> & { activeId: NonNullable<State['activeId']> };
16
+ type StateWithActiveId = Omit< State, 'activeId' > & { activeId: NonNullable< State[ 'activeId' ] > };
17
17
 
18
18
  function hasActiveEntity( state: State ): state is StateWithActiveId {
19
19
  return !! ( state.activeId && state.entities[ state.activeId ] );
@@ -23,22 +23,22 @@ export const slice = __createSlice( {
23
23
  name: 'documents',
24
24
  initialState,
25
25
  reducers: {
26
- init( state, { payload } : PayloadAction<State> ) {
26
+ init( state, { payload }: PayloadAction< State > ) {
27
27
  state.entities = payload.entities;
28
28
  state.hostId = payload.hostId;
29
29
  state.activeId = payload.activeId;
30
30
  },
31
31
 
32
- activateDocument( state, action: PayloadAction<Document> ) {
32
+ activateDocument( state, action: PayloadAction< Document > ) {
33
33
  state.entities[ action.payload.id ] = action.payload;
34
34
  state.activeId = action.payload.id;
35
35
  },
36
36
 
37
- setAsHost( state, action: PayloadAction<Document['id']> ) {
37
+ setAsHost( state, action: PayloadAction< Document[ 'id' ] > ) {
38
38
  state.hostId = action.payload;
39
39
  },
40
40
 
41
- updateActiveDocument( state, action: PayloadAction<Partial<Document>> ) {
41
+ updateActiveDocument( state, action: PayloadAction< Partial< Document > > ) {
42
42
  if ( hasActiveEntity( state ) ) {
43
43
  state.entities[ state.activeId ] = {
44
44
  ...state.entities[ state.activeId ],
@@ -53,7 +53,7 @@ export const slice = __createSlice( {
53
53
  }
54
54
  },
55
55
 
56
- endSaving( state, action: PayloadAction<Document> ) {
56
+ endSaving( state, action: PayloadAction< Document > ) {
57
57
  if ( hasActiveEntity( state ) ) {
58
58
  state.entities[ state.activeId ] = {
59
59
  ...action.payload,
@@ -68,7 +68,7 @@ export const slice = __createSlice( {
68
68
  }
69
69
  },
70
70
 
71
- endSavingDraft( state, action: PayloadAction<Document> ) {
71
+ endSavingDraft( state, action: PayloadAction< Document > ) {
72
72
  if ( hasActiveEntity( state ) ) {
73
73
  state.entities[ state.activeId ] = {
74
74
  ...action.payload,
@@ -1,24 +1,16 @@
1
1
  import { slice } from './index';
2
2
  import { __createSelector, SliceState } from '@elementor/store';
3
3
 
4
- type State = SliceState<typeof slice>;
4
+ type State = SliceState< typeof slice >;
5
5
 
6
6
  const selectEntities = ( state: State ) => state.documents.entities;
7
7
  const selectActiveId = ( state: State ) => state.documents.activeId;
8
8
  const selectHostId = ( state: State ) => state.documents.hostId;
9
9
 
10
- export const selectActiveDocument = __createSelector(
11
- selectEntities,
12
- selectActiveId,
13
- ( entities, activeId ) => activeId && entities[ activeId ]
14
- ? entities[ activeId ]
15
- : null,
10
+ export const selectActiveDocument = __createSelector( selectEntities, selectActiveId, ( entities, activeId ) =>
11
+ activeId && entities[ activeId ] ? entities[ activeId ] : null
16
12
  );
17
13
 
18
- export const selectHostDocument = __createSelector(
19
- selectEntities,
20
- selectHostId,
21
- ( entities, hostId ) => hostId && entities[ hostId ]
22
- ? entities[ hostId ]
23
- : null,
14
+ export const selectHostDocument = __createSelector( selectEntities, selectHostId, ( entities, hostId ) =>
15
+ hostId && entities[ hostId ] ? entities[ hostId ] : null
24
16
  );
@@ -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
- ] )( 'should sync host document when a new host is opened { openAsHost: $openAsHost }', ( { openAsHost, expectedHost } ) => {
164
- // Arrange.
165
- const mockDocument1 = makeMockV1Document( { id: 1 } );
166
- const mockDocument2 = makeMockV1Document( { id: 2 } );
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
- mockV1DocumentsManager( [
169
- mockDocument1,
170
- mockDocument2,
171
- ], 'this_post', 1, 1 );
164
+ mockV1DocumentsManager( [ mockDocument1, mockDocument2 ], 'this_post', 1, 1 );
172
165
 
173
- // Populate the documents state.
174
- dispatchV1ReadyEvent();
166
+ // Populate the documents state.
167
+ dispatchV1ReadyEvent();
175
168
 
176
- // Act - Mock a host document change.
177
- mockV1DocumentsManager( [
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
- dispatchCommandAfter( 'editor/documents/open' );
172
+ dispatchCommandAfter( 'editor/documents/open' );
183
173
 
184
- // Assert.
185
- expect( store.getState().documents.hostId ).toBe( expectedHost );
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
- ...mockDocument,
273
- config: {
274
- ...mockDocument.config,
275
- revisions: {
276
- current_id: 2,
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
- makeMockV1Document( {
378
- id: 1,
379
- status: 'draft',
380
- title: 'test',
381
- } ),
382
- ], 'this_post' );
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
- makeMockV1Document( {
390
- id: 1,
391
- status: 'publish',
392
- title: 'test title changed',
393
- } ),
394
- ], 'dashboard' );
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( 'https://localhost/wp-admin/post.php?post=1&action=edit' );
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
- 'dashboard',
412
- 'this_post',
413
- 'all_posts',
414
- ] as ExitTo[] )( 'should sync active document $ExitTo', ( exitTo ) => {
415
- // Arrange.
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
- // Populate the documents state.
422
- dispatchV1ReadyEvent();
412
+ // Populate the documents state.
413
+ dispatchV1ReadyEvent();
423
414
 
424
- // Assert.
425
- const currentDocument = selectActiveDocument( store.getState() );
426
- const platformEdit = getV1DocumentsExitTo( mockDocument );
427
- const permalink = getV1DocumentPermalink( mockDocument );
415
+ // Assert.
416
+ const currentDocument = selectActiveDocument( store.getState() );
417
+ const platformEdit = getV1DocumentsExitTo( mockDocument );
418
+ const permalink = getV1DocumentPermalink( mockDocument );
428
419
 
429
- expect( currentDocument ).toEqual<Document>( {
430
- id: 1,
431
- title: 'Document 1',
432
- type: {
433
- value: 'wp-page',
434
- label: 'WP-PAGE',
435
- },
436
- links: {
437
- platformEdit,
438
- permalink,
439
- },
440
- status: {
441
- value: 'publish',
442
- label: 'PUBLISH',
443
- },
444
- isDirty: false,
445
- isSaving: false,
446
- isSavingDraft: false,
447
- userCan: {
448
- publish: true,
449
- },
450
- permissions: {
451
- allowAddingWidgets: true,
452
- showCopyAndShare: false,
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( documentsArray: V1Document[], exitTo: ExitTo = 'this_post', current = 1, initial = 1 ) {
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( new CustomEvent( 'elementor/commands/run/before', {
5
- detail: {
6
- command,
7
- args,
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( new CustomEvent( 'elementor/commands/run/after', {
14
- detail: {
15
- command,
16
- args,
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
  }