@getdraft/plugin 1.13.0-beta.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/src/types.ts ADDED
@@ -0,0 +1,247 @@
1
+ export type EventHandler<T> = (data: T) => void;
2
+
3
+ export type AsyncEventHandler<P, R> = (params: P) => Promise<R>;
4
+
5
+ export interface SaveParams {
6
+ json: TemplateJSON;
7
+ html: string;
8
+ }
9
+
10
+ export type OnInitParams = MessageEvent<{
11
+ source: string;
12
+ event: 'loaded' | unknown;
13
+ }>;
14
+
15
+ export enum EditorMenu {
16
+ Audit = 'audit',
17
+ Guide = 'guide',
18
+ Style = 'style',
19
+ Blocks = 'blocks',
20
+ }
21
+
22
+ export enum ViewMode {
23
+ Desktop = 'desktop',
24
+ Mobile = 'mobile',
25
+ }
26
+
27
+ export interface ReadyParams extends SaveParams {
28
+ lastSavedTemplate?: {
29
+ template: TemplateJSON;
30
+ date: Date;
31
+ };
32
+ }
33
+
34
+ export interface ErrorParams {
35
+ message: string;
36
+ code?: string;
37
+ }
38
+
39
+ export interface ExitParams extends SaveParams {}
40
+
41
+ export interface NotificationParams {
42
+ intent: 'error' | 'info' | 'success' | 'warning';
43
+ errorDetails?: object;
44
+ message?: string;
45
+ title: string;
46
+ }
47
+
48
+ export interface OpenGalleryParams {
49
+ id: string;
50
+ }
51
+
52
+ export interface UploadImageParams {
53
+ file: string;
54
+ name: string;
55
+ }
56
+
57
+ export interface ImageParams {
58
+ url: string;
59
+ originalWidth: number;
60
+ originalHeight: number;
61
+ ratio: number;
62
+ fileSize: number;
63
+ }
64
+
65
+ export interface AutosaveParams {
66
+ json: TemplateJSON;
67
+ }
68
+
69
+ interface ToggleParams {
70
+ state: boolean;
71
+ }
72
+
73
+ export interface ToggleGridParams extends ToggleParams {}
74
+
75
+ export interface TogglePreviewParams extends ToggleParams {
76
+ json: TemplateJSON;
77
+ html: string;
78
+ darkHTML: string;
79
+ }
80
+
81
+ export interface ToggleViewModeParams {
82
+ mode: ViewMode;
83
+ }
84
+
85
+ export interface ToggleMenuParams {
86
+ menu: EditorMenu | null;
87
+ }
88
+
89
+ export interface HistoryParams {
90
+ hasUndos: boolean;
91
+ hasRedos: boolean;
92
+ currentStep: number;
93
+ }
94
+
95
+ export type TemplateJSON = Record<string, any>;
96
+
97
+ export type PersonalizationDictionary = Array<
98
+ | { name: string; value: string }
99
+ | { groupName: string; items: Array<{ name: string; value: string }> }
100
+ >;
101
+
102
+ export type ToolbarActionName =
103
+ | 'duplicate'
104
+ | 'destroy'
105
+ | 'edit'
106
+ | 'up'
107
+ | 'down';
108
+ export type HotkeyContext = 'hotkey';
109
+ export type OverlayContext = 'overlay-click' | 'overlay-double-click';
110
+ export type ToolbarContext = 'toolbar';
111
+ export type UnitSettingsContext = 'unit-settings';
112
+ export type BlockType = 'system' | 'user';
113
+ export type DndDragContext = 'canvas' | 'list' | 'palette';
114
+ export type DndDropContext = 'dropline' | 'placeholder' | 'empty-placeholder';
115
+ export type DndDropTarget = 'root' | 'section' | 'column' | 'element';
116
+
117
+ export interface DuplicateEvent {
118
+ name: 'duplicate';
119
+ context: HotkeyContext | ToolbarContext | UnitSettingsContext;
120
+ unitType: string;
121
+ }
122
+
123
+ export interface DestroyEvent {
124
+ name: 'destroy';
125
+ context: HotkeyContext | ToolbarContext | UnitSettingsContext;
126
+ unitType: string;
127
+ }
128
+
129
+ export interface EditEvent {
130
+ name: 'edit';
131
+ context: OverlayContext | ToolbarContext;
132
+ unitType: string;
133
+ }
134
+
135
+ export interface HistoryEvent {
136
+ name: 'history';
137
+ type: 'undo' | 'redo';
138
+ context: HotkeyContext;
139
+ }
140
+
141
+ export interface PreviewEvent {
142
+ name: 'preview';
143
+ context: HotkeyContext;
144
+ }
145
+
146
+ export interface ToolbarMoveEvent {
147
+ name: 'up' | 'down';
148
+ context: ToolbarContext;
149
+ unitType: string;
150
+ }
151
+
152
+ export interface BlockInsertEvent {
153
+ name: 'block-insert';
154
+ context: 'double-click';
155
+ blockType: BlockType;
156
+ blockName: string;
157
+ blockCategory: string | number;
158
+ blockId: string | number;
159
+ }
160
+
161
+ export interface BlockSaveEvent {
162
+ name: 'block-save';
163
+ blockType: 'user';
164
+ blockName: string;
165
+ blockCategory: string | number;
166
+ blockId: string | number;
167
+ elements: Record<string, number> & { sum: number };
168
+ }
169
+
170
+ export interface BlockDestroyEvent {
171
+ name: 'block-destroy';
172
+ }
173
+
174
+ export interface DropEventDragMeta {
175
+ context?: DndDragContext;
176
+ layer?: 'element' | 'section' | 'column' | 'root';
177
+ type?: string | string[];
178
+ blockCategory?: string | number;
179
+ blockId?: string | number;
180
+ blockName?: string;
181
+ blockType?: BlockType;
182
+ alt?: boolean;
183
+ dropKind?: 'create' | 'move';
184
+ }
185
+
186
+ export interface DropEventDropzoneMeta {
187
+ context?: DndDropContext;
188
+ dropTarget?: DndDropTarget;
189
+ }
190
+
191
+ export interface DropEvent {
192
+ name: 'drop';
193
+ drag?: DropEventDragMeta;
194
+ dropzone?: DropEventDropzoneMeta;
195
+ }
196
+
197
+ export type AnalyticsParams =
198
+ | DuplicateEvent
199
+ | DestroyEvent
200
+ | EditEvent
201
+ | HistoryEvent
202
+ | PreviewEvent
203
+ | ToolbarMoveEvent
204
+ | BlockInsertEvent
205
+ | BlockSaveEvent
206
+ | BlockDestroyEvent
207
+ | DropEvent;
208
+
209
+ export interface Handlers {
210
+ ready: EventHandler<ReadyParams>;
211
+ error: EventHandler<ErrorParams>;
212
+ save: EventHandler<SaveParams>;
213
+ exit: EventHandler<ExitParams>;
214
+ notification: EventHandler<NotificationParams>;
215
+ autosave: EventHandler<AutosaveParams>;
216
+ toggleGrid: EventHandler<ToggleGridParams>;
217
+ togglePreview: EventHandler<TogglePreviewParams>;
218
+ toggleViewMode: EventHandler<ToggleViewModeParams>;
219
+ toggleMenu: EventHandler<ToggleMenuParams>;
220
+ history: EventHandler<HistoryParams>;
221
+ analytics: EventHandler<AnalyticsParams>;
222
+ openGallery: EventHandler<(url?: string) => Promise<void>>;
223
+ uploadImage: AsyncEventHandler<UploadImageParams, string>;
224
+ loadPersonalization: AsyncEventHandler<void, PersonalizationDictionary>;
225
+ }
226
+
227
+ export type HandlerParams<H> = H extends
228
+ | EventHandler<infer P>
229
+ | AsyncEventHandler<infer P, unknown>
230
+ ? P
231
+ : never;
232
+
233
+ export interface PluginConfig {
234
+ container: string;
235
+ locale?: string;
236
+ autosave?:
237
+ | boolean
238
+ | {
239
+ interval: number;
240
+ };
241
+ token: string;
242
+ pluginId: string;
243
+ apiUrl: string;
244
+ disableHotkeysPassing?: boolean;
245
+ __config?: object;
246
+ on: Partial<Handlers>;
247
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { ImageParams } from './types';
2
+
3
+ const getImageFileSize = async (imageUrl: string) => {
4
+ try {
5
+ const response = await fetch(imageUrl, { method: 'HEAD' });
6
+
7
+ if (!response.ok) {
8
+ return null;
9
+ }
10
+
11
+ const contentLength = response.headers.get('Content-Length');
12
+
13
+ if (contentLength) {
14
+ return parseInt(contentLength, 10);
15
+ } else {
16
+ return null;
17
+ }
18
+ } catch (error) {
19
+ return null;
20
+ }
21
+ };
22
+
23
+ export const getImageParamsFromFileUrl = (imgSrc: string) =>
24
+ new Promise<Omit<ImageParams, 'url'>>((resolve, reject) => {
25
+ const img = new Image();
26
+
27
+ img.onload = async () => {
28
+ const fileSize = (await getImageFileSize(imgSrc)) ?? 0;
29
+
30
+ resolve({
31
+ originalWidth: img.width,
32
+ originalHeight: img.height,
33
+ ratio: img.width / img.height,
34
+ fileSize,
35
+ });
36
+ };
37
+
38
+ img.onerror = (error) => {
39
+ reject(error);
40
+ };
41
+
42
+ img.src = imgSrc;
43
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "compilerOptions": {
3
+ "experimentalDecorators": true,
4
+ "target": "esnext",
5
+ "lib": ["es6", "dom", "es2015", "es2017"],
6
+ "allowJs": true,
7
+ "skipLibCheck": true,
8
+ "esModuleInterop": true,
9
+ "declarationDir": "dist",
10
+ "declaration": true,
11
+ "declarationMap": true,
12
+ "allowSyntheticDefaultImports": true,
13
+ "strict": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "module": "esnext",
16
+ "baseUrl": ".",
17
+ "resolveJsonModule": true,
18
+ "incremental": true,
19
+ "noFallthroughCasesInSwitch": true,
20
+ "useDefineForClassFields": true,
21
+ "moduleResolution": "Bundler"
22
+ },
23
+ "exclude": ["node_modules"],
24
+ "include": ["src/**/*"]
25
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { defineConfig } from 'vite';
2
+ import path from 'path';
3
+ import dts from 'vite-plugin-dts';
4
+
5
+ export default defineConfig({
6
+ base: './',
7
+ plugins: [dts({ insertTypesEntry: true })],
8
+ build: {
9
+ sourcemap: true,
10
+ lib: {
11
+ entry: path.resolve(__dirname, 'src/index.ts'),
12
+ name: 'seplugin',
13
+ formats: ['es', 'cjs'],
14
+ fileName: (format) => `index.${format}.js`,
15
+ },
16
+ },
17
+ });