@nocode-factory/sanity-kit 2.66.0 → 2.67.0-rc.2

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.
@@ -14,10 +14,12 @@ type PreviewEditingLabels = {
14
14
  inheritedNote: string;
15
15
  /** Pastille au survol d'une image. */
16
16
  replaceImage: string;
17
- /** Interrupteur du kit (mode `toggle`). */
18
- editMode: string;
19
- browseMode: string;
20
- modeLabel: string;
17
+ /** @deprecated Sans effet depuis 2.67.0 : le kit suit l'interrupteur natif « Edit ». */
18
+ editMode?: string;
19
+ /** @deprecated Sans effet depuis 2.67.0. */
20
+ browseMode?: string;
21
+ /** @deprecated Sans effet depuis 2.67.0. */
22
+ modeLabel?: string;
21
23
  /** Indication sous un texte à plusieurs lignes : Maj + Entrée = retour à la ligne. */
22
24
  newLine: string;
23
25
  /** Contrôle automatique : « {n} à vérifier » (éléments non modifiables sur place). */
@@ -40,19 +42,14 @@ export declare const DEFAULT_PREVIEW_EDITING_LABELS: PreviewEditingLabels;
40
42
  * clic un champ de saisie à sa place exacte ; enregistré dans le brouillon ;
41
43
  * - image : « Replace this image » au survol, au clic le Studio ouvre la
42
44
  * médiathèque (pont monté par le plugin du kit) ;
43
- * - « Mode édition / Mode visite » : en visite, un clic suit les liens.
45
+ * - interrupteur natif « Edit » de Presentation (à côté de l'adresse) : éteint, la page
46
+ * se comporte comme le site (un clic suit les liens). Le kit n'a pas d'interrupteur.
44
47
  */
45
48
  export declare const ncfOverlayComponents: OverlayComponentResolver;
46
49
  type NcfPreviewEditingProps = {
47
50
  /** Libellés (anglais par défaut) : passer ceux de la langue du projet. */
48
51
  labels?: Partial<PreviewEditingLabels>;
49
- /**
50
- * `'toggle'` (défaut) : interrupteur « Edit mode / Browse mode » du kit, en bas
51
- * à droite de l'aperçu, mémorisé. Une fonction : le projet décide (ex. son propre
52
- * interrupteur d'animations) ; le kit n'affiche alors rien.
53
- */
54
- editMode?: 'toggle' | (() => boolean);
55
52
  };
56
- export declare function NcfPreviewEditing({ labels, editMode }: NcfPreviewEditingProps): import("react").JSX.Element;
53
+ export declare function NcfPreviewEditing({ labels }: NcfPreviewEditingProps): import("react").JSX.Element;
57
54
  export type { NcfPreviewEditingProps, PreviewEditingLabels };
58
55
  //# sourceMappingURL=web-editing.d.ts.map
@@ -14,9 +14,6 @@ const DEFAULT_PREVIEW_EDITING_LABELS = {
14
14
  saved: "Saved to draft",
15
15
  inheritedNote: "Component default text: editing it here creates text specific to this page.",
16
16
  replaceImage: "Replace this image",
17
- editMode: "Edit mode",
18
- browseMode: "Browse mode",
19
- modeLabel: "Preview mode: edit or browse",
20
17
  newLine: "New line",
21
18
  auditIssues: "{n} to check",
22
19
  auditHelp: "Elements of this page that cannot be edited in place (click to outline them in red)",
@@ -41,17 +38,13 @@ const listeners = /* @__PURE__ */ new Set(), emit = () => listeners.forEach((l)
41
38
  close() {
42
39
  editing = null, emit();
43
40
  }
44
- }, EDIT_MODE_ATTRIBUTE = "data-ncf-edit-mode", PREVIEW_EDIT_ATTRIBUTE = "data-ncf-preview-edit", EDIT_MODE_STORAGE_KEY = "ncf-preview-mode", defaultEditMode = () => typeof document < "u" && document.documentElement.getAttribute("data-ncf-edit-mode") !== "browse";
45
- let editModeResolver = defaultEditMode;
46
- function setEditModeResolver(resolver) {
47
- editModeResolver = resolver ?? defaultEditMode;
41
+ }, EDIT_MODE_ATTRIBUTE = "data-ncf-edit-mode", PREVIEW_EDIT_ATTRIBUTE = "data-ncf-preview-edit";
42
+ let overlayEnabled = !0;
43
+ function setOverlayEnabled(enabled) {
44
+ overlayEnabled = enabled;
48
45
  }
49
46
  function isEditMode() {
50
- try {
51
- return editModeResolver();
52
- } catch {
53
- return !1;
54
- }
47
+ return overlayEnabled;
55
48
  }
56
49
  /** Nombre de textes marqués (stega) dans un texte affiché. */
57
50
  function stegaRuns(text) {
@@ -994,6 +987,61 @@ function InlineTextOverlay({ element, node }) {
994
987
  })
995
988
  });
996
989
  }
990
+ /** Aperçu de Presentation : dans son cadre, ou dans la fenêtre qu'il a ouverte. */
991
+ function inStudio() {
992
+ try {
993
+ return window.self !== window.top || !!window.opener;
994
+ } catch {
995
+ return !0;
996
+ }
997
+ }
998
+ /**
999
+ * Nombre de cadres dessinés par Sanity (null : Sanity pas encore monté). Le cadre de
1000
+ * l'élément SÉLECTIONNÉ (champ ouvert dans le panneau du Studio, `data-focused`) reste
1001
+ * dessiné même « Edit » éteint : il ne compte pas (vu sur Paul Ricard, page Mission).
1002
+ */
1003
+ function overlayCount() {
1004
+ let root = document.querySelector("sanity-visual-editing")?.firstElementChild;
1005
+ if (!root) return null;
1006
+ let count = 0;
1007
+ for (let child of Array.from(root.children)) !child.matches("[data-focused]") && !child.querySelector("[data-focused]") && count++;
1008
+ return count;
1009
+ }
1010
+ function useNativeEditMode() {
1011
+ useEffect(() => {
1012
+ let root = document.documentElement, apply = (edit) => {
1013
+ setOverlayEnabled(edit), root.setAttribute(EDIT_MODE_ATTRIBUTE, edit ? "edit" : "browse");
1014
+ }, cleanup = () => {
1015
+ root.removeAttribute(EDIT_MODE_ATTRIBUTE), setOverlayEnabled(!0);
1016
+ };
1017
+ if (!inStudio()) return apply(!1), cleanup;
1018
+ apply(!0);
1019
+ let lastScroll = 0, observer = null, recheck = 0, read = () => {
1020
+ let count = overlayCount();
1021
+ if (count === null) return;
1022
+ let edit = isEditMode();
1023
+ if (count > 0) edit = !0;
1024
+ else {
1025
+ let since = Date.now() - lastScroll;
1026
+ since > 600 ? edit = !1 : (window.clearTimeout(recheck), recheck = window.setTimeout(read, 600 - since + 50));
1027
+ }
1028
+ edit !== isEditMode() && apply(edit);
1029
+ }, watch = () => {
1030
+ let host = document.querySelector("sanity-visual-editing");
1031
+ host && !observer && (observer = new MutationObserver(read), observer.observe(host, {
1032
+ childList: !0,
1033
+ subtree: !0
1034
+ }), lastScroll = Date.now());
1035
+ }, hostObserver = new MutationObserver(watch);
1036
+ hostObserver.observe(root, { childList: !0 }), watch();
1037
+ let onScroll = (event) => {
1038
+ event.isTrusted && (lastScroll = Date.now());
1039
+ };
1040
+ return window.addEventListener("scroll", onScroll, { passive: !0 }), window.addEventListener("resize", onScroll), () => {
1041
+ observer?.disconnect(), hostObserver.disconnect(), window.removeEventListener("scroll", onScroll), window.clearTimeout(recheck), window.removeEventListener("resize", onScroll), cleanup();
1042
+ };
1043
+ }, []);
1044
+ }
997
1045
  /**
998
1046
  * ÉDITION SUR PLACE dans l'aperçu (Presentation), à monter à côté de
999
1047
  * `<VisualEditing/>` en mode brouillon :
@@ -1007,20 +1055,18 @@ function InlineTextOverlay({ element, node }) {
1007
1055
  * clic un champ de saisie à sa place exacte ; enregistré dans le brouillon ;
1008
1056
  * - image : « Replace this image » au survol, au clic le Studio ouvre la
1009
1057
  * médiathèque (pont monté par le plugin du kit) ;
1010
- * - « Mode édition / Mode visite » : en visite, un clic suit les liens.
1058
+ * - interrupteur natif « Edit » de Presentation (à côté de l'adresse) : éteint, la page
1059
+ * se comporte comme le site (un clic suit les liens). Le kit n'a pas d'interrupteur.
1011
1060
  */
1012
1061
  const ncfOverlayComponents = (context) => {
1013
1062
  if (isEditMode() && isInlineEditablePath(context.type, context.node.path, context.element)) return InlineTextOverlay;
1014
1063
  }, EDIT_CSS = `html[${PREVIEW_EDIT_ATTRIBUTE}] a::after, html[${PREVIEW_EDIT_ATTRIBUTE}] a::before { pointer-events: none !important; }`;
1015
- function NcfPreviewEditing({ labels, editMode = "toggle" }) {
1064
+ function NcfPreviewEditing({ labels }) {
1016
1065
  let text = {
1017
1066
  ...DEFAULT_PREVIEW_EDITING_LABELS,
1018
1067
  ...labels
1019
- }, custom = typeof editMode == "function" ? editMode : null, customRef = useRef(custom);
1020
- customRef.current = custom;
1021
- let hasCustom = custom !== null;
1022
- return useOverlayRectRefresh(), useEffect(() => {
1023
- setEditModeResolver(hasCustom ? () => customRef.current?.() ?? !1 : null);
1068
+ };
1069
+ return useOverlayRectRefresh(), useNativeEditMode(), useEffect(() => {
1024
1070
  let root = document.documentElement, sync = () => {
1025
1071
  let want = isEditMode();
1026
1072
  want && !root.hasAttribute("data-ncf-preview-edit") ? root.setAttribute(PREVIEW_EDIT_ATTRIBUTE, "") : !want && root.hasAttribute("data-ncf-preview-edit") && root.removeAttribute(PREVIEW_EDIT_ATTRIBUTE);
@@ -1028,78 +1074,15 @@ function NcfPreviewEditing({ labels, editMode = "toggle" }) {
1028
1074
  sync();
1029
1075
  let observer = new MutationObserver(sync);
1030
1076
  return observer.observe(root, { attributes: !0 }), () => {
1031
- observer.disconnect(), root.removeAttribute(PREVIEW_EDIT_ATTRIBUTE), setEditModeResolver(null);
1077
+ observer.disconnect(), root.removeAttribute(PREVIEW_EDIT_ATTRIBUTE);
1032
1078
  };
1033
- }, [hasCustom]), /* @__PURE__ */ jsxs(Fragment, { children: [
1079
+ }, []), /* @__PURE__ */ jsxs(Fragment, { children: [
1034
1080
  /* @__PURE__ */ jsx("style", { children: EDIT_CSS }),
1035
1081
  /* @__PURE__ */ jsx(InlineTextEditor, { labels: text }),
1036
1082
  /* @__PURE__ */ jsx(ImageReplaceLayer, { label: text.replaceImage }),
1037
- /* @__PURE__ */ jsx(EditAudit, { labels: text }),
1038
- !hasCustom && /* @__PURE__ */ jsx(EditModeToggle, { labels: text })
1083
+ /* @__PURE__ */ jsx(EditAudit, { labels: text })
1039
1084
  ] });
1040
1085
  }
1041
- /** Interrupteur « Edit mode / Browse mode » : affiché dans l'aperçu du Studio seulement. */
1042
- function EditModeToggle({ labels }) {
1043
- let [embedded, setEmbedded] = useState(!1), [edit, setEdit] = useState(!0);
1044
- if (useEffect(() => {
1045
- let inFrame = !0;
1046
- try {
1047
- inFrame = window.self !== window.top;
1048
- } catch {
1049
- inFrame = !0;
1050
- }
1051
- setEmbedded(inFrame);
1052
- let stored = null;
1053
- try {
1054
- stored = window.localStorage.getItem(EDIT_MODE_STORAGE_KEY);
1055
- } catch {
1056
- stored = null;
1057
- }
1058
- let next = stored !== "browse";
1059
- return setEdit(next), document.documentElement.setAttribute(EDIT_MODE_ATTRIBUTE, next ? "edit" : "browse"), () => document.documentElement.removeAttribute(EDIT_MODE_ATTRIBUTE);
1060
- }, []), !embedded) return null;
1061
- let toggle = () => {
1062
- let next = !edit;
1063
- setEdit(next), document.documentElement.setAttribute(EDIT_MODE_ATTRIBUTE, next ? "edit" : "browse");
1064
- try {
1065
- window.localStorage.setItem(EDIT_MODE_STORAGE_KEY, next ? "edit" : "browse");
1066
- } catch {}
1067
- };
1068
- return /* @__PURE__ */ jsxs("button", {
1069
- type: "button",
1070
- role: "switch",
1071
- "aria-checked": edit,
1072
- "aria-label": labels.modeLabel,
1073
- onClick: toggle,
1074
- "data-sanity-overlay-element": !0,
1075
- style: {
1076
- position: "fixed",
1077
- right: 16,
1078
- bottom: 16,
1079
- zIndex: 2147482500,
1080
- display: "flex",
1081
- alignItems: "center",
1082
- gap: 8,
1083
- padding: "8px 14px 8px 10px",
1084
- border: 0,
1085
- borderRadius: 999,
1086
- background: CHIP,
1087
- color: "#fff",
1088
- boxShadow: "0 8px 24px rgba(0,0,0,0.25)",
1089
- font: `500 12px/1 ${FONT}`,
1090
- cursor: "pointer"
1091
- },
1092
- children: [/* @__PURE__ */ jsx("span", {
1093
- "aria-hidden": !0,
1094
- style: {
1095
- width: 8,
1096
- height: 8,
1097
- borderRadius: 999,
1098
- background: edit ? "#3ecf8e" : "rgba(255,255,255,0.45)"
1099
- }
1100
- }), edit ? labels.editMode : labels.browseMode]
1101
- });
1102
- }
1103
1086
  /**
1104
1087
  * Cadres de Sanity à la bonne place. L'overlay mémorise la position de chaque
1105
1088
  * élément et ne la remesure qu'au redimensionnement : une page qui se décale