PluginProbe
Extendify / 3.0.5
Extendify v3.0.5
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Agent / hooks / useVariationOverride.js

useVariationOverride.js in Extendify 3.0.5, at src/Agent/hooks/useVariationOverride.js

110 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { getDynamicDuotoneMap } from '@agent/lib/svg-blocks-scanner';
2 import { replaceDuotoneSVG } from '@agent/lib/svg-helpers';
3 import { parse } from '@wordpress/blocks';
4 import { useSelect } from '@wordpress/data';
5 import { useCallback, useEffect, useRef, useState } from '@wordpress/element';
6
7 const id = 'global-styles-inline-css';
8 const path = window.location.pathname;
9 const s = new URLSearchParams(window.location.search);
10 const onEditor =
11 path.includes('/wp-admin/post.php') && s.get('action') === 'edit';
12
13 export const useVariationOverride = ({ css, duotoneTheme }) => {
14 const frontStyles = useRef(null);
15 const duotoneCleanup = useRef(null);
16 const [theDocument, setDocument] = useState(null);
17
18 useEffect(() => {
19 if (!css || onEditor) return;
20 const style = document.getElementById(id);
21 if (!style) return;
22 if (!frontStyles.current) {
23 frontStyles.current = style.innerHTML;
24 }
25 style.innerHTML = css;
26 }, [css]);
27
28 // Handle the editor
29 useEffect(() => {
30 if (!css || !theDocument || !onEditor) return;
31 const style = theDocument.getElementById(id);
32 const hasIframe = document.querySelector('iframe[name="editor-canvas"]');
33 style.innerHTML = css.replaceAll(
34 ':root',
35 // If the iframe was removed, target the editor the old way
36 hasIframe ? ':root' : '.editor-styles-wrapper',
37 );
38 }, [css, theDocument]);
39
40 useEffect(() => {
41 if (theDocument) return;
42 const timer = setTimeout(() => {
43 if (theDocument) return;
44 const frame = document.querySelector('iframe[name="editor-canvas"]');
45 const doc = frame?.contentDocument || document;
46 if (!doc || !doc.body) return;
47 // Add a tag to the body
48 const newStyle = doc.createElement('style');
49 newStyle.id = id;
50 doc.body.appendChild(newStyle);
51 setDocument(doc);
52 }, 300); // wait for iframe
53 return () => clearTimeout(timer);
54 }, [theDocument]);
55
56 const dynamicDuotone = useSelect((select) => {
57 let blocks = select('core/block-editor')?.getBlocks?.() ?? [];
58
59 const hasShowTemplateOn = blocks.find(
60 (block) => block.name === 'core/template-part',
61 );
62
63 if (hasShowTemplateOn) {
64 const { getEditedPostContent } = select('core/editor');
65 blocks = parse(getEditedPostContent(), {});
66 }
67
68 return getDynamicDuotoneMap(blocks);
69 }, []);
70
71 // Handle duotone changes
72 useEffect(() => {
73 if (!duotoneTheme) return;
74
75 // Clean up previous duotone changes
76 if (duotoneCleanup.current) {
77 duotoneCleanup.current();
78 duotoneCleanup.current = null;
79 }
80
81 // Apply new duotone changes and store cleanup
82 duotoneCleanup.current = replaceDuotoneSVG({
83 duotoneTheme,
84 dynamicDuotone,
85 });
86 }, [css, duotoneTheme, dynamicDuotone]);
87
88 const undoChange = useCallback(() => {
89 // Revert duotone changes
90 if (duotoneCleanup.current) {
91 duotoneCleanup.current();
92 duotoneCleanup.current = null;
93 }
94
95 // Revert CSS changes
96 const style = document.getElementById(id);
97 if (style && frontStyles.current) {
98 style.innerHTML = frontStyles.current;
99 }
100
101 // Remove editor CSS
102 if (!onEditor) return;
103 const iframe = document.querySelector('iframe[name="editor-canvas"]');
104 const doc = iframe?.contentDocument || document;
105 doc?.getElementById(id)?.remove();
106 }, []);
107
108 return { undoChange };
109 };
110