@shapio/editor-sdk 0.2.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 +201 -0
- package/dist/index.d.ts +174 -0
- package/dist/index.js +46 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { DataType, DataTypeSettings, JsonValue, MEDIA_KINDS } from "@shapio/schema";
|
|
2
|
+
import { ComponentType } from "react";
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The contract between the Shapio admin and a field editor (brief §6, ADR 0009). Built-in editors and
|
|
6
|
+
* custom editors loaded at runtime from a project's `extensions/editors/*.js` receive exactly these props.
|
|
7
|
+
*
|
|
8
|
+
* An editor gets the field's value and metadata plus a small, explicit context. It never gets a network
|
|
9
|
+
* client, a session, tokens or secrets: everything it can do goes through `onChange` and the context
|
|
10
|
+
* functions. The server validates the value the same way whatever editor produced it.
|
|
11
|
+
*
|
|
12
|
+
* Bump EDITOR_CONTRACT_VERSION on any breaking change. The admin refuses editors built for another
|
|
13
|
+
* version and falls back to the field's built-in editor.
|
|
14
|
+
*/
|
|
15
|
+
export declare const EDITOR_CONTRACT_VERSION = 1;
|
|
16
|
+
/** The rich-text envelope stored for `richtext` fields (ADR 0003). `doc` is ProseMirror JSON. */
|
|
17
|
+
type RichTextValue = {
|
|
18
|
+
format: 'shapio-richtext';
|
|
19
|
+
version: number;
|
|
20
|
+
doc: {
|
|
21
|
+
type: 'doc';
|
|
22
|
+
content?: JsonValue[];
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
/** A component value: its fields keyed by API key. */
|
|
26
|
+
type ComponentValue = {
|
|
27
|
+
[apiKey: string]: unknown;
|
|
28
|
+
};
|
|
29
|
+
/** A dynamic-zone item: a component value plus the component's API key in `__component`. */
|
|
30
|
+
type DynamicZoneItem = ComponentValue & {
|
|
31
|
+
__component: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* The value type of each data type, as the admin API reads and writes it. Dates and times are canonical
|
|
35
|
+
* UTC ISO-8601 text; decimals and big integers are strings so no precision is lost; relations and media
|
|
36
|
+
* are IDs (lists of IDs when the field holds several).
|
|
37
|
+
*/
|
|
38
|
+
type EditorValueMap = {
|
|
39
|
+
string: string;
|
|
40
|
+
text: string;
|
|
41
|
+
richtext: RichTextValue;
|
|
42
|
+
number: number;
|
|
43
|
+
integer: number;
|
|
44
|
+
decimal: string;
|
|
45
|
+
biginteger: string;
|
|
46
|
+
boolean: boolean;
|
|
47
|
+
date: string;
|
|
48
|
+
datetime: string;
|
|
49
|
+
time: string;
|
|
50
|
+
enum: string | string[];
|
|
51
|
+
json: JsonValue;
|
|
52
|
+
slug: string;
|
|
53
|
+
email: string;
|
|
54
|
+
url: string;
|
|
55
|
+
uid: string;
|
|
56
|
+
media: string | string[];
|
|
57
|
+
relation: string | string[];
|
|
58
|
+
component: ComponentValue | ComponentValue[];
|
|
59
|
+
dynamiczone: DynamicZoneItem[];
|
|
60
|
+
};
|
|
61
|
+
type EditorValue<T extends DataType = DataType> = EditorValueMap[T];
|
|
62
|
+
/** The field being edited, from the active model definition. */
|
|
63
|
+
type EditorField<T extends DataType = DataType> = {
|
|
64
|
+
/** Stable field ID (never changes when the field is renamed). */
|
|
65
|
+
id: string;
|
|
66
|
+
apiKey: string;
|
|
67
|
+
label: string;
|
|
68
|
+
description: string | undefined;
|
|
69
|
+
type: T;
|
|
70
|
+
required: boolean;
|
|
71
|
+
/** True when the value is per locale; false when it is shared by every locale of the entry. */
|
|
72
|
+
localized: boolean;
|
|
73
|
+
/** Type settings: validation rules (min, max, pattern...), enum values, relation target... */
|
|
74
|
+
settings: DataTypeSettings[T];
|
|
75
|
+
/** This editor's options, as configured on the field in the model builder. */
|
|
76
|
+
options: Readonly<Record<string, JsonValue>>;
|
|
77
|
+
};
|
|
78
|
+
/** The model (or component) the field belongs to. */
|
|
79
|
+
type EditorModel = {
|
|
80
|
+
id: string;
|
|
81
|
+
apiKey: string;
|
|
82
|
+
label: string;
|
|
83
|
+
kind: 'collection' | 'singleton' | 'component';
|
|
84
|
+
localized: boolean;
|
|
85
|
+
};
|
|
86
|
+
type MediaKind = (typeof MEDIA_KINDS)[number];
|
|
87
|
+
type MediaPickOptions = {
|
|
88
|
+
multiple?: boolean;
|
|
89
|
+
/** Restrict the picker to these kinds of files. */
|
|
90
|
+
allowedKinds?: readonly MediaKind[];
|
|
91
|
+
};
|
|
92
|
+
/** What the media picker hands back: enough to preview and reference an asset, never storage details. */
|
|
93
|
+
type PickedMedia = {
|
|
94
|
+
id: string;
|
|
95
|
+
filename: string;
|
|
96
|
+
mimeType: string;
|
|
97
|
+
/** For previews in the admin only. Private assets have expiring URLs; store the `id`, never the URL. */
|
|
98
|
+
url: string;
|
|
99
|
+
alt: string;
|
|
100
|
+
width: number | null;
|
|
101
|
+
height: number | null;
|
|
102
|
+
};
|
|
103
|
+
/** The limited capabilities an editor may use. */
|
|
104
|
+
type EditorContext = {
|
|
105
|
+
/** The content locale being edited, or null when the model is not localized. */
|
|
106
|
+
locale: string | null;
|
|
107
|
+
/** The entry being edited, or null for an entry that is not created yet. */
|
|
108
|
+
entryId: string | null;
|
|
109
|
+
/** The admin's interface language (e.g. `en`), for editors that ship their own strings. */
|
|
110
|
+
uiLanguage: string;
|
|
111
|
+
/** Opens the media library; resolves with the chosen assets, or null when the person cancels. */
|
|
112
|
+
pickMedia: (options?: MediaPickOptions) => Promise<PickedMedia[] | null>;
|
|
113
|
+
/** Translates one of the admin's own keys (e.g. `common.remove`); unknown keys return `fallback`. */
|
|
114
|
+
translate: (key: string, fallback: string, values?: Readonly<Record<string, string | number>>) => string;
|
|
115
|
+
};
|
|
116
|
+
type FieldValidationState = {
|
|
117
|
+
/** Server and client validation messages for this value, already translated. */
|
|
118
|
+
errors: readonly string[];
|
|
119
|
+
invalid: boolean;
|
|
120
|
+
};
|
|
121
|
+
type FieldEditorProps<T extends DataType = DataType> = {
|
|
122
|
+
/** The DOM id the field's label points at: put it on the editor's main focusable element. */
|
|
123
|
+
inputId: string;
|
|
124
|
+
/** The DOM id of the field's visible label: use `aria-labelledby` on group controls (radio groups...). */
|
|
125
|
+
labelId: string;
|
|
126
|
+
/** Ids of the description and error elements: pass to `aria-describedby`. */
|
|
127
|
+
describedBy: string | undefined;
|
|
128
|
+
/** The current value; null when the field is empty. */
|
|
129
|
+
value: EditorValue<T> | null;
|
|
130
|
+
/** Report a new value. Pass null to clear the field. */
|
|
131
|
+
onChange: (value: EditorValue<T> | null) => void;
|
|
132
|
+
/** Call when the editor loses focus (client-side checks run then). */
|
|
133
|
+
onBlur: () => void;
|
|
134
|
+
validation: FieldValidationState;
|
|
135
|
+
/** Shown but not editable (e.g. a field the person may not change). Keep the value selectable. */
|
|
136
|
+
readOnly: boolean;
|
|
137
|
+
/** Temporarily unavailable (e.g. while the entry is being saved or reloaded). */
|
|
138
|
+
disabled: boolean;
|
|
139
|
+
field: EditorField<T>;
|
|
140
|
+
model: EditorModel;
|
|
141
|
+
context: EditorContext;
|
|
142
|
+
};
|
|
143
|
+
/** A field editor: what a module in `extensions/editors/` exports (see `defineEditor`). */
|
|
144
|
+
type EditorDefinition<T extends DataType = DataType> = {
|
|
145
|
+
/** Namespaced ID referenced from field definitions, e.g. `acme.starRating` (letters, digits, `_`, `-`). */
|
|
146
|
+
id: string;
|
|
147
|
+
/** The contract version the editor was built against (set by `defineEditor`). */
|
|
148
|
+
contractVersion: number;
|
|
149
|
+
/** Data types the editor can edit. */
|
|
150
|
+
dataTypes: readonly T[];
|
|
151
|
+
component: ComponentType<FieldEditorProps<T>>;
|
|
152
|
+
};
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/defineEditor.d.ts
|
|
155
|
+
/**
|
|
156
|
+
* Declares a field editor. Export the result from your editor module as `editor`:
|
|
157
|
+
*
|
|
158
|
+
* ```tsx
|
|
159
|
+
* export const editor = defineEditor({
|
|
160
|
+
* id: 'acme.starRating',
|
|
161
|
+
* dataTypes: ['integer'],
|
|
162
|
+
* component: StarRating,
|
|
163
|
+
* });
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
* It stamps the contract version the editor was built against, so the admin can refuse an editor built
|
|
167
|
+
* for an incompatible release instead of failing at runtime.
|
|
168
|
+
*/
|
|
169
|
+
export declare const defineEditor: <T extends DataType>(definition: Omit<EditorDefinition<T>, "contractVersion">) => EditorDefinition<T>;
|
|
170
|
+
/** Runtime check of a module export (the admin's loader uses it on untrusted modules). */
|
|
171
|
+
export declare const isEditorDefinition: (value: unknown) => value is EditorDefinition;
|
|
172
|
+
//#endregion
|
|
173
|
+
export type { ComponentValue, DynamicZoneItem, EditorContext, EditorDefinition, EditorField, EditorModel, EditorValue, EditorValueMap, FieldEditorProps, FieldValidationState, MediaKind, MediaPickOptions, PickedMedia, RichTextValue };
|
|
174
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//#region src/types.ts
|
|
2
|
+
/**
|
|
3
|
+
* The contract between the Shapio admin and a field editor (brief §6, ADR 0009). Built-in editors and
|
|
4
|
+
* custom editors loaded at runtime from a project's `extensions/editors/*.js` receive exactly these props.
|
|
5
|
+
*
|
|
6
|
+
* An editor gets the field's value and metadata plus a small, explicit context. It never gets a network
|
|
7
|
+
* client, a session, tokens or secrets: everything it can do goes through `onChange` and the context
|
|
8
|
+
* functions. The server validates the value the same way whatever editor produced it.
|
|
9
|
+
*
|
|
10
|
+
* Bump EDITOR_CONTRACT_VERSION on any breaking change. The admin refuses editors built for another
|
|
11
|
+
* version and falls back to the field's built-in editor.
|
|
12
|
+
*/
|
|
13
|
+
const EDITOR_CONTRACT_VERSION = 1;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/defineEditor.ts
|
|
16
|
+
/** `vendor.name`, the same rule as `CUSTOM_EDITOR_ID_PATTERN` in @shapio/schema. */
|
|
17
|
+
const EDITOR_ID_PATTERN = /^[A-Za-z][A-Za-z0-9_-]*(\.[A-Za-z][A-Za-z0-9_-]*)+$/;
|
|
18
|
+
/**
|
|
19
|
+
* Declares a field editor. Export the result from your editor module as `editor`:
|
|
20
|
+
*
|
|
21
|
+
* ```tsx
|
|
22
|
+
* export const editor = defineEditor({
|
|
23
|
+
* id: 'acme.starRating',
|
|
24
|
+
* dataTypes: ['integer'],
|
|
25
|
+
* component: StarRating,
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* It stamps the contract version the editor was built against, so the admin can refuse an editor built
|
|
30
|
+
* for an incompatible release instead of failing at runtime.
|
|
31
|
+
*/
|
|
32
|
+
const defineEditor = (definition) => {
|
|
33
|
+
if (!EDITOR_ID_PATTERN.test(definition.id)) throw new Error(`Editor ID "${definition.id}" must be namespaced like "acme.starRating" (letters, digits, _ and -)`);
|
|
34
|
+
if (definition.dataTypes.length === 0) throw new Error(`Editor "${definition.id}" must list at least one data type`);
|
|
35
|
+
return {
|
|
36
|
+
...definition,
|
|
37
|
+
contractVersion: 1
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
41
|
+
/** Runtime check of a module export (the admin's loader uses it on untrusted modules). */
|
|
42
|
+
const isEditorDefinition = (value) => isRecord(value) && typeof value.id === "string" && EDITOR_ID_PATTERN.test(value.id) && typeof value.contractVersion === "number" && Array.isArray(value.dataTypes) && value.dataTypes.every((type) => typeof type === "string") && (typeof value.component === "function" || isRecord(value.component));
|
|
43
|
+
//#endregion
|
|
44
|
+
export { EDITOR_CONTRACT_VERSION, defineEditor, isEditorDefinition };
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/types.ts","../src/defineEditor.ts"],"sourcesContent":["import type { DataType, DataTypeSettings, JsonValue, MEDIA_KINDS } from '@shapio/schema';\nimport type { ComponentType } from 'react';\n\n/**\n * The contract between the Shapio admin and a field editor (brief §6, ADR 0009). Built-in editors and\n * custom editors loaded at runtime from a project's `extensions/editors/*.js` receive exactly these props.\n *\n * An editor gets the field's value and metadata plus a small, explicit context. It never gets a network\n * client, a session, tokens or secrets: everything it can do goes through `onChange` and the context\n * functions. The server validates the value the same way whatever editor produced it.\n *\n * Bump EDITOR_CONTRACT_VERSION on any breaking change. The admin refuses editors built for another\n * version and falls back to the field's built-in editor.\n */\nexport const EDITOR_CONTRACT_VERSION = 1;\n\n/** The rich-text envelope stored for `richtext` fields (ADR 0003). `doc` is ProseMirror JSON. */\nexport type RichTextValue = {\n format: 'shapio-richtext';\n version: number;\n doc: { type: 'doc'; content?: JsonValue[] };\n};\n\n/** A component value: its fields keyed by API key. */\nexport type ComponentValue = { [apiKey: string]: unknown };\n\n/** A dynamic-zone item: a component value plus the component's API key in `__component`. */\nexport type DynamicZoneItem = ComponentValue & { __component: string };\n\n/**\n * The value type of each data type, as the admin API reads and writes it. Dates and times are canonical\n * UTC ISO-8601 text; decimals and big integers are strings so no precision is lost; relations and media\n * are IDs (lists of IDs when the field holds several).\n */\nexport type EditorValueMap = {\n string: string;\n text: string;\n richtext: RichTextValue;\n number: number;\n integer: number;\n decimal: string;\n biginteger: string;\n boolean: boolean;\n date: string;\n datetime: string;\n time: string;\n enum: string | string[];\n json: JsonValue;\n slug: string;\n email: string;\n url: string;\n uid: string;\n media: string | string[];\n relation: string | string[];\n component: ComponentValue | ComponentValue[];\n dynamiczone: DynamicZoneItem[];\n};\n\nexport type EditorValue<T extends DataType = DataType> = EditorValueMap[T];\n\n/** The field being edited, from the active model definition. */\nexport type EditorField<T extends DataType = DataType> = {\n /** Stable field ID (never changes when the field is renamed). */\n id: string;\n apiKey: string;\n label: string;\n description: string | undefined;\n type: T;\n required: boolean;\n /** True when the value is per locale; false when it is shared by every locale of the entry. */\n localized: boolean;\n /** Type settings: validation rules (min, max, pattern...), enum values, relation target... */\n settings: DataTypeSettings[T];\n /** This editor's options, as configured on the field in the model builder. */\n options: Readonly<Record<string, JsonValue>>;\n};\n\n/** The model (or component) the field belongs to. */\nexport type EditorModel = {\n id: string;\n apiKey: string;\n label: string;\n kind: 'collection' | 'singleton' | 'component';\n localized: boolean;\n};\n\nexport type MediaKind = (typeof MEDIA_KINDS)[number];\n\nexport type MediaPickOptions = {\n multiple?: boolean;\n /** Restrict the picker to these kinds of files. */\n allowedKinds?: readonly MediaKind[];\n};\n\n/** What the media picker hands back: enough to preview and reference an asset, never storage details. */\nexport type PickedMedia = {\n id: string;\n filename: string;\n mimeType: string;\n /** For previews in the admin only. Private assets have expiring URLs; store the `id`, never the URL. */\n url: string;\n alt: string;\n width: number | null;\n height: number | null;\n};\n\n/** The limited capabilities an editor may use. */\nexport type EditorContext = {\n /** The content locale being edited, or null when the model is not localized. */\n locale: string | null;\n /** The entry being edited, or null for an entry that is not created yet. */\n entryId: string | null;\n /** The admin's interface language (e.g. `en`), for editors that ship their own strings. */\n uiLanguage: string;\n /** Opens the media library; resolves with the chosen assets, or null when the person cancels. */\n pickMedia: (options?: MediaPickOptions) => Promise<PickedMedia[] | null>;\n /** Translates one of the admin's own keys (e.g. `common.remove`); unknown keys return `fallback`. */\n translate: (key: string, fallback: string, values?: Readonly<Record<string, string | number>>) => string;\n};\n\nexport type FieldValidationState = {\n /** Server and client validation messages for this value, already translated. */\n errors: readonly string[];\n invalid: boolean;\n};\n\nexport type FieldEditorProps<T extends DataType = DataType> = {\n /** The DOM id the field's label points at: put it on the editor's main focusable element. */\n inputId: string;\n /** The DOM id of the field's visible label: use `aria-labelledby` on group controls (radio groups...). */\n labelId: string;\n /** Ids of the description and error elements: pass to `aria-describedby`. */\n describedBy: string | undefined;\n /** The current value; null when the field is empty. */\n value: EditorValue<T> | null;\n /** Report a new value. Pass null to clear the field. */\n onChange: (value: EditorValue<T> | null) => void;\n /** Call when the editor loses focus (client-side checks run then). */\n onBlur: () => void;\n validation: FieldValidationState;\n /** Shown but not editable (e.g. a field the person may not change). Keep the value selectable. */\n readOnly: boolean;\n /** Temporarily unavailable (e.g. while the entry is being saved or reloaded). */\n disabled: boolean;\n field: EditorField<T>;\n model: EditorModel;\n context: EditorContext;\n};\n\n/** A field editor: what a module in `extensions/editors/` exports (see `defineEditor`). */\nexport type EditorDefinition<T extends DataType = DataType> = {\n /** Namespaced ID referenced from field definitions, e.g. `acme.starRating` (letters, digits, `_`, `-`). */\n id: string;\n /** The contract version the editor was built against (set by `defineEditor`). */\n contractVersion: number;\n /** Data types the editor can edit. */\n dataTypes: readonly T[];\n component: ComponentType<FieldEditorProps<T>>;\n};\n","import type { DataType } from '@shapio/schema';\nimport { EDITOR_CONTRACT_VERSION, type EditorDefinition } from './types.js';\n\n/** `vendor.name`, the same rule as `CUSTOM_EDITOR_ID_PATTERN` in @shapio/schema. */\nconst EDITOR_ID_PATTERN = /^[A-Za-z][A-Za-z0-9_-]*(\\.[A-Za-z][A-Za-z0-9_-]*)+$/;\n\n/**\n * Declares a field editor. Export the result from your editor module as `editor`:\n *\n * ```tsx\n * export const editor = defineEditor({\n * id: 'acme.starRating',\n * dataTypes: ['integer'],\n * component: StarRating,\n * });\n * ```\n *\n * It stamps the contract version the editor was built against, so the admin can refuse an editor built\n * for an incompatible release instead of failing at runtime.\n */\nexport const defineEditor = <T extends DataType>(\n definition: Omit<EditorDefinition<T>, 'contractVersion'>,\n): EditorDefinition<T> => {\n if (!EDITOR_ID_PATTERN.test(definition.id)) {\n throw new Error(\n `Editor ID \"${definition.id}\" must be namespaced like \"acme.starRating\" (letters, digits, _ and -)`,\n );\n }\n if (definition.dataTypes.length === 0) {\n throw new Error(`Editor \"${definition.id}\" must list at least one data type`);\n }\n return { ...definition, contractVersion: EDITOR_CONTRACT_VERSION };\n};\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** Runtime check of a module export (the admin's loader uses it on untrusted modules). */\nexport const isEditorDefinition = (value: unknown): value is EditorDefinition =>\n isRecord(value) &&\n typeof value.id === 'string' &&\n EDITOR_ID_PATTERN.test(value.id) &&\n typeof value.contractVersion === 'number' &&\n Array.isArray(value.dataTypes) &&\n value.dataTypes.every((type) => typeof type === 'string') &&\n (typeof value.component === 'function' || isRecord(value.component));\n"],"mappings":";;;;;;;;;;;;AAcA,MAAa,0BAA0B;;;;ACVvC,MAAM,oBAAoB;;;;;;;;;;;;;;;AAgB1B,MAAa,gBACX,eACwB;CACxB,IAAI,CAAC,kBAAkB,KAAK,WAAW,EAAE,GACvC,MAAM,IAAI,MACR,cAAc,WAAW,GAAG,uEAC9B;CAEF,IAAI,WAAW,UAAU,WAAW,GAClC,MAAM,IAAI,MAAM,WAAW,WAAW,GAAG,mCAAmC;CAE9E,OAAO;EAAE,GAAG;EAAY,iBAAA;CAAyC;AACnE;AAEA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,MAAa,sBAAsB,UACjC,SAAS,KAAK,KACd,OAAO,MAAM,OAAO,YACpB,kBAAkB,KAAK,MAAM,EAAE,KAC/B,OAAO,MAAM,oBAAoB,YACjC,MAAM,QAAQ,MAAM,SAAS,KAC7B,MAAM,UAAU,OAAO,SAAS,OAAO,SAAS,QAAQ,MACvD,OAAO,MAAM,cAAc,cAAc,SAAS,MAAM,SAAS"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shapio/editor-sdk",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Contract and helpers for custom Shapio field editors loaded at runtime.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://shapio.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/mybrokengnome/shapio.git",
|
|
10
|
+
"directory": "packages/editor-sdk"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/mybrokengnome/shapio/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=24"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"@shapio/source": "./src/index.ts",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@shapio/schema": "0.2.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": "^19.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/react": "19.3.0",
|
|
37
|
+
"react": "19.3.0",
|
|
38
|
+
"tsdown": "0.23.0",
|
|
39
|
+
"typescript": "6.0.3",
|
|
40
|
+
"vitest": "5.0.3"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsdown",
|
|
47
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
48
|
+
}
|
|
49
|
+
}
|