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

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,52 @@ 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
+ /** Nombre de cadres dessinés par Sanity (null : Sanity pas encore monté). */
999
+ function overlayCount() {
1000
+ let root = document.querySelector("sanity-visual-editing")?.firstElementChild;
1001
+ return root ? root.childElementCount : null;
1002
+ }
1003
+ function useNativeEditMode() {
1004
+ useEffect(() => {
1005
+ let root = document.documentElement, apply = (edit) => {
1006
+ setOverlayEnabled(edit), root.setAttribute(EDIT_MODE_ATTRIBUTE, edit ? "edit" : "browse");
1007
+ }, cleanup = () => {
1008
+ root.removeAttribute(EDIT_MODE_ATTRIBUTE), setOverlayEnabled(!0);
1009
+ };
1010
+ if (!inStudio()) return apply(!1), cleanup;
1011
+ apply(!0);
1012
+ let lastScroll = 0, observer = null, read = () => {
1013
+ let count = overlayCount();
1014
+ if (count === null) return;
1015
+ let edit = isEditMode();
1016
+ count > 0 ? edit = !0 : Date.now() - lastScroll > 600 && (edit = !1), edit !== isEditMode() && apply(edit);
1017
+ }, watch = () => {
1018
+ let host = document.querySelector("sanity-visual-editing");
1019
+ host && !observer && (observer = new MutationObserver(read), observer.observe(host, {
1020
+ childList: !0,
1021
+ subtree: !0
1022
+ }), lastScroll = Date.now());
1023
+ }, hostObserver = new MutationObserver(watch);
1024
+ hostObserver.observe(root, { childList: !0 }), watch();
1025
+ let onScroll = () => {
1026
+ lastScroll = Date.now();
1027
+ };
1028
+ return window.addEventListener("scroll", onScroll, {
1029
+ passive: !0,
1030
+ capture: !0
1031
+ }), window.addEventListener("resize", onScroll), () => {
1032
+ observer?.disconnect(), hostObserver.disconnect(), window.removeEventListener("scroll", onScroll, { capture: !0 }), window.removeEventListener("resize", onScroll), cleanup();
1033
+ };
1034
+ }, []);
1035
+ }
997
1036
  /**
998
1037
  * ÉDITION SUR PLACE dans l'aperçu (Presentation), à monter à côté de
999
1038
  * `<VisualEditing/>` en mode brouillon :
@@ -1007,20 +1046,18 @@ function InlineTextOverlay({ element, node }) {
1007
1046
  * clic un champ de saisie à sa place exacte ; enregistré dans le brouillon ;
1008
1047
  * - image : « Replace this image » au survol, au clic le Studio ouvre la
1009
1048
  * médiathèque (pont monté par le plugin du kit) ;
1010
- * - « Mode édition / Mode visite » : en visite, un clic suit les liens.
1049
+ * - interrupteur natif « Edit » de Presentation (à côté de l'adresse) : éteint, la page
1050
+ * se comporte comme le site (un clic suit les liens). Le kit n'a pas d'interrupteur.
1011
1051
  */
1012
1052
  const ncfOverlayComponents = (context) => {
1013
1053
  if (isEditMode() && isInlineEditablePath(context.type, context.node.path, context.element)) return InlineTextOverlay;
1014
1054
  }, EDIT_CSS = `html[${PREVIEW_EDIT_ATTRIBUTE}] a::after, html[${PREVIEW_EDIT_ATTRIBUTE}] a::before { pointer-events: none !important; }`;
1015
- function NcfPreviewEditing({ labels, editMode = "toggle" }) {
1055
+ function NcfPreviewEditing({ labels }) {
1016
1056
  let text = {
1017
1057
  ...DEFAULT_PREVIEW_EDITING_LABELS,
1018
1058
  ...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);
1059
+ };
1060
+ return useOverlayRectRefresh(), useNativeEditMode(), useEffect(() => {
1024
1061
  let root = document.documentElement, sync = () => {
1025
1062
  let want = isEditMode();
1026
1063
  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 +1065,15 @@ function NcfPreviewEditing({ labels, editMode = "toggle" }) {
1028
1065
  sync();
1029
1066
  let observer = new MutationObserver(sync);
1030
1067
  return observer.observe(root, { attributes: !0 }), () => {
1031
- observer.disconnect(), root.removeAttribute(PREVIEW_EDIT_ATTRIBUTE), setEditModeResolver(null);
1068
+ observer.disconnect(), root.removeAttribute(PREVIEW_EDIT_ATTRIBUTE);
1032
1069
  };
1033
- }, [hasCustom]), /* @__PURE__ */ jsxs(Fragment, { children: [
1070
+ }, []), /* @__PURE__ */ jsxs(Fragment, { children: [
1034
1071
  /* @__PURE__ */ jsx("style", { children: EDIT_CSS }),
1035
1072
  /* @__PURE__ */ jsx(InlineTextEditor, { labels: text }),
1036
1073
  /* @__PURE__ */ jsx(ImageReplaceLayer, { label: text.replaceImage }),
1037
- /* @__PURE__ */ jsx(EditAudit, { labels: text }),
1038
- !hasCustom && /* @__PURE__ */ jsx(EditModeToggle, { labels: text })
1074
+ /* @__PURE__ */ jsx(EditAudit, { labels: text })
1039
1075
  ] });
1040
1076
  }
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
1077
  /**
1104
1078
  * Cadres de Sanity à la bonne place. L'overlay mémorise la position de chaque
1105
1079
  * élément et ne la remesure qu'au redimensionnement : une page qui se décale