@jhits/plugin-blog 0.0.14 → 0.0.16

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 (208) hide show
  1. package/package.json +5 -4
  2. package/src/api/categories.ts +43 -0
  3. package/src/api/check-title.ts +60 -0
  4. package/src/api/config-handler.ts +76 -0
  5. package/src/api/handler.ts +418 -0
  6. package/src/api/index.ts +33 -0
  7. package/src/api/route.ts +116 -0
  8. package/src/api/router.ts +128 -0
  9. package/src/api-server.ts +11 -0
  10. package/src/config.ts +161 -0
  11. package/src/hooks/index.d.ts +8 -0
  12. package/src/hooks/index.d.ts.map +1 -0
  13. package/src/hooks/index.ts +9 -0
  14. package/src/hooks/useBlog.d.ts +31 -0
  15. package/src/hooks/useBlog.d.ts.map +1 -0
  16. package/src/hooks/useBlog.ts +85 -0
  17. package/src/hooks/useBlogs.d.ts +39 -0
  18. package/src/hooks/useBlogs.d.ts.map +1 -0
  19. package/src/hooks/useBlogs.ts +123 -0
  20. package/src/hooks/useCategories.d.ts +9 -0
  21. package/src/hooks/useCategories.d.ts.map +1 -0
  22. package/src/hooks/useCategories.ts +76 -0
  23. package/src/index.server.ts +14 -0
  24. package/src/index.tsx +335 -0
  25. package/src/init.tsx +63 -0
  26. package/src/lib/blocks/BlockRenderer.d.ts +54 -0
  27. package/src/lib/blocks/BlockRenderer.d.ts.map +1 -0
  28. package/src/lib/blocks/BlockRenderer.tsx +141 -0
  29. package/src/lib/blocks/index.ts +6 -0
  30. package/src/lib/config-storage.d.ts +30 -0
  31. package/src/lib/config-storage.d.ts.map +1 -0
  32. package/src/lib/config-storage.ts +65 -0
  33. package/src/lib/index.ts +9 -0
  34. package/src/lib/layouts/blocks/ColumnsBlock.d.ts +25 -0
  35. package/src/lib/layouts/blocks/ColumnsBlock.d.ts.map +1 -0
  36. package/src/lib/layouts/blocks/ColumnsBlock.tsx +298 -0
  37. package/src/lib/layouts/blocks/ColumnsBlock.tsx.tmp +81 -0
  38. package/src/lib/layouts/blocks/SectionBlock.d.ts +25 -0
  39. package/src/lib/layouts/blocks/SectionBlock.d.ts.map +1 -0
  40. package/src/lib/layouts/blocks/SectionBlock.tsx +104 -0
  41. package/src/lib/layouts/blocks/index.ts +8 -0
  42. package/src/lib/layouts/index.d.ts +23 -0
  43. package/src/lib/layouts/index.d.ts.map +1 -0
  44. package/src/lib/layouts/index.ts +52 -0
  45. package/src/lib/layouts/registerLayoutBlocks.d.ts +9 -0
  46. package/src/lib/layouts/registerLayoutBlocks.d.ts.map +1 -0
  47. package/src/lib/layouts/registerLayoutBlocks.ts +64 -0
  48. package/src/lib/mappers/apiMapper.d.ts +66 -0
  49. package/src/lib/mappers/apiMapper.d.ts.map +1 -0
  50. package/src/lib/mappers/apiMapper.ts +254 -0
  51. package/src/lib/migration/index.ts +6 -0
  52. package/src/lib/migration/mapper.ts +140 -0
  53. package/src/lib/rich-text/RichTextEditor.d.ts +45 -0
  54. package/src/lib/rich-text/RichTextEditor.d.ts.map +1 -0
  55. package/src/lib/rich-text/RichTextEditor.tsx +826 -0
  56. package/src/lib/rich-text/RichTextPreview.d.ts +16 -0
  57. package/src/lib/rich-text/RichTextPreview.d.ts.map +1 -0
  58. package/src/lib/rich-text/RichTextPreview.tsx +210 -0
  59. package/src/lib/rich-text/index.d.ts +9 -0
  60. package/src/lib/rich-text/index.d.ts.map +1 -0
  61. package/src/lib/rich-text/index.ts +10 -0
  62. package/src/lib/utils/blockHelpers.d.ts +23 -0
  63. package/src/lib/utils/blockHelpers.d.ts.map +1 -0
  64. package/src/lib/utils/blockHelpers.ts +72 -0
  65. package/src/lib/utils/configValidation.d.ts +23 -0
  66. package/src/lib/utils/configValidation.d.ts.map +1 -0
  67. package/src/lib/utils/configValidation.ts +137 -0
  68. package/src/lib/utils/index.ts +8 -0
  69. package/src/lib/utils/slugify.ts +79 -0
  70. package/src/registry/BlockRegistry.d.ts +62 -0
  71. package/src/registry/BlockRegistry.d.ts.map +1 -0
  72. package/src/registry/BlockRegistry.ts +139 -0
  73. package/src/registry/index.d.ts +6 -0
  74. package/src/registry/index.d.ts.map +1 -0
  75. package/src/registry/index.ts +11 -0
  76. package/src/state/EditorContext.d.ts +45 -0
  77. package/src/state/EditorContext.d.ts.map +1 -0
  78. package/src/state/EditorContext.tsx +283 -0
  79. package/src/state/index.d.ts +7 -0
  80. package/src/state/index.d.ts.map +1 -0
  81. package/src/state/index.ts +8 -0
  82. package/src/state/reducer.d.ts +11 -0
  83. package/src/state/reducer.d.ts.map +1 -0
  84. package/src/state/reducer.ts +694 -0
  85. package/src/state/types.d.ts +162 -0
  86. package/src/state/types.d.ts.map +1 -0
  87. package/src/state/types.ts +160 -0
  88. package/src/types/block.d.ts +221 -0
  89. package/src/types/block.d.ts.map +1 -0
  90. package/src/types/block.ts +269 -0
  91. package/src/types/index.d.ts +8 -0
  92. package/src/types/index.d.ts.map +1 -0
  93. package/src/types/index.ts +17 -0
  94. package/src/types/post.d.ts +136 -0
  95. package/src/types/post.d.ts.map +1 -0
  96. package/src/types/post.ts +169 -0
  97. package/src/utils/client.d.ts +48 -0
  98. package/src/utils/client.d.ts.map +1 -0
  99. package/src/utils/client.ts +122 -0
  100. package/src/utils/index.ts +7 -0
  101. package/src/views/CanvasEditor/BlockWrapper.d.ts +16 -0
  102. package/src/views/CanvasEditor/BlockWrapper.d.ts.map +1 -0
  103. package/src/views/CanvasEditor/BlockWrapper.tsx +522 -0
  104. package/src/views/CanvasEditor/CanvasEditorView.d.ts +14 -0
  105. package/src/views/CanvasEditor/CanvasEditorView.d.ts.map +1 -0
  106. package/src/views/CanvasEditor/CanvasEditorView.tsx +337 -0
  107. package/src/views/CanvasEditor/EditorBody.d.ts +22 -0
  108. package/src/views/CanvasEditor/EditorBody.d.ts.map +1 -0
  109. package/src/views/CanvasEditor/EditorBody.tsx +665 -0
  110. package/src/views/CanvasEditor/EditorHeader.d.ts +18 -0
  111. package/src/views/CanvasEditor/EditorHeader.d.ts.map +1 -0
  112. package/src/views/CanvasEditor/EditorHeader.tsx +268 -0
  113. package/src/views/CanvasEditor/LayoutContainer.d.ts +17 -0
  114. package/src/views/CanvasEditor/LayoutContainer.d.ts.map +1 -0
  115. package/src/views/CanvasEditor/LayoutContainer.tsx +322 -0
  116. package/src/views/CanvasEditor/SaveConfirmationModal.d.ts +13 -0
  117. package/src/views/CanvasEditor/SaveConfirmationModal.d.ts.map +1 -0
  118. package/src/views/CanvasEditor/SaveConfirmationModal.tsx +233 -0
  119. package/src/views/CanvasEditor/components/CustomBlockItem.d.ts +14 -0
  120. package/src/views/CanvasEditor/components/CustomBlockItem.d.ts.map +1 -0
  121. package/src/views/CanvasEditor/components/CustomBlockItem.tsx +92 -0
  122. package/src/views/CanvasEditor/components/EditorCanvas.d.ts +29 -0
  123. package/src/views/CanvasEditor/components/EditorCanvas.d.ts.map +1 -0
  124. package/src/views/CanvasEditor/components/EditorCanvas.tsx +160 -0
  125. package/src/views/CanvasEditor/components/EditorLibrary.d.ts +7 -0
  126. package/src/views/CanvasEditor/components/EditorLibrary.d.ts.map +1 -0
  127. package/src/views/CanvasEditor/components/EditorLibrary.tsx +122 -0
  128. package/src/views/CanvasEditor/components/EditorSidebar.d.ts +13 -0
  129. package/src/views/CanvasEditor/components/EditorSidebar.d.ts.map +1 -0
  130. package/src/views/CanvasEditor/components/EditorSidebar.tsx +181 -0
  131. package/src/views/CanvasEditor/components/ErrorBanner.d.ts +6 -0
  132. package/src/views/CanvasEditor/components/ErrorBanner.d.ts.map +1 -0
  133. package/src/views/CanvasEditor/components/ErrorBanner.tsx +31 -0
  134. package/src/views/CanvasEditor/components/FeaturedMediaSection.d.ts +25 -0
  135. package/src/views/CanvasEditor/components/FeaturedMediaSection.d.ts.map +1 -0
  136. package/src/views/CanvasEditor/components/FeaturedMediaSection.tsx +341 -0
  137. package/src/views/CanvasEditor/components/LibraryItem.d.ts +14 -0
  138. package/src/views/CanvasEditor/components/LibraryItem.d.ts.map +1 -0
  139. package/src/views/CanvasEditor/components/LibraryItem.tsx +80 -0
  140. package/src/views/CanvasEditor/components/PrivacySettingsSection.d.ts +15 -0
  141. package/src/views/CanvasEditor/components/PrivacySettingsSection.d.ts.map +1 -0
  142. package/src/views/CanvasEditor/components/PrivacySettingsSection.tsx +212 -0
  143. package/src/views/CanvasEditor/components/index.d.ts +21 -0
  144. package/src/views/CanvasEditor/components/index.d.ts.map +1 -0
  145. package/src/views/CanvasEditor/components/index.ts +28 -0
  146. package/src/views/CanvasEditor/hooks/index.d.ts +10 -0
  147. package/src/views/CanvasEditor/hooks/index.d.ts.map +1 -0
  148. package/src/views/CanvasEditor/hooks/index.ts +10 -0
  149. package/src/views/CanvasEditor/hooks/useHeroBlock.d.ts +8 -0
  150. package/src/views/CanvasEditor/hooks/useHeroBlock.d.ts.map +1 -0
  151. package/src/views/CanvasEditor/hooks/useHeroBlock.ts +103 -0
  152. package/src/views/CanvasEditor/hooks/useKeyboardShortcuts.d.ts +3 -0
  153. package/src/views/CanvasEditor/hooks/useKeyboardShortcuts.d.ts.map +1 -0
  154. package/src/views/CanvasEditor/hooks/useKeyboardShortcuts.ts +142 -0
  155. package/src/views/CanvasEditor/hooks/usePostLoader.d.ts +5 -0
  156. package/src/views/CanvasEditor/hooks/usePostLoader.d.ts.map +1 -0
  157. package/src/views/CanvasEditor/hooks/usePostLoader.ts +39 -0
  158. package/src/views/CanvasEditor/hooks/useRegisteredBlocks.d.ts +2 -0
  159. package/src/views/CanvasEditor/hooks/useRegisteredBlocks.d.ts.map +1 -0
  160. package/src/views/CanvasEditor/hooks/useRegisteredBlocks.ts +55 -0
  161. package/src/views/CanvasEditor/hooks/useUnsavedChanges.d.ts +25 -0
  162. package/src/views/CanvasEditor/hooks/useUnsavedChanges.d.ts.map +1 -0
  163. package/src/views/CanvasEditor/hooks/useUnsavedChanges.ts +339 -0
  164. package/src/views/CanvasEditor/index.d.ts +16 -0
  165. package/src/views/CanvasEditor/index.d.ts.map +1 -0
  166. package/src/views/CanvasEditor/index.ts +16 -0
  167. package/src/views/PostManager/EmptyState.d.ts +10 -0
  168. package/src/views/PostManager/EmptyState.d.ts.map +1 -0
  169. package/src/views/PostManager/EmptyState.tsx +42 -0
  170. package/src/views/PostManager/PostActionsMenu.d.ts +12 -0
  171. package/src/views/PostManager/PostActionsMenu.d.ts.map +1 -0
  172. package/src/views/PostManager/PostActionsMenu.tsx +112 -0
  173. package/src/views/PostManager/PostCards.d.ts +15 -0
  174. package/src/views/PostManager/PostCards.d.ts.map +1 -0
  175. package/src/views/PostManager/PostCards.tsx +197 -0
  176. package/src/views/PostManager/PostFilters.d.ts +16 -0
  177. package/src/views/PostManager/PostFilters.d.ts.map +1 -0
  178. package/src/views/PostManager/PostFilters.tsx +95 -0
  179. package/src/views/PostManager/PostManagerView.d.ts +11 -0
  180. package/src/views/PostManager/PostManagerView.d.ts.map +1 -0
  181. package/src/views/PostManager/PostManagerView.tsx +289 -0
  182. package/src/views/PostManager/PostStats.d.ts +11 -0
  183. package/src/views/PostManager/PostStats.d.ts.map +1 -0
  184. package/src/views/PostManager/PostStats.tsx +81 -0
  185. package/src/views/PostManager/PostTable.d.ts +15 -0
  186. package/src/views/PostManager/PostTable.d.ts.map +1 -0
  187. package/src/views/PostManager/PostTable.tsx +230 -0
  188. package/src/views/PostManager/index.d.ts +12 -0
  189. package/src/views/PostManager/index.d.ts.map +1 -0
  190. package/src/views/PostManager/index.ts +15 -0
  191. package/src/views/Preview/PreviewBridgeView.d.ts +12 -0
  192. package/src/views/Preview/PreviewBridgeView.d.ts.map +1 -0
  193. package/src/views/Preview/PreviewBridgeView.tsx +64 -0
  194. package/src/views/Preview/index.d.ts +6 -0
  195. package/src/views/Preview/index.d.ts.map +1 -0
  196. package/src/views/Preview/index.ts +7 -0
  197. package/src/views/Settings/SettingsView.d.ts +10 -0
  198. package/src/views/Settings/SettingsView.d.ts.map +1 -0
  199. package/src/views/Settings/SettingsView.tsx +298 -0
  200. package/src/views/Settings/index.d.ts +6 -0
  201. package/src/views/Settings/index.d.ts.map +1 -0
  202. package/src/views/Settings/index.ts +7 -0
  203. package/src/views/SlugSEO/SlugSEOManagerView.d.ts +12 -0
  204. package/src/views/SlugSEO/SlugSEOManagerView.d.ts.map +1 -0
  205. package/src/views/SlugSEO/SlugSEOManagerView.tsx +94 -0
  206. package/src/views/SlugSEO/index.d.ts +6 -0
  207. package/src/views/SlugSEO/index.d.ts.map +1 -0
  208. package/src/views/SlugSEO/index.ts +7 -0
@@ -0,0 +1,5 @@
1
+ import type { BlogPost } from '../../../types/post';
2
+ export declare function usePostLoader(postId: string | undefined, currentPostId: string | null, loadPost: (post: BlogPost) => void, resetHeroBlock: () => void): {
3
+ isLoadingPost: boolean;
4
+ };
5
+ //# sourceMappingURL=usePostLoader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePostLoader.d.ts","sourceRoot":"","sources":["usePostLoader.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAEpD,wBAAgB,aAAa,CACzB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,aAAa,EAAE,MAAM,GAAG,IAAI,EAC5B,QAAQ,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,EAClC,cAAc,EAAE,MAAM,IAAI;;EA8B7B"}
@@ -0,0 +1,39 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { apiToBlogPost, type APIBlogDocument } from '../../../lib/mappers/apiMapper';
3
+ import type { BlogPost } from '../../../types/post';
4
+
5
+ export function usePostLoader(
6
+ postId: string | undefined,
7
+ currentPostId: string | null,
8
+ loadPost: (post: BlogPost) => void,
9
+ resetHeroBlock: () => void
10
+ ) {
11
+ const [isLoadingPost, setIsLoadingPost] = useState(false);
12
+
13
+ useEffect(() => {
14
+ if (postId && !currentPostId) {
15
+ const loadPostData = async () => {
16
+ try {
17
+ setIsLoadingPost(true);
18
+ // Reset hero block before loading new post so it gets re-initialized from the new post's blocks
19
+ resetHeroBlock();
20
+ const response = await fetch(`/api/plugin-blog/${postId}`);
21
+ if (!response.ok) {
22
+ throw new Error('Failed to load post');
23
+ }
24
+ const apiDoc: APIBlogDocument = await response.json();
25
+ const blogPost = apiToBlogPost(apiDoc);
26
+ loadPost(blogPost);
27
+ } catch (error) {
28
+ console.error('Failed to load post:', error);
29
+ alert('Failed to load post. Please try again.');
30
+ } finally {
31
+ setIsLoadingPost(false);
32
+ }
33
+ };
34
+ loadPostData();
35
+ }
36
+ }, [postId, currentPostId, loadPost, resetHeroBlock]);
37
+
38
+ return { isLoadingPost };
39
+ }
@@ -0,0 +1,2 @@
1
+ export declare function useRegisteredBlocks(): import("../../..").BlockTypeDefinition[];
2
+ //# sourceMappingURL=useRegisteredBlocks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRegisteredBlocks.d.ts","sourceRoot":"","sources":["useRegisteredBlocks.ts"],"names":[],"mappings":"AAGA,wBAAgB,mBAAmB,6CAmDlC"}
@@ -0,0 +1,55 @@
1
+ import { useState, useEffect } from 'react';
2
+ import { blockRegistry } from '../../../registry/BlockRegistry';
3
+
4
+ export function useRegisteredBlocks() {
5
+ const [registeredBlocks, setRegisteredBlocks] = useState(() => {
6
+ const initial = blockRegistry.getAll();
7
+ return initial;
8
+ });
9
+
10
+ // Watch for registry changes and update state
11
+ useEffect(() => {
12
+ // Check immediately
13
+ const checkBlocks = () => {
14
+ const currentBlocks = blockRegistry.getAll();
15
+ const hasChanged = currentBlocks.length !== registeredBlocks.length ||
16
+ currentBlocks.some((b, i) => b.type !== registeredBlocks[i]?.type) ||
17
+ registeredBlocks.some((b, i) => b.type !== currentBlocks[i]?.type);
18
+
19
+ if (hasChanged) {
20
+ setRegisteredBlocks([...currentBlocks]);
21
+ }
22
+ };
23
+
24
+ // Initial check
25
+ checkBlocks();
26
+
27
+ // Poll for registry changes (blocks are registered asynchronously in useEffect)
28
+ // Use a shorter interval initially, then longer
29
+ let pollCount = 0;
30
+ const interval = setInterval(() => {
31
+ pollCount++;
32
+ checkBlocks();
33
+ // Stop polling after 5 seconds (25 checks at 200ms)
34
+ if (pollCount > 25) {
35
+ clearInterval(interval);
36
+ }
37
+ }, 200);
38
+
39
+ // Also check after delays to catch initial registrations
40
+ const timeouts = [
41
+ setTimeout(checkBlocks, 50),
42
+ setTimeout(checkBlocks, 100),
43
+ setTimeout(checkBlocks, 300),
44
+ setTimeout(checkBlocks, 500),
45
+ setTimeout(checkBlocks, 1000),
46
+ ];
47
+
48
+ return () => {
49
+ clearInterval(interval);
50
+ timeouts.forEach(clearTimeout);
51
+ };
52
+ }, [registeredBlocks.length]);
53
+
54
+ return registeredBlocks;
55
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Hook for managing unsaved changes warnings and auto-save
3
+ */
4
+ import type { EditorState } from '../../../state/types';
5
+ import type { Block } from '../../../types/block';
6
+ interface UseUnsavedChangesOptions {
7
+ state: EditorState;
8
+ isDirty: boolean;
9
+ onSave: (heroBlock?: Block | null) => Promise<void>;
10
+ heroBlock?: Block | null;
11
+ autoSaveEnabled?: boolean;
12
+ autoSaveDelay?: number;
13
+ postId?: string | null;
14
+ }
15
+ /**
16
+ * Hook to manage unsaved changes warnings and auto-save
17
+ */
18
+ export declare function useUnsavedChanges({ state, isDirty, onSave, heroBlock, autoSaveEnabled: propAutoSaveEnabled, autoSaveDelay, postId, }: UseUnsavedChangesOptions): {
19
+ autoSaveEnabled: boolean;
20
+ setAutoSaveEnabled: (enabled: boolean) => void;
21
+ countdown: number | null;
22
+ saveStatus: "error" | "idle" | "saving" | "saved";
23
+ };
24
+ export {};
25
+ //# sourceMappingURL=useUnsavedChanges.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUnsavedChanges.d.ts","sourceRoot":"","sources":["useUnsavedChanges.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAElD,UAAU,wBAAwB;IAC9B,KAAK,EAAE,WAAW,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,CAAC,SAAS,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACpD,SAAS,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;IACzB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAKD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,EAC9B,KAAK,EACL,OAAO,EACP,MAAM,EACN,SAAS,EACT,eAAe,EAAE,mBAAmB,EACpC,aAAuC,EACvC,MAAM,GACT,EAAE,wBAAwB;;kCAoD6B,OAAO;;;EA4P9D"}
@@ -0,0 +1,339 @@
1
+ /**
2
+ * Hook for managing unsaved changes warnings and auto-save
3
+ */
4
+
5
+ 'use client';
6
+
7
+ import { useEffect, useRef, useCallback, useState } from 'react';
8
+ import type { EditorState } from '../../../state/types';
9
+ import type { Block } from '../../../types/block';
10
+
11
+ interface UseUnsavedChangesOptions {
12
+ state: EditorState;
13
+ isDirty: boolean;
14
+ onSave: (heroBlock?: Block | null) => Promise<void>;
15
+ heroBlock?: Block | null;
16
+ autoSaveEnabled?: boolean;
17
+ autoSaveDelay?: number; // in milliseconds
18
+ postId?: string | null; // Post ID to detect when a post is loaded
19
+ }
20
+
21
+ const AUTO_SAVE_STORAGE_KEY = 'blog-editor-autosave-enabled';
22
+ const DEFAULT_AUTO_SAVE_DELAY = 10000; // 10 seconds
23
+
24
+ /**
25
+ * Hook to manage unsaved changes warnings and auto-save
26
+ */
27
+ export function useUnsavedChanges({
28
+ state,
29
+ isDirty,
30
+ onSave,
31
+ heroBlock,
32
+ autoSaveEnabled: propAutoSaveEnabled,
33
+ autoSaveDelay = DEFAULT_AUTO_SAVE_DELAY,
34
+ postId,
35
+ }: UseUnsavedChangesOptions) {
36
+ const autoSaveTimeoutRef = useRef<NodeJS.Timeout | null>(null);
37
+ const isSavingRef = useRef(false);
38
+ const lastSavedStateRef = useRef<string>('');
39
+ const countdownIntervalRef = useRef<NodeJS.Timeout | null>(null);
40
+ const [countdown, setCountdown] = useState<number | null>(null);
41
+ const [saveStatus, setSaveStatus] = useState<'idle' | 'saving' | 'saved' | 'error'>('idle');
42
+ const saveStatusTimeoutRef = useRef<NodeJS.Timeout | null>(null);
43
+ const previousIsDirtyRef = useRef<boolean>(false);
44
+ const countdownStartTimeRef = useRef<number | null>(null);
45
+ const performAutoSaveRef = useRef<(() => Promise<void>) | null>(null);
46
+
47
+ // Get auto-save preference from localStorage
48
+ const getAutoSavePreference = useCallback((): boolean => {
49
+ if (typeof window === 'undefined') return false;
50
+ try {
51
+ const stored = localStorage.getItem(AUTO_SAVE_STORAGE_KEY);
52
+ if (stored !== null) {
53
+ return stored === 'true';
54
+ }
55
+ } catch (error) {
56
+ console.error('[useUnsavedChanges] Failed to read auto-save preference:', error);
57
+ }
58
+ // Default to false if not set
59
+ return false;
60
+ }, []);
61
+
62
+ // State to track auto-save enabled status (reactive)
63
+ const [autoSaveEnabledState, setAutoSaveEnabledState] = useState<boolean>(() => {
64
+ // Initialize from prop or localStorage
65
+ if (propAutoSaveEnabled !== undefined) {
66
+ return propAutoSaveEnabled;
67
+ }
68
+ return getAutoSavePreference();
69
+ });
70
+
71
+ // Sync with prop changes
72
+ useEffect(() => {
73
+ if (propAutoSaveEnabled !== undefined) {
74
+ setAutoSaveEnabledState(propAutoSaveEnabled);
75
+ }
76
+ }, [propAutoSaveEnabled]);
77
+
78
+ // Initialize from localStorage on mount (if no prop provided)
79
+ useEffect(() => {
80
+ if (propAutoSaveEnabled === undefined && typeof window !== 'undefined') {
81
+ const stored = getAutoSavePreference();
82
+ setAutoSaveEnabledState(stored);
83
+ }
84
+ }, []); // Only run on mount
85
+
86
+ // Set auto-save preference in localStorage and state
87
+ const setAutoSavePreference = useCallback((enabled: boolean) => {
88
+ if (typeof window === 'undefined') return;
89
+ try {
90
+ localStorage.setItem(AUTO_SAVE_STORAGE_KEY, enabled.toString());
91
+ setAutoSaveEnabledState(enabled);
92
+ console.log('[useUnsavedChanges] Auto-save preference updated:', enabled);
93
+ } catch (error) {
94
+ console.error('[useUnsavedChanges] Failed to save auto-save preference:', error);
95
+ }
96
+ }, []);
97
+
98
+ // Determine if auto-save is enabled (prop takes precedence over state)
99
+ const autoSaveEnabled = propAutoSaveEnabled !== undefined
100
+ ? propAutoSaveEnabled
101
+ : autoSaveEnabledState;
102
+
103
+ // Create a stable reference of the current state for comparison
104
+ const getStateSnapshot = useCallback(() => {
105
+ return JSON.stringify({
106
+ title: state.title,
107
+ slug: state.slug,
108
+ blocks: state.blocks,
109
+ seo: state.seo,
110
+ metadata: state.metadata,
111
+ status: state.status,
112
+ });
113
+ }, [state]);
114
+
115
+ // Initialize lastSavedStateRef when a post is loaded and marked as clean
116
+ useEffect(() => {
117
+ // When a post is loaded (postId exists) and isDirty is false, update the saved state reference
118
+ if (postId && !isDirty && lastSavedStateRef.current === '') {
119
+ lastSavedStateRef.current = getStateSnapshot();
120
+ console.log('[useUnsavedChanges] Initialized saved state reference after post load');
121
+ }
122
+ // Also update if isDirty becomes false after being true (e.g., after save or MARK_CLEAN)
123
+ if (!isDirty && lastSavedStateRef.current !== getStateSnapshot()) {
124
+ lastSavedStateRef.current = getStateSnapshot();
125
+ }
126
+ }, [postId, isDirty, getStateSnapshot]);
127
+
128
+ // Auto-save function
129
+ const performAutoSave = useCallback(async () => {
130
+ if (isSavingRef.current || !isDirty) {
131
+ return;
132
+ }
133
+
134
+ try {
135
+ isSavingRef.current = true;
136
+ setSaveStatus('saving');
137
+ setCountdown(null);
138
+ countdownStartTimeRef.current = null;
139
+ console.log('[useUnsavedChanges] Auto-saving...');
140
+ await onSave(heroBlock);
141
+ lastSavedStateRef.current = getStateSnapshot();
142
+ setSaveStatus('saved');
143
+ console.log('[useUnsavedChanges] Auto-save completed');
144
+
145
+ // Clear save status after 2 seconds
146
+ if (saveStatusTimeoutRef.current) {
147
+ clearTimeout(saveStatusTimeoutRef.current);
148
+ }
149
+ saveStatusTimeoutRef.current = setTimeout(() => {
150
+ setSaveStatus('idle');
151
+ }, 2000);
152
+ } catch (error) {
153
+ console.error('[useUnsavedChanges] Auto-save failed:', error);
154
+ setSaveStatus('error');
155
+ // Clear error status after 3 seconds
156
+ if (saveStatusTimeoutRef.current) {
157
+ clearTimeout(saveStatusTimeoutRef.current);
158
+ }
159
+ saveStatusTimeoutRef.current = setTimeout(() => {
160
+ setSaveStatus('idle');
161
+ }, 3000);
162
+ } finally {
163
+ isSavingRef.current = false;
164
+ }
165
+ }, [isDirty, onSave, heroBlock, getStateSnapshot]);
166
+
167
+ // Keep ref updated with latest function
168
+ useEffect(() => {
169
+ performAutoSaveRef.current = performAutoSave;
170
+ }, [performAutoSave]);
171
+
172
+ // Set up auto-save timer and countdown when state changes
173
+ useEffect(() => {
174
+ // Only reset countdown if isDirty changed from false to true, or if auto-save was just disabled
175
+ const isDirtyChanged = previousIsDirtyRef.current !== isDirty;
176
+ const becameDirty = !previousIsDirtyRef.current && isDirty;
177
+
178
+ // Update previous isDirty ref
179
+ previousIsDirtyRef.current = isDirty;
180
+
181
+ // Clear existing countdown interval
182
+ if (countdownIntervalRef.current) {
183
+ clearInterval(countdownIntervalRef.current);
184
+ countdownIntervalRef.current = null;
185
+ }
186
+
187
+ if (!autoSaveEnabled || !isDirty || isSavingRef.current) {
188
+ setCountdown(null);
189
+ countdownStartTimeRef.current = null;
190
+ // Clear timeout if auto-save is disabled or not dirty
191
+ if (autoSaveTimeoutRef.current) {
192
+ clearTimeout(autoSaveTimeoutRef.current);
193
+ autoSaveTimeoutRef.current = null;
194
+ }
195
+ return;
196
+ }
197
+
198
+ // Only reset countdown if:
199
+ // 1. isDirty just became true (new changes)
200
+ // 2. Or if there's no active countdown
201
+ const shouldResetCountdown = becameDirty || countdownStartTimeRef.current === null;
202
+
203
+ if (shouldResetCountdown) {
204
+ // Clear existing timeout
205
+ if (autoSaveTimeoutRef.current) {
206
+ clearTimeout(autoSaveTimeoutRef.current);
207
+ }
208
+
209
+ // Initialize countdown
210
+ const secondsRemaining = Math.ceil(autoSaveDelay / 1000);
211
+ setCountdown(secondsRemaining);
212
+ countdownStartTimeRef.current = Date.now();
213
+
214
+ // Set new timeout for auto-save
215
+ autoSaveTimeoutRef.current = setTimeout(() => {
216
+ if (performAutoSaveRef.current) {
217
+ performAutoSaveRef.current();
218
+ }
219
+ }, autoSaveDelay);
220
+ } else {
221
+ // Countdown is already running, just update it based on elapsed time
222
+ if (countdownStartTimeRef.current && autoSaveTimeoutRef.current) {
223
+ const elapsed = Date.now() - countdownStartTimeRef.current;
224
+ const remaining = Math.max(0, autoSaveDelay - elapsed);
225
+ const secondsRemaining = Math.ceil(remaining / 1000);
226
+ setCountdown(secondsRemaining);
227
+ }
228
+ }
229
+
230
+ // Update countdown every second (only if we have an active countdown)
231
+ if (shouldResetCountdown || countdownIntervalRef.current === null) {
232
+ countdownIntervalRef.current = setInterval(() => {
233
+ if (countdownStartTimeRef.current && autoSaveTimeoutRef.current) {
234
+ const elapsed = Date.now() - countdownStartTimeRef.current;
235
+ const remaining = Math.max(0, autoSaveDelay - elapsed);
236
+ const secondsRemaining = Math.ceil(remaining / 1000);
237
+
238
+ if (secondsRemaining <= 0) {
239
+ setCountdown(null);
240
+ if (countdownIntervalRef.current) {
241
+ clearInterval(countdownIntervalRef.current);
242
+ countdownIntervalRef.current = null;
243
+ }
244
+ } else {
245
+ setCountdown(secondsRemaining);
246
+ }
247
+ }
248
+ }, 1000);
249
+ }
250
+
251
+ return () => {
252
+ if (autoSaveTimeoutRef.current) {
253
+ clearTimeout(autoSaveTimeoutRef.current);
254
+ }
255
+ if (countdownIntervalRef.current) {
256
+ clearInterval(countdownIntervalRef.current);
257
+ }
258
+ };
259
+ }, [autoSaveEnabled, isDirty, autoSaveDelay]);
260
+
261
+ // Handle browser beforeunload event (page refresh/close)
262
+ useEffect(() => {
263
+ if (!isDirty) {
264
+ return;
265
+ }
266
+
267
+ const handleBeforeUnload = (e: BeforeUnloadEvent) => {
268
+ e.preventDefault();
269
+ // Modern browsers ignore custom messages, but we still need to set returnValue
270
+ e.returnValue = '';
271
+ return '';
272
+ };
273
+
274
+ window.addEventListener('beforeunload', handleBeforeUnload);
275
+
276
+ return () => {
277
+ window.removeEventListener('beforeunload', handleBeforeUnload);
278
+ };
279
+ }, [isDirty]);
280
+
281
+ // Handle link clicks (for Next.js App Router)
282
+ useEffect(() => {
283
+ if (!isDirty) {
284
+ return;
285
+ }
286
+
287
+ const handleLinkClick = (e: MouseEvent) => {
288
+ const target = e.target as HTMLElement;
289
+ const link = target.closest('a');
290
+
291
+ if (link && link.href) {
292
+ const url = new URL(link.href);
293
+ const currentPath = window.location.pathname;
294
+
295
+ // Check if navigating away from editor
296
+ if (!url.pathname.includes('/blog/editor') && !url.pathname.includes('/blog/new')) {
297
+ const confirmed = window.confirm(
298
+ 'You have unsaved changes. Are you sure you want to leave? Your changes will be lost.'
299
+ );
300
+
301
+ if (!confirmed) {
302
+ e.preventDefault();
303
+ e.stopPropagation();
304
+ return false;
305
+ }
306
+ }
307
+ }
308
+ };
309
+
310
+ // Listen for clicks on links
311
+ document.addEventListener('click', handleLinkClick, true);
312
+
313
+ return () => {
314
+ document.removeEventListener('click', handleLinkClick, true);
315
+ };
316
+ }, [isDirty]);
317
+
318
+ // Cleanup on unmount
319
+ useEffect(() => {
320
+ return () => {
321
+ if (autoSaveTimeoutRef.current) {
322
+ clearTimeout(autoSaveTimeoutRef.current);
323
+ }
324
+ if (countdownIntervalRef.current) {
325
+ clearInterval(countdownIntervalRef.current);
326
+ }
327
+ if (saveStatusTimeoutRef.current) {
328
+ clearTimeout(saveStatusTimeoutRef.current);
329
+ }
330
+ };
331
+ }, []);
332
+
333
+ return {
334
+ autoSaveEnabled,
335
+ setAutoSaveEnabled: setAutoSavePreference,
336
+ countdown,
337
+ saveStatus,
338
+ };
339
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Canvas Editor View Exports
3
+ */
4
+ export { CanvasEditorView } from './CanvasEditorView';
5
+ export type { CanvasEditorViewProps } from './CanvasEditorView';
6
+ export { EditorBody } from './EditorBody';
7
+ export { LayoutContainer } from './LayoutContainer';
8
+ export type { LayoutContainerProps } from './LayoutContainer';
9
+ export type { EditorBodyProps } from './EditorBody';
10
+ export { BlockWrapper } from './BlockWrapper';
11
+ export type { BlockWrapperProps } from './BlockWrapper';
12
+ export { EditorHeader } from './EditorHeader';
13
+ export type { EditorHeaderProps } from './EditorHeader';
14
+ export { SaveConfirmationModal } from './SaveConfirmationModal';
15
+ export type { SaveConfirmationModalProps } from './SaveConfirmationModal';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,YAAY,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,YAAY,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAChE,YAAY,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Canvas Editor View Exports
3
+ */
4
+
5
+ export { CanvasEditorView } from './CanvasEditorView';
6
+ export type { CanvasEditorViewProps } from './CanvasEditorView';
7
+ export { EditorBody } from './EditorBody';
8
+ export { LayoutContainer } from './LayoutContainer';
9
+ export type { LayoutContainerProps } from './LayoutContainer';
10
+ export type { EditorBodyProps } from './EditorBody';
11
+ export { BlockWrapper } from './BlockWrapper';
12
+ export type { BlockWrapperProps } from './BlockWrapper';
13
+ export { EditorHeader } from './EditorHeader';
14
+ export type { EditorHeaderProps } from './EditorHeader';
15
+ export { SaveConfirmationModal } from './SaveConfirmationModal';
16
+ export type { SaveConfirmationModalProps } from './SaveConfirmationModal';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Empty State Component
3
+ * Botanical-themed empty state for when no posts are found
4
+ */
5
+ export interface EmptyStateProps {
6
+ hasFilters: boolean;
7
+ onCreatePost: () => void;
8
+ }
9
+ export declare function EmptyState({ hasFilters, onCreatePost }: EmptyStateProps): import("react/jsx-runtime").JSX.Element;
10
+ //# sourceMappingURL=EmptyState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EmptyState.d.ts","sourceRoot":"","sources":["EmptyState.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,MAAM,WAAW,eAAe;IAC5B,UAAU,EAAE,OAAO,CAAC;IACpB,YAAY,EAAE,MAAM,IAAI,CAAC;CAC5B;AAED,wBAAgB,UAAU,CAAC,EAAE,UAAU,EAAE,YAAY,EAAE,EAAE,eAAe,2CAyBvE"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Empty State Component
3
+ * Botanical-themed empty state for when no posts are found
4
+ */
5
+
6
+ 'use client';
7
+
8
+ import React from 'react';
9
+ import { Sprout, Plus } from 'lucide-react';
10
+
11
+ export interface EmptyStateProps {
12
+ hasFilters: boolean;
13
+ onCreatePost: () => void;
14
+ }
15
+
16
+ export function EmptyState({ hasFilters, onCreatePost }: EmptyStateProps) {
17
+ return (
18
+ <div className="flex flex-col items-center justify-center py-20 px-8 bg-neutral-100 dark:bg-neutral-800/50 rounded-[2.5rem] border-2 border-dashed border-neutral-300 dark:border-neutral-700">
19
+ <div className="w-24 h-24 rounded-full bg-green-500/10 dark:bg-green-500/20 flex items-center justify-center mb-6">
20
+ <Sprout className="text-green-600 dark:text-green-400 size-12" />
21
+ </div>
22
+ <h3 className="text-xl font-black text-neutral-950 dark:text-white uppercase tracking-tight mb-2">
23
+ {hasFilters ? 'No Posts Found' : 'No Posts Yet'}
24
+ </h3>
25
+ <p className="text-sm text-neutral-500 dark:text-neutral-400 text-center mb-6 max-w-md">
26
+ {hasFilters
27
+ ? 'Try adjusting your search or filter criteria to find what you\'re looking for.'
28
+ : 'Start growing your content garden. Create your first blog post to share your botanical knowledge with the world.'}
29
+ </p>
30
+ {!hasFilters && (
31
+ <button
32
+ onClick={onCreatePost}
33
+ className="inline-flex items-center gap-2 px-6 py-3 bg-primary text-white rounded-full text-[10px] font-black uppercase tracking-widest hover:bg-primary/90 transition-all shadow-lg shadow-primary/20"
34
+ >
35
+ <Plus size={16} />
36
+ Create Your First Post
37
+ </button>
38
+ )}
39
+ </div>
40
+ );
41
+ }
42
+
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Post Actions Menu Component
3
+ * Three-dot menu with actions for each post
4
+ */
5
+ export interface PostActionsMenuProps {
6
+ onEdit: () => void;
7
+ onPreview: () => void;
8
+ onDuplicate: () => void;
9
+ onDelete: () => void;
10
+ }
11
+ export declare function PostActionsMenu({ onEdit, onPreview, onDuplicate, onDelete, }: PostActionsMenuProps): import("react/jsx-runtime").JSX.Element;
12
+ //# sourceMappingURL=PostActionsMenu.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostActionsMenu.d.ts","sourceRoot":"","sources":["PostActionsMenu.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,MAAM,WAAW,oBAAoB;IACjC,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,SAAS,EAAE,MAAM,IAAI,CAAC;IACtB,WAAW,EAAE,MAAM,IAAI,CAAC;IACxB,QAAQ,EAAE,MAAM,IAAI,CAAC;CACxB;AAED,wBAAgB,eAAe,CAAC,EAC5B,MAAM,EACN,SAAS,EACT,WAAW,EACX,QAAQ,GACX,EAAE,oBAAoB,2CAuFtB"}