datocms-plugin-sdk 2.1.0-alpha.0 → 2.1.0-alpha.2

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 (81) hide show
  1. package/dist/cjs/connect.js +4 -0
  2. package/dist/cjs/connect.js.map +1 -1
  3. package/dist/cjs/guardUtils.js +12 -0
  4. package/dist/cjs/guardUtils.js.map +1 -1
  5. package/dist/cjs/hooks/executeSchemaItemTypeDropdownAction.js +3 -0
  6. package/dist/cjs/hooks/executeSchemaItemTypeDropdownAction.js.map +1 -0
  7. package/dist/cjs/hooks/mainNavigationTabs.js +4 -1
  8. package/dist/cjs/hooks/mainNavigationTabs.js.map +1 -1
  9. package/dist/cjs/hooks/renderInspector.js +11 -0
  10. package/dist/cjs/hooks/renderInspector.js.map +1 -0
  11. package/dist/cjs/hooks/renderInspectorPanel.js +11 -0
  12. package/dist/cjs/hooks/renderInspectorPanel.js.map +1 -0
  13. package/dist/cjs/hooks/renderPage.js.map +1 -1
  14. package/dist/cjs/hooks/schemaItemTypeDropdownActions.js +3 -0
  15. package/dist/cjs/hooks/schemaItemTypeDropdownActions.js.map +1 -0
  16. package/dist/cjs/icon.js +12 -5
  17. package/dist/cjs/icon.js.map +1 -1
  18. package/dist/cjs/index.js +4 -0
  19. package/dist/cjs/index.js.map +1 -1
  20. package/dist/cjs/manifest.js +268 -32
  21. package/dist/cjs/manifest.js.map +1 -1
  22. package/dist/esm/connect.d.ts +5 -1
  23. package/dist/esm/connect.js +4 -0
  24. package/dist/esm/connect.js.map +1 -1
  25. package/dist/esm/ctx/base.d.ts +2 -0
  26. package/dist/esm/guardUtils.d.ts +4 -0
  27. package/dist/esm/guardUtils.js +8 -0
  28. package/dist/esm/guardUtils.js.map +1 -1
  29. package/dist/esm/hooks/executeSchemaItemTypeDropdownAction.d.ts +20 -0
  30. package/dist/esm/hooks/executeSchemaItemTypeDropdownAction.js +2 -0
  31. package/dist/esm/hooks/executeSchemaItemTypeDropdownAction.js.map +1 -0
  32. package/dist/esm/hooks/mainNavigationTabs.d.ts +15 -1
  33. package/dist/esm/hooks/mainNavigationTabs.js +4 -1
  34. package/dist/esm/hooks/mainNavigationTabs.js.map +1 -1
  35. package/dist/esm/hooks/onBeforeItemUpsert.d.ts +28 -3
  36. package/dist/esm/hooks/renderInspector.d.ts +147 -0
  37. package/dist/esm/hooks/renderInspector.js +8 -0
  38. package/dist/esm/hooks/renderInspector.js.map +1 -0
  39. package/dist/esm/hooks/renderInspectorPanel.d.ts +20 -0
  40. package/dist/esm/hooks/renderInspectorPanel.js +8 -0
  41. package/dist/esm/hooks/renderInspectorPanel.js.map +1 -0
  42. package/dist/esm/hooks/renderPage.d.ts +6 -0
  43. package/dist/esm/hooks/renderPage.js.map +1 -1
  44. package/dist/esm/hooks/schemaItemTypeDropdownActions.d.ts +17 -0
  45. package/dist/esm/hooks/schemaItemTypeDropdownActions.js +2 -0
  46. package/dist/esm/hooks/schemaItemTypeDropdownActions.js.map +1 -0
  47. package/dist/esm/icon.d.ts +61 -2
  48. package/dist/esm/icon.js +11 -6
  49. package/dist/esm/icon.js.map +1 -1
  50. package/dist/esm/index.d.ts +4 -0
  51. package/dist/esm/index.js +4 -0
  52. package/dist/esm/index.js.map +1 -1
  53. package/dist/esm/manifest.js +268 -32
  54. package/dist/esm/manifest.js.map +1 -1
  55. package/dist/types/connect.d.ts +5 -1
  56. package/dist/types/ctx/base.d.ts +2 -0
  57. package/dist/types/guardUtils.d.ts +4 -0
  58. package/dist/types/hooks/executeSchemaItemTypeDropdownAction.d.ts +20 -0
  59. package/dist/types/hooks/mainNavigationTabs.d.ts +15 -1
  60. package/dist/types/hooks/onBeforeItemUpsert.d.ts +28 -3
  61. package/dist/types/hooks/renderInspector.d.ts +147 -0
  62. package/dist/types/hooks/renderInspectorPanel.d.ts +20 -0
  63. package/dist/types/hooks/renderPage.d.ts +6 -0
  64. package/dist/types/hooks/schemaItemTypeDropdownActions.d.ts +17 -0
  65. package/dist/types/icon.d.ts +61 -2
  66. package/dist/types/index.d.ts +4 -0
  67. package/manifest.json +268 -32
  68. package/package.json +4 -3
  69. package/src/connect.ts +16 -0
  70. package/src/ctx/base.ts +3 -0
  71. package/src/guardUtils.ts +13 -0
  72. package/src/hooks/executeSchemaItemTypeDropdownAction.ts +24 -0
  73. package/src/hooks/mainNavigationTabs.ts +24 -5
  74. package/src/hooks/onBeforeItemUpsert.ts +31 -3
  75. package/src/hooks/renderInspector.ts +176 -0
  76. package/src/hooks/renderInspectorPanel.ts +38 -0
  77. package/src/hooks/renderPage.ts +7 -0
  78. package/src/hooks/schemaItemTypeDropdownActions.ts +22 -0
  79. package/src/icon.ts +80 -9
  80. package/src/index.ts +4 -0
  81. package/src/manifest.ts +284 -32
@@ -36,10 +36,26 @@ export type MainNavigationTab = {
36
36
  * whenever possible.
37
37
  */
38
38
  icon: Icon;
39
- /** ID of the page linked to the tab */
40
- pointsTo: {
41
- pageId: string;
42
- };
39
+ /** ID of the page/inspector linked to the tab */
40
+ pointsTo:
41
+ | {
42
+ pageId: string;
43
+ }
44
+ | {
45
+ inspectorId: string;
46
+ /** The preferred width for the sidebar */
47
+ preferredWidth?: number;
48
+ /** The inspector panel to render when first loaded */
49
+ initialInspectorPanel?: {
50
+ /** ID of the inspector panel to render */
51
+ panelId: string;
52
+ /**
53
+ * An arbitrary configuration object that will be passed as the `parameters`
54
+ * property of the second argument of the `renderInspectorPanel` function
55
+ */
56
+ parameters?: Record<string, unknown>;
57
+ };
58
+ };
43
59
  /**
44
60
  * Expresses where you want to place the tab in the top-bar. If not specified,
45
61
  * the tab will be placed after the standard tabs provided by DatoCMS itself.
@@ -66,7 +82,10 @@ export function isMainNavigationTab(
66
82
  isString(value.label) &&
67
83
  isIcon(value.icon) &&
68
84
  isRecord(value.pointsTo) &&
69
- isString(value.pointsTo.pageId) &&
85
+ (isString(value.pointsTo.pageId) ||
86
+ (isString(value.pointsTo.inspectorId) &&
87
+ (isNullish(value.pointsTo.preferredWidth) ||
88
+ isNumber(value.pointsTo.preferredWidth)))) &&
70
89
  (isNullish(value.placement) || isPlacement(value.placement)) &&
71
90
  (isNullish(value.rank) || isNumber(value.rank))
72
91
  );
@@ -7,8 +7,15 @@ type ItemCreateSchema = SchemaTypes.ItemCreateSchema;
7
7
 
8
8
  export type OnBeforeItemUpsertHook = {
9
9
  /**
10
- * This function will be called before saving a new version of a record. You
11
- * can stop the action by returning `false`
10
+ * This hook is called when the user attempts to save a record. You can use it to block record saving.
11
+ *
12
+ * If you return `false`, the record will NOT be saved. A small on-page error will say "A plugin blocked the action".
13
+ * However, for better UX, consider also using `ctx.alert()` to better explain to the user why their save was blocked.
14
+ *
15
+ * If you return `true`, the save will proceed as normal.
16
+ *
17
+ * This hook runs BEFORE serverside validation. You can use it to do your own additional validation before returning.
18
+ * Clientside validations are not affected by this hook, since those occur on individual fields' `onBlur()` events.
12
19
  *
13
20
  * @tag beforeHooks
14
21
  */
@@ -18,4 +25,25 @@ export type OnBeforeItemUpsertHook = {
18
25
  ) => MaybePromise<boolean>;
19
26
  };
20
27
 
21
- export type OnBeforeItemUpsertCtx = Ctx;
28
+ export type OnBeforeItemUpsertCtx = Ctx<
29
+ {},
30
+ {
31
+ /**
32
+ * Smoothly navigates to a specific field in the form. If the field is
33
+ * localized it will switch language tab and then navigate to the chosen
34
+ * field.
35
+ *
36
+ * @example
37
+ *
38
+ * ```js
39
+ * const fieldPath = prompt(
40
+ * 'Please insert the path of a field in the form',
41
+ * ctx.fieldPath,
42
+ * );
43
+ *
44
+ * await ctx.scrollToField(fieldPath);
45
+ * ```
46
+ */
47
+ scrollToField: (path: string, locale?: string) => Promise<void>;
48
+ }
49
+ >;
@@ -0,0 +1,176 @@
1
+ import { ImposedSizePluginFrameCtx } from '../ctx/pluginFrame';
2
+ import { fullScreenRenderModeBootstrapper } from '../utils';
3
+
4
+ /**
5
+ * Defines the different modes in which an inspector can be displayed
6
+ */
7
+ export type InspectorMode =
8
+ | {
9
+ /** Display a list of records in the inspector */
10
+ type: 'itemList';
11
+ }
12
+ | {
13
+ /** Display a single record editor in the inspector */
14
+ type: 'itemEditor';
15
+ /** The ID of the record to edit */
16
+ itemId: string;
17
+ /** Optional field path to highlight/focus within the record editor */
18
+ fieldPath?: string;
19
+ }
20
+ | {
21
+ /** Display a custom panel in the inspector */
22
+ type: 'customPanel';
23
+ /** ID of the inspector panel to render */
24
+ panelId: string;
25
+ /**
26
+ * An arbitrary configuration object that will be passed as the `parameters`
27
+ * property of the second argument of the `renderInspectorPanel` function
28
+ */
29
+ parameters?: Record<string, unknown>;
30
+ };
31
+
32
+ /**
33
+ * Options for configuring inspector mode changes
34
+ */
35
+ export type SetInspectorModeOptions = {
36
+ /**
37
+ * When true, the mode change will be ignored if there are unsaved changes
38
+ * in the current inspector. Useful for "low intent" mode changes that
39
+ * shouldn't interrupt active editing sessions.
40
+ * @default false
41
+ */
42
+ ignoreIfUnsavedChanges?: boolean;
43
+ };
44
+
45
+ export type RenderInspectorHook = {
46
+ /**
47
+ * This function will be called when the plugin needs to render a specific
48
+ * inspector. Inspectors provide a side panel interface for displaying and
49
+ * interacting with content alongside a custom interface.
50
+ *
51
+ * @tag inspector
52
+ *
53
+ * @example
54
+ *
55
+ * ```js
56
+ * connect({
57
+ * renderInspector(inspectorId, ctx) {
58
+ * render(
59
+ * <div>
60
+ * <h1>Inspector: {inspectorId}</h1>
61
+ * <button onClick={() => ctx.setInspectorMode({
62
+ * type: 'itemEditor',
63
+ * itemId: 'some-item-id'
64
+ * })}>
65
+ * Show Item Editor
66
+ * </button>
67
+ * </div>
68
+ * );
69
+ * }
70
+ * });
71
+ * ```
72
+ */
73
+ renderInspector: (inspectorId: string, ctx: RenderInspectorCtx) => void;
74
+ };
75
+
76
+ export type RenderInspectorCtx = ImposedSizePluginFrameCtx<
77
+ 'renderInspector',
78
+ {
79
+ /** The ID of the inspector that needs to be rendered */
80
+ inspectorId: string;
81
+
82
+ /** The ID of the record the currently is highlighted by the user */
83
+ highlightedItemId: string | undefined;
84
+
85
+ /** Current page location */
86
+ location: {
87
+ pathname: string;
88
+ search: string;
89
+ hash: string;
90
+ };
91
+ },
92
+ {
93
+ /**
94
+ * Changes the current display mode of the inspector. This allows the plugin
95
+ * to dynamically switch between showing a record list, record editor, or custom
96
+ * panel within the inspector interface.
97
+ *
98
+ * @param mode - The inspector mode to switch to
99
+ * @param options - Optional configuration for the mode change
100
+ * @param options.ignoreIfUnsavedChanges - When true, the mode change request will be
101
+ * ignored if the current inspector is in itemEditor mode and has unsaved changes.
102
+ * This allows for "low intent" mode changes that shouldn't interrupt active editing.
103
+ * Default is false, meaning mode changes will proceed regardless of unsaved changes.
104
+ *
105
+ * @example
106
+ *
107
+ * ```js
108
+ * // Switch to record editor mode
109
+ * await ctx.setInspectorMode({
110
+ * type: 'itemEditor',
111
+ * itemId: 'item-123',
112
+ * fieldPath: 'title'
113
+ * });
114
+ *
115
+ * // Switch to record list mode
116
+ * await ctx.setInspectorMode({ type: 'itemList' });
117
+ * await ctx.setInspectorItemListData({
118
+ * title: 'Related Records',
119
+ * itemIds: ['item-1', 'item-2', 'item-3']
120
+ * });
121
+ *
122
+ * // Switch to custom panel mode
123
+ * await ctx.setInspectorMode({
124
+ * type: 'customPanel',
125
+ * panelId: 'my-custom-panel',
126
+ * parameters: { filter: 'active' }
127
+ * });
128
+ *
129
+ * // Low intent mode change - won't interrupt editing with unsaved changes
130
+ * await ctx.setInspectorMode(
131
+ * { type: 'itemList' },
132
+ * { ignoreIfUnsavedChanges: true }
133
+ * );
134
+ * ```
135
+ */
136
+ setInspectorMode: (
137
+ mode: InspectorMode,
138
+ options?: SetInspectorModeOptions,
139
+ ) => Promise<void>;
140
+
141
+ /**
142
+ * Sets the data for the item list inspector mode.
143
+ *
144
+ * @example
145
+ *
146
+ * ```js
147
+ * // Set the item list data
148
+ * await ctx.setInspectorItemListData({
149
+ * title: 'Related Records',
150
+ * itemIds: ['item-1', 'item-2', 'item-3']
151
+ * });
152
+ *
153
+ * // Switch to item list mode
154
+ * await ctx.setInspectorMode({ type: 'itemList' });
155
+ * ```
156
+ */
157
+ setInspectorItemListData: (data: {
158
+ /** The title to show in the inspector header */
159
+ title: string;
160
+ /** Array of record IDs to display in the list */
161
+ itemIds: string[];
162
+ }) => Promise<void>;
163
+ }
164
+ >;
165
+
166
+ export const renderInspectorBootstrapper =
167
+ fullScreenRenderModeBootstrapper<RenderInspectorCtx>(
168
+ 'renderInspector',
169
+ (configuration, ctx) => {
170
+ if (!configuration.renderInspector) {
171
+ return;
172
+ }
173
+
174
+ configuration.renderInspector(ctx.inspectorId, ctx);
175
+ },
176
+ );
@@ -0,0 +1,38 @@
1
+ import { ImposedSizePluginFrameCtx } from '../ctx/pluginFrame';
2
+ import { fullScreenRenderModeBootstrapper } from '../utils';
3
+
4
+ export type RenderInspectorPanelHook = {
5
+ /**
6
+ * This function will be called when an inspector needs to render a specific
7
+ * panel (see the `renderInspector` and `setInspectorMode` functions)
8
+ *
9
+ * @tag inspector
10
+ */
11
+ renderInspectorPanel: (panelId: string, ctx: RenderInspectorPanelCtx) => void;
12
+ };
13
+
14
+ export type RenderInspectorPanelCtx = ImposedSizePluginFrameCtx<
15
+ 'renderInspectorPanel',
16
+ {
17
+ /** The ID of the inspector panel that needs to be rendered */
18
+ panelId: string;
19
+
20
+ /**
21
+ * The arbitrary `parameters` of the modal declared in the `setInspectorMode`
22
+ * function
23
+ */
24
+ parameters: Record<string, unknown>;
25
+ }
26
+ >;
27
+
28
+ export const renderInspectorPanelBootstrapper =
29
+ fullScreenRenderModeBootstrapper<RenderInspectorPanelCtx>(
30
+ 'renderInspectorPanel',
31
+ (configuration, ctx) => {
32
+ if (!configuration.renderInspectorPanel) {
33
+ return;
34
+ }
35
+
36
+ configuration.renderInspectorPanel(ctx.panelId, ctx);
37
+ },
38
+ );
@@ -17,6 +17,13 @@ export type RenderPageCtx = ImposedSizePluginFrameCtx<
17
17
  {
18
18
  /** The ID of the page that needs to be rendered */
19
19
  pageId: string;
20
+
21
+ /** Current page location */
22
+ location: {
23
+ pathname: string;
24
+ search: string;
25
+ hash: string;
26
+ };
20
27
  }
21
28
  >;
22
29
 
@@ -0,0 +1,22 @@
1
+ import type { SchemaTypes } from '@datocms/cma-client';
2
+ import { Ctx } from '../ctx/base';
3
+ import { DropdownAction, DropdownActionGroup } from '../shared';
4
+
5
+ type ItemType = SchemaTypes.ItemType;
6
+
7
+ export type SchemaItemTypeDropdownActionsHook = {
8
+ /**
9
+ * Use this function to define custom actions (or groups of actions) for a model/block model in the Schema section.
10
+ *
11
+ * The `executeSchemaItemTypeDropdownAction()` hook will be triggered once the user
12
+ * clicks on one of the defined actions.
13
+ *
14
+ * @tag dropdownActions
15
+ */
16
+ schemaItemTypeDropdownActions: (
17
+ itemType: ItemType,
18
+ ctx: SchemaItemTypeDropdownActionsCtx,
19
+ ) => Array<DropdownAction | DropdownActionGroup>;
20
+ };
21
+
22
+ export type SchemaItemTypeDropdownActionsCtx = Ctx;
package/src/icon.ts CHANGED
@@ -1,19 +1,90 @@
1
- import { isRecord, isString } from './guardUtils.js';
1
+ import { isEmoji, isRecord, isString } from './guardUtils.js';
2
2
 
3
- export type Icon =
4
- | AwesomeFontIconIdentifier
5
- | { type: 'svg'; viewBox: string; content: string };
3
+ export type Icon = AwesomeFontIconIdentifier | SvgDefinition | EmojiDefinition;
6
4
 
7
5
  export function isIcon(value: unknown): value is Icon {
6
+ return isString(value) || isSvgDefinition(value) || isEmojiDefinition(value);
7
+ }
8
+
9
+ /**
10
+ * Defines a custom SVG icon for use in DatoCMS plugins.
11
+ *
12
+ * To create an SVG definition from an existing SVG file:
13
+ * 1. Grab the `viewBox` attribute from your SVG element (e.g., "0 0 24 24")
14
+ * 2. Grab everything between the `<svg>` tags as the content (all the paths, circles, etc.)
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * // From this SVG:
19
+ * // <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
20
+ * // <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
21
+ * // </svg>
22
+ *
23
+ * const starIcon: SvgDefinition = {
24
+ * type: 'svg',
25
+ * viewBox: '0 0 24 24',
26
+ * content: '<path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>'
27
+ * };
28
+ * ```
29
+ */
30
+ export type SvgDefinition = {
31
+ /** Always set to 'svg' to indicate this is a custom SVG icon */
32
+ type: 'svg';
33
+
34
+ /**
35
+ * The viewBox attribute from your SVG element (e.g., "0 0 24 24").
36
+ * This defines the coordinate system and aspect ratio of the SVG.
37
+ */
38
+ viewBox: string;
39
+
40
+ /**
41
+ * The inner content of your SVG element — everything between the opening and closing <svg> tags.
42
+ * This includes all paths, circles, rectangles, and other SVG elements that make up your icon.
43
+ */
44
+ content: string;
45
+ };
46
+
47
+ export function isSvgDefinition(value: unknown): value is SvgDefinition {
8
48
  return (
9
- isString(value) ||
10
- (isRecord(value) &&
11
- value.type === 'svg' &&
12
- isString(value.viewBox) &&
13
- isString(value.content))
49
+ isRecord(value) &&
50
+ value.type === 'svg' &&
51
+ isString(value.viewBox) &&
52
+ isString(value.content)
14
53
  );
15
54
  }
16
55
 
56
+ /**
57
+ * Defines an emoji icon for use in DatoCMS plugins.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const starIcon: EmojiDefinition = {
62
+ * type: 'emoji',
63
+ * emoji: '⭐'
64
+ * };
65
+ * ```
66
+ */
67
+ export type EmojiDefinition = {
68
+ /** Always set to 'emoji' to indicate this is an emoji icon */
69
+ type: 'emoji';
70
+
71
+ /**
72
+ * A string contaning a single emoji.
73
+ */
74
+ emoji: string;
75
+ };
76
+
77
+ export function isEmojiDefinition(value: unknown): value is EmojiDefinition {
78
+ return isRecord(value) && value.type === 'emoji' && isEmoji(value.emoji);
79
+ }
80
+
81
+ /**
82
+ * Font Awesome icon identifier for use in DatoCMS plugins.
83
+ *
84
+ * Use Font Awesome icons for consistent visual styling across the admin interface.
85
+ * This is the recommended approach for most plugin icons, with custom SVGs reserved
86
+ * primarily for brand/company logos where Font Awesome doesn't have an appropriate match.
87
+ */
17
88
  export type AwesomeFontIconIdentifier =
18
89
  | '0'
19
90
  | '00'
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ export * from './hooks/customMarksForStructuredTextField';
25
25
  export * from './hooks/executeFieldDropdownAction';
26
26
  export * from './hooks/executeItemFormDropdownAction';
27
27
  export * from './hooks/executeItemsDropdownAction';
28
+ export * from './hooks/executeSchemaItemTypeDropdownAction';
28
29
  export * from './hooks/executeUploadsDropdownAction';
29
30
  export * from './hooks/fieldDropdownActions';
30
31
  export * from './hooks/initialLocationQueryForItemSelector';
@@ -52,8 +53,11 @@ export * from './hooks/renderItemFormSidebarPanel';
52
53
  export * from './hooks/renderManualFieldExtensionConfigScreen';
53
54
  export * from './hooks/renderModal';
54
55
  export * from './hooks/renderPage';
56
+ export * from './hooks/renderInspector';
57
+ export * from './hooks/renderInspectorPanel';
55
58
  export * from './hooks/renderUploadSidebar';
56
59
  export * from './hooks/renderUploadSidebarPanel';
60
+ export * from './hooks/schemaItemTypeDropdownActions';
57
61
  export * from './hooks/settingsAreaSidebarItemGroups';
58
62
  export * from './hooks/uploadsDropdownActions';
59
63
  export * from './hooks/uploadSidebarPanels';