eddev 0.1.29 → 0.1.33-beta-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.
@@ -0,0 +1,36 @@
1
+ import { ReactElement } from "react";
2
+ export declare type ContentBlock<T extends keyof BlockProps | string = any> = {
3
+ blockName: T;
4
+ props: BlockProps extends keyof BlockProps ? BlockProps[T] : any;
5
+ innerBlocks: ContentBlock[];
6
+ innerHTML?: string;
7
+ innerContent?: string[];
8
+ rule: string;
9
+ inline: any;
10
+ };
11
+ declare type BlocksContext = {
12
+ ancestors: ContentBlock[];
13
+ parent?: ContentBlock;
14
+ prev?: ContentBlock;
15
+ next?: ContentBlock;
16
+ current: ContentBlock;
17
+ };
18
+ export declare const BlocksContext: import("react").Context<BlocksContext | undefined>;
19
+ declare type Props = {
20
+ blocks: ContentBlock[];
21
+ wrapBlock?: (child: ReactElement, ctx: BlocksContext) => ReactElement;
22
+ spacer?: (ctx: {
23
+ ancestors: ContentBlock[];
24
+ parent?: ContentBlock;
25
+ prev: ContentBlock | null;
26
+ next: ContentBlock | null;
27
+ }) => ReactElement | null;
28
+ };
29
+ export declare type GenericBlockProps = {
30
+ innerHTML?: string;
31
+ innerContent?: string[];
32
+ innerBlocks?: ContentBlock[];
33
+ children: ReactElement | null;
34
+ };
35
+ export declare function ContentBlocks(props: Props): JSX.Element | null;
36
+ export {};
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ContentBlocks = exports.BlocksContext = void 0;
27
+ var jsx_runtime_1 = require("react/jsx-runtime");
28
+ // @ts-ignore
29
+ var blocks_1 = __importDefault(require("@manifest/blocks"));
30
+ var blockAttributes_1 = require("./blockAttributes");
31
+ var react_1 = require("react");
32
+ exports.BlocksContext = (0, react_1.createContext)(undefined);
33
+ function ContentBlocks(props) {
34
+ if (!Array.isArray(props.blocks))
35
+ return null;
36
+ var parentContext = (0, react_1.useContext)(exports.BlocksContext);
37
+ var blocks = (0, react_1.useMemo)(function () {
38
+ return props.blocks.map(function (block, index) {
39
+ // Create a context object for the current block
40
+ var ctx = __assign(__assign({}, parentContext), { ancestors: __spreadArray(__spreadArray([], ((parentContext === null || parentContext === void 0 ? void 0 : parentContext.ancestors) || []), true), [parentContext === null || parentContext === void 0 ? void 0 : parentContext.current], false).filter(Boolean), parent: parentContext === null || parentContext === void 0 ? void 0 : parentContext.current, current: block, prev: props.blocks[index - 1], next: props.blocks[index + 1] });
41
+ // Figure out the
42
+ var blockNode;
43
+ if (block.blockName in blocks_1.default) {
44
+ var Component = blocks_1.default[block.blockName];
45
+ if (!Component)
46
+ return (0, jsx_runtime_1.jsx)(react_1.Fragment, {}, void 0);
47
+ blockNode = ((0, jsx_runtime_1.jsx)(blockAttributes_1.InlineEditingContextProvider, __assign({ values: block.inline, innerBlocks: block.innerBlocks }, { children: (0, jsx_runtime_1.jsx)(Component, __assign({}, block.props, { innerHTML: block.innerHTML,
48
+ // innerBlocks={block.innerBlocks}
49
+ children: Array.isArray(block.innerBlocks) && block.innerBlocks.length ? ((0, jsx_runtime_1.jsx)(ContentBlocks, { blocks: block.innerBlocks }, void 0)) : null }), void 0) }), void 0));
50
+ }
51
+ else if (block.innerHTML) {
52
+ blockNode = (0, jsx_runtime_1.jsx)("div", { dangerouslySetInnerHTML: { __html: block.innerHTML } }, void 0);
53
+ }
54
+ else {
55
+ blockNode = null;
56
+ }
57
+ return [ctx, block, blockNode];
58
+ });
59
+ }, [props.blocks]);
60
+ (0, react_1.useEffect)(function () {
61
+ console.log("RESTART CONTENT BLOCKS");
62
+ }, []);
63
+ var layout = (0, react_1.useMemo)(function () {
64
+ return blocks.filter(Boolean).map(function (_a, index) {
65
+ var ctx = _a[0], block = _a[1], blockNode = _a[2];
66
+ // Exit early if the block is null
67
+ if (blockNode === null)
68
+ return (0, jsx_runtime_1.jsx)(react_1.Fragment, {}, index);
69
+ // Custom wrap function?
70
+ if (props.wrapBlock) {
71
+ blockNode = props.wrapBlock(blockNode, ctx);
72
+ }
73
+ // Wrap the block in a context provider
74
+ blockNode = (0, jsx_runtime_1.jsx)(exports.BlocksContext.Provider, __assign({ value: ctx }, { children: blockNode }), void 0);
75
+ // Custom spacer function?
76
+ var spaceBefore = null;
77
+ var spaceAfter = null;
78
+ if (props.spacer) {
79
+ // Before block
80
+ spaceBefore = props.spacer(__assign(__assign({}, ctx), { prev: ctx.prev || null, next: ctx.current || null }));
81
+ // After block (last block only)
82
+ if (index === props.blocks.length - 1) {
83
+ spaceAfter = props.spacer(__assign(__assign({}, ctx), { prev: block, next: null }));
84
+ }
85
+ }
86
+ // Attempt to wrap the block
87
+ return ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [spaceBefore, blockNode, spaceAfter] }, index));
88
+ });
89
+ }, [blocks, props.wrapBlock, props.spacer]);
90
+ return (0, jsx_runtime_1.jsx)(react_1.Fragment, { children: layout }, void 0);
91
+ }
92
+ exports.ContentBlocks = ContentBlocks;
@@ -1,4 +1,4 @@
1
- import { ContentBlock } from "../components/ContentBlocks";
1
+ import { ContentBlock } from "./ContentBlocks";
2
2
  import { PropsWithChildren } from "react";
3
3
  interface Attributes {
4
4
  [key: string]: any;
package/blocks/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./installGutenbergHooks";
2
2
  export * from "./defineBlock";
3
3
  export * from "./inlineEditing";
4
+ export * from "./ContentBlocks";
4
5
  export { useInlineEditableValue, useInnerBlocks } from "./blockAttributes";
package/blocks/index.js CHANGED
@@ -14,6 +14,7 @@ exports.useInnerBlocks = exports.useInlineEditableValue = void 0;
14
14
  __exportStar(require("./installGutenbergHooks"), exports);
15
15
  __exportStar(require("./defineBlock"), exports);
16
16
  __exportStar(require("./inlineEditing"), exports);
17
+ __exportStar(require("./ContentBlocks"), exports);
17
18
  var blockAttributes_1 = require("./blockAttributes");
18
19
  Object.defineProperty(exports, "useInlineEditableValue", { enumerable: true, get: function () { return blockAttributes_1.useInlineEditableValue; } });
19
20
  Object.defineProperty(exports, "useInnerBlocks", { enumerable: true, get: function () { return blockAttributes_1.useInnerBlocks; } });
@@ -26,7 +26,7 @@ exports.InnerBlocks = exports.EditableText = void 0;
26
26
  var jsx_runtime_1 = require("react/jsx-runtime");
27
27
  // @ts-ignore
28
28
  var block_editor_1 = require("@wordpress/block-editor");
29
- var components_1 = require("../components");
29
+ var ContentBlocks_1 = require("./ContentBlocks");
30
30
  var react_1 = require("react");
31
31
  var blockAttributes_1 = require("./blockAttributes");
32
32
  var react_2 = require("@stitches/react");
@@ -84,7 +84,7 @@ function InnerBlocks(props) {
84
84
  }
85
85
  else {
86
86
  var blocks = (0, blockAttributes_1.useInnerBlocks)();
87
- return (0, jsx_runtime_1.jsx)(components_1.ContentBlocks, { blocks: blocks }, void 0);
87
+ return (0, jsx_runtime_1.jsx)(ContentBlocks_1.ContentBlocks, { blocks: blocks }, void 0);
88
88
  }
89
89
  }
90
90
  exports.InnerBlocks = InnerBlocks;
@@ -373,7 +373,8 @@ function getWebpackConfig(opts) {
373
373
  },
374
374
  output: {
375
375
  filename: "[name].".concat(opts.isAdmin ? "admin" : "frontend", ".js"),
376
- publicPath: isDev ? "//127.0.0.1:".concat(opts.hotPort, "/") : "/wp-content/themes/".concat(opts.themeName, "/dist/").concat(distSuffix, "/"),
376
+ // publicPath: isDev ? `//127.0.0.1:${opts.hotPort}/` : `/wp-content/themes/${opts.themeName}/dist/${distSuffix}/`,
377
+ publicPath: "/wp-content/themes/".concat(opts.themeName, "/dist/").concat(distSuffix, "/"),
377
378
  path: outputFolder,
378
379
  globalObject: isSSR ? "this" : "self",
379
380
  clean: true,
@@ -105,6 +105,7 @@ var graphqlPatterns = {
105
105
  blocks: "./blocks/**/*.graphql",
106
106
  views: "./views/**/*.graphql",
107
107
  queries: "./queries/**/*.graphql",
108
+ fragments: "./queries/fragments/**/*.graphql",
108
109
  };
109
110
  var tsxPatterns = {
110
111
  blocks: "./blocks/**/*.tsx",
@@ -288,10 +289,12 @@ function beginWork(opts) {
288
289
  // exportFragmentSpreadSubTypes: true,
289
290
  inlineFragmentTypes: "combine",
290
291
  onlyOperationTypes: true,
292
+ skipTypename: true,
293
+ preResolveTypes: true,
291
294
  },
292
295
  },
293
296
  "types.views.ts": {
294
- documents: __spreadArray([], documentSets.views, true),
297
+ documents: __spreadArray(__spreadArray([], documentSets.views, true), documentSets.fragments, true),
295
298
  plugins: [
296
299
  {
297
300
  files: {},
@@ -311,7 +314,7 @@ function beginWork(opts) {
311
314
  },
312
315
  },
313
316
  "types.blocks.ts": {
314
- documents: __spreadArray([], documentSets.blocks, true),
317
+ documents: __spreadArray(__spreadArray([], documentSets.blocks, true), documentSets.fragments, true),
315
318
  plugins: [
316
319
  {
317
320
  files: {},
@@ -332,7 +335,7 @@ function beginWork(opts) {
332
335
  var item = _d[_i];
333
336
  if (item.name.value === "block") {
334
337
  var blockField = (_c = (_b = (_a = item === null || item === void 0 ? void 0 : item.selectionSet) === null || _a === void 0 ? void 0 : _a.selections[0]) === null || _b === void 0 ? void 0 : _b.name) === null || _c === void 0 ? void 0 : _c.value;
335
- blockSelection = "Exclude<".concat(importedName, "[\"block\"][").concat(JSON.stringify(blockField), "], null>");
338
+ blockSelection = "Exclude<".concat(importedName, "[\"block\"][").concat(JSON.stringify(blockField), "], null | undefined>");
336
339
  }
337
340
  else {
338
341
  queryKeyVals.push([JSON.stringify(item.name.value), "".concat(importedName, "[").concat(JSON.stringify(item.name.value), "]")].join(": "));
@@ -459,9 +462,11 @@ function beginWork(opts) {
459
462
  });
460
463
  }); };
461
464
  watchGlob(Path.resolve(opts.baseDirectory, "./acf-json/*.json"), function (path) {
465
+ needsSchemaFetch = true;
462
466
  maybeRegenerate_1(path);
463
467
  });
464
468
  watchGlob(Path.resolve(opts.baseDirectory, "./backend/**/*.php"), function (path) {
469
+ needsSchemaFetch = true;
465
470
  maybeRegenerate_1(path);
466
471
  });
467
472
  watchGlob(envFile, function () {
@@ -18,7 +18,7 @@ exports.BrowserRouter = void 0;
18
18
  var jsx_runtime_1 = require("react/jsx-runtime");
19
19
  var react_1 = require("react");
20
20
  var remoteProps_1 = require("../utils/remoteProps");
21
- var routing_1 = require("./routing");
21
+ var routing_1 = require("../routing");
22
22
  // @ts-ignore
23
23
  var views_1 = __importDefault(require("@manifest/views"));
24
24
  var usePageLoad_1 = require("../hooks/usePageLoad");
@@ -18,7 +18,7 @@ exports.InlinePage = void 0;
18
18
  var jsx_runtime_1 = require("react/jsx-runtime");
19
19
  var react_1 = require("react");
20
20
  var remoteProps_1 = require("../utils/remoteProps");
21
- var routing_1 = require("./routing");
21
+ var routing_1 = require("../routing");
22
22
  // @ts-ignore
23
23
  var views_1 = __importDefault(require("@manifest/views"));
24
24
  /**
@@ -1,4 +1,2 @@
1
- export * from "./routing";
2
- export * from "./ContentBlocks";
3
1
  export * from "./AdminBar";
4
2
  export * from "./InlinePage";
@@ -10,7 +10,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./routing"), exports);
14
- __exportStar(require("./ContentBlocks"), exports);
15
13
  __exportStar(require("./AdminBar"), exports);
16
14
  __exportStar(require("./InlinePage"), exports);
package/entry/Root.js CHANGED
@@ -34,13 +34,13 @@ var jsx_runtime_1 = require("react/jsx-runtime");
34
34
  // @ts-ignore
35
35
  var views_1 = __importStar(require("@manifest/views"));
36
36
  var react_1 = require("react");
37
- var components_1 = require("../components");
37
+ var routing_1 = require("../routing");
38
38
  function Root() {
39
- var route = (0, components_1.useRoute)();
40
- return ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: (0, jsx_runtime_1.jsx)(views_1.App, { children: (0, jsx_runtime_1.jsx)(components_1.Switch, { children: Object.entries(views_1.default).map(function (_a) {
39
+ var route = (0, routing_1.useRoute)();
40
+ return ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: (0, jsx_runtime_1.jsx)(views_1.App, { children: (0, jsx_runtime_1.jsx)(routing_1.Switch, { children: Object.entries(views_1.default).map(function (_a) {
41
41
  var _b, _c;
42
42
  var name = _a[0], Component = _a[1];
43
- return ((0, jsx_runtime_1.jsx)(components_1.Route, __assign({ match: function (route) { var _a; return ((_a = route.data) === null || _a === void 0 ? void 0 : _a.view) === name; } }, { children: (0, jsx_runtime_1.jsx)(Component, __assign({}, (_c = (_b = route.data) === null || _b === void 0 ? void 0 : _b.viewData) === null || _c === void 0 ? void 0 : _c.data), void 0) }), name));
43
+ return ((0, jsx_runtime_1.jsx)(routing_1.Route, __assign({ match: function (route) { var _a; return ((_a = route.data) === null || _a === void 0 ? void 0 : _a.view) === name; } }, { children: (0, jsx_runtime_1.jsx)(Component, __assign({}, (_c = (_b = route.data) === null || _b === void 0 ? void 0 : _b.viewData) === null || _c === void 0 ? void 0 : _c.data), void 0) }), name));
44
44
  }) }, void 0) }, void 0) }, void 0));
45
45
  }
46
46
  exports.default = Root;
package/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./components";
2
2
  export * from "./hooks";
3
+ export * from "./routing";
package/index.js CHANGED
@@ -12,3 +12,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./components"), exports);
14
14
  __exportStar(require("./hooks"), exports);
15
+ __exportStar(require("./routing"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eddev",
3
- "version": "0.1.29",
3
+ "version": "0.1.33-beta-1",
4
4
  "main": "./index.js",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -0,0 +1 @@
1
+ export * from "./routing";
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./routing"), exports);
@@ -0,0 +1,45 @@
1
+ import { ReactNode, PropsWithChildren } from "react";
2
+ import { APropsWithoutRef } from "react-html-props";
3
+ import URLParse from "url-parse";
4
+ import { RouteData } from "../utils/remoteProps";
5
+ declare type RouteItem = URLParse<any> & {
6
+ data?: RouteData;
7
+ };
8
+ declare type AnyPromise = Promise<any>;
9
+ declare type MatchResult = boolean | string;
10
+ declare type RouteStatus = "leaving" | "parent-leaving" | "active" | "inactive";
11
+ declare type RouteContextConfig = {
12
+ debugName?: string;
13
+ match?(next: RouteItem): MatchResult;
14
+ beforeLeave?(current: RouteItem, next: RouteItem, parentIsLeaving: boolean): AnyPromise | undefined;
15
+ onChange?(route: RouteItem, status: RouteStatus): void;
16
+ };
17
+ export declare function useRoute(): RouteItem;
18
+ export declare type RouteProps = RouteContextConfig & {
19
+ children: ReactNode;
20
+ };
21
+ export declare function Route(props: RouteProps): JSX.Element | null;
22
+ export declare function createRouteItem(url: string, data: RouteData): RouteItem;
23
+ declare type RouterRootProps = {
24
+ url: string;
25
+ data: RouteData;
26
+ children: ReactNode;
27
+ onNavigateRequest?: (url: string) => void;
28
+ onPreload?: (url: string) => void;
29
+ onNavigated?: (url: string) => void;
30
+ };
31
+ export declare function RouterRoot(props: RouterRootProps): JSX.Element;
32
+ declare type SwitchProps = {
33
+ children: ReactNode | ReactNode[];
34
+ };
35
+ export declare const isInternalPageLink: (href: string) => boolean;
36
+ export declare const Link: import("react").ForwardRefExoticComponent<APropsWithoutRef & {
37
+ preload?: boolean | undefined;
38
+ } & {
39
+ className?: string | undefined;
40
+ } & import("react").RefAttributes<HTMLAnchorElement>>;
41
+ export declare function Switch({ children }: SwitchProps): JSX.Element;
42
+ export declare function RouteItemContext(props: PropsWithChildren<{
43
+ route: RouteItem;
44
+ }>): JSX.Element;
45
+ export {};
@@ -0,0 +1,373 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.RouteItemContext = exports.Switch = exports.Link = exports.isInternalPageLink = exports.RouterRoot = exports.createRouteItem = exports.Route = exports.useRoute = void 0;
54
+ var jsx_runtime_1 = require("react/jsx-runtime");
55
+ var react_1 = require("react");
56
+ var react_merge_refs_1 = __importDefault(require("react-merge-refs"));
57
+ var url_parse_1 = __importDefault(require("url-parse"));
58
+ // const logger = (...args: any[]) => console.log(...args)
59
+ var logger = function () {
60
+ var args = [];
61
+ for (var _i = 0; _i < arguments.length; _i++) {
62
+ args[_i] = arguments[_i];
63
+ }
64
+ };
65
+ function createRouteContext(opts) {
66
+ var _a, _b;
67
+ var subscribers = [];
68
+ var notify = function (item, status) {
69
+ subscribers.forEach(function (handler) { return handler(item, status); });
70
+ };
71
+ var parentIsSwitch = opts.parent && opts.parent.isSwitch;
72
+ var ctx = {
73
+ item: opts.initial,
74
+ config: opts.config || {},
75
+ parent: opts.parent,
76
+ children: [],
77
+ status: "inactive",
78
+ currentMatch: false,
79
+ isSwitch: !!opts.isSwitch,
80
+ onNavigate: opts.onNavigate,
81
+ onPreload: opts.onPreload,
82
+ propagateChange: function (item, matches) {
83
+ ctx.item = item;
84
+ ctx.currentMatch = matches;
85
+ ctx.status = matches ? "active" : "inactive";
86
+ ctx.children.forEach(function (child) {
87
+ child.propagateChange(item, child.match(item));
88
+ });
89
+ notify(item, ctx.status);
90
+ },
91
+ propagateCandidate: function (next, prev) {
92
+ // if (ctx.status === "leaving") return Promise.resolve();
93
+ var promises = [Promise.resolve()];
94
+ var matches = ctx.match(next);
95
+ logger(ctx.config.debugName, "was", ctx.status, matches);
96
+ if (ctx.status === "active" && (!matches || matches !== ctx.currentMatch)) {
97
+ logger(ctx.config.debugName, "is leaving because ", matches, "!=", ctx.currentMatch);
98
+ ctx.status = "leaving";
99
+ }
100
+ logger(ctx.config.debugName, "is now", ctx.status);
101
+ if (ctx.status === "leaving") {
102
+ promises.push(this.propagateLeaving(prev, next, false));
103
+ }
104
+ else {
105
+ ctx.children.forEach(function (child) {
106
+ promises.push(child.propagateCandidate(next, prev));
107
+ });
108
+ }
109
+ notify(prev, ctx.status);
110
+ return Promise.all(promises);
111
+ },
112
+ propagateLeaving: function (current, next, parentIsLeaving) {
113
+ ctx.status = parentIsLeaving ? "parent-leaving" : "leaving";
114
+ logger(ctx.status);
115
+ var promises = [Promise.resolve()];
116
+ if (ctx.config.beforeLeave) {
117
+ var result = ctx.config.beforeLeave(current, next, parentIsLeaving);
118
+ if (result)
119
+ promises.push(result);
120
+ }
121
+ ctx.children.forEach(function (child) {
122
+ var result = child.propagateLeaving(current, next, true);
123
+ if (result) {
124
+ promises.push(result);
125
+ }
126
+ });
127
+ notify(current, ctx.status);
128
+ return Promise.all(promises);
129
+ },
130
+ subscribe: function (handler) {
131
+ subscribers.push(handler);
132
+ handler(ctx.item, ctx.status);
133
+ return function () {
134
+ var i = subscribers.indexOf(handler);
135
+ if (i !== -1) {
136
+ subscribers.splice(i, 1);
137
+ }
138
+ };
139
+ },
140
+ getFirstMatch: function (item) {
141
+ logger("Checking items", this.children, item);
142
+ for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
143
+ var child = _a[_i];
144
+ if (child.matchSelf(item)) {
145
+ logger("Matched", child.config.debugName);
146
+ return child;
147
+ }
148
+ }
149
+ },
150
+ match: function (item) {
151
+ if (parentIsSwitch) {
152
+ var parent_1 = ctx.parent;
153
+ return parent_1.getFirstMatch(item) === ctx && this.matchSelf(item);
154
+ }
155
+ return this.matchSelf(item);
156
+ },
157
+ matchSelf: function (item) {
158
+ var _a, _b;
159
+ if ((_a = ctx === null || ctx === void 0 ? void 0 : ctx.config) === null || _a === void 0 ? void 0 : _a.match) {
160
+ return (_b = ctx === null || ctx === void 0 ? void 0 : ctx.config) === null || _b === void 0 ? void 0 : _b.match(item);
161
+ }
162
+ else {
163
+ return true;
164
+ }
165
+ },
166
+ update: function () { },
167
+ dispose: (_a = opts.dispose) !== null && _a !== void 0 ? _a : (function () { }),
168
+ fork: function (config, opts) {
169
+ if (opts === void 0) { opts = {}; }
170
+ var child = createRouteContext({
171
+ initial: ctx.item,
172
+ config: config,
173
+ parent: ctx,
174
+ isSwitch: opts.isSwitch,
175
+ onNavigate: ctx.onNavigate,
176
+ onPreload: ctx.onPreload,
177
+ dispose: function () {
178
+ var i = ctx.children.indexOf(child);
179
+ if (i !== -1)
180
+ ctx.children.splice(i, 1);
181
+ },
182
+ });
183
+ ctx.children.push(child);
184
+ if (ctx.isSwitch) {
185
+ }
186
+ return child;
187
+ },
188
+ };
189
+ logger("Initting", ctx.config.debugName, opts.initial);
190
+ if (parentIsSwitch) {
191
+ logger("Parent is switch");
192
+ if (!((_b = opts.parent) === null || _b === void 0 ? void 0 : _b.getFirstMatch(opts.initial))) {
193
+ logger("FFFF", ctx.config.match, ctx.match(opts.initial));
194
+ ctx.currentMatch = ctx.matchSelf(opts.initial);
195
+ ctx.status = ctx.currentMatch ? "active" : "inactive";
196
+ }
197
+ }
198
+ else {
199
+ ctx.currentMatch = ctx.matchSelf(opts.initial);
200
+ ctx.status = ctx.currentMatch ? "active" : "inactive";
201
+ }
202
+ subscribers.push(function (item, status) {
203
+ if (ctx.config.onChange) {
204
+ ctx.config.onChange(item, status);
205
+ }
206
+ });
207
+ // notify(ctx.item, ctx.status);
208
+ return ctx;
209
+ }
210
+ var RouterContext = (0, react_1.createContext)(undefined);
211
+ function useForkedRouter(conf, opts) {
212
+ var parent = (0, react_1.useContext)(RouterContext);
213
+ var ctx = (0, react_1.useMemo)(function () {
214
+ return parent.fork(conf, opts);
215
+ }, [parent]);
216
+ // useEffect(() => {
217
+ // if (onChange) {
218
+ // return ctx.subscribe(onChange);
219
+ // }
220
+ // }, [ctx, onChange]);
221
+ Object.assign(ctx.config, conf);
222
+ // ctx.config.match = conf.match;
223
+ // ctx.config.beforeLeave = conf.beforeLeave;
224
+ // ctx.config.beforeLeave = conf.beforeLeave;
225
+ (0, react_1.useEffect)(function () {
226
+ return function () { return ctx.dispose(); };
227
+ }, [ctx]);
228
+ return ctx;
229
+ }
230
+ function useRoute() {
231
+ var router = (0, react_1.useContext)(RouterContext);
232
+ var _a = (0, react_1.useState)(router.item), route = _a[0], setRoute = _a[1];
233
+ (0, react_1.useEffect)(function () {
234
+ return router.subscribe(function (item, status) {
235
+ setRoute(item);
236
+ });
237
+ }, [router]);
238
+ return route;
239
+ }
240
+ exports.useRoute = useRoute;
241
+ function Route(props) {
242
+ var ctx = useForkedRouter({
243
+ match: props.match,
244
+ beforeLeave: props.beforeLeave,
245
+ debugName: props.debugName,
246
+ onChange: props.onChange,
247
+ });
248
+ logger("Intial route for", props.debugName, ctx.status, ctx.currentMatch);
249
+ var _a = (0, react_1.useState)(ctx.status), status = _a[0], setStatus = _a[1];
250
+ (0, react_1.useEffect)(function () {
251
+ return ctx.subscribe(function (item, status) {
252
+ setStatus(status);
253
+ });
254
+ }, [ctx]);
255
+ if (status === "inactive")
256
+ return null;
257
+ return (0, jsx_runtime_1.jsx)(RouterContext.Provider, __assign({ value: ctx }, { children: props.children }), void 0);
258
+ }
259
+ exports.Route = Route;
260
+ function createRouteItem(url, data) {
261
+ var parsed = (0, url_parse_1.default)(url, true);
262
+ return __assign(__assign({}, parsed), { data: data });
263
+ }
264
+ exports.createRouteItem = createRouteItem;
265
+ function RouterRoot(props) {
266
+ var _this = this;
267
+ var onNavigateRef = (0, react_1.useRef)(props.onNavigateRequest);
268
+ var onPreNavigateRef = (0, react_1.useRef)(props.onPreload);
269
+ var ctx = (0, react_1.useMemo)(function () {
270
+ return createRouteContext({
271
+ initial: createRouteItem(props.url, props.data),
272
+ config: {
273
+ debugName: "ROOT",
274
+ },
275
+ onNavigate: function (path) { return __awaiter(_this, void 0, void 0, function () {
276
+ return __generator(this, function (_a) {
277
+ // User has clicked a link
278
+ if (onNavigateRef.current) {
279
+ onNavigateRef.current(path);
280
+ }
281
+ return [2 /*return*/];
282
+ });
283
+ }); },
284
+ onPreload: function (path) {
285
+ if (onPreNavigateRef.current) {
286
+ onPreNavigateRef.current(path);
287
+ }
288
+ },
289
+ });
290
+ }, []);
291
+ onNavigateRef.current = props.onNavigateRequest;
292
+ onPreNavigateRef.current = props.onPreload;
293
+ (0, react_1.useEffect)(function () {
294
+ var cancelled = false;
295
+ var item = createRouteItem(props.url, props.data);
296
+ if (item.href !== ctx.item.href || item.data !== ctx.item.data) {
297
+ ctx.propagateCandidate(item, ctx.item).then(function () {
298
+ if (!cancelled) {
299
+ ctx.propagateChange(item, true);
300
+ }
301
+ });
302
+ return function () {
303
+ cancelled = true;
304
+ };
305
+ }
306
+ }, [props.url, props.data]);
307
+ return (0, jsx_runtime_1.jsx)(RouterContext.Provider, __assign({ value: ctx }, { children: props.children }), void 0);
308
+ }
309
+ exports.RouterRoot = RouterRoot;
310
+ var isInternalPageLink = function (href) {
311
+ var url = (0, url_parse_1.default)(href, true);
312
+ if (url && url.origin === document.location.origin && !url.pathname.match(/\./)) {
313
+ return true;
314
+ }
315
+ else {
316
+ return false;
317
+ }
318
+ };
319
+ exports.isInternalPageLink = isInternalPageLink;
320
+ exports.Link = (0, react_1.forwardRef)(function (props, ref) {
321
+ var localRef = (0, react_1.useRef)();
322
+ var router = (0, react_1.useContext)(RouterContext);
323
+ (0, react_1.useEffect)(function () {
324
+ if (props.preload && localRef.current && (0, exports.isInternalPageLink)(localRef.current.href)) {
325
+ router === null || router === void 0 ? void 0 : router.onPreload(localRef.current.href);
326
+ }
327
+ }, [props.preload]);
328
+ return ((0, jsx_runtime_1.jsx)("a", __assign({ ref: (0, react_merge_refs_1.default)([localRef, ref]) }, props, { onMouseEnter: function (e) {
329
+ if (props.onMouseOver) {
330
+ props.onMouseOver(e);
331
+ }
332
+ router === null || router === void 0 ? void 0 : router.onPreload(e.currentTarget.href);
333
+ }, onClick: function (e) {
334
+ if (process.admin) {
335
+ e.preventDefault();
336
+ return;
337
+ }
338
+ if (props.onClick) {
339
+ props.onClick(e);
340
+ }
341
+ if (!e.isDefaultPrevented() && router && !e.ctrlKey && !e.metaKey) {
342
+ var href = e.currentTarget.href;
343
+ if ((0, exports.isInternalPageLink)(href)) {
344
+ router.onNavigate(e.currentTarget.href);
345
+ e.preventDefault();
346
+ }
347
+ }
348
+ } }), void 0));
349
+ });
350
+ function Switch(_a) {
351
+ var children = _a.children;
352
+ var ctx = useForkedRouter({
353
+ debugName: "SWITCH",
354
+ match: function () {
355
+ return true;
356
+ },
357
+ beforeLeave: function () {
358
+ return undefined;
359
+ },
360
+ }, { isSwitch: true });
361
+ return (0, jsx_runtime_1.jsx)(RouterContext.Provider, __assign({ value: ctx }, { children: children }), void 0);
362
+ }
363
+ exports.Switch = Switch;
364
+ function RouteItemContext(props) {
365
+ var ctx = useForkedRouter({
366
+ match: function () { return true; },
367
+ beforeLeave: undefined,
368
+ debugName: undefined,
369
+ onChange: undefined,
370
+ });
371
+ return (0, jsx_runtime_1.jsx)(RouterContext.Provider, __assign({ value: ctx }, { children: props.children }), void 0);
372
+ }
373
+ exports.RouteItemContext = RouteItemContext;