@easy-cms/fields 0.25.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/LICENSE +21 -0
- package/README.md +40 -0
- package/dist/admin.js +43 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +30 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kanawoot K.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @easy-cms/fields
|
|
2
|
+
|
|
3
|
+
More field types for Easy CMS, starting with `color`: a color picker with suggested colors in the admin, a check that every value is a color, and swatches in lists. Part of [Easy CMS](https://github.com/maritonx/easy-cms), the embedded, code-first headless CMS for Nuxt and Next.js.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @easy-cms/fields
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or `pnpm add`, `yarn add` or `bun add`.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { color } from '@easy-cms/fields'
|
|
17
|
+
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
// …
|
|
20
|
+
fieldTypes: [color],
|
|
21
|
+
collections: [
|
|
22
|
+
{
|
|
23
|
+
slug: 'categories',
|
|
24
|
+
fields: [
|
|
25
|
+
{ name: 'name', type: 'text', required: true },
|
|
26
|
+
// `#rrggbb`; with `alpha: true`, `#rrggbbaa` too.
|
|
27
|
+
{ name: 'color', type: 'color', presets: ['#2f6f5e', '#e8a33d'] },
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Values are stored as text, so queries and the REST API treat them like `text` fields. Importing the package types them as `string`.
|
|
35
|
+
|
|
36
|
+
## Links
|
|
37
|
+
|
|
38
|
+
[Custom field types](https://maritonx.github.io/easy-cms/guide/field-types) · [Documentation](https://maritonx.github.io/easy-cms/) ([ภาษาไทย](https://maritonx.github.io/easy-cms/th/)) · [GitHub](https://github.com/maritonx/easy-cms)
|
|
39
|
+
|
|
40
|
+
MIT License
|
package/dist/admin.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
var m={en:{picker:"Pick a color",hex:"Color code",presets:"Suggested colors",clear:"Clear"},th:{picker:"\u0E40\u0E25\u0E37\u0E2D\u0E01\u0E2A\u0E35",hex:"\u0E23\u0E2B\u0E31\u0E2A\u0E2A\u0E35",presets:"\u0E2A\u0E35\u0E41\u0E19\u0E30\u0E19\u0E33",clear:"\u0E25\u0E49\u0E32\u0E07\u0E04\u0E48\u0E32"}},v=/^#[0-9a-f]{6}$/i,x=/^#[0-9a-f]{6}([0-9a-f]{2})?$/i,b=(n,e)=>(e?x:v).test(n),f=`
|
|
2
|
+
background-image: linear-gradient(var(--c), var(--c)),
|
|
3
|
+
repeating-conic-gradient(#d4d4d4 0 25%, #fff 0 50%);
|
|
4
|
+
background-size: auto, 8px 8px;
|
|
5
|
+
`,E=`
|
|
6
|
+
:host { display: block; font: inherit; color: var(--text); }
|
|
7
|
+
[hidden] { display: none !important; }
|
|
8
|
+
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; }
|
|
9
|
+
input[type='color'] {
|
|
10
|
+
inline-size: 2.4rem; block-size: 2.25rem; padding: 0.15rem; border: 1px solid var(--border-strong);
|
|
11
|
+
border-radius: var(--radius-sm, 8px); background: var(--surface); cursor: pointer;
|
|
12
|
+
}
|
|
13
|
+
input[type='text'] {
|
|
14
|
+
inline-size: 9rem; min-block-size: 2.25rem; padding: 0 0.6rem; border: 1px solid var(--border-strong);
|
|
15
|
+
border-radius: var(--radius-sm, 8px); background: var(--surface); color: var(--text);
|
|
16
|
+
font: inherit; font-family: var(--font-mono, ui-monospace, monospace); font-size: 0.875rem;
|
|
17
|
+
}
|
|
18
|
+
input[aria-invalid='true'] { border-color: var(--danger, #c0392b); }
|
|
19
|
+
input:focus-visible, button:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
|
|
20
|
+
input:disabled { opacity: 0.6; cursor: not-allowed; }
|
|
21
|
+
.presets { display: flex; flex-wrap: wrap; gap: 0.35rem; margin: 0.5rem 0 0; padding: 0; list-style: none; }
|
|
22
|
+
.preset {
|
|
23
|
+
${f}
|
|
24
|
+
inline-size: 1.6rem; block-size: 1.6rem; padding: 0; border: 1px solid var(--border-strong);
|
|
25
|
+
border-radius: 999px; cursor: pointer;
|
|
26
|
+
}
|
|
27
|
+
.preset[aria-pressed='true'] { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }
|
|
28
|
+
.preset:disabled { cursor: not-allowed; opacity: 0.6; }
|
|
29
|
+
.clear {
|
|
30
|
+
min-block-size: 2.25rem; padding: 0 0.6rem; border: 0; border-radius: var(--radius-sm, 8px);
|
|
31
|
+
background: none; color: var(--text-muted); font: inherit; font-size: 0.8rem; cursor: pointer;
|
|
32
|
+
}
|
|
33
|
+
.clear:hover { background: var(--surface-2); color: var(--text); }
|
|
34
|
+
`;function g(n,e){for(let t of e)Object.defineProperty(n.prototype,t,{get(){return this.props[t]},set(s){this.props[t]=s,this.update()}})}var y=["value","options","readOnly","uiLocale","label","field"],l=class n extends HTMLElement{props={};#t=document.createElement("input");#e=document.createElement("input");#s=document.createElement("button");#r=document.createElement("ul");#n="";static{g(n,y)}constructor(){super(),this.#t.type="color",this.#e.type="text",this.#e.spellcheck=!1,this.#e.autocomplete="off",this.#e.placeholder="#000000",this.#s.type="button",this.#s.className="clear",this.#r.className="presets",this.#t.addEventListener("input",()=>{let s=this.#i.alpha?this.#a.slice(7,9):"";this.#o(`${this.#t.value}${s}`)}),this.#e.addEventListener("input",()=>{let s=this.#e.value.trim(),i=s&&!s.startsWith("#")?`#${s}`:s;this.#o(i===""?null:i.toLowerCase())}),this.#s.addEventListener("click",()=>this.#o(null));let e=document.createElement("div");e.className="row",e.append(this.#t,this.#e,this.#s);let t=document.createElement("style");t.textContent=E,this.attachShadow({mode:"open"}).append(t,e,this.#r)}connectedCallback(){this.update()}get#i(){return this.props.options??{}}get#a(){return typeof this.props.value=="string"?this.props.value:""}get#l(){return this.props.uiLocale==="th"?m.th:m.en}#o(e){this.dispatchEvent(new CustomEvent("change",{detail:e}))}update(){let e=this.#a,{alpha:t=!1,presets:s=[]}=this.#i,i=this.props.readOnly===!0,a=this.#l,d=typeof this.props.label=="string"?`${this.props.label}: `:"",p=e===""||b(e,t);p&&e&&(this.#t.value=e.slice(0,7).toLowerCase()),this.#t.disabled=i,this.#t.setAttribute("aria-label",`${d}${a.picker}`),this.shadowRoot?.activeElement!==this.#e&&(this.#e.value=e),this.#e.readOnly=i,this.#e.maxLength=t?9:7,this.#e.setAttribute("aria-label",`${d}${a.hex}`),this.#e.setAttribute("aria-invalid",String(!p)),this.#s.textContent=a.clear,this.#s.hidden=i||e==="";let u=JSON.stringify([s,a.presets]);u!==this.#n&&(this.#n=u,this.#r.setAttribute("aria-label",a.presets),this.#r.replaceChildren(...s.map(r=>{let h=document.createElement("li"),o=document.createElement("button");return o.type="button",o.className="preset",o.dataset.color=r,o.title=r,o.setAttribute("aria-label",r),o.style.setProperty("--c",r),o.addEventListener("click",()=>this.#o(r.toLowerCase())),h.append(o),h}))),this.#r.hidden=s.length===0;for(let r of this.#r.querySelectorAll("button"))r.disabled=i,r.setAttribute("aria-pressed",String(r.dataset.color?.toLowerCase()===e.toLowerCase()))}},k=`
|
|
35
|
+
:host { display: inline-flex; align-items: center; gap: 0.4rem; }
|
|
36
|
+
[hidden] { display: none !important; }
|
|
37
|
+
.swatch {
|
|
38
|
+
${f}
|
|
39
|
+
inline-size: 0.95rem; block-size: 0.95rem; flex: none; border: 1px solid var(--border-strong);
|
|
40
|
+
border-radius: 999px;
|
|
41
|
+
}
|
|
42
|
+
code { font-family: var(--font-mono, ui-monospace, monospace); font-size: 0.8rem; }
|
|
43
|
+
`,c=class n extends HTMLElement{props={};#t=document.createElement("span");#e=document.createElement("code");static{g(n,y)}constructor(){super(),this.#t.className="swatch",this.#t.setAttribute("aria-hidden","true");let e=document.createElement("style");e.textContent=k,this.attachShadow({mode:"open"}).append(e,this.#t,this.#e)}connectedCallback(){this.update()}update(){let e=typeof this.props.value=="string"?this.props.value:"",t=b(e,!0);this.#t.hidden=!t,t&&this.#t.style.setProperty("--c",e),this.#e.textContent=e||"\u2014"}};customElements.get("ecms-color-field")||customElements.define("ecms-color-field",l);customElements.get("ecms-color-cell")||customElements.define("ecms-color-cell",c);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { FieldTypeDefinition } from '@easy-cms/core';
|
|
2
|
+
|
|
3
|
+
declare module '@easy-cms/core' {
|
|
4
|
+
interface CustomFieldTypes {
|
|
5
|
+
/** A color: `#rrggbb`, or `#rrggbbaa` with `alpha`. */
|
|
6
|
+
color: {
|
|
7
|
+
value: string;
|
|
8
|
+
options: {
|
|
9
|
+
/** Colors offered as swatches, e.g. your brand's. */
|
|
10
|
+
readonly presets?: readonly string[];
|
|
11
|
+
/** Allow transparency (`#rrggbbaa`). Default false. */
|
|
12
|
+
readonly alpha?: boolean;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* `type: 'color'`: a color as `#rrggbb` (or `#rrggbbaa` with `alpha: true`), picked in the admin
|
|
19
|
+
* with a color picker and swatches (`presets`), and shown as a swatch in lists.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { color } from '@easy-cms/fields'
|
|
23
|
+
*
|
|
24
|
+
* fieldTypes: [color],
|
|
25
|
+
* // fields: [{ name: 'brandColor', type: 'color', presets: ['#2f6f5e', '#e8a33d'] }]
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare const color: FieldTypeDefinition;
|
|
29
|
+
|
|
30
|
+
export { color };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var HEX = /^#[0-9a-f]{6}$/i;
|
|
3
|
+
var HEX_ALPHA = /^#[0-9a-f]{6}([0-9a-f]{2})?$/i;
|
|
4
|
+
var color = {
|
|
5
|
+
name: "color",
|
|
6
|
+
base: "text",
|
|
7
|
+
validate: (value, { field }) => {
|
|
8
|
+
const alpha = field.alpha === true;
|
|
9
|
+
if ((alpha ? HEX_ALPHA : HEX).test(String(value))) return true;
|
|
10
|
+
return alpha ? "must be a color like #2f6f5e or #2f6f5e80" : "must be a color like #2f6f5e";
|
|
11
|
+
},
|
|
12
|
+
checkOptions: (field) => {
|
|
13
|
+
const presets = field.presets;
|
|
14
|
+
if (presets === void 0) return void 0;
|
|
15
|
+
const alpha = field.alpha === true;
|
|
16
|
+
if (!Array.isArray(presets) || presets.some((p) => !(alpha ? HEX_ALPHA : HEX).test(String(p))))
|
|
17
|
+
return "presets must be colors like #2f6f5e";
|
|
18
|
+
return void 0;
|
|
19
|
+
},
|
|
20
|
+
admin: {
|
|
21
|
+
component: "ecms-color-field",
|
|
22
|
+
cell: "ecms-color-cell",
|
|
23
|
+
module: "@easy-cms/fields/admin",
|
|
24
|
+
props: ["presets", "alpha"]
|
|
25
|
+
},
|
|
26
|
+
typescript: "string"
|
|
27
|
+
};
|
|
28
|
+
export {
|
|
29
|
+
color
|
|
30
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@easy-cms/fields",
|
|
3
|
+
"version": "0.25.0",
|
|
4
|
+
"description": "Field types for Easy CMS: color, with a picker and swatches in the admin",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./admin": "./dist/admin.js",
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@easy-cms/core": "^0.25.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"happy-dom": "^20.14.5",
|
|
24
|
+
"@easy-cms/core": "^0.25.0",
|
|
25
|
+
"@easy-cms/db-sqlite": "^0.25.0"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=22.12"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/maritonx/easy-cms.git",
|
|
36
|
+
"directory": "packages/fields"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"easy-cms",
|
|
40
|
+
"easy-cms-plugin",
|
|
41
|
+
"field-types",
|
|
42
|
+
"color-picker"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"test": "vitest run"
|
|
48
|
+
}
|
|
49
|
+
}
|