UNPKG

@sanity/visual-editing

Version:

[![npm stat](https://img.shields.io/npm/dm/@sanity/visual-editing.svg?style=flat-square)](https://npm-stat.com/charts.html?package=@sanity/visual-editing) [![npm version](https://img.shields.io/npm/v/@sanity/visual-editing.svg?style=flat-square)](https://

2 lines (1 loc) 2.93 kB
import{createListenLogic as e,createRequestMachine as t,DOMAIN as n,MSG_HANDSHAKE_SYN as a,MSG_HANDSHAKE_SYN_ACK as o,MSG_HANDSHAKE_ACK as i,MSG_RESPONSE as r,MSG_HEARTBEAT as s,MSG_DISCONNECT as c}from"@sanity/comlink";import{studioPath as l}from"@sanity/client/csm";import{e as d}from"./transformSanityNodeData.js";const p={"handshake/syn":a,"handshake/syn-ack":o,"handshake/ack":i,"channel/response":r,"channel/heartbeat":s,"channel/disconnect":c,"overlay/focus":"visual-editing/focus","overlay/navigate":"visual-editing/navigate","overlay/toggle":"visual-editing/toggle","presentation/toggleOverlay":"presentation/toggle-overlay"},y={[a]:"handshake/syn",[o]:"handshake/syn-ack",[i]:"handshake/ack",[r]:"channel/response",[s]:"channel/heartbeat",[c]:"channel/disconnect","visual-editing/focus":"overlay/focus","visual-editing/navigate":"overlay/navigate","visual-editing/toggle":"overlay/toggle","presentation/toggle-overlay":"presentation/toggleOverlay"},h=e=>{const{data:t}=e;return t&&"object"==typeof t&&"domain"in t&&"type"in t&&"from"in t&&"to"in t&&("sanity/channels"===t.domain&&(t.domain=n),"overlays"===t.to&&(t.to="visual-editing"),"overlays"===t.from&&(t.from="visual-editing"),t.channelId=t.connectionId,delete t.connectionId,t.type=p[t.type]??t.type),e},g=({context:e},t)=>{const{sources:a,targetOrigin:o}=e,i=(e=>{const{channelId:t,...a}=e,o={...a,connectionId:t};return o.domain===n&&(o.domain="sanity/channels"),"visual-editing"===o.to&&(o.to="overlays"),"visual-editing"===o.from&&(o.from="overlays"),o.type=y[o.type]??o.type,"channel/response"===o.type&&o.responseTo&&!o.data&&(o.data={responseTo:o.responseTo}),("handshake/syn"===o.type||"handshake/syn-ack"===o.type||"handshake/ack"===o.type)&&(o.data={id:o.connectionId}),o})(t.message);a.forEach((e=>{e.postMessage(i,{targetOrigin:o})}))},u=()=>({listen:e(h),requestMachine:t().provide({actions:{"send message":g}})});function f(e){function t(e){return e?"string"==typeof e?l.fromString(e):e:[]}function n(e){if(!e.id)throw new Error("`id` is required to create a data attribute");if(!e.type)throw new Error("`type` is required to create a data attribute");if(!e.path||!e.path.length)throw new Error("`path` is required to create a data attribute");const t={baseUrl:e.baseUrl||"/",workspace:e.workspace,tool:e.tool,type:e.type,id:e.id,path:"string"==typeof e.path?e.path:l.toString(e.path)};return d(t)}const a=a=>n({...e,path:[...t(e.path),...t(a)]});return a.toString=function(){return n(e)},a.combine=function(t){return f({...e,...t})},a.scope=function(n){return f({...e,path:[...t(e.path),...t(n)]})},a}const v={alt:"altKey",ctrl:"ctrlKey",mod:typeof window<"u"&&/Mac|iPod|iPhone|iPad/.test(window.navigator.platform)?"metaKey":"ctrlKey",shift:"shiftKey"};function m(e,t){return e.every((e=>v[e]?t[v[e]]:t.key===e.toUpperCase()))}function k(e){return"Alt"===e.key}export{m as a,u as b,f as c,k as i};//# sourceMappingURL=create-data-attribute.js.map