@sealcode/jdd-editor 0.2.9 → 0.2.10

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,31 @@
1
+ import { printArgPath } from "./print-arg-path.js";
2
+ function ComponentCheckbox({
3
+ arg_path,
4
+ value,
5
+ onchange
6
+ }) {
7
+ const if_checked = value ? "checked" : "";
8
+ return `<div
9
+ id=${`component-input-boolean-${arg_path.join("-")}`}
10
+ >
11
+ <label>
12
+ ${arg_path.at(-1) || ""}
13
+ ${""}
14
+ <input
15
+ type="hidden"
16
+ name="${`$${printArgPath(arg_path)}`}"
17
+ value="false"
18
+ />
19
+ <input
20
+ type="checkbox"
21
+ name="${`$${printArgPath(arg_path)}`}"
22
+ onchange="${onchange || ""}"
23
+ ${if_checked}
24
+ />
25
+ </label>
26
+ </div>`;
27
+ }
28
+ export {
29
+ ComponentCheckbox
30
+ };
31
+ //# sourceMappingURL=component-input-boolean.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/inputs/component-input-boolean.ts"],
4
+ "sourcesContent": ["import type { Boolean as BooleanArgument } from \"@sealcode/jdd\";\nimport { printArgPath } from \"./print-arg-path.js\";\n\nexport function ComponentCheckbox({\n\targ_path,\n\tvalue,\n\tonchange,\n}: {\n\targ_path: string[];\n\targ: BooleanArgument;\n\tvalue: boolean;\n\tonchange?: string;\n}) {\n\tconst if_checked = value ? \"checked\" : \"\";\n\treturn /* HTML */ `<div\n\t\tid=${`component-input-boolean-${arg_path.join(\"-\")}`}\n\t>\n\t\t<label>\n\t\t\t${arg_path.at(-1) || \"\"}\n\t\t\t${\n\t\t\t\t/* JDD sends the values of the fields from the form and then they are merged with the previous state of the form.\n\t\t HTML does not send any value for a checkbox that is unchecked \u2014 so we don't get a chance to parse it.\n\t\t To overcome it, we always add a hidden input that sets the value to \"false\"\n\t\t if the checkbox is unchecked, then only the \"false\" value gets sent\n\t\t if the checkbox is checked, then two values are sent under the same name, and the value is parsed as an array:\n\t\t [\"false\", \"on\"]\n\t\t */ \"\"\n\t\t\t}\n\t\t\t<input\n\t\t\t\ttype=\"hidden\"\n\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\tvalue=\"false\"\n\t\t\t/>\n\t\t\t<input\n\t\t\t\ttype=\"checkbox\"\n\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\tonchange=\"${onchange || \"\"}\"\n\t\t\t\t${if_checked}\n\t\t\t/>\n\t\t</label>\n\t</div>`;\n}\n"],
5
+ "mappings": "AACA,SAAS,oBAAoB;AAEtB,SAAS,kBAAkB;AAAA,EACjC;AAAA,EACA;AAAA,EACA;AACD,GAKG;AACF,QAAM,aAAa,QAAQ,YAAY;AACvC,SAAkB;AAAA,OACZ,2BAA2B,SAAS,KAAK,GAAG;AAAA;AAAA;AAAA,KAG9C,SAAS,GAAG,EAAE,KAAK;AAAA,KAQlB;AAAA;AAAA;AAAA,YAIM,IAAI,aAAa,QAAQ;AAAA;AAAA;AAAA;AAAA;AAAA,YAKzB,IAAI,aAAa,QAAQ;AAAA,gBACrB,YAAY;AAAA,MACtB;AAAA;AAAA;AAAA;AAIN;",
6
+ "names": []
7
+ }
@@ -13,6 +13,7 @@ import {
13
13
  import { is, predicates } from "@sealcode/ts-predicates";
14
14
  import { htmlEscape } from "escape-goat";
15
15
  import { tempstream } from "tempstream";
16
+ import { ComponentCheckbox } from "./component-input-boolean.js";
16
17
  import { ComponentInputCode } from "./component-input-code.js";
17
18
  import { ComponentInputColor } from "./component-input-color.js";
18
19
  import { ComponentInputEnum } from "./component-input-enum.js";
@@ -61,6 +62,9 @@ async function ComponentInput({
61
62
  makeAssetURL
62
63
  });
63
64
  }
65
+ if (arg instanceof ComponentArguments.Boolean) {
66
+ return ComponentCheckbox({ arg_path, arg, value });
67
+ }
64
68
  const argType = arg.getTypeName();
65
69
  const isUrlAbsolute = arg instanceof ComponentArguments.URL && arg.urlType === "absolute";
66
70
  const inputType = isUrlAbsolute ? "url" : "text";
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/inputs/component-input.ts"],
4
- "sourcesContent": ["import type { FilePointer } from \"@sealcode/file-manager\";\nimport type { ComponentArgument, JDDContext, TableData } from \"@sealcode/jdd\";\nimport {\n\tCode,\n\tColor,\n\tComponentArguments,\n\tEnum,\n\tImage,\n\tList,\n\tNestedComponent,\n\tSingleReference,\n\tStructured,\n\tTable,\n} from \"@sealcode/jdd\";\nimport type { StatefulPage } from \"@sealcode/sealgen\";\nimport { is, predicates } from \"@sealcode/ts-predicates\";\nimport { htmlEscape } from \"escape-goat\";\nimport type { Context } from \"koa\";\nimport { tempstream } from \"tempstream\";\nimport type { ComponentPreviewActions } from \"../component-preview-actions.js\";\nimport type { JDDPageState } from \"../jdd-page.js\";\nimport { ComponentInputCode } from \"./component-input-code.js\";\nimport { ComponentInputColor } from \"./component-input-color.js\";\nimport { ComponentInputEnum } from \"./component-input-enum.js\";\nimport { ComponentInputImage } from \"./component-input-image.js\";\nimport { ComponentInputList } from \"./component-input-list.js\";\nimport { ComponentInputSingleReference } from \"./component-input-single-reference.js\";\nimport { ComponentInputStructured } from \"./component-input-structured.js\";\nimport { ComponentInputTable } from \"./component-input-table.js\";\nimport { printArgPath } from \"./print-arg-path.js\";\nexport const actionName = \"Components\";\nconst absoluteUrlPattern = \"http(s?)(://)((www.)?)(([^.]+).)?([a-zA-z0-9-_]+)\";\n\nexport async function ComponentInput<State extends JDDPageState, T>({\n\tctx,\n\tstate,\n\targ_path,\n\targ,\n\tvalue,\n\tpage,\n\tmakeJDDContext,\n\tmakeAssetURL,\n}: {\n\tstate: State;\n\tctx: Context;\n\targ_path: string[];\n\targ: ComponentArgument<T>;\n\tvalue: T;\n\tpage: StatefulPage<JDDPageState, typeof ComponentPreviewActions>;\n\tmakeJDDContext: (ctx: Context) => JDDContext;\n\tmakeAssetURL: (asset: string) => string;\n}): Promise<string> {\n\tif (value === undefined) {\n\t\tvalue = await arg.getEmptyValue(makeJDDContext(ctx));\n\t}\n\tif (arg instanceof Color) {\n\t\treturn ComponentInputColor({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as string,\n\t\t\tpage,\n\t\t\tonchange: page.rerender(),\n\t\t\tmakeJDDContext,\n\t\t});\n\t}\n\tif (arg instanceof List) {\n\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\treturn ComponentInputList({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as T[],\n\t\t\tpage,\n\t\t\tmakeJDDContext,\n\t\t\tmakeAssetURL,\n\t\t});\n\t}\n\n\tconst argType = arg.getTypeName();\n\tconst isUrlAbsolute =\n\t\targ instanceof ComponentArguments.URL && arg.urlType === \"absolute\";\n\tconst inputType = isUrlAbsolute ? \"url\" : \"text\";\n\n\tif (arg instanceof Structured || arg instanceof NestedComponent) {\n\t\treturn ComponentInputStructured({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as Record<string, unknown>,\n\t\t\tpage,\n\t\t\tmakeJDDContext,\n\t\t\tmakeAssetURL,\n\t\t});\n\t}\n\n\tif (arg instanceof SingleReference) {\n\t\treturn ComponentInputSingleReference({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as string,\n\t\t\tonchange: page.rerender(),\n\t\t\tmakeJDDContext,\n\t\t});\n\t}\n\n\tif (arg instanceof Enum) {\n\t\treturn ComponentInputEnum({\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as string,\n\t\t\tonchange: page.rerender(),\n\t\t\tjdd_context: makeJDDContext(ctx),\n\t\t});\n\t}\n\n\tif (arg instanceof Image) {\n\t\treturn ComponentInputImage({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as FilePointer,\n\t\t\tpage,\n\t\t\tmakeJDDContext,\n\t\t});\n\t}\n\n\tif (arg instanceof Table) {\n\t\treturn ComponentInputTable({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as TableData<unknown, unknown>,\n\t\t\tpage,\n\t\t\tmakeJDDContext,\n\t\t\tmakeAssetURL,\n\t\t});\n\t}\n\n\tif (arg instanceof Code) {\n\t\treturn ComponentInputCode({\n\t\t\tlanguage: arg.language,\n\t\t\tvalue: is(value, predicates.string) ? value : \"\",\n\t\t\targ_path,\n\t\t});\n\t}\n\n\tconst inputElement = () => {\n\t\tif (arg instanceof ComponentArguments.Number) {\n\t\t\treturn tempstream /* HTML */ ` <input\n\t\t\t\ttype=\"number\"\n\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\tvalue=\"${htmlEscape((value || \"\").toString())}\"\n\t\t\t\tmin=\"${arg.min || \"\"}\"\n\t\t\t\tmax=\"${arg.max || \"\"}\"\n\t\t\t\tstep=\"${arg.step || \"\"}\"\n\t\t\t/>`;\n\t\t} else if (arg instanceof ComponentArguments.URL) {\n\t\t\treturn tempstream /* HTML */ ` <input\n\t\t\t\ttype=\"${isUrlAbsolute ? \"url\" : \"text\"}\"\n\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\tvalue=\"${htmlEscape((value || \"\").toString())}\"\n\t\t\t\tsize=\"40\"\n\t\t\t\t${isUrlAbsolute ? `pattern=\"${absoluteUrlPattern}\"` : \"\"}\n\t\t\t/>`;\n\t\t} else {\n\t\t\treturn tempstream /* HTML */ ` <input\n\t\t\t\ttype=\"${inputType}\"\n\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\tvalue=\"${is(value, predicates.string) ? htmlEscape(value) : \"\"}\"\n\t\t\t\tsize=\"40\"\n\t\t\t\t${isUrlAbsolute ? `pattern=\"${absoluteUrlPattern}\"` : \"\"}\n\t\t\t/>`;\n\t\t}\n\t};\n\n\treturn /* HTML */ `<div>\n\t\t<label>\n\t\t\t${arg_path.at(-1) || \"\"}\n\t\t\t${argType == \"markdown\"\n\t\t\t\t? /* HTML */ `<div class=\"grow-wrap\">\n\t\t\t\t\t\t<textarea\n\t\t\t\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\t\t\t\tonblur=\"${page.rerender()}\"\n\t\t\t\t\t\t\tcols=\"40\"\n\t\t\t\t\t\t\tdata-controller=\"markdown-textarea submit-on-input\"\n\t\t\t\t\t\t\tdata-action=\"autogrow-textarea#autogrow blur->autogrow-textarea#autogrow resize->autogrow-textarea#autogrow submit-on-input#sendValues focus->submit-on-input#makePermanent blur->submit-on-input#makeNotPermanent\"\n\t\t\t\t\t\t\tautocomplete=\"off\"\n\t\t\t\t\t\t>\n${is(value, predicates.string) ? value : \"\"}</textarea\n\t\t\t\t\t\t>\n\t\t\t\t\t</div>`\n\t\t\t\t: await inputElement()}\n\t\t</label>\n\t</div>`;\n}\n"],
5
- "mappings": "AAEA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEP,SAAS,IAAI,kBAAkB;AAC/B,SAAS,kBAAkB;AAE3B,SAAS,kBAAkB;AAG3B,SAAS,0BAA0B;AACnC,SAAS,2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;AAAA,IAExD;AAAA,EACD;AAEA,SAAkB;AAAA;AAAA,KAEd,SAAS,GAAG,EAAE,KAAK;AAAA,KACnB,WAAW,aACC;AAAA;AAAA,eAEF,IAAI,aAAa,QAAQ;AAAA,iBACvB,KAAK,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAM7B,GAAG,OAAO,WAAW,MAAM,IAAI,QAAQ;AAAA;AAAA,eAGnC,MAAM,aAAa;AAAA;AAAA;AAGzB;",
4
+ "sourcesContent": ["import type { FilePointer } from \"@sealcode/file-manager\";\nimport type { ComponentArgument, JDDContext, TableData } from \"@sealcode/jdd\";\nimport {\n\tCode,\n\tColor,\n\tComponentArguments,\n\tEnum,\n\tImage,\n\tList,\n\tNestedComponent,\n\tSingleReference,\n\tStructured,\n\tTable,\n} from \"@sealcode/jdd\";\nimport type { StatefulPage } from \"@sealcode/sealgen\";\nimport { is, predicates } from \"@sealcode/ts-predicates\";\nimport { htmlEscape } from \"escape-goat\";\nimport type { Context } from \"koa\";\nimport { tempstream } from \"tempstream\";\nimport type { ComponentPreviewActions } from \"../component-preview-actions.js\";\nimport type { JDDPageState } from \"../jdd-page.js\";\nimport { ComponentCheckbox } from \"./component-input-boolean.js\";\nimport { ComponentInputCode } from \"./component-input-code.js\";\nimport { ComponentInputColor } from \"./component-input-color.js\";\nimport { ComponentInputEnum } from \"./component-input-enum.js\";\nimport { ComponentInputImage } from \"./component-input-image.js\";\nimport { ComponentInputList } from \"./component-input-list.js\";\nimport { ComponentInputSingleReference } from \"./component-input-single-reference.js\";\nimport { ComponentInputStructured } from \"./component-input-structured.js\";\nimport { ComponentInputTable } from \"./component-input-table.js\";\nimport { printArgPath } from \"./print-arg-path.js\";\n\nexport const actionName = \"Components\";\nconst absoluteUrlPattern = \"http(s?)(://)((www.)?)(([^.]+).)?([a-zA-z0-9-_]+)\";\n\nexport async function ComponentInput<State extends JDDPageState, T>({\n\tctx,\n\tstate,\n\targ_path,\n\targ,\n\tvalue,\n\tpage,\n\tmakeJDDContext,\n\tmakeAssetURL,\n}: {\n\tstate: State;\n\tctx: Context;\n\targ_path: string[];\n\targ: ComponentArgument<T>;\n\tvalue: T;\n\tpage: StatefulPage<JDDPageState, typeof ComponentPreviewActions>;\n\tmakeJDDContext: (ctx: Context) => JDDContext;\n\tmakeAssetURL: (asset: string) => string;\n}): Promise<string> {\n\tif (value === undefined) {\n\t\tvalue = await arg.getEmptyValue(makeJDDContext(ctx));\n\t}\n\tif (arg instanceof Color) {\n\t\treturn ComponentInputColor({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as string,\n\t\t\tpage,\n\t\t\tonchange: page.rerender(),\n\t\t\tmakeJDDContext,\n\t\t});\n\t}\n\tif (arg instanceof List) {\n\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\treturn ComponentInputList({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as T[],\n\t\t\tpage,\n\t\t\tmakeJDDContext,\n\t\t\tmakeAssetURL,\n\t\t});\n\t}\n\n\tif (arg instanceof ComponentArguments.Boolean) {\n\t\treturn ComponentCheckbox({ arg_path, arg, value: value as boolean });\n\t}\n\n\tconst argType = arg.getTypeName();\n\tconst isUrlAbsolute =\n\t\targ instanceof ComponentArguments.URL && arg.urlType === \"absolute\";\n\tconst inputType = isUrlAbsolute ? \"url\" : \"text\";\n\n\tif (arg instanceof Structured || arg instanceof NestedComponent) {\n\t\treturn ComponentInputStructured({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as Record<string, unknown>,\n\t\t\tpage,\n\t\t\tmakeJDDContext,\n\t\t\tmakeAssetURL,\n\t\t});\n\t}\n\n\tif (arg instanceof SingleReference) {\n\t\treturn ComponentInputSingleReference({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as string,\n\t\t\tonchange: page.rerender(),\n\t\t\tmakeJDDContext,\n\t\t});\n\t}\n\n\tif (arg instanceof Enum) {\n\t\treturn ComponentInputEnum({\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as string,\n\t\t\tonchange: page.rerender(),\n\t\t\tjdd_context: makeJDDContext(ctx),\n\t\t});\n\t}\n\n\tif (arg instanceof Image) {\n\t\treturn ComponentInputImage({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as FilePointer,\n\t\t\tpage,\n\t\t\tmakeJDDContext,\n\t\t});\n\t}\n\n\tif (arg instanceof Table) {\n\t\treturn ComponentInputTable({\n\t\t\tctx,\n\t\t\tstate,\n\t\t\targ_path,\n\t\t\targ,\n\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\tvalue: value as TableData<unknown, unknown>,\n\t\t\tpage,\n\t\t\tmakeJDDContext,\n\t\t\tmakeAssetURL,\n\t\t});\n\t}\n\n\tif (arg instanceof Code) {\n\t\treturn ComponentInputCode({\n\t\t\tlanguage: arg.language,\n\t\t\tvalue: is(value, predicates.string) ? value : \"\",\n\t\t\targ_path,\n\t\t});\n\t}\n\n\tconst inputElement = () => {\n\t\tif (arg instanceof ComponentArguments.Number) {\n\t\t\treturn tempstream /* HTML */ ` <input\n\t\t\t\ttype=\"number\"\n\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\tvalue=\"${htmlEscape((value || \"\").toString())}\"\n\t\t\t\tmin=\"${arg.min || \"\"}\"\n\t\t\t\tmax=\"${arg.max || \"\"}\"\n\t\t\t\tstep=\"${arg.step || \"\"}\"\n\t\t\t/>`;\n\t\t} else if (arg instanceof ComponentArguments.URL) {\n\t\t\treturn tempstream /* HTML */ ` <input\n\t\t\t\ttype=\"${isUrlAbsolute ? \"url\" : \"text\"}\"\n\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\tvalue=\"${htmlEscape((value || \"\").toString())}\"\n\t\t\t\tsize=\"40\"\n\t\t\t\t${isUrlAbsolute ? `pattern=\"${absoluteUrlPattern}\"` : \"\"}\n\t\t\t/>`;\n\t\t} else {\n\t\t\treturn tempstream /* HTML */ ` <input\n\t\t\t\ttype=\"${inputType}\"\n\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\tvalue=\"${is(value, predicates.string) ? htmlEscape(value) : \"\"}\"\n\t\t\t\tsize=\"40\"\n\t\t\t\t${isUrlAbsolute ? `pattern=\"${absoluteUrlPattern}\"` : \"\"}\n\t\t\t/>`;\n\t\t}\n\t};\n\n\treturn /* HTML */ `<div>\n\t\t<label>\n\t\t\t${arg_path.at(-1) || \"\"}\n\t\t\t${argType == \"markdown\"\n\t\t\t\t? /* HTML */ `<div class=\"grow-wrap\">\n\t\t\t\t\t\t<textarea\n\t\t\t\t\t\t\tname=\"${`$${printArgPath(arg_path)}`}\"\n\t\t\t\t\t\t\tonblur=\"${page.rerender()}\"\n\t\t\t\t\t\t\tcols=\"40\"\n\t\t\t\t\t\t\tdata-controller=\"markdown-textarea submit-on-input\"\n\t\t\t\t\t\t\tdata-action=\"autogrow-textarea#autogrow blur->autogrow-textarea#autogrow resize->autogrow-textarea#autogrow submit-on-input#sendValues focus->submit-on-input#makePermanent blur->submit-on-input#makeNotPermanent\"\n\t\t\t\t\t\t\tautocomplete=\"off\"\n\t\t\t\t\t\t>\n${is(value, predicates.string) ? value : \"\"}</textarea\n\t\t\t\t\t\t>\n\t\t\t\t\t</div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AI,OAAO;AAAA,YACV,IAAI,QAAQ;AAAA;AAAA,IAEtB,WAAW,eAAe,mBAAmB,KAAK;AACjD,aAAO;AAAA,YACE,gBAAgB,QAAQ;AAAA,YACxB,IAAI,aAAa,QAAQ;AAAA,aACxB,YAAY,SAAS,IAAI,SAAS,CAAC;AAAA;AAAA,MAE1C,gBAAgB,YAAY,wBAAwB;AAAA;AAAA,IAExD,OAAO;AACN,aAAO;AAAA,YACE;AAAA,YACA,IAAI,aAAa,QAAQ;AAAA,aACxB,GAAG,OAAO,WAAW,MAAM,IAAI,WAAW,KAAK,IAAI;AAAA;AAAA,MAE1D,gBAAgB,YAAY,wBAAwB;AAAA;AAAA,IAExD;AAAA,EACD;AAEA,SAAkB;AAAA;AAAA,KAEd,SAAS,GAAG,EAAE,KAAK;AAAA,KACnB,WAAW,aACC;AAAA;AAAA,eAEF,IAAI,aAAa,QAAQ;AAAA,iBACvB,KAAK,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAM7B,GAAG,OAAO,WAAW,MAAM,IAAI,QAAQ;AAAA;AAAA,eAGnC,MAAM,aAAa;AAAA;AAAA;AAGzB;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/jdd-editor",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "main": "dist/src/index.js",
5
5
  "scripts": {
6
6
  "build": "rm -rf dist && node ./esbuild.cjs",
@@ -0,0 +1,42 @@
1
+ import type { Boolean as BooleanArgument } from "@sealcode/jdd";
2
+ import { printArgPath } from "./print-arg-path.js";
3
+
4
+ export function ComponentCheckbox({
5
+ arg_path,
6
+ value,
7
+ onchange,
8
+ }: {
9
+ arg_path: string[];
10
+ arg: BooleanArgument;
11
+ value: boolean;
12
+ onchange?: string;
13
+ }) {
14
+ const if_checked = value ? "checked" : "";
15
+ return /* HTML */ `<div
16
+ id=${`component-input-boolean-${arg_path.join("-")}`}
17
+ >
18
+ <label>
19
+ ${arg_path.at(-1) || ""}
20
+ ${
21
+ /* JDD sends the values of the fields from the form and then they are merged with the previous state of the form.
22
+ HTML does not send any value for a checkbox that is unchecked — so we don't get a chance to parse it.
23
+ To overcome it, we always add a hidden input that sets the value to "false"
24
+ if the checkbox is unchecked, then only the "false" value gets sent
25
+ if the checkbox is checked, then two values are sent under the same name, and the value is parsed as an array:
26
+ ["false", "on"]
27
+ */ ""
28
+ }
29
+ <input
30
+ type="hidden"
31
+ name="${`$${printArgPath(arg_path)}`}"
32
+ value="false"
33
+ />
34
+ <input
35
+ type="checkbox"
36
+ name="${`$${printArgPath(arg_path)}`}"
37
+ onchange="${onchange || ""}"
38
+ ${if_checked}
39
+ />
40
+ </label>
41
+ </div>`;
42
+ }
@@ -19,6 +19,7 @@ import type { Context } from "koa";
19
19
  import { tempstream } from "tempstream";
20
20
  import type { ComponentPreviewActions } from "../component-preview-actions.js";
21
21
  import type { JDDPageState } from "../jdd-page.js";
22
+ import { ComponentCheckbox } from "./component-input-boolean.js";
22
23
  import { ComponentInputCode } from "./component-input-code.js";
23
24
  import { ComponentInputColor } from "./component-input-color.js";
24
25
  import { ComponentInputEnum } from "./component-input-enum.js";
@@ -28,6 +29,7 @@ import { ComponentInputSingleReference } from "./component-input-single-referenc
28
29
  import { ComponentInputStructured } from "./component-input-structured.js";
29
30
  import { ComponentInputTable } from "./component-input-table.js";
30
31
  import { printArgPath } from "./print-arg-path.js";
32
+
31
33
  export const actionName = "Components";
32
34
  const absoluteUrlPattern = "http(s?)(://)((www.)?)(([^.]+).)?([a-zA-z0-9-_]+)";
33
35
 
@@ -81,6 +83,10 @@ export async function ComponentInput<State extends JDDPageState, T>({
81
83
  });
82
84
  }
83
85
 
86
+ if (arg instanceof ComponentArguments.Boolean) {
87
+ return ComponentCheckbox({ arg_path, arg, value: value as boolean });
88
+ }
89
+
84
90
  const argType = arg.getTypeName();
85
91
  const isUrlAbsolute =
86
92
  arg instanceof ComponentArguments.URL && arg.urlType === "absolute";