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://

154 lines (153 loc) 5.43 kB
import { subscribe, getSnapshot, subscribe$1, comlink, comlinkProjectId, comlinkDataset, comlinkPerspective, addQueryListener } from "../_chunks-es/VisualEditing.js"; import { VisualEditing } from "../_chunks-es/VisualEditing.js"; import { createDataAttribute } from "@sanity/visual-editing-csm"; import { createDatasetMutator, createDocumentMutator } from "../optimistic/index.js"; import { emptyActor } from "../_chunks-es/context.js"; import { useOptimistic } from "../_chunks-es/SharedStateContext.js"; import { useDocuments, useOptimisticActor } from "../_chunks-es/mutations.js"; import { useSyncExternalStore, useDeferredValue, useReducer, useEffect, useEffectEvent } from "react"; import { c } from "react/compiler-runtime"; import { stegaEncodeSourceMap } from "@sanity/client/stega"; import { dequal } from "dequal/lite"; function useVisualEditingEnvironment() { const environment = useSyncExternalStore(subscribe, getSnapshot, _temp$1); return useDeferredValue(environment, null); } function _temp$1() { return null; } function useIsPresentationTool() { const env = useVisualEditingEnvironment(); return env === null ? null : env === "presentation-iframe" || env === "presentation-window"; } function reducer(state, { type, payload }) { return type === "query-change" ? dequal(state, payload) ? state : { ...state, data: dequal(state.data, payload.data) ? state.data : payload.data, sourceMap: dequal(state.sourceMap, payload.sourceMap) ? state.sourceMap : payload.sourceMap, perspective: dequal(state.perspective, payload.perspective) ? state.perspective : payload.perspective } : state; } const initialState = { data: null, sourceMap: null, perspective: null }, EMPTY_QUERY_PARAMS = {}, LISTEN_HEARTBEAT_INTERVAL = 1e4; function usePresentationQuery(props) { const $ = c(24), [state, dispatch] = useReducer(reducer, initialState), { query, params: t0, stega: t1 } = props, params = t0 === void 0 ? EMPTY_QUERY_PARAMS : t0, stega = t1 === void 0 ? !0 : t1, comlink2 = useSyncExternalStore(subscribe$1, _temp, _temp2), projectId = useSyncExternalStore(subscribe$1, _temp3, _temp4), dataset = useSyncExternalStore(subscribe$1, _temp5, _temp6), perspective = useSyncExternalStore(subscribe$1, _temp7, _temp8); let t2; $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t2 = [], $[0] = t2) : t2 = $[0], useEffect(_temp9, t2); let t3; $[1] !== dataset || $[2] !== params || $[3] !== perspective || $[4] !== projectId || $[5] !== query ? (t3 = (comlink_0) => { !projectId || !dataset || !perspective || comlink_0.post("loader/query-listen", { projectId, dataset, perspective, query, params, heartbeat: LISTEN_HEARTBEAT_INTERVAL }); }, $[1] = dataset, $[2] = params, $[3] = perspective, $[4] = projectId, $[5] = query, $[6] = t3) : t3 = $[6]; const handleQueryHeartbeat = useEffectEvent(t3); let t4; $[7] !== dataset || $[8] !== params || $[9] !== projectId || $[10] !== query ? (t4 = (event) => { dequal({ projectId, dataset, query, params }, { projectId: event.projectId, dataset: event.dataset, query: event.query, params: event.params }) && dispatch({ type: "query-change", payload: { data: event.result, sourceMap: event.resultSourceMap || null, perspective: event.perspective } }); }, $[7] = dataset, $[8] = params, $[9] = projectId, $[10] = query, $[11] = t4) : t4 = $[11]; const handleQueryChange = useEffectEvent(t4); let t5; $[12] !== comlink2 || $[13] !== handleQueryChange || $[14] !== handleQueryHeartbeat ? (t5 = () => { if (!comlink2) return; const unsubscribe = comlink2.on("loader/query-change", handleQueryChange); handleQueryHeartbeat(comlink2); const interval = setInterval(() => handleQueryHeartbeat(comlink2), LISTEN_HEARTBEAT_INTERVAL); return () => { clearInterval(interval), unsubscribe(); }; }, $[12] = comlink2, $[13] = handleQueryChange, $[14] = handleQueryHeartbeat, $[15] = t5) : t5 = $[15]; let t6; $[16] !== comlink2 ? (t6 = [comlink2], $[16] = comlink2, $[17] = t6) : t6 = $[17], useEffect(t5, t6); let t7; bb0: { if (stega && state.sourceMap) { let t8; $[18] !== state.data || $[19] !== state.sourceMap ? (t8 = stegaEncodeSourceMap(state.data, state.sourceMap, { enabled: !0, studioUrl: "/" }), $[18] = state.data, $[19] = state.sourceMap, $[20] = t8) : t8 = $[20]; let t9; $[21] !== state || $[22] !== t8 ? (t9 = { ...state, data: t8 }, $[21] = state, $[22] = t8, $[23] = t9) : t9 = $[23], t7 = t9; break bb0; } t7 = state; } return t7; } function _temp9() { return addQueryListener(); } function _temp8() { return null; } function _temp7() { return comlinkPerspective; } function _temp6() { return null; } function _temp5() { return comlinkDataset; } function _temp4() { return null; } function _temp3() { return comlinkProjectId; } function _temp2() { return null; } function _temp() { return comlink; } export { VisualEditing, createDataAttribute, createDatasetMutator, createDocumentMutator, emptyActor, useDocuments, useIsPresentationTool, useOptimistic, useOptimisticActor, usePresentationQuery, useVisualEditingEnvironment }; //# sourceMappingURL=index.js.map