eddev 2.0.0-beta.26 → 2.0.0-beta.28

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.
@@ -7,7 +7,7 @@ export function EditableText({ id, as, appendOnEnter, store, ...props }) {
7
7
  if (!readOnly) {
8
8
  const [value, setValue] = useValueStore(store ?? id);
9
9
  const appendBlocks = useBlockAppender();
10
- return (_jsx(wp.blockEditor.RichText, { ...props, tagName: as, value: value || "", onChange: setValue, disableLineBreaks: props.disableLineBreaks, onKeyDownCapture: (e) => {
10
+ return (_jsx(wp.blockEditor.RichText, { ...props, tagName: as, value: value || "", onChange: setValue, inlineToolbar: props.inlineToolbar, disableLineBreaks: props.disableLineBreaks, onKeyDownCapture: (e) => {
11
11
  if (e.key === "Enter" && appendOnEnter && appendBlocks) {
12
12
  appendBlocks([
13
13
  wp.blocks.createBlock(typeof appendOnEnter === "string" ? appendOnEnter : "core/paragraph"),
@@ -9,6 +9,7 @@ export class ErrorBoundaryFrontend extends Component {
9
9
  return { hasError: true, error: err };
10
10
  }
11
11
  componentDidCatch(error, errorInfo) {
12
+ console.log("Caught error:", error, errorInfo);
12
13
  console.error("Uncaught error:", error, errorInfo);
13
14
  }
14
15
  render() {
@@ -2,9 +2,19 @@ import { FunctionComponent } from "react";
2
2
  import { ContentBlockLayoutProps } from "./ContentBlocks.js";
3
3
  import { BlockTemplate } from "./editor/block-templates.js";
4
4
  type AppenderConfig = {
5
- type: "default" | "button" | FunctionComponent<any>;
5
+ type: "default" | "button" | CustomBlockAppender;
6
6
  className?: string;
7
7
  };
8
+ export type CustomBlockAppender = FunctionComponent<{
9
+ onToggle: () => void;
10
+ disabled: boolean;
11
+ isOpen: boolean;
12
+ blockTitle: string;
13
+ hasSingleBlockType: boolean;
14
+ }>;
15
+ export declare function createAppender(comp: CustomBlockAppender): {
16
+ type: CustomBlockAppender;
17
+ };
8
18
  type InnerBlocksProps = {
9
19
  /** A list of allowed blocks, and/or block tags. */
10
20
  allowedBlocks?: (ChildBlockTypeName | BlockTagName)[];
@@ -9,7 +9,10 @@ const Appender = (props) => {
9
9
  return _jsx(wp.blockEditor.ButtonBlockAppender, { className: props.className, rootClientId: clientId });
10
10
  }
11
11
  else if (typeof props.type === "function") {
12
- return props.type({});
12
+ const Type = props.type;
13
+ return (_jsx(wp.blockEditor.Inserter, { renderToggle: (p) => {
14
+ return _jsx(Type, { ...p });
15
+ }, rootClientId: clientId }));
13
16
  }
14
17
  else {
15
18
  return (_jsx(wp.blockEditor.DefaultBlockAppender
@@ -19,6 +22,11 @@ const Appender = (props) => {
19
22
  className: props.className, rootClientId: clientId, lastBlockClientId: clientId }));
20
23
  }
21
24
  };
25
+ export function createAppender(comp) {
26
+ return {
27
+ type: comp,
28
+ };
29
+ }
22
30
  /**
23
31
  * Allows child blocks to be added to the current block context.
24
32
  */
@@ -13,6 +13,7 @@ export type APIConfigStore = {
13
13
  /**
14
14
  * Update aspects of the API client
15
15
  */
16
+ onResponse?: (response: Response) => void;
16
17
  set: (config: Partial<APIConfigStore>) => void;
17
18
  };
18
19
  export declare const apiConfig: APIConfigStore;
@@ -30,6 +30,8 @@ const fetchGETQuery = async (name, params, opts) => {
30
30
  options = await apiConfig.customQueryFetchOptions?.("query", name, params, url, options);
31
31
  }
32
32
  const response = await fetch(url, options);
33
+ if (apiConfig.onResponse)
34
+ apiConfig.onResponse(response);
33
35
  const payload = await response.json();
34
36
  if (payload.errors?.length > 0) {
35
37
  throw createQueryError(payload.errors.map((e) => e.message), response.status);
@@ -153,6 +155,8 @@ const fetchMutation = async (name, params, opts) => {
153
155
  options = await apiConfig.customQueryFetchOptions?.("mutation", name, params, url, options);
154
156
  }
155
157
  const response = await fetch(url, options);
158
+ if (apiConfig.onResponse)
159
+ apiConfig.onResponse(response);
156
160
  const payload = await response.json();
157
161
  if (payload.errors?.length > 0) {
158
162
  throw createQueryError(payload.errors.map((e) => e.message), response.status);
@@ -0,0 +1,39 @@
1
+ import { ReactNode } from "react";
2
+ import { RouteState } from "../types";
3
+ type BackButtonProps = {
4
+ /**
5
+ * An optional matching function, which will stop the render button from appearing unless the function returns true.
6
+ *
7
+ * The below example will only render a back button if the last route was a project archive.
8
+ *
9
+ * eg. `filter={(route) => route.view === 'archive-projects'}`
10
+ *
11
+ * @param route The last route in the history stack.
12
+ */
13
+ filter?: (route: RouteState) => boolean;
14
+ /**
15
+ * An optional href, which will be used if no back route is found.
16
+ *
17
+ * When used, the back button will always render.
18
+ */
19
+ fallbackHref?: string;
20
+ /**
21
+ * A function to render the back button, if one should be rendered.
22
+ *
23
+ * Receives a `route` prop to further inspect the route, and an `onClick` function to navigate back.
24
+ *
25
+ * @param props
26
+ * @returns
27
+ */
28
+ render: (props: {
29
+ route: RouteState;
30
+ onClick: (e?: any) => void;
31
+ }) => ReactNode;
32
+ };
33
+ /**
34
+ * Display a back button that will navigate to the previous page in the router's history.
35
+ *
36
+ * This will allow you to render a back button on the condition that the back button will not send the user to a different website.
37
+ */
38
+ export declare function BackButton(props: BackButtonProps): ReactNode;
39
+ export {};
@@ -0,0 +1,45 @@
1
+ import { useMemo } from "react";
2
+ import { useRouter } from "../hooks/useRouter";
3
+ import { useRouterState } from "../hooks/useRouterState";
4
+ import { getLinkHandlerMode } from "../utils";
5
+ import { useRoute } from "../hooks/useRoute";
6
+ /**
7
+ * Display a back button that will navigate to the previous page in the router's history.
8
+ *
9
+ * This will allow you to render a back button on the condition that the back button will not send the user to a different website.
10
+ */
11
+ export function BackButton(props) {
12
+ const router = useRouter();
13
+ const route = useRoute();
14
+ const prevRoute = useRouterState((state) => state.history[state.history.length - 2]);
15
+ const backAction = useMemo(() => {
16
+ if (prevRoute && (!props.filter || props.filter(prevRoute))) {
17
+ return (e) => {
18
+ const { mode } = getLinkHandlerMode(e, prevRoute.uri);
19
+ if (mode === "navigate") {
20
+ e.preventDefault();
21
+ history.back();
22
+ // history.length
23
+ }
24
+ else if (mode === "ignore") {
25
+ e.preventDefault();
26
+ }
27
+ };
28
+ }
29
+ else if (props.fallbackHref) {
30
+ return (e) => {
31
+ const { mode } = getLinkHandlerMode(e, props.fallbackHref);
32
+ if (mode === "navigate") {
33
+ e.preventDefault();
34
+ router.navigate(props.fallbackHref);
35
+ }
36
+ else if (mode === "ignore") {
37
+ e.preventDefault();
38
+ }
39
+ };
40
+ }
41
+ }, [route, props.filter, props.fallbackHref]);
42
+ if (backAction) {
43
+ return props.render ? props.render({ route: prevRoute, onClick: backAction }) : null;
44
+ }
45
+ }
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useEffect, useMemo, useRef, useState, useTransition } from "react";
3
- import { isRelative, parseURL, resolveURL } from "ufo";
3
+ import { isEqual, isRelative, parseURL, resolveURL } from "ufo";
4
4
  import { RouterContext, RouterStateContext } from "../context.js";
5
5
  import { RouteLoader } from "../loader.js";
6
6
  import { getLinkHandlerMode, getRouteMeta, normalizeRoute, parseQuery, stringifyRouteLink } from "../utils.js";
@@ -28,6 +28,7 @@ if (env.client && !env.admin) {
28
28
  tags: initialData.meta?.head,
29
29
  },
30
30
  });
31
+ history.replaceState(historyStateForRoute(initialRoute), "", document.location.href);
31
32
  $routeMetaStore.data = getRouteMeta(initialData);
32
33
  }
33
34
  function parseHrefPath(url) {
@@ -120,6 +121,7 @@ export function BrowserRouter(props) {
120
121
  }
121
122
  internals.poppedState = (href, data) => {
122
123
  const stack = state.history;
124
+ console.log("Popped state", stack);
123
125
  // Is the route in our history stack? (going back)
124
126
  const index = stack.findIndex((item) => item.id === data.id);
125
127
  if (index >= 0) {
@@ -255,6 +257,9 @@ export function BrowserRouter(props) {
255
257
  goingBack: false,
256
258
  });
257
259
  },
260
+ getState() {
261
+ return state;
262
+ },
258
263
  replaceHash(hash) {
259
264
  replaceRoute({
260
265
  ...getActiveRoute(),
@@ -305,7 +310,7 @@ export function BrowserRouter(props) {
305
310
  subscribers.delete(fn);
306
311
  };
307
312
  },
308
- handleClickEvent(e, originalHref) {
313
+ handleClickEvent(e, originalHref, preferBack) {
309
314
  const { mode, href } = getLinkHandlerMode(e, originalHref);
310
315
  if (mode === "ignore") {
311
316
  e.preventDefault();
@@ -316,10 +321,50 @@ export function BrowserRouter(props) {
316
321
  }
317
322
  else if (mode === "navigate" && href) {
318
323
  e.preventDefault();
324
+ if (preferBack) {
325
+ const lastState = state.history.length > 1 && state.history[state.history.length - 2];
326
+ if (lastState && isEqual(href, lastState.uri)) {
327
+ history.back();
328
+ return;
329
+ }
330
+ }
319
331
  api.navigate(href);
320
332
  }
321
333
  },
322
334
  emitEvent,
335
+ restoreRoute(route) {
336
+ const stack = state.history;
337
+ // Is the route in our history stack? (going back)
338
+ const index = stack.findIndex((item) => item.id === route.id);
339
+ if (index >= 0) {
340
+ return doRouteTransition({
341
+ url: route.uri,
342
+ route,
343
+ goingBack: true,
344
+ history: stack.slice(0, index),
345
+ restoreState: route.returnState,
346
+ shouldPush: false,
347
+ });
348
+ }
349
+ // Is the route in our history _cache_? (probably going forward)
350
+ if (historyCache.has(route.id)) {
351
+ return doRouteTransition({
352
+ url: route.uri,
353
+ route,
354
+ goingBack: false,
355
+ history: stack,
356
+ restoreState: route.returnState,
357
+ shouldPush: false,
358
+ });
359
+ }
360
+ return doRouteTransition({
361
+ url: route.uri,
362
+ goingBack: false,
363
+ history: stack,
364
+ restoreState: route.returnState,
365
+ shouldPush: false,
366
+ });
367
+ },
323
368
  };
324
369
  return {
325
370
  api,
@@ -348,6 +393,7 @@ export function BrowserRouter(props) {
348
393
  // Handle popState
349
394
  // const router = useRouterStore.getState()
350
395
  const onPopState = (e) => {
396
+ console.log("Popped", e.state);
351
397
  internals.poppedState?.(document.location.href, e.state ?? {});
352
398
  };
353
399
  window.addEventListener("popstate", onPopState);
@@ -3,6 +3,7 @@ type Props<T extends ElementType = "a"> = NoInfer<Omit<ComponentPropsWithRef<T>,
3
3
  href?: string | null;
4
4
  target?: string | null;
5
5
  as?: T;
6
+ preferBack?: boolean;
6
7
  };
7
8
  export declare const Link: <T extends ElementType = "a">(props: Props<T>) => ReactElement;
8
9
  /**
@@ -6,7 +6,7 @@ import { useRouter } from "../hooks/useRouter.js";
6
6
  import { isSameOrigin } from "../utils.js";
7
7
  import { useRoute } from "../hooks/useRoute.js";
8
8
  import { useRouterState } from "../hooks/useRouterState.js";
9
- export const Link = forwardRef((props, ref) => {
9
+ export const Link = forwardRef(({ preferBack, ...props }, ref) => {
10
10
  const Comp = props.as || "a";
11
11
  if (env.admin) {
12
12
  return (_jsx(Comp, { ref: ref, ...props, href: props.href ?? undefined, onClick: (e) => {
@@ -15,17 +15,16 @@ export const Link = forwardRef((props, ref) => {
15
15
  } }));
16
16
  }
17
17
  else {
18
- const preload = useRouter((r) => r.preload);
19
- const handleClickEvent = useRouter((r) => r.handleClickEvent);
18
+ const router = useRouter();
20
19
  const state = useLinkState(props.href ?? "");
21
20
  return (_jsx(Comp, { ref: ref, "data-active": state.active ?? undefined, "data-child-active": state.childActive ?? undefined, "data-pending": state.pending ?? undefined, ...props, href: props.href ?? undefined, onMouseEnter: (e) => {
22
21
  if (props.onMouseEnter) {
23
22
  props.onMouseEnter(e);
24
23
  }
25
- preload(props.href);
24
+ router.preload(props.href);
26
25
  }, onClick: (e) => {
27
26
  props.onClick?.(e);
28
- handleClickEvent(e, props.href ?? undefined);
27
+ router.handleClickEvent(e, props.href ?? undefined, preferBack);
29
28
  } }));
30
29
  }
31
30
  });
@@ -3,6 +3,7 @@ import { memo, useMemo } from "react";
3
3
  import { RouteItemContext } from "../context.js";
4
4
  import { useRouter } from "../hooks/useRouter.js";
5
5
  import { useRouterState } from "../hooks/useRouterState.js";
6
+ import { ErrorBoundaryFrontend } from "../../blocks/ErrorBoundaryFrontend.js";
6
7
  export const AppRenderer = memo(() => {
7
8
  const { appData, appComponent: AppComponent } = useRouter().loader;
8
9
  const { activeRoute } = useRouterState();
@@ -22,7 +23,7 @@ export function RouteDisplay(props) {
22
23
  }
23
24
  return useMemo(() => {
24
25
  let child = !!Component && _jsx(Component, { ...props.route.props });
25
- return _jsx(RouteItemContext.Provider, { value: props.route, children: child });
26
+ return (_jsx(ErrorBoundaryFrontend, { children: _jsx(RouteItemContext.Provider, { value: props.route, children: child }) }));
26
27
  }, [Component, props.route, props.route]);
27
28
  }
28
29
  RouteDisplay.displayName = "RouteDisplay";
@@ -26,6 +26,10 @@ export const RouterContext = createContext({
26
26
  replaceQuery(query) { },
27
27
  handleClickEvent(e, originalHref) { },
28
28
  emitEvent(event) { },
29
+ getState() {
30
+ return null;
31
+ },
32
+ restoreRoute(route) { },
29
33
  });
30
34
  export const RouterStateContext = createContext({
31
35
  history: [NOOP_ROUTE],
@@ -1,2 +1,17 @@
1
1
  import type { RouteState } from "../types.js";
2
+ /**
3
+ * Returns the current route object.
4
+ *
5
+ * In most circumstances, this will be the active top-level route.
6
+ *
7
+ * In cases where manual rendering of routes is taking place, such as during page transitions or in some modal cases.
8
+ *
9
+ * @returns The current route
10
+ */
2
11
  export declare function useRoute(): RouteState;
12
+ /**
13
+ * Returns the current route, if it matches the specified view, otherwise null.
14
+ */
15
+ export declare function useViewRoute<V extends keyof ViewProps>(view: V): Extract<RouteState, {
16
+ view: V;
17
+ }> | null;
@@ -1,5 +1,26 @@
1
1
  import { useContext } from "react";
2
2
  import { RouteItemContext } from "../context.js";
3
+ /**
4
+ * Returns the current route object.
5
+ *
6
+ * In most circumstances, this will be the active top-level route.
7
+ *
8
+ * In cases where manual rendering of routes is taking place, such as during page transitions or in some modal cases.
9
+ *
10
+ * @returns The current route
11
+ */
3
12
  export function useRoute() {
4
13
  return useContext(RouteItemContext);
5
14
  }
15
+ /**
16
+ * Returns the current route, if it matches the specified view, otherwise null.
17
+ */
18
+ export function useViewRoute(view) {
19
+ const route = useContext(RouteItemContext);
20
+ if (route.view === view) {
21
+ return route;
22
+ }
23
+ else {
24
+ return null;
25
+ }
26
+ }
@@ -3,6 +3,7 @@ export * from "./components/Link.js";
3
3
  export * from "./components/ScrollRestoration.js";
4
4
  export * from "./components/RouteRenderer.js";
5
5
  export * from "./components/ClientOnly.js";
6
+ export * from "./components/BackButton.js";
6
7
  export * from "./hooks/useRoute.js";
7
8
  export * from "./hooks/useRouteTransition.js";
8
9
  export * from "./hooks/useRouter.js";
@@ -3,6 +3,7 @@ export * from "./components/Link.js";
3
3
  export * from "./components/ScrollRestoration.js";
4
4
  export * from "./components/RouteRenderer.js";
5
5
  export * from "./components/ClientOnly.js";
6
+ export * from "./components/BackButton.js";
6
7
  export * from "./hooks/useRoute.js";
7
8
  export * from "./hooks/useRouteTransition.js";
8
9
  export * from "./hooks/useRouter.js";
@@ -102,13 +102,17 @@ export type RouterAPI = {
102
102
  /** Replace the hash */
103
103
  replaceHash: (hash: string) => void;
104
104
  /** Handle link clicking events */
105
- handleClickEvent(e: PointerOrMouseEvent, href?: string): void;
105
+ handleClickEvent(e: PointerOrMouseEvent, href?: string, preferBack?: boolean): void;
106
106
  /** A reference to the route loader (mostly for internal use) */
107
107
  loader: RouteLoader;
108
108
  /** Subscribe to events */
109
109
  subscribe: (subscribe: RouterSubscriber) => () => void;
110
110
  /** This function is used by the routing system automatically */
111
111
  emitEvent: (event: RouterEvent) => void;
112
+ /** Returns the current RouterState */
113
+ getState: () => RouterAPIState | null;
114
+ /** Go back to a previous route in the history stack, or push it onto the stack if it isn't currently on the stack. */
115
+ restoreRoute: (route: RouteState) => void;
112
116
  };
113
117
  export type TypedRouteState<T, TProps> = {
114
118
  /** A unique ID, representing this history item. */
@@ -0,0 +1,2 @@
1
+ import { Hono } from "hono";
2
+ export declare function defineRouter(): Hono;
@@ -0,0 +1,4 @@
1
+ import { Hono } from "hono";
2
+ export function defineRouter() {
3
+ return new Hono();
4
+ }
@@ -1,3 +1,4 @@
1
1
  export * from "./render-ssr-page.js";
2
2
  export * from "./server-context.js";
3
3
  export * from "./proxy-wp-admin.js";
4
+ export * from "./defineRouter.js";
@@ -1,3 +1,4 @@
1
1
  export * from "./render-ssr-page.js";
2
2
  export * from "./server-context.js";
3
3
  export * from "./proxy-wp-admin.js";
4
+ export * from "./defineRouter.js";
@@ -1,7 +1,7 @@
1
1
  import { parseURL, stringifyParsedURL, withQuery } from "ufo";
2
2
  import { filterHeader } from "./utils/headers.js";
3
3
  import { createUrlReplacer } from "./utils/replace-host.js";
4
- const PROXY_RESPONSE_HEADERS = ["content-type", "set-cookie", /^x-/, "cache-control"];
4
+ const PROXY_RESPONSE_HEADERS = ["content-type", "set-cookie", /^x-/, "cache-control", /woocommerce/];
5
5
  const PROXY_REQUEST_HEADERS = [
6
6
  "content-type",
7
7
  "accept",
@@ -11,6 +11,7 @@ const PROXY_REQUEST_HEADERS = [
11
11
  "referer",
12
12
  "user-agent",
13
13
  "authorization",
14
+ "woocommerce-session",
14
15
  ];
15
16
  let runtime;
16
17
  export class ServerContext {
@@ -50,12 +51,14 @@ export class ServerContext {
50
51
  if (!response.ok) {
51
52
  return response;
52
53
  }
54
+ // console.log("Response headers", response.headers)
53
55
  const headers = {};
54
56
  response.headers.forEach((value, key) => {
55
57
  if (filterHeader(key, PROXY_RESPONSE_HEADERS)) {
56
58
  headers[key] = value;
57
59
  }
58
60
  });
61
+ // console.log("Returning headers", headers)
59
62
  let text = await response.text();
60
63
  if (opts?.replaceUrls && this.replaceUrls) {
61
64
  text = this.replaceUrls(text);
@@ -116,6 +119,7 @@ export class ServerContext {
116
119
  "Content-Type": "application/json",
117
120
  Accept: "application/json",
118
121
  },
122
+ redirect: "manual",
119
123
  });
120
124
  }
121
125
  async fetchMutation(req) {
@@ -130,6 +134,7 @@ export class ServerContext {
130
134
  Accept: "application/json",
131
135
  },
132
136
  body: JSON.stringify(req.body),
137
+ redirect: "manual",
133
138
  });
134
139
  }
135
140
  }
@@ -1 +1 @@
1
- export declare const VERSION = "2.0.0-beta.26";
1
+ export declare const VERSION = "2.0.0-beta.28";
@@ -1 +1 @@
1
- export const VERSION = "2.0.0-beta.26";
1
+ export const VERSION = "2.0.0-beta.28";
@@ -227,7 +227,7 @@ export function createVinxiCodegen(opts) {
227
227
  return await serverContext.fetchMutation({
228
228
  name: id,
229
229
  body,
230
- headers: {},
230
+ headers: getRequestHeaders(event),
231
231
  })
232
232
  }),
233
233
  )
@@ -227,6 +227,9 @@ export class GraphQLGenerator {
227
227
  documents: getDocuments(["queries", "views", "blocks", "fragments"]),
228
228
  hasDocuments: true,
229
229
  banner: `import { ContentBlock } from "eddev/blocks"`,
230
+ footer: Object.keys(wp.scalarTypes)
231
+ .map((name) => `export type ${name}Scalar = Scalars["${name}"]["output"]`)
232
+ .join("\n"),
230
233
  plugins: {
231
234
  typescript: [typescriptPlugin, {}],
232
235
  typescriptOperations: [typescriptOperationsPlugin, {}],
@@ -400,7 +403,7 @@ export class GraphQLGenerator {
400
403
  errors.push(String(err));
401
404
  }
402
405
  if (args.banner) {
403
- output = args.banner + "\n\n" + output;
406
+ output = args.banner + "\n\n" + output + "\n" + (args.footer ?? "");
404
407
  }
405
408
  if (errors.length === 0) {
406
409
  const didWrite = await fs.writeIfUnchanged(config.filename, output);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eddev",
3
- "version": "2.0.0-beta.26",
3
+ "version": "2.0.0-beta.28",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",