@jhits/plugin-blog 0.0.1

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 (75) hide show
  1. package/README.md +216 -0
  2. package/package.json +57 -0
  3. package/src/api/README.md +224 -0
  4. package/src/api/categories.ts +43 -0
  5. package/src/api/check-title.ts +60 -0
  6. package/src/api/handler.ts +419 -0
  7. package/src/api/index.ts +33 -0
  8. package/src/api/route.ts +116 -0
  9. package/src/api/router.ts +114 -0
  10. package/src/api-server.ts +11 -0
  11. package/src/config.ts +161 -0
  12. package/src/hooks/README.md +91 -0
  13. package/src/hooks/index.ts +8 -0
  14. package/src/hooks/useBlog.ts +85 -0
  15. package/src/hooks/useBlogs.ts +123 -0
  16. package/src/index.server.ts +12 -0
  17. package/src/index.tsx +354 -0
  18. package/src/init.tsx +72 -0
  19. package/src/lib/blocks/BlockRenderer.tsx +141 -0
  20. package/src/lib/blocks/index.ts +6 -0
  21. package/src/lib/index.ts +9 -0
  22. package/src/lib/layouts/blocks/ColumnsBlock.tsx +134 -0
  23. package/src/lib/layouts/blocks/SectionBlock.tsx +104 -0
  24. package/src/lib/layouts/blocks/index.ts +8 -0
  25. package/src/lib/layouts/index.ts +52 -0
  26. package/src/lib/layouts/registerLayoutBlocks.ts +59 -0
  27. package/src/lib/mappers/apiMapper.ts +223 -0
  28. package/src/lib/migration/index.ts +6 -0
  29. package/src/lib/migration/mapper.ts +140 -0
  30. package/src/lib/rich-text/RichTextEditor.tsx +826 -0
  31. package/src/lib/rich-text/RichTextPreview.tsx +210 -0
  32. package/src/lib/rich-text/index.ts +10 -0
  33. package/src/lib/utils/blockHelpers.ts +72 -0
  34. package/src/lib/utils/configValidation.ts +137 -0
  35. package/src/lib/utils/index.ts +8 -0
  36. package/src/lib/utils/slugify.ts +79 -0
  37. package/src/registry/BlockRegistry.ts +142 -0
  38. package/src/registry/index.ts +11 -0
  39. package/src/state/EditorContext.tsx +277 -0
  40. package/src/state/index.ts +8 -0
  41. package/src/state/reducer.ts +694 -0
  42. package/src/state/types.ts +160 -0
  43. package/src/types/block.ts +269 -0
  44. package/src/types/index.ts +15 -0
  45. package/src/types/post.ts +165 -0
  46. package/src/utils/README.md +75 -0
  47. package/src/utils/client.ts +122 -0
  48. package/src/utils/index.ts +9 -0
  49. package/src/views/CanvasEditor/BlockWrapper.tsx +459 -0
  50. package/src/views/CanvasEditor/CanvasEditorView.tsx +917 -0
  51. package/src/views/CanvasEditor/EditorBody.tsx +475 -0
  52. package/src/views/CanvasEditor/EditorHeader.tsx +179 -0
  53. package/src/views/CanvasEditor/LayoutContainer.tsx +494 -0
  54. package/src/views/CanvasEditor/SaveConfirmationModal.tsx +233 -0
  55. package/src/views/CanvasEditor/components/CustomBlockItem.tsx +92 -0
  56. package/src/views/CanvasEditor/components/FeaturedMediaSection.tsx +130 -0
  57. package/src/views/CanvasEditor/components/LibraryItem.tsx +80 -0
  58. package/src/views/CanvasEditor/components/PrivacySettingsSection.tsx +212 -0
  59. package/src/views/CanvasEditor/components/index.ts +17 -0
  60. package/src/views/CanvasEditor/index.ts +16 -0
  61. package/src/views/PostManager/EmptyState.tsx +42 -0
  62. package/src/views/PostManager/PostActionsMenu.tsx +112 -0
  63. package/src/views/PostManager/PostCards.tsx +192 -0
  64. package/src/views/PostManager/PostFilters.tsx +80 -0
  65. package/src/views/PostManager/PostManagerView.tsx +280 -0
  66. package/src/views/PostManager/PostStats.tsx +81 -0
  67. package/src/views/PostManager/PostTable.tsx +225 -0
  68. package/src/views/PostManager/index.ts +15 -0
  69. package/src/views/Preview/PreviewBridgeView.tsx +64 -0
  70. package/src/views/Preview/index.ts +7 -0
  71. package/src/views/README.md +82 -0
  72. package/src/views/Settings/SettingsView.tsx +298 -0
  73. package/src/views/Settings/index.ts +7 -0
  74. package/src/views/SlugSEO/SlugSEOManagerView.tsx +94 -0
  75. package/src/views/SlugSEO/index.ts +7 -0
@@ -0,0 +1,917 @@
1
+ 'use client';
2
+
3
+ import React, { useState, useEffect, useRef, useMemo } from 'react';
4
+ import {
5
+ Globe,
6
+ Image as ImageIcon, Library,
7
+ LayoutTemplate, Type,
8
+ Box,
9
+ Search,
10
+ AlertTriangle, X
11
+ } from 'lucide-react';
12
+ import { useEditor } from '../../state/EditorContext';
13
+ import { EditorBody } from './EditorBody';
14
+ import { BlockWrapper } from './BlockWrapper';
15
+ import { EditorHeader } from './EditorHeader';
16
+ import { blockRegistry } from '../../registry/BlockRegistry';
17
+ import { BlockRenderer } from '../../lib/blocks/BlockRenderer';
18
+ import { Block } from '../../types/block';
19
+ import { apiToBlogPost, type APIBlogDocument } from '../../lib/mappers/apiMapper';
20
+ import {
21
+ LibraryItem,
22
+ CustomBlockItem,
23
+ FeaturedMediaSection,
24
+ PrivacySettingsSection
25
+ } from './components';
26
+
27
+ // Generate a unique block ID
28
+ function generateBlockId(): string {
29
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
30
+ return crypto.randomUUID();
31
+ }
32
+ return `block-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
33
+ }
34
+
35
+
36
+ export interface CanvasEditorViewProps {
37
+ postId?: string;
38
+ siteId: string;
39
+ locale: string;
40
+ /** Enable dark mode for content area and wrappers (default: true) */
41
+ darkMode?: boolean;
42
+ /** Background colors for the editor */
43
+ backgroundColors?: {
44
+ light: string;
45
+ dark?: string;
46
+ };
47
+ }
48
+
49
+ export function CanvasEditorView({ postId, darkMode, backgroundColors: propsBackgroundColors, siteId, locale }: CanvasEditorViewProps) {
50
+ const { state, helpers, dispatch, darkMode: contextDarkMode, backgroundColors: contextBackgroundColors, canUndo, canRedo } = useEditor();
51
+ const effectiveDarkMode = darkMode !== undefined ? darkMode : contextDarkMode;
52
+ const effectiveBackgroundColors = propsBackgroundColors || contextBackgroundColors;
53
+ const [isSidebarOpen, setSidebarOpen] = useState(true);
54
+ const [isLibraryOpen, setLibraryOpen] = useState(true);
55
+ const [isPreviewMode, setIsPreviewMode] = useState(false);
56
+ const [isLoadingPost, setIsLoadingPost] = useState(false);
57
+ const [isSaving, setIsSaving] = useState(false);
58
+ const [saveError, setSaveError] = useState<string | null>(null);
59
+ const titleRef = useRef<HTMLTextAreaElement>(null);
60
+
61
+ // Load post when postId (slug) is provided
62
+ useEffect(() => {
63
+ if (postId && !state.postId) {
64
+ const loadPost = async () => {
65
+ try {
66
+ setIsLoadingPost(true);
67
+ const response = await fetch(`/api/plugin-blog/${postId}`);
68
+ if (!response.ok) {
69
+ throw new Error('Failed to load post');
70
+ }
71
+ const apiDoc: APIBlogDocument = await response.json();
72
+ console.log('[CanvasEditorView] Loaded API document:', {
73
+ title: apiDoc.title,
74
+ slug: apiDoc.slug,
75
+ contentBlocksCount: apiDoc.contentBlocks?.length || 0,
76
+ contentBlocks: apiDoc.contentBlocks?.map((b: any) => ({ id: b.id, type: b.type })) || [],
77
+ hasContent: !!apiDoc.contentBlocks,
78
+ hasLegacyContent: !!apiDoc.content,
79
+ });
80
+ const blogPost = apiToBlogPost(apiDoc);
81
+ console.log('[CanvasEditorView] Converted to BlogPost:', {
82
+ id: blogPost.id,
83
+ title: blogPost.title,
84
+ blocksCount: blogPost.blocks.length,
85
+ blocks: blogPost.blocks.map(b => ({ id: b.id, type: b.type })),
86
+ });
87
+ helpers.loadPost(blogPost);
88
+ } catch (error) {
89
+ console.error('Failed to load post:', error);
90
+ alert('Failed to load post. Please try again.');
91
+ } finally {
92
+ setIsLoadingPost(false);
93
+ }
94
+ };
95
+ loadPost();
96
+ }
97
+ }, [postId, state.postId, helpers]);
98
+
99
+ // Reactive state for registered blocks (updates when registry changes)
100
+ const [registeredBlocks, setRegisteredBlocks] = useState(() => {
101
+ const initial = blockRegistry.getAll();
102
+ console.log('[CanvasEditorView] Initial blocks:', initial.length, initial.map(b => b.type));
103
+ return initial;
104
+ });
105
+
106
+ // Watch for registry changes and update state
107
+ useEffect(() => {
108
+ // Check immediately
109
+ const checkBlocks = () => {
110
+ const currentBlocks = blockRegistry.getAll();
111
+ const hasChanged = currentBlocks.length !== registeredBlocks.length ||
112
+ currentBlocks.some((b, i) => b.type !== registeredBlocks[i]?.type) ||
113
+ registeredBlocks.some((b, i) => b.type !== currentBlocks[i]?.type);
114
+
115
+ if (hasChanged) {
116
+ console.log('[CanvasEditorView] Blocks updated:', currentBlocks.length, currentBlocks.map(b => `${b.type}(${b.category})`));
117
+ setRegisteredBlocks([...currentBlocks]);
118
+ }
119
+ };
120
+
121
+ // Initial check
122
+ checkBlocks();
123
+
124
+ // Poll for registry changes (blocks are registered asynchronously in useEffect)
125
+ // Use a shorter interval initially, then longer
126
+ let pollCount = 0;
127
+ const interval = setInterval(() => {
128
+ pollCount++;
129
+ checkBlocks();
130
+ // Stop polling after 5 seconds (25 checks at 200ms)
131
+ if (pollCount > 25) {
132
+ clearInterval(interval);
133
+ }
134
+ }, 200);
135
+
136
+ // Also check after delays to catch initial registrations
137
+ const timeouts = [
138
+ setTimeout(checkBlocks, 50),
139
+ setTimeout(checkBlocks, 100),
140
+ setTimeout(checkBlocks, 300),
141
+ setTimeout(checkBlocks, 500),
142
+ setTimeout(checkBlocks, 1000),
143
+ ];
144
+
145
+ return () => {
146
+ clearInterval(interval);
147
+ timeouts.forEach(clearTimeout);
148
+ };
149
+ }, [registeredBlocks.length]);
150
+
151
+
152
+ // Get Hero block definition from registered blocks (REQUIRED)
153
+ const heroBlockDefinition = useMemo(() => {
154
+ return blockRegistry.get('hero');
155
+ }, [registeredBlocks]);
156
+
157
+ // Hero block state - separate from content blocks (statically positioned at top)
158
+ const [heroBlock, setHeroBlock] = useState<Block | null>(null);
159
+
160
+ // Initialize hero block if Hero block definition exists
161
+ useEffect(() => {
162
+ if (heroBlockDefinition) {
163
+ // Get default data from block definition
164
+ const heroData = heroBlockDefinition.defaultData || {};
165
+
166
+ // Initialize hero block only if it doesn't exist yet
167
+ setHeroBlock(prev => {
168
+ if (!prev) {
169
+ // Initialize hero block from editor state (if editing existing post) or use defaults
170
+ const initialData = {
171
+ ...heroData,
172
+ title: state.title || heroData.title || '',
173
+ summary: state.metadata.excerpt || heroData.summary || '',
174
+ image: state.metadata.featuredImage ? {
175
+ src: state.metadata.featuredImage.src,
176
+ alt: state.metadata.featuredImage.alt,
177
+ brightness: state.metadata.featuredImage.brightness,
178
+ blur: state.metadata.featuredImage.blur,
179
+ } : heroData.image,
180
+ };
181
+ return {
182
+ id: generateBlockId(),
183
+ type: 'hero',
184
+ data: initialData,
185
+ };
186
+ }
187
+ // Keep existing hero block data - let the Edit component manage it
188
+ return prev;
189
+ });
190
+ } else {
191
+ setHeroBlock(null);
192
+ }
193
+ }, [heroBlockDefinition]);
194
+
195
+ // Sync hero block with editor state when post is loaded (for existing posts)
196
+ useEffect(() => {
197
+ if (heroBlock && heroBlockDefinition && state.postId) {
198
+ // Only update if the hero block data doesn't match the editor state
199
+ // This prevents overwriting user edits with stale data
200
+ const currentTitle = (heroBlock.data as any)?.title || '';
201
+ const currentSummary = (heroBlock.data as any)?.summary || '';
202
+ const currentImage = (heroBlock.data as any)?.image;
203
+ const currentCategory = (heroBlock.data as any)?.category || '';
204
+
205
+ const stateTitle = state.title || '';
206
+ const stateSummary = state.metadata.excerpt || '';
207
+ const stateImage = state.metadata.featuredImage;
208
+ const stateCategory = state.metadata.categories?.[0] || '';
209
+
210
+ // Check if hero block is out of sync with editor state
211
+ const titleMismatch = currentTitle !== stateTitle;
212
+ const summaryMismatch = currentSummary !== stateSummary;
213
+ const imageMismatch = !currentImage && stateImage ||
214
+ currentImage?.src !== stateImage?.src ||
215
+ currentImage?.alt !== stateImage?.alt ||
216
+ currentImage?.brightness !== stateImage?.brightness ||
217
+ currentImage?.blur !== stateImage?.blur;
218
+ const categoryMismatch = currentCategory !== stateCategory;
219
+
220
+ // Only update if there's a mismatch and the editor state has data (post was loaded)
221
+ if ((titleMismatch || summaryMismatch || imageMismatch || categoryMismatch) && (stateTitle || stateSummary || stateImage || stateCategory)) {
222
+ setHeroBlock({
223
+ ...heroBlock,
224
+ data: {
225
+ ...heroBlock.data,
226
+ title: stateTitle || (heroBlock.data as any)?.title || '',
227
+ summary: stateSummary || (heroBlock.data as any)?.summary || '',
228
+ image: stateImage ? {
229
+ src: stateImage.src,
230
+ alt: stateImage.alt,
231
+ brightness: stateImage.brightness,
232
+ blur: stateImage.blur,
233
+ } : (heroBlock.data as any)?.image,
234
+ category: stateCategory || (heroBlock.data as any)?.category || '',
235
+ },
236
+ });
237
+ }
238
+ }
239
+ }, [state.postId, state.title, state.metadata.excerpt, state.metadata.featuredImage, state.metadata.categories, heroBlockDefinition]);
240
+
241
+ // Listen for hero title updates from HeroBlock (if it dispatches events)
242
+ useEffect(() => {
243
+ const handleHeroTitleUpdate = (e: CustomEvent) => {
244
+ dispatch({ type: 'SET_TITLE', payload: e.detail });
245
+ };
246
+ window.addEventListener('hero-title-update', handleHeroTitleUpdate as EventListener);
247
+ return () => window.removeEventListener('hero-title-update', handleHeroTitleUpdate as EventListener);
248
+ }, [dispatch]);
249
+
250
+ // Remove any hero blocks from the content blocks array
251
+ useEffect(() => {
252
+ const heroBlocksInContent = state.blocks.filter(b => b.type === 'hero');
253
+ if (heroBlocksInContent.length > 0) {
254
+ heroBlocksInContent.forEach(block => {
255
+ dispatch({ type: 'DELETE_BLOCK', payload: { id: block.id } });
256
+ });
257
+ }
258
+ }, [state.blocks, dispatch]);
259
+
260
+ // Filter out hero blocks from content blocks
261
+ const contentBlocks = state.blocks.filter(b => b.type !== 'hero');
262
+
263
+ // Get all registered blocks from state (excluding Hero block from sidebar)
264
+ const allBlocks = registeredBlocks.filter(block => block.type !== 'hero');
265
+ const textBlocks = allBlocks.filter(block => block.category === 'text');
266
+ const customBlocks = allBlocks.filter(block => block.category === 'custom');
267
+ const mediaBlocks = allBlocks.filter(block => block.category === 'media');
268
+ const layoutBlocks = allBlocks.filter(block => block.category === 'layout');
269
+
270
+ // Handler to add block at the bottom when clicking (not dragging)
271
+ const handleAddBlockAtBottom = (blockType: string) => {
272
+ // Add at the end of content blocks (excluding hero)
273
+ helpers.addBlock(blockType, contentBlocks.length, undefined);
274
+ };
275
+
276
+ // Handle Title Auto-resize
277
+ useEffect(() => {
278
+ if (titleRef.current) {
279
+ titleRef.current.style.height = 'auto';
280
+ titleRef.current.style.height = `${titleRef.current.scrollHeight}px`;
281
+ }
282
+ }, [state.title]);
283
+
284
+ // Keyboard shortcuts: Ctrl+V to paste, Ctrl+Z to undo, Ctrl+Shift+Z/Ctrl+Y to redo
285
+ useEffect(() => {
286
+ const handleKeyDown = (e: KeyboardEvent) => {
287
+ // Don't handle shortcuts if user is typing in an input/textarea/contentEditable
288
+ const target = e.target as HTMLElement;
289
+ const isEditableElement = target.tagName === 'INPUT' ||
290
+ target.tagName === 'TEXTAREA' ||
291
+ target.isContentEditable ||
292
+ target.closest('input, textarea, [contenteditable="true"]');
293
+
294
+ // Check for Ctrl+Z / Cmd+Z (Undo)
295
+ if ((e.ctrlKey || e.metaKey) && e.key === 'z' && !e.shiftKey) {
296
+ if (!isEditableElement) {
297
+ e.preventDefault();
298
+ e.stopPropagation();
299
+ if (canUndo) {
300
+ helpers.undo();
301
+ }
302
+ return;
303
+ }
304
+ }
305
+
306
+ // Check for Ctrl+Shift+Z / Cmd+Shift+Z or Ctrl+Y / Cmd+Y (Redo)
307
+ if (((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'z') || ((e.ctrlKey || e.metaKey) && e.key === 'y')) {
308
+ if (!isEditableElement) {
309
+ e.preventDefault();
310
+ e.stopPropagation();
311
+ if (canRedo) {
312
+ helpers.redo();
313
+ }
314
+ return;
315
+ }
316
+ }
317
+
318
+ // Check for Ctrl+V (Windows/Linux) or Cmd+V (Mac)
319
+ if ((e.ctrlKey || e.metaKey) && e.key === 'v') {
320
+ // Don't paste if user is typing in an input/textarea/contentEditable
321
+ const target = e.target as HTMLElement;
322
+ const isEditableElement = target.tagName === 'INPUT' ||
323
+ target.tagName === 'TEXTAREA' ||
324
+ target.isContentEditable ||
325
+ target.closest('input, textarea, [contenteditable="true"]');
326
+
327
+ if (isEditableElement) {
328
+ return; // Let the browser handle paste in editable elements
329
+ }
330
+
331
+ // Check if there's a copied block
332
+ if (typeof window !== 'undefined') {
333
+ const copiedBlockJson = localStorage.getItem('__BLOG_EDITOR_COPIED_BLOCK__');
334
+ if (copiedBlockJson) {
335
+ try {
336
+ e.preventDefault();
337
+ e.stopPropagation();
338
+
339
+ const copiedBlock = JSON.parse(copiedBlockJson) as Block;
340
+
341
+ // Generate a unique block ID
342
+ const generateBlockId = (): string => {
343
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
344
+ return crypto.randomUUID();
345
+ }
346
+ return `block-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
347
+ };
348
+
349
+ // Clone a block with new IDs (recursive for nested blocks)
350
+ const cloneBlock = (blockToClone: Block): Block => {
351
+ const cloned: Block = {
352
+ ...blockToClone,
353
+ id: generateBlockId(),
354
+ data: { ...blockToClone.data },
355
+ meta: blockToClone.meta ? { ...blockToClone.meta } : undefined,
356
+ };
357
+
358
+ // Handle children if they exist
359
+ if (blockToClone.children) {
360
+ if (Array.isArray(blockToClone.children) && blockToClone.children.length > 0) {
361
+ if (typeof blockToClone.children[0] === 'object') {
362
+ cloned.children = (blockToClone.children as Block[]).map(cloneBlock);
363
+ } else {
364
+ // If children are IDs, find and clone the actual blocks
365
+ const allBlocks = state.blocks;
366
+ const childIds = blockToClone.children as string[];
367
+ const childBlocks = childIds
368
+ .map((childId: string) => allBlocks.find(b => b.id === childId))
369
+ .filter((b): b is Block => b !== undefined);
370
+ cloned.children = childBlocks.map(cloneBlock);
371
+ }
372
+ }
373
+ }
374
+
375
+ return cloned;
376
+ };
377
+
378
+ const pastedBlock = cloneBlock(copiedBlock);
379
+
380
+ // Find where to paste - use hovered block or selected block, or paste at end
381
+ const hoveredBlockId = (window as any).__BLOG_EDITOR_HOVERED_BLOCK_ID__;
382
+ const targetBlockId = hoveredBlockId || state.selectedBlockId;
383
+
384
+ let pasteIndex: number | undefined;
385
+ if (targetBlockId) {
386
+ const targetIndex = state.blocks.findIndex(b => b.id === targetBlockId);
387
+ if (targetIndex !== -1) {
388
+ pasteIndex = targetIndex + 1;
389
+ }
390
+ }
391
+
392
+ // Dispatch ADD_BLOCK with the full block structure
393
+ dispatch({
394
+ type: 'ADD_BLOCK',
395
+ payload: {
396
+ block: pastedBlock,
397
+ index: pasteIndex,
398
+ containerId: undefined
399
+ }
400
+ });
401
+ } catch (error) {
402
+ console.error('Failed to paste block:', error);
403
+ }
404
+ }
405
+ }
406
+ }
407
+ };
408
+
409
+ window.addEventListener('keydown', handleKeyDown);
410
+ return () => {
411
+ window.removeEventListener('keydown', handleKeyDown);
412
+ };
413
+ }, [state.blocks, state.selectedBlockId, dispatch]);
414
+
415
+ return (
416
+ <div className="h-full rounded-[2.5rem] w-full bg-dashboard-card text-dashboard-text flex flex-col font-sans transition-colors duration-300 overflow-hidden relative">
417
+
418
+ <main className="flex flex-1 flex-col relative min-h-0">
419
+ {/* Error Banner */}
420
+ {saveError && (
421
+ <div className="bg-red-50 dark:bg-red-900/20 border-b border-red-200 dark:border-red-800 px-6 py-3 flex items-center justify-between">
422
+ <div className="flex items-center gap-3 flex-1">
423
+ <AlertTriangle className="text-red-600 dark:text-red-400 flex-shrink-0" size={20} />
424
+ <p className="text-red-800 dark:text-red-300 text-sm font-medium">
425
+ {saveError}
426
+ </p>
427
+ </div>
428
+ <button
429
+ onClick={() => setSaveError(null)}
430
+ className="text-red-600 dark:text-red-400 hover:text-red-800 dark:hover:text-red-200 transition-colors"
431
+ aria-label="Dismiss error"
432
+ >
433
+ <X size={18} />
434
+ </button>
435
+ </div>
436
+ )}
437
+ <EditorHeader
438
+ isLibraryOpen={isLibraryOpen}
439
+ onLibraryToggle={() => setLibraryOpen(!isLibraryOpen)}
440
+ isPreviewMode={isPreviewMode}
441
+ onPreviewToggle={() => setIsPreviewMode(!isPreviewMode)}
442
+ isSidebarOpen={isSidebarOpen}
443
+ onSidebarToggle={() => setSidebarOpen(!isSidebarOpen)}
444
+ isSaving={isSaving}
445
+ onSave={async (publish?: boolean) => {
446
+ setIsSaving(true);
447
+ setSaveError(null);
448
+ try {
449
+ // Status should already be set in EditorHeader, but verify and log
450
+ console.log('[CanvasEditorView] onSave called with publish:', publish, 'current status:', state.status);
451
+
452
+ // Double-check status is set correctly before saving
453
+ if (publish === true && state.status !== 'published') {
454
+ console.warn('[CanvasEditorView] Status mismatch! Setting to published...');
455
+ dispatch({ type: 'SET_STATUS', payload: 'published' });
456
+ await new Promise(resolve => setTimeout(resolve, 100));
457
+ } else if (publish === false && state.status !== 'draft') {
458
+ console.warn('[CanvasEditorView] Status mismatch! Setting to draft...');
459
+ dispatch({ type: 'SET_STATUS', payload: 'draft' });
460
+ await new Promise(resolve => setTimeout(resolve, 100));
461
+ }
462
+
463
+ console.log('[CanvasEditorView] Final status before save:', state.status);
464
+ await helpers.save();
465
+ setIsSaving(false);
466
+ } catch (error: any) {
467
+ console.error('[CanvasEditorView] Save error:', error);
468
+ // Extract and format user-friendly error message
469
+ let errorMessage = error.message || 'Failed to save post';
470
+
471
+ // Make error messages more user-friendly
472
+ if (errorMessage.includes('Missing required fields')) {
473
+ errorMessage = errorMessage.replace('Missing required fields for publishing:', 'To publish, please fill in:');
474
+ } else if (errorMessage.includes('All required fields')) {
475
+ errorMessage = 'To publish, please fill in all required fields: summary, featured image, category, and content.';
476
+ } else if (errorMessage.includes('Unauthorized')) {
477
+ errorMessage = 'You are not authorized to save this post. Please log in again.';
478
+ } else if (errorMessage.includes('Failed to save')) {
479
+ errorMessage = 'Unable to save the post. Please check your connection and try again.';
480
+ }
481
+
482
+ setSaveError(errorMessage);
483
+ setIsSaving(false); // Always reset saving state on error
484
+ throw error; // Re-throw so EditorHeader can handle it
485
+ }
486
+ }}
487
+ onSaveError={(error) => {
488
+ // Format error message for display
489
+ if (error) {
490
+ let formattedError = error;
491
+ if (formattedError.includes('Missing required fields')) {
492
+ formattedError = formattedError.replace('Missing required fields for publishing:', 'To publish, please fill in:');
493
+ }
494
+ setSaveError(formattedError);
495
+ } else {
496
+ setSaveError(null);
497
+ }
498
+ }}
499
+ />
500
+
501
+ {/* Editor Content Wrapper */}
502
+ <div className="flex flex-1 relative overflow-hidden min-h-0 flex-nowrap">
503
+
504
+ {/* LEFT SIDEBAR: COMPONENT LIBRARY */}
505
+ {!isPreviewMode && (
506
+ <aside
507
+ className={`transition-all duration-500 ease-[cubic-bezier(0.4,0,0.2,1)] border-r border-dashboard-border bg-dashboard-sidebar overflow-y-auto overflow-x-hidden h-full ${isLibraryOpen ? 'w-72' : 'w-0 opacity-0 pointer-events-none'
508
+ }`}
509
+ >
510
+ <div className="p-6 w-72 min-w-0 max-w-full">
511
+ {/* Text Blocks */}
512
+ {textBlocks.length > 0 && (
513
+ <div className="mb-10">
514
+ <h3 className="text-[10px] uppercase tracking-[0.2em] text-neutral-500 dark:text-neutral-400 font-black mb-6">Text</h3>
515
+ <div className="grid grid-cols-2 gap-3">
516
+ {textBlocks.map((block) => {
517
+ const IconComponent = block.icon || block.components.Icon || Type;
518
+ return (
519
+ <LibraryItem
520
+ key={block.type}
521
+ icon={<IconComponent size={16} />}
522
+ label={block.name}
523
+ blockType={block.type}
524
+ description={block.description}
525
+ onAddBlock={handleAddBlockAtBottom}
526
+ />
527
+ );
528
+ })}
529
+ </div>
530
+ </div>
531
+ )}
532
+
533
+ {/* Media Blocks */}
534
+ {mediaBlocks.length > 0 && (
535
+ <div className="mb-10">
536
+ <h3 className="text-[10px] uppercase tracking-[0.2em] text-neutral-500 dark:text-neutral-400 font-black mb-6">Media</h3>
537
+ <div className="grid grid-cols-2 gap-3">
538
+ {mediaBlocks.map((block) => {
539
+ const IconComponent = block.icon || block.components.Icon || ImageIcon;
540
+ return (
541
+ <LibraryItem
542
+ key={block.type}
543
+ icon={<IconComponent size={16} />}
544
+ label={block.name}
545
+ blockType={block.type}
546
+ description={block.description}
547
+ onAddBlock={handleAddBlockAtBottom}
548
+ />
549
+ );
550
+ })}
551
+ </div>
552
+ </div>
553
+ )}
554
+
555
+ {/* Layout Blocks */}
556
+ {layoutBlocks.length > 0 && (
557
+ <div className="mb-10">
558
+ <h3 className="text-[10px] uppercase tracking-[0.2em] text-neutral-500 dark:text-neutral-400 font-black mb-6">Layout</h3>
559
+ <div className="grid grid-cols-2 gap-3">
560
+ {layoutBlocks.map((block) => {
561
+ const IconComponent = block.icon || block.components.Icon || LayoutTemplate;
562
+ return (
563
+ <LibraryItem
564
+ key={block.type}
565
+ icon={<IconComponent size={16} />}
566
+ label={block.name}
567
+ blockType={block.type}
568
+ description={block.description}
569
+ onAddBlock={handleAddBlockAtBottom}
570
+ />
571
+ );
572
+ })}
573
+ </div>
574
+ </div>
575
+ )}
576
+
577
+ {/* Custom Blocks */}
578
+ {customBlocks.length > 0 && (
579
+ <div>
580
+ <h3 className="text-[10px] uppercase tracking-[0.2em] text-neutral-500 dark:text-neutral-400 font-black mb-6">Custom Blocks</h3>
581
+ <div className="space-y-3">
582
+ {customBlocks.map((block) => {
583
+ const IconComponent = block.icon || block.components.Icon || Box;
584
+ return (
585
+ <CustomBlockItem
586
+ key={block.type}
587
+ blockType={block.type}
588
+ name={block.name}
589
+ description={block.description}
590
+ icon={<IconComponent size={14} />}
591
+ onAddBlock={handleAddBlockAtBottom}
592
+ />
593
+ );
594
+ })}
595
+ </div>
596
+ </div>
597
+ )}
598
+
599
+ {/* Empty State */}
600
+ {allBlocks.length === 0 && (
601
+ <div className="text-center py-12">
602
+ <Library size={32} className="mx-auto text-neutral-300 dark:text-neutral-700 mb-4" />
603
+ <p className="text-xs text-neutral-500 dark:text-neutral-400">
604
+ No blocks registered yet.
605
+ </p>
606
+ </div>
607
+ )}
608
+ </div>
609
+ </aside>
610
+ )}
611
+
612
+ {/* CENTER: THE WRITING CANVAS */}
613
+ <div
614
+ className="flex-1 overflow-y-auto overflow-x-hidden pb-40 px-6 custom-scrollbar selection:bg-primary/20 dark:selection:bg-primary/30 min-h-0"
615
+ style={{
616
+ backgroundColor: effectiveBackgroundColors
617
+ ? (effectiveDarkMode && effectiveBackgroundColors.dark
618
+ ? effectiveBackgroundColors.dark
619
+ : effectiveBackgroundColors.light)
620
+ : undefined,
621
+ }}
622
+ >
623
+ <div className={`mx-auto transition-all duration-500 max-w-7xl w-full`}>
624
+ {/* Hero Block - Only show editable version when NOT in preview mode */}
625
+ {!isPreviewMode && heroBlockDefinition && heroBlock && (
626
+ <div className="mb-12">
627
+ <BlockWrapper
628
+ block={heroBlock}
629
+ onUpdate={(data) => {
630
+ setHeroBlock({
631
+ ...heroBlock,
632
+ data: { ...heroBlock.data, ...data },
633
+ });
634
+ // Sync title to editor state
635
+ if (data.title !== undefined && typeof data.title === 'string') {
636
+ dispatch({ type: 'SET_TITLE', payload: data.title });
637
+ }
638
+ // Sync summary to editor state metadata
639
+ if (data.summary !== undefined && typeof data.summary === 'string') {
640
+ dispatch({
641
+ type: 'SET_METADATA',
642
+ payload: { excerpt: data.summary }
643
+ });
644
+ }
645
+ // Sync image to editor state metadata
646
+ if (data.image !== undefined) {
647
+ const imageData = data.image as any;
648
+ dispatch({
649
+ type: 'SET_METADATA',
650
+ payload: {
651
+ featuredImage: imageData ? {
652
+ src: imageData.src || '',
653
+ alt: imageData.alt || '',
654
+ brightness: imageData.brightness ?? 100,
655
+ blur: imageData.blur ?? 0,
656
+ } : undefined
657
+ }
658
+ });
659
+ }
660
+ // Sync category to editor state metadata
661
+ if (data.category !== undefined && typeof data.category === 'string') {
662
+ dispatch({
663
+ type: 'SET_METADATA',
664
+ payload: {
665
+ categories: data.category.trim() ? [data.category.trim()] : []
666
+ }
667
+ });
668
+ }
669
+ }}
670
+ onDelete={() => {
671
+ const defaultData = heroBlockDefinition.defaultData || {};
672
+ setHeroBlock({
673
+ ...heroBlock,
674
+ data: { ...defaultData },
675
+ });
676
+ }}
677
+ onMoveUp={() => { }}
678
+ onMoveDown={() => { }}
679
+ allBlocks={[heroBlock]}
680
+ />
681
+ </div>
682
+ )}
683
+
684
+ {isPreviewMode ? (
685
+ <div className="space-y-8">
686
+ {heroBlockDefinition && heroBlock && (
687
+ <BlockRenderer
688
+ block={heroBlock}
689
+ context={{ siteId, locale }}
690
+ />
691
+ )}
692
+
693
+ {!heroBlockDefinition && state.title && (
694
+ <h1 className="text-5xl font-serif font-medium text-neutral-950 dark:text-white leading-tight mb-12">
695
+ {state.title}
696
+ </h1>
697
+ )}
698
+
699
+ {contentBlocks.length > 0 ? (
700
+ <div className="space-y-8">
701
+ {contentBlocks.map((block) => (
702
+ <BlockRenderer
703
+ key={block.id}
704
+ block={block}
705
+ context={{ siteId, locale }}
706
+ />
707
+ ))}
708
+ </div>
709
+ ) : (
710
+ <div className="text-center py-20 text-neutral-400 dark:text-neutral-500">
711
+ <p className="text-sm">No content blocks yet. Switch to Edit mode to add blocks.</p>
712
+ </div>
713
+ )}
714
+ </div>
715
+ ) : (
716
+ <>
717
+ {!heroBlockDefinition && (
718
+ <div className="mb-12">
719
+ <textarea
720
+ ref={titleRef}
721
+ rows={1}
722
+ value={state.title}
723
+ onChange={(e) => dispatch({ type: 'SET_TITLE', payload: e.target.value })}
724
+ placeholder="The title of your story..."
725
+ className="w-full bg-transparent border-none outline-none text-5xl font-serif font-medium placeholder:text-neutral-500 dark:placeholder:text-neutral-500 resize-none leading-tight transition-colors duration-300 text-neutral-950 dark:text-white"
726
+ />
727
+ </div>
728
+ )}
729
+
730
+ <EditorBody
731
+ blocks={contentBlocks}
732
+ darkMode={effectiveDarkMode}
733
+ backgroundColors={effectiveBackgroundColors}
734
+ onBlockAdd={(type, index, containerId) => {
735
+ helpers.addBlock(type, index, containerId);
736
+ }}
737
+ onBlockUpdate={(id, data) => {
738
+ helpers.updateBlock(id, data);
739
+ }}
740
+ onBlockDelete={(id) => {
741
+ helpers.deleteBlock(id);
742
+ }}
743
+ onBlockMove={(id, newIndex, containerId) => {
744
+ helpers.moveBlock(id, newIndex, containerId);
745
+ }}
746
+ />
747
+ </>
748
+ )}
749
+ </div>
750
+ </div>
751
+
752
+ {/* RIGHT SIDEBAR: THE "DESK" (SETTINGS) */}
753
+ {!isPreviewMode && (
754
+ <aside
755
+ className={`transition-all duration-500 ease-[cubic-bezier(0.4,0,0.2,1)] border-l border-dashboard-border bg-dashboard-sidebar overflow-y-auto overflow-x-hidden h-full ${isSidebarOpen ? 'w-80' : 'w-0 opacity-0 pointer-events-none'
756
+ }`}
757
+ >
758
+ <div className="p-8 w-80 min-w-0 max-w-full space-y-12 overflow-y-auto max-h-full">
759
+ {/* SEO Section */}
760
+ <section>
761
+ <div className="flex items-center gap-3 mb-6">
762
+ <Search size={14} className="text-neutral-500 dark:text-neutral-400" />
763
+ <label className="text-[10px] uppercase tracking-[0.2em] text-neutral-500 dark:text-neutral-400 font-black">
764
+ SEO Settings
765
+ </label>
766
+ </div>
767
+ <div className="space-y-4">
768
+ <div>
769
+ <label className="text-[10px] text-neutral-500 dark:text-neutral-400 uppercase font-bold block mb-2">
770
+ SEO Title
771
+ </label>
772
+ <input
773
+ type="text"
774
+ value={state.seo.title || ''}
775
+ onChange={(e) => dispatch({ type: 'SET_SEO', payload: { title: e.target.value } })}
776
+ placeholder="SEO title (defaults to post title)"
777
+ className="w-full px-3 py-2 text-xs bg-dashboard-card border border-dashboard-border rounded-lg outline-none focus:border-primary transition-all text-dashboard-text"
778
+ />
779
+ <p className="text-[9px] text-neutral-400 dark:text-neutral-500 mt-1">
780
+ {state.seo.title?.length || 0} / 60 characters
781
+ </p>
782
+ </div>
783
+ <div>
784
+ <label className="text-[10px] text-neutral-500 dark:text-neutral-400 uppercase font-bold block mb-2">
785
+ Meta Description
786
+ </label>
787
+ <textarea
788
+ value={state.seo.description || ''}
789
+ onChange={(e) => dispatch({ type: 'SET_SEO', payload: { description: e.target.value } })}
790
+ placeholder="Brief description for search engines"
791
+ rows={3}
792
+ className="w-full px-3 py-2 text-xs bg-white dark:bg-neutral-900/50 border border-neutral-300 dark:border-neutral-700 rounded-lg outline-none focus:border-primary transition-all dark:text-neutral-100 resize-none"
793
+ />
794
+ <p className="text-[9px] text-neutral-400 dark:text-neutral-500 mt-1">
795
+ {state.seo.description?.length || 0} / 160 characters
796
+ </p>
797
+ </div>
798
+ <div>
799
+ <label className="text-[10px] text-neutral-500 dark:text-neutral-400 uppercase font-bold block mb-2">
800
+ Keywords (comma-separated)
801
+ </label>
802
+ <input
803
+ type="text"
804
+ value={state.seo.keywords?.join(', ') || ''}
805
+ onChange={(e) => {
806
+ const keywords = e.target.value.split(',').map(k => k.trim()).filter(k => k);
807
+ dispatch({ type: 'SET_SEO', payload: { keywords } });
808
+ }}
809
+ placeholder="keyword1, keyword2, keyword3"
810
+ className="w-full px-3 py-2 text-xs bg-dashboard-card border border-dashboard-border rounded-lg outline-none focus:border-primary transition-all text-dashboard-text"
811
+ />
812
+ </div>
813
+ <div>
814
+ <label className="text-[10px] text-neutral-500 dark:text-neutral-400 uppercase font-bold block mb-2">
815
+ Open Graph Image URL
816
+ </label>
817
+ <input
818
+ type="url"
819
+ value={state.seo.ogImage || ''}
820
+ onChange={(e) => dispatch({ type: 'SET_SEO', payload: { ogImage: e.target.value } })}
821
+ placeholder="https://example.com/image.jpg"
822
+ className="w-full px-3 py-2 text-xs bg-dashboard-card border border-dashboard-border rounded-lg outline-none focus:border-primary transition-all text-dashboard-text"
823
+ />
824
+ </div>
825
+ </div>
826
+ </section>
827
+
828
+ {/* Publishing Section */}
829
+ <section className="pt-8 border-t border-neutral-200 dark:border-neutral-800">
830
+ <div className="flex items-center gap-3 mb-6">
831
+ <Globe size={14} className="text-neutral-500 dark:text-neutral-400" />
832
+ <label className="text-[10px] uppercase tracking-[0.2em] text-neutral-500 dark:text-neutral-400 font-black">
833
+ Publishing
834
+ </label>
835
+ </div>
836
+ <div className="bg-dashboard-bg p-5 rounded-2xl border border-dashboard-border">
837
+ <span className="text-[10px] text-neutral-500 dark:text-neutral-400 uppercase font-bold block mb-3">Slug / Permalink</span>
838
+ <div className="text-xs font-mono break-all text-neutral-600 dark:text-neutral-400 leading-relaxed">
839
+ /blog/<span className="text-neutral-950 dark:text-white bg-amber-50 dark:bg-amber-900/20 px-1 rounded">{state.slug || 'untitled-post'}</span>
840
+ </div>
841
+ </div>
842
+ </section>
843
+
844
+ {/* Category Section */}
845
+ <section className="pt-8 border-t border-neutral-200 dark:border-neutral-800">
846
+ <div className="flex items-center gap-3 mb-6">
847
+ <Box size={14} className="text-neutral-500 dark:text-neutral-400" />
848
+ <label className="text-[10px] uppercase tracking-[0.2em] text-neutral-500 dark:text-neutral-400 font-black">
849
+ Category
850
+ </label>
851
+ </div>
852
+ <div className="space-y-4">
853
+ <div>
854
+ <label className="text-[10px] text-neutral-500 dark:text-neutral-400 uppercase font-bold block mb-2">
855
+ Category
856
+ </label>
857
+ <input
858
+ type="text"
859
+ value={state.metadata.categories?.[0] || ''}
860
+ onChange={(e) => {
861
+ const category = e.target.value.trim();
862
+ dispatch({
863
+ type: 'SET_METADATA',
864
+ payload: {
865
+ categories: category ? [category] : []
866
+ }
867
+ });
868
+ }}
869
+ placeholder="Enter category (required for publishing)"
870
+ className="w-full px-3 py-2 text-xs bg-dashboard-card border border-dashboard-border rounded-lg outline-none focus:border-primary transition-all text-dashboard-text"
871
+ />
872
+ <p className="text-[9px] text-neutral-400 dark:text-neutral-500 mt-1">
873
+ {state.metadata.categories?.[0] ? 'Category set' : 'No category set'}
874
+ </p>
875
+ </div>
876
+ </div>
877
+ </section>
878
+
879
+ {/* Featured Media Section */}
880
+ <FeaturedMediaSection
881
+ featuredImage={state.metadata.featuredImage}
882
+ onUpdate={(image) => dispatch({ type: 'SET_METADATA', payload: { featuredImage: image } })}
883
+ />
884
+
885
+ {/* Privacy Settings Section */}
886
+ <PrivacySettingsSection
887
+ privacy={state.metadata.privacy}
888
+ onUpdate={(privacy) => dispatch({ type: 'SET_METADATA', payload: { privacy } })}
889
+ />
890
+
891
+ {/* Post Status Section */}
892
+ <section className="pt-8 border-t border-neutral-200 dark:border-neutral-800">
893
+ <div className="flex items-center justify-between mb-4">
894
+ <label className="text-[10px] uppercase tracking-[0.2em] text-neutral-500 dark:text-neutral-400 font-black">
895
+ Post Status
896
+ </label>
897
+ <span className="text-[10px] font-black text-amber-700 dark:text-amber-400 bg-amber-50 dark:bg-amber-900/20 px-2.5 py-1 rounded-full uppercase tracking-tighter">
898
+ {state.status}
899
+ </span>
900
+ </div>
901
+ <p className="text-[11px] text-neutral-500 dark:text-neutral-400 leading-relaxed italic">
902
+ {state.status === 'draft'
903
+ ? 'This post is private. Only you can see it until you hit publish.'
904
+ : state.status === 'published'
905
+ ? 'This post is live and visible to everyone.'
906
+ : 'This post is scheduled for publication.'}
907
+ </p>
908
+ </section>
909
+ </div>
910
+ </aside>
911
+ )}
912
+ </div>
913
+ </main>
914
+ </div>
915
+ );
916
+ }
917
+