@sealcode/jdd-editor 0.2.1 → 0.2.4

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.
Files changed (29) hide show
  1. package/@types/components.sreact.d.ts +1 -5
  2. package/@types/controllers/json-editor.stimulus.d.ts +2 -0
  3. package/@types/inputs/component-input-color.d.ts +15 -0
  4. package/@types/jdd-page.d.ts +1 -6
  5. package/dist/src/components.sreact.js +1 -14
  6. package/dist/src/components.sreact.js.map +2 -2
  7. package/dist/src/controllers/json-editor.stimulus.js +33 -23
  8. package/dist/src/controllers/json-editor.stimulus.js.map +2 -2
  9. package/dist/src/inputs/component-input-color.js +27 -0
  10. package/dist/src/inputs/component-input-color.js.map +7 -0
  11. package/dist/src/inputs/component-input.js +14 -1
  12. package/dist/src/inputs/component-input.js.map +2 -2
  13. package/dist/src/jdd-page.js +1 -14
  14. package/dist/src/jdd-page.js.map +2 -2
  15. package/package.json +3 -3
  16. package/src/components.sreact.ts +1 -14
  17. package/src/controllers/json-editor.stimulus.ts +33 -20
  18. package/src/inputs/component-input-color.ts +38 -0
  19. package/src/inputs/component-input.ts +15 -2
  20. package/src/jdd-page.ts +2 -15
  21. package/.nyc_output/2ad4e34c-cebb-4299-9698-08eccbbe71f7.json +0 -1
  22. package/.nyc_output/71f2685b-e2c9-4db2-9f31-0dbdcacfcea1.json +0 -1
  23. package/.nyc_output/cfebf13d-f940-426b-a4d3-af28d17bb6b8.json +0 -1
  24. package/.nyc_output/processinfo/2ad4e34c-cebb-4299-9698-08eccbbe71f7.json +0 -1
  25. package/.nyc_output/processinfo/71f2685b-e2c9-4db2-9f31-0dbdcacfcea1.json +0 -1
  26. package/.nyc_output/processinfo/cfebf13d-f940-426b-a4d3-af28d17bb6b8.json +0 -1
  27. package/.nyc_output/processinfo/index.json +0 -1
  28. package/.xunit +0 -2
  29. package/coverage/clover.xml +0 -866
@@ -20,6 +20,11 @@ export default class JSONEditor extends Controller<HTMLTextAreaElement> {
20
20
  cm: any;
21
21
  saving = false;
22
22
 
23
+ getSessionStorageKey() {
24
+ const page_url = document.location.pathname;
25
+ return `jdd-editor-autosave__${page_url}`;
26
+ }
27
+
23
28
  addCSS() {
24
29
  const tag = document.querySelector(`head #${CSS_ID}`);
25
30
  if (!tag) {
@@ -32,6 +37,18 @@ export default class JSONEditor extends Controller<HTMLTextAreaElement> {
32
37
  }
33
38
  }
34
39
 
40
+ resetValue() {
41
+ sessionStorage.setItem(
42
+ this.getSessionStorageKey(),
43
+ this.element.innerHTML
44
+ );
45
+ if (!this.saving) {
46
+ if (this.cm.getValue() != this.element.innerHTML) {
47
+ this.cm.setValue(this.element.innerHTML);
48
+ }
49
+ }
50
+ }
51
+
35
52
  async addJS(js_id: string, js_path: string) {
36
53
  return new Promise<void>((resolve, reject) => {
37
54
  const once_loaded = (e: MouseEvent) => {
@@ -60,6 +77,20 @@ export default class JSONEditor extends Controller<HTMLTextAreaElement> {
60
77
  }
61
78
 
62
79
  async connect() {
80
+ const autosave = sessionStorage.getItem(this.getSessionStorageKey());
81
+ if (autosave) {
82
+ this.element.innerHTML = autosave;
83
+ const action = this.element.form?.getAttribute("action");
84
+ if (!action) return;
85
+
86
+ this.element.form?.setAttribute(
87
+ "action",
88
+ "./?action=replace_state&action_args=W10%3D"
89
+ );
90
+ this.element.form?.requestSubmit();
91
+ this.element.form?.setAttribute("action", action);
92
+ }
93
+
63
94
  if (this.element.parentNode?.querySelector(".CodeMirror")) {
64
95
  //already loaded, quit;
65
96
  return;
@@ -84,16 +115,7 @@ export default class JSONEditor extends Controller<HTMLTextAreaElement> {
84
115
  // to prevent morhping from removing the element
85
116
  this.cm.getWrapperElement().setAttribute("data-turbo-permanent", "");
86
117
  const observer = new MutationObserver((mutation) => {
87
- console.log("DETECTED TEXTAREA MUTATION CHANGE!", mutation);
88
- if (!this.saving) {
89
- if (this.cm.getValue() != this.element.innerHTML) {
90
- console.log(
91
- "SETTING CM VALUE TO",
92
- this.element.innerHTML.slice(0, 100)
93
- );
94
- this.cm.setValue(this.element.innerHTML);
95
- }
96
- }
118
+ this.resetValue();
97
119
  });
98
120
  observer.observe(this.element, {
99
121
  characterData: true,
@@ -101,16 +123,7 @@ export default class JSONEditor extends Controller<HTMLTextAreaElement> {
101
123
  });
102
124
 
103
125
  this.element.addEventListener("change", () => {
104
- console.log("DETECTED TEXTAREA CHANGE!");
105
- if (!this.saving) {
106
- if (this.cm.getValue() != this.element.innerHTML) {
107
- console.log(
108
- "SETTING CM VALUE TO",
109
- this.element.innerHTML.slice(0, 100)
110
- );
111
- this.cm.setValue(this.element.innerHTML);
112
- }
113
- }
126
+ this.resetValue();
114
127
  });
115
128
  this.element
116
129
  .closest("details")
@@ -0,0 +1,38 @@
1
+ import type { Context } from "koa";
2
+ import type { Color, JDDContext } from "@sealcode/jdd";
3
+ import type { StatefulPage } from "@sealcode/sealgen";
4
+ import type { ComponentPreviewActions } from "../component-preview-actions.js";
5
+ import type { JDDPageState } from "../jdd-page.js";
6
+ import { tempstream } from "tempstream";
7
+ import { printArgPath } from "./print-arg-path.js";
8
+ import { htmlEscape } from "escape-goat";
9
+
10
+ export function ComponentInputColor<State extends JDDPageState>({
11
+ arg_path,
12
+ value,
13
+ onchange,
14
+ }: {
15
+ state: State;
16
+ arg_path: string[];
17
+ arg: Color;
18
+ value: string | null;
19
+ page: StatefulPage<JDDPageState, typeof ComponentPreviewActions>;
20
+ ctx: Context;
21
+ onchange?: string;
22
+ makeJDDContext: (ctx: Context) => JDDContext;
23
+ }): JSX.Element {
24
+ return tempstream/* HTML */ `<div style="margin-bottom: 10px">
25
+ <div>
26
+ <label>
27
+ ${arg_path.at(-1) || ""}
28
+ <input
29
+ type="color"
30
+ name="${`$${printArgPath(arg_path)}`}"
31
+ value="${htmlEscape(value || "#000000")}"
32
+ autocomplete="off"
33
+ onchange="${onchange || ""}"
34
+ />
35
+ </label>
36
+ </div>
37
+ </div>`;
38
+ }
@@ -1,7 +1,7 @@
1
1
  import { printArgPath } from "./print-arg-path.js";
2
2
  import type { Context } from "koa";
3
3
  import type { ComponentArgument, JDDContext, TableData } from "@sealcode/jdd";
4
- import { NestedComponent, SingleReference } from "@sealcode/jdd";
4
+ import { NestedComponent, Color, SingleReference } from "@sealcode/jdd";
5
5
  import {
6
6
  ComponentArguments,
7
7
  Enum,
@@ -22,7 +22,7 @@ import type { FilePointer } from "@sealcode/file-manager";
22
22
  import { ComponentInputSingleReference } from "./component-input-single-reference.js";
23
23
  import { is, predicates } from "@sealcode/ts-predicates";
24
24
  import { tempstream } from "tempstream";
25
-
25
+ import { ComponentInputColor } from "./component-input-color.js";
26
26
  export const actionName = "Components";
27
27
  const absoluteUrlPattern = "http(s?)(://)((www.)?)(([^.]+).)?([a-zA-z0-9-_]+)";
28
28
 
@@ -48,6 +48,19 @@ export async function ComponentInput<State extends JDDPageState, T>({
48
48
  if (value === undefined) {
49
49
  value = await arg.getEmptyValue(makeJDDContext(ctx));
50
50
  }
51
+ if (arg instanceof Color) {
52
+ return ComponentInputColor({
53
+ ctx,
54
+ state,
55
+ arg_path,
56
+ arg,
57
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
58
+ value: value as string,
59
+ page,
60
+ onchange: page.rerender(),
61
+ makeJDDContext,
62
+ });
63
+ }
51
64
  if (arg instanceof List) {
52
65
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
53
66
  return ComponentInputList({
package/src/jdd-page.ts CHANGED
@@ -65,22 +65,9 @@ export default abstract class JDDPage extends StatefulPage<
65
65
  return this.registry.getAll();
66
66
  }
67
67
 
68
- async getInitialState(ctx: Context) {
69
- const all_components = Object.entries(this.getRegistryComponents());
70
- const first_component = all_components[0];
71
- if (!first_component) {
72
- throw new Error("No defined components!");
73
- }
74
- const [component_name, component] = first_component;
68
+ async getInitialState(ctx: Context): Promise<JDDPageState> {
75
69
  const initial_state = {
76
- components: [
77
- {
78
- component_name: component_name,
79
- args: await component.getExampleValues(
80
- this.makeJDDContext(ctx)
81
- ),
82
- },
83
- ],
70
+ components: [],
84
71
  };
85
72
  return initial_state;
86
73
  }