recommendations-pages
1 year ago
recommendations-templates
1 year ago
supported-themes-templates
4 years ago
default-image.png
4 years ago
default-site.dat
1 year ago
global-data.json
2 years ago
settings.php
4 years ago
style-manager-web-worker-template.js
1 month ago
style-manager-web-worker-template.js
115 lines
| 1 | // defaults and polyfills |
| 2 | // eslint-disable-next-line no-undef |
| 3 | window = self; |
| 4 | // eslint-disable-next-line no-undef |
| 5 | top = self; |
| 6 | const kubioNoop = function () {}; |
| 7 | |
| 8 | document = { |
| 9 | createElement() { |
| 10 | return { |
| 11 | style: [], |
| 12 | setAttribute: kubioNoop, |
| 13 | attachEvent: kubioNoop, |
| 14 | appendChild: kubioNoop, |
| 15 | }; |
| 16 | }, |
| 17 | createTextNode: kubioNoop, |
| 18 | attachEvent: kubioNoop, |
| 19 | addEventListener: kubioNoop, |
| 20 | querySelectorAll() { |
| 21 | return []; |
| 22 | }, |
| 23 | }; |
| 24 | const emptyNodeStructure = { |
| 25 | addEventListener: kubioNoop, |
| 26 | appendChild: kubioNoop, |
| 27 | querySelectorAll() { |
| 28 | return []; |
| 29 | }, |
| 30 | querySelector() { |
| 31 | return null; |
| 32 | }, |
| 33 | }; |
| 34 | document.head = emptyNodeStructure; |
| 35 | document.body = emptyNodeStructure; |
| 36 | |
| 37 | // wp imported scripts need to load kubio-style-manager |
| 38 | // {{{importScriptsPlaceholder}}} |
| 39 | |
| 40 | const fonts = {}; |
| 41 | |
| 42 | // eslint-disable-next-line no-undef |
| 43 | wp.hooks.addAction( |
| 44 | 'kubio.google-fonts.load', |
| 45 | 'kubio.google-fonts.load', |
| 46 | function ( nextFonts ) { |
| 47 | nextFonts.forEach( function ( font ) { |
| 48 | fonts[ font.family ] = fonts[ font.family ] || []; |
| 49 | // eslint-disable-next-line no-undef |
| 50 | fonts[ font.family ] = lodash.uniq( |
| 51 | fonts[ font.family ].concat( font.variants ) |
| 52 | ); |
| 53 | } ); |
| 54 | } |
| 55 | ); |
| 56 | |
| 57 | const renderStyle = function ( payload ) { |
| 58 | // eslint-disable-next-line no-undef |
| 59 | const dynamicStyle = lodash.get( payload.data, 'dynamicStyle', {} ); |
| 60 | |
| 61 | // eslint-disable-next-line no-undef |
| 62 | const renderer = new kubio.styleManager.BlockStyleRender( |
| 63 | // eslint-disable-next-line no-undef |
| 64 | lodash.omit( payload.data, 'dynamicStyle' ), |
| 65 | payload.parentDetails, |
| 66 | payload.canUseHtml, |
| 67 | payload.document || null |
| 68 | ); |
| 69 | |
| 70 | const styleRef = renderer.model ? renderer.model.styleRef : null; |
| 71 | const localId = renderer.model ? renderer.model.id : null; |
| 72 | |
| 73 | return { |
| 74 | css: renderer.export(), |
| 75 | dynamicRules: renderer.exportDynamicStyle( dynamicStyle ), |
| 76 | styleRef, |
| 77 | localId, |
| 78 | responseHash: payload.hash, |
| 79 | fonts: Object.keys( fonts ).map( ( family ) => ( { |
| 80 | family, |
| 81 | variants: fonts[ family ], |
| 82 | } ) ), |
| 83 | }; |
| 84 | }; |
| 85 | |
| 86 | // actual web worker runner |
| 87 | // eslint-disable-next-line no-undef |
| 88 | self.addEventListener( 'message', ( event ) => { |
| 89 | const action = event.data.action; |
| 90 | const hash = event.data.hash; |
| 91 | const payload = lodash.isObject( event.data.payload ) |
| 92 | ? event.data.payload |
| 93 | : JSON.parse( event.data.payload ); |
| 94 | |
| 95 | let response = null; |
| 96 | |
| 97 | switch ( action ) { |
| 98 | case 'EXPORT_CSS': |
| 99 | response = renderStyle( payload ); |
| 100 | break; |
| 101 | case 'TEST': |
| 102 | response = 'test'; |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | // eslint-disable-next-line no-undef |
| 107 | self.postMessage( { |
| 108 | hash, |
| 109 | payload: response, |
| 110 | } ); |
| 111 | } ); |
| 112 | |
| 113 | // eslint-disable-next-line no-undef |
| 114 | self.postMessage( 'WORKER_LOADED' ); |
| 115 |