@sealcode/sealgen 0.12.6 → 0.12.8

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.
@@ -13,6 +13,7 @@ export * as Fields from "./forms/fields/fields.js";
13
13
  export * from "./page/mountable.js";
14
14
  export * from "./page/page.js";
15
15
  export * from "./page/stateful-page.js";
16
+ export * from "./forms/form-types.js";
16
17
  export * from "./forms/form.js";
17
18
  export * from "./page/props-parser.js";
18
19
  export * from "./utils/utils.js";
@@ -4,6 +4,10 @@ import { BaseContext } from "koa";
4
4
  import { Templatable } from "tempstream";
5
5
  import { ExtractTail } from "../utils/utils.js";
6
6
  import { Mountable } from "./mountable.js";
7
+ export type StatefulPageActionDescription<ActionName> = {
8
+ action: ActionName;
9
+ label?: string;
10
+ } | ActionName;
7
11
  export declare abstract class StatefulPage<State extends Record<string, unknown>, Actions extends Record<string, (state: State, inputs: Record<string, string>, ...args: unknown[]) => State>> extends Mountable {
8
12
  abstract actions: Actions;
9
13
  abstract getInitialState(): State;
@@ -15,10 +19,17 @@ export declare abstract class StatefulPage<State extends Record<string, unknown>
15
19
  constructor();
16
20
  abstract wrapInLayout(ctx: BaseContext, content: Templatable): Templatable;
17
21
  wrapInForm(state: State, content: Templatable): Templatable;
18
- makeActionButton<ActionName extends keyof Actions>(state: State, action_description: {
22
+ extractActionAndLabel<ActionName extends keyof Actions>(action_description: StatefulPageActionDescription<ActionName>): {
23
+ action: string;
24
+ label: string;
25
+ };
26
+ makeActionURL<ActionName extends keyof Actions>(action_description: StatefulPageActionDescription<ActionName>, ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>): string;
27
+ makeActionButton<ActionName extends keyof Actions>(state: State, action_description: StatefulPageActionDescription<ActionName>, ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>): string;
28
+ makeActionCallback<ActionName extends keyof Actions>(action_description: {
19
29
  action: ActionName;
20
30
  label?: string;
21
31
  } | ActionName, ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>): string;
32
+ rerender(): string;
22
33
  extractState(ctx: BaseContext): {
23
34
  state: State;
24
35
  inputs: Record<string, string>;
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import * as Fields from "./forms/fields/fields.js";
4
4
  export * from "./page/mountable.js";
5
5
  export * from "./page/page.js";
6
6
  export * from "./page/stateful-page.js";
7
+ export * from "./forms/form-types.js";
7
8
  export * from "./forms/form.js";
8
9
  export * from "./page/props-parser.js";
9
10
  export * from "./utils/utils.js";
@@ -30,7 +30,7 @@ class StatefulPage extends Mountable {
30
30
  ${content}
31
31
  </form>`;
32
32
  }
33
- makeActionButton(state, action_description, ...args) {
33
+ extractActionAndLabel(action_description) {
34
34
  let label, action;
35
35
  if (is(action_description, predicates.object)) {
36
36
  action = action_description.action.toString();
@@ -39,19 +39,34 @@ class StatefulPage extends Mountable {
39
39
  action = action_description.toString();
40
40
  label = action;
41
41
  }
42
+ return { action, label };
43
+ }
44
+ makeActionURL(action_description, ...args) {
45
+ const { action } = this.extractActionAndLabel(action_description);
46
+ return `./?action=${action}&args=${to_base64(JSON.stringify(args))}`;
47
+ }
48
+ makeActionButton(state, action_description, ...args) {
49
+ const { label } = this.extractActionAndLabel(action_description);
42
50
  return (
43
51
  /* HTML */
44
52
  `
45
53
  <input
46
54
  type="submit"
47
55
  value=${label}
48
- formaction="./?action=${action}&args=${to_base64(
49
- JSON.stringify(args)
50
- )}"
56
+ formaction="${this.makeActionURL(action_description, ...args)}"
51
57
  />
52
58
  `
53
59
  );
54
60
  }
61
+ makeActionCallback(action_description, ...args) {
62
+ return `(()=>{const form = this.closest('form'); form.action='${this.makeActionURL(
63
+ action_description,
64
+ ...args
65
+ )}'; form.requestSubmit()})()`;
66
+ }
67
+ rerender() {
68
+ return "this.closest('form').requestSubmit()";
69
+ }
55
70
  extractState(ctx) {
56
71
  if (!hasShape(
57
72
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.12.6",
3
+ "version": "0.12.8",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ export * as Fields from "./forms/fields/fields.js";
15
15
  export * from "./page/mountable.js";
16
16
  export * from "./page/page.js";
17
17
  export * from "./page/stateful-page.js";
18
+ export * from "./forms/form-types.js";
18
19
  export * from "./forms/form.js";
19
20
  export * from "./page/props-parser.js";
20
21
  export * from "./utils/utils.js";
@@ -7,6 +7,13 @@ import { from_base64, to_base64 } from "../utils/base64.js";
7
7
  import { ExtractTail } from "../utils/utils.js";
8
8
  import { Mountable } from "./mountable.js";
9
9
 
10
+ export type StatefulPageActionDescription<ActionName> =
11
+ | {
12
+ action: ActionName;
13
+ label?: string;
14
+ }
15
+ | ActionName;
16
+
10
17
  export abstract class StatefulPage<
11
18
  State extends Record<string, unknown>,
12
19
  Actions extends Record<
@@ -61,16 +68,9 @@ export abstract class StatefulPage<
61
68
  </form>`;
62
69
  }
63
70
 
64
- makeActionButton<ActionName extends keyof Actions>(
65
- state: State,
66
- action_description:
67
- | {
68
- action: ActionName;
69
- label?: string;
70
- }
71
- | ActionName,
72
- ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>
73
- ) {
71
+ extractActionAndLabel<ActionName extends keyof Actions>(
72
+ action_description: StatefulPageActionDescription<ActionName>
73
+ ): { action: string; label: string } {
74
74
  let label, action: string;
75
75
  if (is(action_description, predicates.object)) {
76
76
  action = action_description.action.toString();
@@ -79,17 +79,51 @@ export abstract class StatefulPage<
79
79
  action = action_description.toString();
80
80
  label = action;
81
81
  }
82
+ return { action, label };
83
+ }
84
+
85
+ makeActionURL<ActionName extends keyof Actions>(
86
+ action_description: StatefulPageActionDescription<ActionName>,
87
+ ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>
88
+ ) {
89
+ const { action } = this.extractActionAndLabel(action_description);
90
+ return `./?action=${action}&args=${to_base64(JSON.stringify(args))}`;
91
+ }
92
+
93
+ makeActionButton<ActionName extends keyof Actions>(
94
+ state: State,
95
+ action_description: StatefulPageActionDescription<ActionName>,
96
+ ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>
97
+ ) {
98
+ const { label } = this.extractActionAndLabel(action_description);
82
99
  return /* HTML */ `
83
100
  <input
84
101
  type="submit"
85
102
  value=${label}
86
- formaction="./?action=${action}&args=${to_base64(
87
- JSON.stringify(args)
88
- )}"
103
+ formaction="${this.makeActionURL(action_description, ...args)}"
89
104
  />
90
105
  `;
91
106
  }
92
107
 
108
+ makeActionCallback<ActionName extends keyof Actions>(
109
+ action_description:
110
+ | {
111
+ action: ActionName;
112
+ label?: string;
113
+ }
114
+ | ActionName,
115
+ ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>
116
+ ) {
117
+ return `(()=>{const form = this.closest('form'); form.action='${this.makeActionURL(
118
+ action_description,
119
+ ...args
120
+ )}'; form.requestSubmit()})()`;
121
+ }
122
+
123
+ rerender() {
124
+ return "this.closest('form').requestSubmit()";
125
+ }
126
+
93
127
  extractState(ctx: BaseContext):
94
128
  | {
95
129
  state: State;