@codepress/blog 0.1.0 → 0.2.0

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.
@@ -1150,10 +1150,21 @@ function Toolbar({
1150
1150
  };
1151
1151
  }, [top]);
1152
1152
  useEffect(() => {
1153
- const update = () => setScrolled(window.scrollY > 0);
1153
+ const strip = stripRef.current;
1154
+ const update = (event) => {
1155
+ const target = event?.target;
1156
+ if (target instanceof Element) {
1157
+ if (target.contains(strip)) setScrolled(target.scrollTop > 0);
1158
+ return;
1159
+ }
1160
+ setScrolled(window.scrollY > 0);
1161
+ };
1154
1162
  update();
1155
- window.addEventListener("scroll", update, { passive: true });
1156
- return () => window.removeEventListener("scroll", update);
1163
+ document.addEventListener("scroll", update, {
1164
+ capture: true,
1165
+ passive: true
1166
+ });
1167
+ return () => document.removeEventListener("scroll", update, { capture: true });
1157
1168
  }, []);
1158
1169
  const off = frozen || formattingOff;
1159
1170
  const shortcut = (combo) => formatShortcut(combo, mac);
@@ -1843,6 +1854,7 @@ function LoadedEditor({
1843
1854
  askOpen = false,
1844
1855
  designHref,
1845
1856
  historyHref,
1857
+ controller,
1846
1858
  onReload,
1847
1859
  onExit,
1848
1860
  onPublished,
@@ -1874,6 +1886,7 @@ function LoadedEditor({
1874
1886
  const [confirmDiscard, setConfirmDiscard] = useState3(false);
1875
1887
  const [published, setPublished] = useState3(false);
1876
1888
  const [leaveWarning, setLeaveWarning] = useState3(false);
1889
+ const leaveToRef = useRef2(onExit);
1877
1890
  const [formattingOff, setFormattingOff] = useState3(false);
1878
1891
  const [linkOpen, setLinkOpen] = useState3(false);
1879
1892
  const linkShortcutRef = useRef2(() => {
@@ -2140,18 +2153,31 @@ function LoadedEditor({
2140
2153
  const composed = compose();
2141
2154
  return !composed || composed.text === savedTextRef.current;
2142
2155
  };
2143
- const handleDone = async () => {
2156
+ const attemptLeave = async (go) => {
2157
+ leaveToRef.current = go;
2144
2158
  setLeaveWarning(false);
2145
2159
  while (inflightRef.current) await inflightRef.current;
2146
2160
  if (!isPersisted()) {
2147
2161
  if (!blockedRef.current) await flushSave();
2148
2162
  if (!isPersisted()) {
2149
2163
  setLeaveWarning(true);
2150
- return;
2164
+ return false;
2151
2165
  }
2152
2166
  }
2153
- onExit();
2167
+ go();
2168
+ return true;
2154
2169
  };
2170
+ const attemptLeaveRef = useRef2(attemptLeave);
2171
+ useEffect2(() => {
2172
+ attemptLeaveRef.current = attemptLeave;
2173
+ });
2174
+ useEffect2(() => {
2175
+ if (!controller) return;
2176
+ controller.current = { leave: (go) => attemptLeaveRef.current(go) };
2177
+ return () => {
2178
+ controller.current = null;
2179
+ };
2180
+ }, [controller]);
2155
2181
  if (!editor) return null;
2156
2182
  const canSave = status !== "conflict";
2157
2183
  return /* @__PURE__ */ jsxs7("div", { "data-testid": "blog-editor", children: [
@@ -2182,7 +2208,7 @@ function LoadedEditor({
2182
2208
  confirmDiscard,
2183
2209
  onConfirmDiscardChange: setConfirmDiscard,
2184
2210
  onDiscard: handleDiscard,
2185
- onDone: handleDone,
2211
+ onDone: () => void attemptLeave(onExit),
2186
2212
  onPublish: handlePublish
2187
2213
  }
2188
2214
  )
@@ -2214,7 +2240,7 @@ function LoadedEditor({
2214
2240
  type: "button",
2215
2241
  className: "rounded-md border border-[#D9D5CC] px-3 py-1",
2216
2242
  "data-testid": "blog-leave-anyway",
2217
- onClick: onExit,
2243
+ onClick: () => leaveToRef.current(),
2218
2244
  children: "Leave without saving"
2219
2245
  }
2220
2246
  )
@@ -1,4 +1,5 @@
1
1
  import type { DraftClient, DraftFile } from "./client.js";
2
+ import type { BlogEditorControllerRef } from "./controller.js";
2
3
  import { type BlogEditorTimings } from "./config.js";
3
4
  import type { BlogEditorHeader } from "./header.js";
4
5
  /** A database site's revision, as the save status names it; null for a git site. */
@@ -29,6 +30,8 @@ export interface BlogEditorProps {
29
30
  designHref?: string | null;
30
31
  /** A database site's revision history, which its saved status links to. */
31
32
  historyHref?: string | null;
33
+ /** Filled while the loaded editor is open, so the host can leave through its save. */
34
+ controller?: BlogEditorControllerRef;
32
35
  onExit: () => void;
33
36
  onPublished: () => void;
34
37
  onDiscarded: () => void;
@@ -1,6 +1,7 @@
1
1
  import { type ReactNode } from "react";
2
2
  import { type BlogEditorAuth } from "./client.js";
3
3
  import { type BlogEditorTimings } from "./config.js";
4
+ import type { BlogEditorControllerRef } from "./controller.js";
4
5
  import type { BlogEditorHeader } from "./header.js";
5
6
  /** What the host's Ask CodePress panel is given. */
6
7
  export interface AskPanelProps {
@@ -38,6 +39,12 @@ export interface BlogEditorMountProps {
38
39
  onPublished?: (slug: string) => void;
39
40
  /** A new post was closed or discarded, so there is no reader page to return to. */
40
41
  onLeave?: () => void;
42
+ /**
43
+ * Filled while the editor is open with a loaded draft. A host that navigates without a page
44
+ * load (a client-side route change unmounts the editor, and the unload warning cannot see it)
45
+ * leaves through `controller.current.leave(go)`, which saves first or asks.
46
+ */
47
+ controller?: BlogEditorControllerRef;
41
48
  slots?: {
42
49
  /** An Ask CodePress chat beside the editor; kept mounted (hidden) once opened. */
43
50
  askPanel?: (props: AskPanelProps) => ReactNode;
@@ -61,4 +68,4 @@ export declare function appPageUrl(appUrl: string, organizationSlug: string | nu
61
68
  * the answer is `can_edit`; everyone else gets nothing and never downloads the
62
69
  * editor.
63
70
  */
64
- export declare function BlogEditorMount({ site, slug, apiBase, auth, isNew, header, column, appUrl, timings, onEditingChange, onPublished, onLeave, slots, actions, }: BlogEditorMountProps): import("react/jsx-runtime").JSX.Element | null;
71
+ export declare function BlogEditorMount({ site, slug, apiBase, auth, isNew, header, column, appUrl, timings, onEditingChange, onPublished, onLeave, controller, slots, actions, }: BlogEditorMountProps): import("react/jsx-runtime").JSX.Element | null;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * What a host holds to leave the editor without losing work. The editor guards a page unload
3
+ * itself; a client-side route change unmounts it without warning, so a host that navigates in
4
+ * place routes its navigation through `leave`.
5
+ */
6
+ export interface BlogEditorController {
7
+ /**
8
+ * Leaves the way Done does: settles and saves anything unsaved, then calls `go`. When the
9
+ * document cannot be saved (offline, rejected, conflict) the editor stays and asks: "Leave
10
+ * without saving" calls `go`, "Keep editing" does not. Resolves true when `go` ran straight away.
11
+ */
12
+ leave(go: () => void): Promise<boolean>;
13
+ }
14
+ /** Filled while an editor with a loaded draft is open, and null otherwise. */
15
+ export interface BlogEditorControllerRef {
16
+ current: BlogEditorController | null;
17
+ }
@@ -4,6 +4,7 @@
4
4
  * Edit, so importing this entry downloads no ProseMirror code.
5
5
  */
6
6
  export { BlogEditorMount, appPageUrl, type AskPanelProps, type BlogEditorMountProps, type DesignLinkContext, } from "./BlogEditorMount.js";
7
+ export type { BlogEditorController, BlogEditorControllerRef, } from "./controller.js";
7
8
  export { NewPostForm, isValidNewSlug, type NewPostFormProps, } from "./NewPostForm.js";
8
9
  export { BLOG_TOOLBAR_BOTTOM_VAR, DEFAULT_TIMINGS, type BlogEditorTimings, } from "./config.js";
9
10
  export { DEFAULT_HEADER_SELECTOR, type BlogEditorHeader } from "./header.js";
@@ -23,7 +23,7 @@ import {
23
23
  useState
24
24
  } from "react";
25
25
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
26
- var BlogEditor = lazy(() => import("../chunks/BlogEditor-VF3UAJB6.js"));
26
+ var BlogEditor = lazy(() => import("../chunks/BlogEditor-EDAPNNP5.js"));
27
27
  function appPageUrl(appUrl, organizationSlug, site, slug, tab) {
28
28
  if (!organizationSlug) return `${appUrl}/dashboard`;
29
29
  const query = new URLSearchParams({ site, post: slug });
@@ -43,6 +43,7 @@ function BlogEditorMount({
43
43
  onEditingChange,
44
44
  onPublished,
45
45
  onLeave,
46
+ controller,
46
47
  slots,
47
48
  actions
48
49
  }) {
@@ -133,6 +134,7 @@ function BlogEditorMount({
133
134
  askHref: actions?.askCodePress?.href ?? appPageUrl(appUrl, organizationSlug, site, slug),
134
135
  askIconSrc: `${appUrl}/logo-mark-square.png`,
135
136
  askOpen: ask === "open",
137
+ controller,
136
138
  onExit: () => isNew ? onLeave?.() : changeEditing(false),
137
139
  onPublished: () => onPublished ? onPublished(slug) : window.location.reload(),
138
140
  onDiscarded: () => isNew ? onLeave?.() : window.location.reload()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codepress/blog",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Render a CodePress blog: registry, sanitizer, schema, SEO, feed, sitemap, content source and React renderer.",
5
5
  "type": "module",
6
6
  "sideEffects": [