@easy-cms/plugin-form-builder 0.20.0 → 0.21.0
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.
- package/dist/element.js +5 -5
- package/package.json +5 -5
package/dist/element.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
var b=o=>`/form/${encodeURIComponent(o)}`;var v=o=>o.replace(/\/+$/,"");async function k(o,e){let
|
|
1
|
+
var b=o=>`/form/${encodeURIComponent(o)}`;var v=o=>o.replace(/\/+$/,"");async function k(o,e){let t=e.locale?`?locale=${encodeURIComponent(e.locale)}`:"",r=await(e.fetch??fetch)(`${v(e.api)}${b(o)}${t}`,{credentials:"omit"});if(r.status===404)return null;if(!r.ok)throw new Error(`Could not load form "${o}" (${r.status})`);return await r.json()}async function _(o,e,t){let r=await(t.fetch??fetch)(`${v(t.api)}${b(o)}/submit`,{method:"POST",credentials:"omit",headers:{"content-type":"application/json"},body:JSON.stringify(e)}),s=await r.json().catch(()=>({}));return r.ok&&s.confirmation?{ok:!0,confirmation:s.confirmation}:{ok:!1,status:r.status,errors:s.errors??[{message:`Could not send the form (${r.status})`}]}}var x={en:{loading:"Loading the form\u2026",missing:"This form is not available.",sending:"Sending\u2026",failed:"Could not send the form. Check your connection and try again.",fix:"Please check the fields marked below.",choose:"Choose\u2026",required:"Required"},th:{loading:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E42\u0E2B\u0E25\u0E14\u0E1F\u0E2D\u0E23\u0E4C\u0E21\u2026",missing:"\u0E1F\u0E2D\u0E23\u0E4C\u0E21\u0E19\u0E35\u0E49\u0E44\u0E21\u0E48\u0E1E\u0E23\u0E49\u0E2D\u0E21\u0E43\u0E0A\u0E49\u0E07\u0E32\u0E19",sending:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E2A\u0E48\u0E07\u2026",failed:"\u0E2A\u0E48\u0E07\u0E1F\u0E2D\u0E23\u0E4C\u0E21\u0E44\u0E21\u0E48\u0E2A\u0E33\u0E40\u0E23\u0E47\u0E08 \u0E15\u0E23\u0E27\u0E08\u0E2A\u0E2D\u0E1A\u0E01\u0E32\u0E23\u0E40\u0E0A\u0E37\u0E48\u0E2D\u0E21\u0E15\u0E48\u0E2D\u0E41\u0E25\u0E49\u0E27\u0E25\u0E2D\u0E07\u0E2D\u0E35\u0E01\u0E04\u0E23\u0E31\u0E49\u0E07",fix:"\u0E42\u0E1B\u0E23\u0E14\u0E15\u0E23\u0E27\u0E08\u0E0A\u0E48\u0E2D\u0E07\u0E17\u0E35\u0E48\u0E21\u0E35\u0E40\u0E04\u0E23\u0E37\u0E48\u0E2D\u0E07\u0E2B\u0E21\u0E32\u0E22\u0E14\u0E49\u0E32\u0E19\u0E25\u0E48\u0E32\u0E07",choose:"\u0E40\u0E25\u0E37\u0E2D\u0E01\u2026",required:"\u0E08\u0E33\u0E40\u0E1B\u0E47\u0E19"}},i=o=>o.replace(/[&<>"']/g,e=>({"&":"&","<":"<",">":">",'"':""","'":"'"})[e]),C="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit",S;function T(){return S??=new Promise((o,e)=>{let t=document.createElement("script");t.src=C,t.async=!0,t.onload=()=>o(),t.onerror=()=>e(new Error("Could not load Cloudflare Turnstile")),document.head.append(t)}),S}var L=0,g=class extends HTMLElement{static observedAttributes=["form","api","locale"];loaded=null;turnstileToken="";turnstileId;uid=`easy-form-${++L}`;get form(){return this.getAttribute("form")}set form(e){this.reflect("form",e)}get api(){return this.getAttribute("api")}set api(e){this.reflect("api",e)}get locale(){return this.getAttribute("locale")}set locale(e){this.reflect("locale",e)}reflect(e,t){t==null||t===""?this.removeAttribute(e):this.setAttribute(e,String(t))}get apiBase(){return this.getAttribute("api")||"/api/cms"}get contentLocale(){return this.getAttribute("locale")||document.documentElement.lang||null}t(e){return((this.loaded?.locale??this.contentLocale)?.startsWith("th")?x.th:x.en)[e]}connectedCallback(){this.load()}attributeChangedCallback(e,t,r){t!==r&&this.isConnected&&this.load()}async load(){let e=this.getAttribute("form");if(e){this.innerHTML=`<p class="easy-form__status" role="status">${i(this.t("loading"))}</p>`;try{this.loaded=await k(e,{api:this.apiBase,locale:this.contentLocale})}catch{this.loaded=null}if(!this.loaded){this.innerHTML=`<p class="easy-form__status easy-form__status--error" role="alert">${i(this.t("missing"))}</p>`;return}this.render(this.loaded)}}render(e){let t=e.fields.map(s=>this.field(s)).join("");this.innerHTML=`
|
|
2
2
|
<form class="easy-form" novalidate>
|
|
3
|
-
<div class="easy-form__fields">${
|
|
3
|
+
<div class="easy-form__fields">${t}</div>
|
|
4
4
|
<div class="easy-form__hp" aria-hidden="true" style="position:absolute;left:-10000px;width:1px;height:1px;overflow:hidden">
|
|
5
5
|
<label>Leave this empty <input type="text" name="${i(e.honeypot)}" tabindex="-1" autocomplete="off"></label>
|
|
6
6
|
</div>
|
|
7
7
|
${e.turnstile?'<div class="easy-form__turnstile"></div>':""}
|
|
8
8
|
<p class="easy-form__status" role="status" aria-live="polite"></p>
|
|
9
9
|
<button type="submit" class="easy-form__submit">${i(e.submitLabel)}</button>
|
|
10
|
-
</form>`;let
|
|
11
|
-
<label class="easy-form__check"><input type="checkbox" ${
|
|
12
|
-
${l}</div>`;case"select":{let p=e.options??[];if(e.display==="radio"){let f=e.multiple?"checkbox":"radio",w=p.map(($,E)=>`<label class="easy-form__choice"><input type="${f}" name="${i(e.name)}" value="${i($.value)}" id="${
|
|
10
|
+
</form>`;let r=this.querySelector("form");r.addEventListener("submit",s=>{s.preventDefault(),this.submit(r)}),e.turnstile&&this.mountTurnstile(e.turnstile)}field(e){let t=`${this.uid}-${e.name}`,r=`easy-form__field easy-form__field--${e.kind} easy-form__field--${e.width}`;if(e.kind==="message")return`<div class="easy-form__message">${e.html??""}</div>`;let s=e.required?' required aria-required="true"':"",n=e.required?` <span class="easy-form__required" aria-label="${i(this.t("required"))}">*</span>`:"",u=`<label class="easy-form__label" for="${t}">${i(e.label)}${n}</label>`,d=e.placeholder?` placeholder="${i(e.placeholder)}"`:"",a=typeof e.defaultValue=="string"?` value="${i(e.defaultValue)}"`:"",l=`<p class="easy-form__error" id="${t}-error" hidden></p>`,c=`id="${t}" name="${i(e.name)}" aria-describedby="${t}-error"${s}`,m;switch(e.kind){case"textarea":m=`<textarea class="easy-form__input" ${c}${d} rows="5">${i(typeof e.defaultValue=="string"?e.defaultValue:"")}</textarea>`;break;case"checkbox":return`<div class="${r}">
|
|
11
|
+
<label class="easy-form__check"><input type="checkbox" ${c}${e.defaultValue===!0?" checked":""}> ${i(e.label)}${n}</label>
|
|
12
|
+
${l}</div>`;case"select":{let p=e.options??[];if(e.display==="radio"){let f=e.multiple?"checkbox":"radio",w=p.map(($,E)=>`<label class="easy-form__choice"><input type="${f}" name="${i(e.name)}" value="${i($.value)}" id="${t}-${E}"${e.required&&!e.multiple?" required":""}> ${i($.label)}</label>`).join("");return`<fieldset class="${r}" aria-describedby="${t}-error"><legend class="easy-form__label">${i(e.label)}${n}</legend>${w}${l}</fieldset>`}let h=p.map(f=>`<option value="${i(f.value)}">${i(f.label)}</option>`).join(""),y=e.multiple?"":`<option value="">${i(e.placeholder??this.t("choose"))}</option>`;m=`<select class="easy-form__input" ${c}${e.multiple?" multiple":""}>${y}${h}</select>`;break}default:{let p=e.kind==="email"?"email":e.kind==="number"?"number":e.kind==="phone"?"tel":e.kind==="date"?"date":"text",h=`${e.min!==void 0?` min="${e.min}"`:""}${e.max!==void 0?` max="${e.max}"`:""}`,y=e.kind==="email"?' autocomplete="email"':e.kind==="phone"?' autocomplete="tel"':"";m=`<input class="easy-form__input" type="${p}" ${c}${d}${a}${h}${y}>`}}return`<div class="${r}">${u}${m}${l}</div>`}async mountTurnstile(e){try{await T();let t=window.turnstile,r=this.querySelector(".easy-form__turnstile");if(!t||!r)return;this.turnstileId=t.render(r,{sitekey:e,callback:s=>{this.turnstileToken=s}})}catch{this.status(this.t("failed"),!0)}}values(e){let t={};for(let r of this.loaded?.fields??[]){if(r.kind==="message")continue;let s=[...e.querySelectorAll(`[name="${CSS.escape(r.name)}"]`)];r.kind==="checkbox"?t[r.name]=s[0]?.checked===!0:r.kind==="select"&&r.multiple?t[r.name]=r.display==="radio"?s.filter(n=>n.checked).map(n=>n.value):[...s[0]?.selectedOptions??[]].map(n=>n.value):r.kind==="select"&&r.display==="radio"?t[r.name]=s.find(n=>n.checked)?.value??"":t[r.name]=s[0]?.value??""}return t}async submit(e){let t=this.loaded;if(!t)return;let r=e.querySelector(".easy-form__submit");r&&(r.disabled=!0),this.clearErrors(),this.status(this.t("sending"),!1);let s=e.querySelector(`[name="${CSS.escape(t.honeypot)}"]`),n;try{n=await _(t.slug,{data:this.values(e),token:t.token,page:location.pathname+location.search,locale:t.locale,...t.turnstile?{turnstile:this.turnstileToken}:{},[t.honeypot]:s?.value??""},{api:this.apiBase})}catch{n={ok:!1,status:0,errors:[{message:this.t("failed")}]}}if(r&&(r.disabled=!1),n.ok){this.dispatchEvent(new CustomEvent("easy-form:submitted",{detail:{confirmation:n.confirmation},bubbles:!0})),this.confirm(n.confirmation);return}this.dispatchEvent(new CustomEvent("easy-form:error",{detail:{errors:n.errors},bubbles:!0}));let u=0;for(let a of n.errors){if(!a.field)continue;let l=this.querySelector(`#${CSS.escape(`${this.uid}-${a.field}-error`)}`);l&&(l.textContent=a.message,l.hidden=!1,this.querySelector(`#${CSS.escape(`${this.uid}-${a.field}`)}`)?.setAttribute("aria-invalid","true"),u++)}let d=n.errors.filter(a=>!a.field).map(a=>a.message);this.status(d.join(" ")||(u?this.t("fix"):this.t("failed")),!0),n.status===400&&!u?this.load():this.turnstileId!==void 0&&window.turnstile?.reset(this.turnstileId)}confirm(e){if(e.type==="redirect"){location.assign(e.url);return}this.innerHTML=`<div class="easy-form__confirmation" role="status" tabindex="-1">${e.html}</div>`,this.querySelector(".easy-form__confirmation")?.focus()}clearErrors(){for(let e of this.querySelectorAll(".easy-form__error"))e.hidden=!0,e.textContent="";for(let e of this.querySelectorAll("[aria-invalid]"))e.removeAttribute("aria-invalid")}status(e,t){let r=this.querySelector(".easy-form__status");r&&(r.textContent=e,r.classList.toggle("easy-form__status--error",t))}};typeof customElements<"u"&&!customElements.get("easy-form")&&customElements.define("easy-form",g);export{g as EasyFormElement};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easy-cms/plugin-form-builder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "Forms for Easy CMS: build forms in the admin, collect submissions, email notifications, spam protection and an <easy-form> element",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -23,15 +23,15 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@easy-cms/richtext": "^0.
|
|
26
|
+
"@easy-cms/richtext": "^0.21.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@easy-cms/core": "^0.
|
|
29
|
+
"@easy-cms/core": "^0.21.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"happy-dom": "^20.14.5",
|
|
33
|
-
"@easy-cms/core": "^0.
|
|
34
|
-
"@easy-cms/db-sqlite": "^0.
|
|
33
|
+
"@easy-cms/core": "^0.21.0",
|
|
34
|
+
"@easy-cms/db-sqlite": "^0.21.0"
|
|
35
35
|
},
|
|
36
36
|
"engines": {
|
|
37
37
|
"node": ">=22.12"
|