Changeset 3289551
- Timestamp:
- 05/08/2025 06:06:49 AM (10 months ago)
- Location:
- free-weather/trunk
- Files:
-
- 4 edited
-
free-weather.php (modified) (1 diff)
-
public/css/weather-block-editor.css (modified) (1 diff)
-
public/js/weather-block-editor.js (modified) (6 diffs)
-
public/js/weather-public.js (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
free-weather/trunk/free-weather.php
r3288609 r3289551 538 538 } 539 539 540 541 function weather_enqueue_block_editor_assets() 542 { 543 wp_enqueue_script( 540 // 1. Register your block (with proper handles) 541 function weather_register_block() { 542 wp_register_script( 544 543 'weather-block-editor', 545 544 plugins_url('public/js/weather-block-editor.js', __FILE__), 546 array('wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-editor'),545 ['wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-editor'], 547 546 filemtime(plugin_dir_path(__FILE__) . 'public/js/weather-block-editor.js'), 548 547 true 549 548 ); 550 549 550 wp_register_style( 551 'weather-block-editor-style', 552 plugins_url('public/css/weather-block-editor.css', __FILE__), 553 [], 554 filemtime(plugin_dir_path(__FILE__) . 'public/css/weather-block-editor.css') 555 ); 556 557 wp_register_style( 558 'weather-public-style', 559 plugins_url('public/css/weather-public.css', __FILE__), 560 [], 561 filemtime(plugin_dir_path(__FILE__) . 'public/css/weather-public.css') 562 ); 563 564 register_block_type('weather/widget', [ 565 'editor_script' => 'weather-block-editor', 566 'editor_style' => 'weather-block-editor-style', 567 'style' => 'weather-public-style', // Shared on frontend 568 'render_callback' => 'weather_render_widget', 569 ]); 570 } 571 add_action('init', 'weather_register_block'); 572 // 2. Load weather-public.js on frontend AND iframe preview 573 function weather_enqueue_block_assets() { 551 574 wp_enqueue_script( 552 575 'weather-widget-runtime', 553 576 plugins_url('public/js/weather-public.js', __FILE__), 554 array('jquery'),577 ['jquery'], 555 578 filemtime(plugin_dir_path(__FILE__) . 'public/js/weather-public.js'), 556 579 true 557 580 ); 558 559 // If you have localizations to pass to your script 560 wp_localize_script('weather-block-editor', 'weatherLocalize', array( 581 } 582 add_action('enqueue_block_assets', 'weather_enqueue_block_assets'); 583 // 3. Optional: Load localized data for block editor only 584 function weather_enqueue_block_editor_localization() { 585 wp_localize_script('weather-block-editor', 'weatherLocalize', [ 561 586 'some_data' => 'Some value', 562 // Add other data here 563 )); 564 565 wp_enqueue_style( 566 'weather-style', 567 plugins_url('public/css/weather-public.css', __FILE__), 568 array(), 569 filemtime(plugin_dir_path(__FILE__) . 'public/css/weather-public.css') 570 ); 571 } 572 573 add_action('enqueue_block_editor_assets', 'weather_enqueue_block_editor_assets'); 574 575 576 function weather_register_block() 577 { 578 // Register the block editor script 579 wp_register_script( 580 'weather-block-editor', 581 plugins_url('public/js/weather-block-editor.js', __FILE__), 582 array('wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-editor'), 583 filemtime(plugin_dir_path(__FILE__) . 'public/js/weather-block-editor.js') 584 ); 585 586 // Enqueue the block editor style 587 wp_enqueue_style( 588 'weather-block-editor-style', 589 plugins_url('public/css/weather-block-editor.css', __FILE__), // Path to your custom CSS file 590 array(), 591 filemtime(plugin_dir_path(__FILE__) . 'public/css/weather-block-editor.css') 592 ); 593 594 // Register the block with a render callback for server-side rendering 595 register_block_type('weather/widget', array( 596 'editor_script' => 'weather-block-editor', 597 'render_callback' => 'weather_render_widget', // Specify the render callback function 598 )); 599 } 587 ]); 588 } 589 add_action('enqueue_block_editor_assets', 'weather_enqueue_block_editor_localization'); 600 590 601 591 // The render callback function to generate the block's HTML based on attributes -
free-weather/trunk/public/css/weather-block-editor.css
r3288609 r3289551 38 38 font-weight: normal; 39 39 color: #555; 40 margin -bottom: 8px;40 margin: 0 0 8px 0; 41 41 } 42 42 .wp-block[data-type="weather/widget"] .components-panel__body-title, -
free-weather/trunk/public/js/weather-block-editor.js
r3288609 r3289551 15 15 props.setAttributes(obj); 16 16 setTimeout(() => { 17 // fire in parent (just in case) 18 window.dispatchEvent(new Event('weather-widget-refresh')); 19 20 // fire in every Gutenberg preview iframe 21 document.querySelectorAll('iframe').forEach(frame => { 22 try { 23 frame.contentWindow.dispatchEvent(new Event('weather-widget-refresh')); 24 } catch (e) { 25 /* cross‑origin or inaccessible frame — skip */ 26 } 27 }); 28 }, 2000); 17 refresh_widget(); 18 }, 300); 19 } 20 21 function refresh_widget(){ 22 // fire in parent (just in case) 23 window.dispatchEvent(new Event('weather-widget-refresh')); 24 25 // fire in every Gutenberg preview iframe 26 document.querySelectorAll('iframe').forEach(frame => { 27 try { 28 frame.contentWindow.dispatchEvent(new Event('weather-widget-refresh')); 29 } catch (e) { 30 /* cross‑origin or inaccessible frame — skip */ 31 } 32 }); 29 33 } 30 34 … … 50 54 backgroundColor: { 51 55 type: 'string', 52 default: ' #becffb',56 default: 'linear-gradient(to left,#d3dfff,#fbfcff)', 53 57 }, 54 58 widgetWidth: { … … 65 69 }, 66 70 showSunrise: { 67 type: ' boolean',68 default: false,71 type: 'string', 72 default: '', 69 73 }, 70 74 showWind: { 71 type: ' boolean',72 default: false,75 type: 'string', 76 default: '', 73 77 }, 74 78 language: { … … 77 81 }, 78 82 showCurrent: { 79 type: ' boolean',80 default: true,83 type: 'string', 84 default: 'on', 81 85 }, 82 86 }, 83 87 84 88 edit: function(props) { 89 setTimeout(() => { 90 refresh_widget(); 91 }, 300); 92 setTimeout(() => { 93 refresh_widget(); 94 }, 2500); 85 95 return el( 86 96 'div', {}, … … 136 146 checked: props.attributes.showCurrent, 137 147 onChange: function(newValue) { 138 props.setAttributes({ showCurrent: newValue});139 setAttrAndRefresh(props, { showCurrent: newValue})148 props.setAttributes({ showCurrent: newValue ? 'on' : '' }); 149 setAttrAndRefresh(props, { showCurrent: newValue ? 'on' : '' }) 140 150 } 141 151 }), … … 218 228 ) 219 229 ), 220 // ... Your block preview or other components here 230 el('div', { 231 className: 'preview-weather-widget weather-widget weather_widget_wrap', 232 'data-text-color': props.attributes.textColor, 233 'data-background': props.attributes.backgroundColor, 234 'data-width': props.attributes.widgetWidth, 235 'data-days': props.attributes.days, 236 'data-sunrise': props.attributes.showSunrise, 237 'data-wind': props.attributes.showWind, 238 'data-current': props.attributes.showCurrent, 239 'data-language': props.attributes.language, 240 'data-city': props.attributes.city, 241 'data-country': props.attributes.country, 242 style: { 243 backgroundColor: props.attributes.backgroundColor, 244 color: props.attributes.textColor, 245 padding: '10px', 246 marginTop: '20px', 247 border: '1px dashed #ccc' 248 } 249 }, 250 el('div', { 251 className: 'weather_widget_placeholder' 252 }), 253 el('div', { 254 style: { 255 fontSize: '14px', 256 textAlign: 'center', 257 paddingTop: '6px', 258 paddingBottom: '4px', 259 background: 'rgba(0,0,0,0.03)' 260 } 261 }, 262 'Data from ', 263 el('a', { 264 target: '_blank', 265 href: 'https://www.weather25.com' 266 }, 'weather25.com') 267 ) 268 ) 221 269 ); 222 270 }, -
free-weather/trunk/public/js/weather-public.js
r3288823 r3289551 52 52 * ----------------------------------------------------------- */ 53 53 function initWidget($w) { 54 const city = $w. data('city');55 const country = $w. data('country');56 const language = $w. data('language') || 'english';57 const days = parseInt($w. data('days'), 10) || 0;58 const widthPref = $w. data('width') === 'tight' ? 'tight' : 'maxwidth';59 const showCur = $w. data('current') === 'on';60 const showWind = $w. data('wind') === 'on';61 const showSun = $w. data('sunrise') === 'on';62 const bg = $w. data('background') || 'linear-gradient(to left,#d3dfff,#fbfcff)';63 const fg = $w. data('text-color') || 'black';54 const city = $w.attr('data-city'); 55 const country = $w.attr('data-country'); 56 const language = $w.attr('data-language') || 'english'; 57 const days = parseInt($w.attr('data-days'), 10) || 0; 58 const widthPref = $w.attr('data-width') === 'tight' ? 'tight' : 'maxwidth'; 59 const showCur = $w.attr('data-current') === 'on'; 60 const showWind = $w.attr('data-wind') === 'on'; 61 const showSun = $w.attr('data-sunrise') === 'on'; 62 const bg = $w.attr('data-background') || 'linear-gradient(to left,#d3dfff,#fbfcff)'; 63 const fg = $w.attr('data-text-color') || 'black'; 64 64 const place = city.replace(' ', '_') + ',' + country; 65 65 … … 73 73 $w.attr('data-render-hash', hash); 74 74 75 $w.removeClass('tight maxwidth').addClass(widthPref); 76 75 77 $.getJSON( 76 78 'https://www.weatherwp.com/api/common/publicWeatherForLocation.php', … … 80 82 function draw(r) { 81 83 const wrap = $('<div>', { 82 class : `main_wrap ${widthPref}`,84 class : `main_wrap`, 83 85 css : { background: bg, color: fg } 84 86 }); … … 157 159 link.append(wrap); 158 160 159 $w.find('.weather_widget_placeholder').replaceWith(link); 161 $w.find('.weather_widget_placeholder') 162 .empty() 163 .append(link); 160 164 } 161 165 } … … 166 170 function refresh() { 167 171 $('.weather-widget').each(function () { initWidget($(this)); }); 172 $('.preview-weather-widget.weather-widget').each(function () { 173 initWidget($(this)); 174 }); 175 document.querySelectorAll('iframe').forEach(frame => { 176 try { 177 const doc = frame.contentDocument || frame.contentWindow.document; 178 if (!doc) return; 179 180 $(doc).find('.preview-weather-widget.weather-widget').each(function () { 181 initWidget($(this)); 182 }); 183 } catch (e) { 184 // Cross-origin or inaccessible iframe — safely skip 185 // console.warn('Skipping iframe due to CORS or sandbox:', e); 186 } 187 }); 168 188 } 169 189 window.refreshMeteoWidgets = refresh;
Note: See TracChangeset
for help on using the changeset viewer.