-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(live-preview): add functionality to subscribe to the save event …
…of an entity
- Loading branch information
1 parent
29c9eeb
commit ac9a8bc
Showing
9 changed files
with
140 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { debug } from './helpers'; | ||
import { EntrySavedMessage, LivePreviewPostMessageMethods, MessageFromEditor } from './messages'; | ||
import { SubscribeCallback } from './types'; | ||
import { getEntryList } from './utils'; | ||
|
||
export class SaveEvent { | ||
locale: string; | ||
subscription: SubscribeCallback | undefined; | ||
|
||
constructor({ locale }: { locale: string }) { | ||
this.locale = locale; | ||
} | ||
|
||
public subscribe(cb: SubscribeCallback): VoidFunction { | ||
if (this.subscription) { | ||
debug.log( | ||
'There is already a subscription for the save event, the existing will be replaced.' | ||
); | ||
} | ||
|
||
this.subscription = cb; | ||
|
||
// TODO: How would we like to handle this for the subscribe event? | ||
// sendMessageToEditor(LivePreviewPostMessageMethods.SUBSCRIBED, { | ||
// action: LivePreviewPostMessageMethods.SUBSCRIBED, | ||
// type: isGQL ? 'GQL' : 'REST', // we don't know that | ||
// locale, | ||
// entryId: sysId, // we don't have that | ||
// event: 'save', | ||
// } as SubscribedMessage); | ||
|
||
return () => { | ||
this.subscription = undefined; | ||
}; | ||
} | ||
|
||
public receiveMessage(message: MessageFromEditor): void { | ||
if (message.method === LivePreviewPostMessageMethods.ENTRY_SAVED && this.subscription) { | ||
const { entity } = message as EntrySavedMessage; | ||
const entries = getEntryList(); | ||
|
||
if (entries.includes(entity.sys.id)) { | ||
this.subscription(entity); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { TagAttributes } from './types'; | ||
|
||
/** | ||
* Returns a list of tagged entries on the page | ||
*/ | ||
export function getEntryList(): string[] { | ||
return [ | ||
...new Set( | ||
[...document.querySelectorAll(TagAttributes.ENTRY_ID)] | ||
.map((element) => element.getAttribute(TagAttributes.ENTRY_ID)) | ||
.filter(Boolean) as string[] | ||
), | ||
]; | ||
} |