@limitless-angular/sanity
Version:
A powerful Angular library for Sanity.io integration, featuring Portable Text rendering and optimized image loading.
96 lines (92 loc) • 3.28 kB
JavaScript
import { MSG_HANDSHAKE_SYN, MSG_HANDSHAKE_SYN_ACK, MSG_HANDSHAKE_ACK, MSG_RESPONSE, MSG_HEARTBEAT, MSG_DISCONNECT, DOMAIN, createListenLogic, createRequestMachine } from '@sanity/comlink';
export { MSG_RESPONSE } from '@sanity/comlink';
const channelsToComlinkMap = {
'handshake/syn': MSG_HANDSHAKE_SYN,
'handshake/syn-ack': MSG_HANDSHAKE_SYN_ACK,
'handshake/ack': MSG_HANDSHAKE_ACK,
'channel/response': MSG_RESPONSE,
'channel/heartbeat': MSG_HEARTBEAT,
'channel/disconnect': MSG_DISCONNECT,
'overlay/focus': 'visual-editing/focus',
'overlay/navigate': 'visual-editing/navigate',
'overlay/toggle': 'visual-editing/toggle',
'presentation/toggleOverlay': 'presentation/toggle-overlay',
};
const comlinkToChannelsMap = {
[]: 'handshake/syn',
[]: 'handshake/syn-ack',
[]: 'handshake/ack',
[]: 'channel/response',
[]: 'channel/heartbeat',
[]: 'channel/disconnect',
'visual-editing/focus': 'overlay/focus',
'visual-editing/navigate': 'overlay/navigate',
'visual-editing/toggle': 'overlay/toggle',
'presentation/toggle-overlay': 'presentation/toggleOverlay',
};
const convertToComlinkEvent = (event) => {
const { data } = event;
if (data &&
typeof data === 'object' &&
'domain' in data &&
'type' in data &&
'from' in data &&
'to' in data) {
if (data.domain === 'sanity/channels') {
data.domain = DOMAIN;
}
if (data.to === 'overlays') {
data.to = 'visual-editing';
}
if (data.from === 'overlays') {
data.from = 'visual-editing';
}
data.type =
channelsToComlinkMap[data.type] ?? data.type;
}
return event;
};
const convertToChannelsMessage = (message) => {
if (message.domain === DOMAIN) {
message.domain = 'sanity/channels';
}
if (message.to === 'visual-editing') {
message.to = 'overlays';
}
if (message.from === 'visual-editing') {
message.from = 'overlays';
}
message.type =
comlinkToChannelsMap[message.type] ?? message.type;
if (message.type === 'channel/response' &&
message.responseTo &&
!message.data) {
message.data = { responseTo: message.responseTo };
}
if (message.type === 'handshake/syn' ||
message.type === 'handshake/syn-ack' ||
message.type === 'handshake/ack') {
message.data = { id: message.connectionId };
}
return message;
};
const sendAsChannelsMessage = ({ context }, params) => {
const { sources, targetOrigin } = context;
const message = convertToChannelsMessage(params.message);
sources.forEach((source) => {
source.postMessage(message, { targetOrigin });
});
};
const createCompatibilityActors = () => ({
listen: createListenLogic(convertToComlinkEvent),
requestMachine: createRequestMachine().provide({
actions: {
'send message': sendAsChannelsMessage,
},
}),
});
/**
* Generated bundle index. Do not edit.
*/
export { createCompatibilityActors };
//# sourceMappingURL=limitless-angular-sanity-visual-editing-helpers.mjs.map