PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.3
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.3
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / src / hooks / useEditorSaveStatus.js

useEditorSaveStatus.js in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.3, at src/hooks/useEditorSaveStatus.js

57 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useState, useEffect, useRef } from "@wordpress/element";
2 import { select, subscribe } from "@wordpress/data";
3
4 /**
5 * useEditorSaveStatus
6 *
7 * Detects if *any* entity (post, page, template, widget, etc.)
8 * is currently being saved — works in Site Editor, Post Editor, and Widgets.
9 *
10 * @return {Object} { isSaving, isAutosaving }
11 */
12 export const useEditorSaveStatus = () => {
13 const [isSaving, setIsSaving] = useState(false);
14 const [isAutosaving, setIsAutosaving] = useState(false);
15 const prevSavingRef = useRef(false);
16
17 useEffect(() => {
18 const unsubscribe = subscribe(() => {
19 const core = select("core");
20 if (!core) {
21 return;
22 }
23
24 const { __experimentalGetDirtyEntityRecords, isSavingEntityRecord } = core;
25
26 if (typeof __experimentalGetDirtyEntityRecords !== "function") {
27 return;
28 }
29
30 const dirtyEntities = __experimentalGetDirtyEntityRecords();
31
32 // Check if any entity is saving
33 const isNowSaving = dirtyEntities.some((record) =>
34 isSavingEntityRecord(record.kind, record.name, record.key)
35 );
36
37 // Detect autosaving from post editor if available
38 const coreEditor = select("core/editor");
39 const isNowAutosaving =
40 typeof coreEditor?.isAutosavingPost === "function" ? coreEditor.isAutosavingPost() : false;
41
42 // Update only when changed
43 if (prevSavingRef.current !== isNowSaving) {
44 setIsSaving(isNowSaving);
45 prevSavingRef.current = isNowSaving;
46 }
47 setIsAutosaving(isNowAutosaving);
48 });
49
50 return () => {
51 unsubscribe();
52 };
53 }, []);
54
55 return { isSaving, isAutosaving };
56 };
57