@jhits/plugin-newsletter 0.0.10 → 0.0.11

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 (57) hide show
  1. package/package.json +3 -2
  2. package/src/api/email-utils.ts +165 -0
  3. package/src/api/handler.ts +28 -0
  4. package/src/api/handlers/index.ts +44 -0
  5. package/src/api/handlers/newsletters.ts +332 -0
  6. package/src/api/handlers/send-newsletter.ts +288 -0
  7. package/src/api/handlers/settings.ts +403 -0
  8. package/src/api/handlers/subscribers.ts +152 -0
  9. package/src/api/handlers/upload.ts +47 -0
  10. package/src/api/handlers/welcome-email.ts +210 -0
  11. package/src/api/router.ts +166 -0
  12. package/src/index.server.ts +12 -0
  13. package/src/index.tsx +353 -0
  14. package/src/index.tsx.patch +98 -0
  15. package/src/init.tsx +72 -0
  16. package/src/lib/blocks/BlockRenderer.tsx +125 -0
  17. package/src/lib/email/EmailRenderer.tsx +420 -0
  18. package/src/lib/email/index.ts +6 -0
  19. package/src/lib/i18n.ts +82 -0
  20. package/src/lib/mappers/apiMapper.ts +57 -0
  21. package/src/lib/utils/blockHelpers.ts +71 -0
  22. package/src/lib/utils/slugify.ts +43 -0
  23. package/src/registry/BlockRegistry.ts +53 -0
  24. package/src/registry/index.ts +5 -0
  25. package/src/state/EditorContext.tsx +278 -0
  26. package/src/state/index.ts +10 -0
  27. package/src/state/reducer.ts +561 -0
  28. package/src/state/types.ts +154 -0
  29. package/src/types/block.ts +275 -0
  30. package/src/types/newsletter.ts +152 -0
  31. package/src/types/registry.ts +14 -0
  32. package/src/views/CanvasEditor/BlockWrapper.tsx +143 -0
  33. package/src/views/CanvasEditor/CanvasEditorView.tsx +343 -0
  34. package/src/views/CanvasEditor/EditorBody.tsx +95 -0
  35. package/src/views/CanvasEditor/EditorHeader.tsx +255 -0
  36. package/src/views/CanvasEditor/components/CustomBlockItem.tsx +83 -0
  37. package/src/views/CanvasEditor/components/EditorCanvas.tsx +674 -0
  38. package/src/views/CanvasEditor/components/EditorLibrary.tsx +120 -0
  39. package/src/views/CanvasEditor/components/EditorSidebar.tsx +139 -0
  40. package/src/views/CanvasEditor/components/ErrorBanner.tsx +31 -0
  41. package/src/views/CanvasEditor/components/LibraryItem.tsx +71 -0
  42. package/src/views/CanvasEditor/components/SlashCommandDetector.tsx +196 -0
  43. package/src/views/CanvasEditor/components/SlashCommandMenu.tsx +131 -0
  44. package/src/views/CanvasEditor/components/index.ts +16 -0
  45. package/src/views/CanvasEditor/hooks/index.ts +7 -0
  46. package/src/views/CanvasEditor/hooks/useKeyboardShortcuts.ts +136 -0
  47. package/src/views/CanvasEditor/hooks/useNewsletterLoader.ts +73 -0
  48. package/src/views/CanvasEditor/hooks/useRegisteredBlocks.ts +54 -0
  49. package/src/views/CanvasEditor/hooks/useSlashCommand.ts +106 -0
  50. package/src/views/CanvasEditor/index.ts +12 -0
  51. package/src/views/NewsletterEditor.tsx +42 -0
  52. package/src/views/NewsletterManager.tsx +483 -0
  53. package/src/views/SettingsView.tsx +216 -0
  54. package/src/views/SubscribersView.tsx +269 -0
  55. package/src/views/components/SendNewsletterModal.tsx +322 -0
  56. package/src/views/components/SmtpSettingsModal.tsx +433 -0
  57. package/src/views/components/TestEmailModal.tsx +268 -0
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Block Registry for Newsletter Plugin
3
+ * Dynamic registry for all block types in system
4
+ * Multi-Tenant Architecture: Blocks are provided by client applications
5
+ *
6
+ * The registry is a singleton that starts empty and is populated by client apps
7
+ */
8
+
9
+ import {
10
+ BlockTypeDefinition,
11
+ ClientBlockDefinition
12
+ } from '../types/block';
13
+
14
+ // Local interface to avoid import issues
15
+ interface IBlockRegistry {
16
+ register(definition: BlockTypeDefinition): void;
17
+ get(type: string): BlockTypeDefinition | undefined;
18
+ getAll(): BlockTypeDefinition[];
19
+ has(type: string): boolean;
20
+ clear(): void;
21
+ }
22
+
23
+ /**
24
+ * Block Registry Implementation
25
+ * Singleton that manages all block types in the system
26
+ */
27
+ class BlockRegistryImpl implements IBlockRegistry {
28
+ private blocks: Map<string, BlockTypeDefinition> = new Map();
29
+
30
+ register(definition: BlockTypeDefinition): void {
31
+ this.blocks.set(definition.type, definition);
32
+ }
33
+
34
+ get(type: string): BlockTypeDefinition | undefined {
35
+ return this.blocks.get(type);
36
+ }
37
+
38
+ getAll(): BlockTypeDefinition[] {
39
+ return Array.from(this.blocks.values());
40
+ }
41
+
42
+ has(type: string): boolean {
43
+ return this.blocks.has(type);
44
+ }
45
+
46
+ clear(): void {
47
+ this.blocks.clear();
48
+ }
49
+ }
50
+
51
+ // Export singleton instance
52
+ export const BlockRegistry = new BlockRegistryImpl();
53
+ export const blockRegistry = BlockRegistry;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Registry Exports
3
+ */
4
+
5
+ export { blockRegistry } from './BlockRegistry';
@@ -0,0 +1,278 @@
1
+ /**
2
+ * Newsletter Editor Context
3
+ * React Context for managing newsletter editor state
4
+ * Multi-Tenant: Accepts custom blocks from client applications
5
+ */
6
+
7
+ 'use client';
8
+
9
+ import React, { createContext, useContext, useReducer, useCallback, useMemo, useEffect, useRef, useState } from 'react';
10
+ import { editorReducer } from './reducer';
11
+ import { EditorState, EditorAction, initialEditorState, EditorContextValue } from './types';
12
+ import { Block } from '../types/block';
13
+ import { Newsletter } from '../types/newsletter';
14
+ import { ClientBlockDefinition } from '../types/block';
15
+ import { BlockRegistry } from '../registry/BlockRegistry';
16
+
17
+ // Create the context
18
+ const EditorContext = createContext<EditorContextValue | null>(null);
19
+
20
+ /**
21
+ * Editor Provider Props
22
+ */
23
+ export interface EditorProviderProps {
24
+ children: React.ReactNode;
25
+ /** Initial state (optional) */
26
+ initialState?: Partial<EditorState>;
27
+ /** Callback when save is triggered */
28
+ onSave?: (state: EditorState) => Promise<void>;
29
+ /**
30
+ * Custom blocks from client application
31
+ * These blocks will be registered in the BlockRegistry on mount
32
+ */
33
+ customBlocks?: ClientBlockDefinition[];
34
+ /** Enable dark mode for content area and wrappers (default: true) */
35
+ darkMode?: boolean;
36
+ /** Background colors for the editor */
37
+ backgroundColors?: {
38
+ /** Background color for light mode (REQUIRED) */
39
+ light: string;
40
+ /** Background color for dark mode (optional) */
41
+ dark?: string;
42
+ };
43
+ /** If true, this editor is for the welcome email */
44
+ isWelcomeEmail?: boolean;
45
+ }
46
+
47
+ /**
48
+ * Editor Provider
49
+ * Provides editor state and actions to child components
50
+ * Automatically registers client-provided blocks on mount
51
+ */
52
+ export function EditorProvider({
53
+ children,
54
+ initialState,
55
+ onSave,
56
+ customBlocks = [],
57
+ darkMode = true,
58
+ backgroundColors,
59
+ isWelcomeEmail
60
+ }: EditorProviderProps) {
61
+ // Register client blocks on mount
62
+ useEffect(() => {
63
+ if (customBlocks && customBlocks.length > 0) {
64
+ try {
65
+ customBlocks.forEach(block => BlockRegistry.register(block));
66
+ } catch (error) {
67
+ console.error('[NewsletterEditorContext] Failed to register custom blocks:', error);
68
+ }
69
+ }
70
+ }, [customBlocks]);
71
+
72
+ const [state, dispatch] = useReducer(
73
+ editorReducer,
74
+ { ...initialEditorState, ...initialState }
75
+ );
76
+
77
+ // Use a ref to always have access to the latest state in callbacks
78
+ const stateRef = useRef(state);
79
+ stateRef.current = state;
80
+
81
+ // History state for undo/redo
82
+ const [history, setHistory] = useState<EditorState[]>([]);
83
+ const [historyIndex, setHistoryIndex] = useState(-1);
84
+ const isRestoringRef = useRef(false);
85
+ const MAX_HISTORY = 50; // Limit history to prevent memory issues
86
+
87
+ // Save current state to history after state changes (but not during undo/redo)
88
+ // Debounce history updates to avoid excessive re-renders
89
+ const historyTimeoutRef = useRef<NodeJS.Timeout | null>(null);
90
+
91
+ useEffect(() => {
92
+ if (isRestoringRef.current) {
93
+ isRestoringRef.current = false;
94
+ return;
95
+ }
96
+
97
+ // Clear existing timeout
98
+ if (historyTimeoutRef.current) {
99
+ clearTimeout(historyTimeoutRef.current);
100
+ }
101
+
102
+ // Debounce history updates to reduce re-renders
103
+ historyTimeoutRef.current = setTimeout(() => {
104
+ // Save current state to history
105
+ setHistory(prev => {
106
+ const newHistory = [...prev];
107
+ // Remove any future history if we're not at the end
108
+ if (historyIndex < newHistory.length - 1) {
109
+ newHistory.splice(historyIndex + 1);
110
+ }
111
+ // Add current state
112
+ newHistory.push({ ...state });
113
+ // Limit history size
114
+ if (newHistory.length > MAX_HISTORY) {
115
+ newHistory.shift();
116
+ return newHistory;
117
+ }
118
+ return newHistory;
119
+ });
120
+ setHistoryIndex(prev => {
121
+ const newIndex = prev + 1;
122
+ return newIndex >= MAX_HISTORY ? MAX_HISTORY - 1 : newIndex;
123
+ });
124
+ }, 300); // Debounce by 300ms
125
+
126
+ return () => {
127
+ if (historyTimeoutRef.current) {
128
+ clearTimeout(historyTimeoutRef.current);
129
+ }
130
+ };
131
+ }, [state.blocks, state.title, state.slug, state.metadata, state.status, historyIndex]);
132
+
133
+ // Helper: Add a new block (supports nested containers)
134
+ const addBlock = useCallback((type: string, index?: number, containerId?: string) => {
135
+ const blockDefinition = BlockRegistry.get(type);
136
+ if (!blockDefinition) {
137
+ console.warn(`Block type "${type}" not found in registry. Available types:`,
138
+ BlockRegistry.getAll().map((b: any) => b.type).join(', '));
139
+ return;
140
+ }
141
+
142
+ const newBlock: Block = {
143
+ id: `block-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
144
+ type,
145
+ data: { ...blockDefinition.defaultData },
146
+ };
147
+
148
+ dispatch({ type: 'ADD_BLOCK', payload: { block: newBlock, index, containerId } });
149
+ }, []);
150
+
151
+ // Helper: Update a block
152
+ const updateBlock = useCallback((id: string, data: Partial<Block['data']>) => {
153
+ dispatch({ type: 'UPDATE_BLOCK', payload: { id, data } });
154
+ }, []);
155
+
156
+ // Helper: Delete a block
157
+ const deleteBlock = useCallback((id: string) => {
158
+ dispatch({ type: 'DELETE_BLOCK', payload: { id } });
159
+ }, []);
160
+
161
+ // Helper: Duplicate a block
162
+ const duplicateBlock = useCallback((id: string) => {
163
+ dispatch({ type: 'DUPLICATE_BLOCK', payload: { id } });
164
+ }, []);
165
+
166
+ // Helper: Move a block (supports nested containers)
167
+ const moveBlock = useCallback((id: string, newIndex: number, containerId?: string) => {
168
+ dispatch({ type: 'MOVE_BLOCK', payload: { id, newIndex, containerId } });
169
+ }, []);
170
+
171
+ // Helper: Load a newsletter
172
+ const loadNewsletter = useCallback((newsletter: Newsletter) => {
173
+ dispatch({ type: 'LOAD_NEWSLETTER', payload: newsletter });
174
+ }, []);
175
+
176
+ // Helper: Reset editor
177
+ const resetEditor = useCallback(() => {
178
+ dispatch({ type: 'RESET_EDITOR' });
179
+ }, []);
180
+
181
+ const save = useCallback(async () => {
182
+ if (onSave) {
183
+ await onSave(stateRef.current);
184
+ dispatch({ type: 'MARK_CLEAN' });
185
+ }
186
+ }, [onSave]);
187
+
188
+ // Helper: Undo
189
+ const undo = useCallback(() => {
190
+ if (historyIndex > 0 && history.length > 0) {
191
+ const previousState = history[historyIndex - 1];
192
+ if (previousState) {
193
+ isRestoringRef.current = true;
194
+ setHistoryIndex(prev => prev - 1);
195
+ dispatch({
196
+ type: 'LOAD_NEWSLETTER', payload: {
197
+ id: previousState.newsletterId || '',
198
+ title: previousState.title,
199
+ slug: previousState.slug,
200
+ blocks: previousState.blocks,
201
+ publication: {
202
+ status: previousState.status,
203
+ authorId: undefined,
204
+ },
205
+ metadata: previousState.metadata,
206
+ createdAt: new Date().toISOString(),
207
+ updatedAt: new Date().toISOString(),
208
+ }
209
+ });
210
+ }
211
+ }
212
+ }, [history, historyIndex, dispatch]);
213
+
214
+ // Helper: Redo
215
+ const redo = useCallback(() => {
216
+ if (historyIndex < history.length - 1) {
217
+ const nextState = history[historyIndex + 1];
218
+ if (nextState) {
219
+ isRestoringRef.current = true;
220
+ setHistoryIndex(prev => prev + 1);
221
+ dispatch({
222
+ type: 'LOAD_NEWSLETTER', payload: {
223
+ id: nextState.newsletterId || '',
224
+ title: nextState.title,
225
+ slug: nextState.slug,
226
+ blocks: nextState.blocks,
227
+ publication: {
228
+ status: nextState.status,
229
+ authorId: undefined,
230
+ },
231
+ metadata: nextState.metadata,
232
+ createdAt: new Date().toISOString(),
233
+ updatedAt: new Date().toISOString(),
234
+ }
235
+ });
236
+ }
237
+ }
238
+ }, [history, historyIndex, dispatch]);
239
+
240
+ // Memoize the context value
241
+ const value = useMemo<EditorContextValue>(
242
+ () => ({
243
+ state,
244
+ dispatch,
245
+ darkMode,
246
+ backgroundColors,
247
+ helpers: {
248
+ addBlock,
249
+ updateBlock,
250
+ deleteBlock,
251
+ duplicateBlock,
252
+ moveBlock,
253
+ loadNewsletter,
254
+ resetEditor,
255
+ save,
256
+ undo,
257
+ redo,
258
+ },
259
+ canUndo: historyIndex > 0 && history.length > 0,
260
+ canRedo: historyIndex < history.length - 1,
261
+ }),
262
+ [state, dispatch, darkMode, backgroundColors, addBlock, updateBlock, deleteBlock, duplicateBlock, moveBlock, loadNewsletter, resetEditor, save, undo, redo, historyIndex, history.length]
263
+ );
264
+
265
+ return <EditorContext.Provider value={value}>{children}</EditorContext.Provider>;
266
+ }
267
+
268
+ /**
269
+ * Hook to access editor context
270
+ * @throws Error if used outside EditorProvider
271
+ */
272
+ export function useEditor(): EditorContextValue {
273
+ const context = useContext(EditorContext);
274
+ if (!context) {
275
+ throw new Error('useEditor must be used within an EditorProvider');
276
+ }
277
+ return context;
278
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * State Exports
3
+ */
4
+
5
+ export { EditorProvider, useEditor } from './EditorContext';
6
+ export type { EditorProviderProps } from './EditorContext';
7
+ export type { EditorContextValue } from './types';
8
+ export { editorReducer } from './reducer';
9
+ export type { EditorState, EditorAction } from './types';
10
+ export { initialEditorState } from './types';