PluginProbe
Extendify / 2.2.0
Extendify v2.2.0
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 0.7.0 All 126 releases
extendify / src / Agent / hooks / useVariationOverride.js

useVariationOverride.js in Extendify 2.2.0, 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 { parse } from '@wordpress/blocks';
2 import { useSelect } from '@wordpress/data';
3 import { useEffect, useRef, useState } from '@wordpress/element';
4 import { getDynamicDuotoneMap } from '@agent/lib/svg-blocks-scanner';
5 import { replaceDuotoneSVG } from '@agent/lib/svg-helpers';
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 }, [duotoneTheme, dynamicDuotone]);
87
88 return {
89 undoChange: () => {
90 // Revert duotone changes
91 if (duotoneCleanup.current) {
92 duotoneCleanup.current();
93 duotoneCleanup.current = null;
94 }
95
96 // Revert CSS changes
97 const style = document.getElementById(id);
98 if (style && frontStyles.current) {
99 style.innerHTML = frontStyles.current;
100 }
101
102 // Remove editor CSS
103 if (!onEditor) return;
104 const iframe = document.querySelector('iframe[name="editor-canvas"]');
105 const doc = iframe?.contentDocument || document;
106 doc?.getElementById(id)?.remove();
107 },
108 };
109 };
110