-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HypeDataMagic.min.js
30 lines (30 loc) · 13.1 KB
/
HypeDataMagic.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Hype DataMagic 1.4.1
copyright (c) 2024 Max Ziebell, (https://maxziebell.de). MIT-license
*/
!1==="HypeDataMagic"in window&&(window.HypeDataMagic=function(){function w(a,b){return"string"===typeof a?x(a,b):"object"===typeof a?u(a,b):a}function y(a,b){return Object.assign({},f.variables||a.customData,{resourcesFolderName:a.resourcesFolderURL()},b&&f.allowDatasets?{dataset:K(b)}:null)}function L(a,b,c){if(c.handler)if("()"==c.handler.slice(-2)){if(!n&&a.functions){try{var d=a.functions()[c.handler.slice(0,-2)](a,b,c)}catch(e){console.log('There was an error in your handler "'+c.handler+'": ',
e)}return d}}else if("object"==typeof l[c.handler]){if("function"==typeof l[c.handler][c.type]){try{d=l[c.handler][c.type](a,b,c)}catch(e){console.log('There was an error in your handler "'+c.handler+'": ',e)}return d}if(n&&"function"==typeof l[c.handler].DataMagicPrepareForDisplay){try{d=l[c.handler].DataMagicPrepareForDisplay(a,b,c)}catch(e){console.log('There was an error in your handler "'+c.handler+'": ',e)}return d}}}function E(a,b,c){if(b.getAttribute("data-magic-key")){var d=M(b.getAttribute("data-magic-key")).split(":"),
e=d[1]?d[1]:d[0],g=(d[1]?d[0].trim():null)||z(b,"data-magic-source")||f.source,h="customData"==g?a.customData:F(g);if(h)if(h=(d=d[1]?"":z(b,"data-magic-branch",!0,b.closest("[data-magic-source]")))?p(h,d):h,h=p(h,e),null!=h){if("object"!=typeof h&&"function"!=typeof h){d=b.getAttribute("data-magic-prefix")||"";var N=b.getAttribute("data-magic-append")||"";if(d||N)h=d+h+N}f.autoVariables&&f.allowVariables&&(d=y(a,b),h=w(h,d));c=Object.assign({},c,{data:h,source:g,key:e});e=c.type?c.type.replace("HypeScene",
"DataMagic").split():n?["DataMagicPreviewUpdate"]:["DataMagicPrepareForDisplay","DataMagicLoad"];var V=(b.getAttribute("data-magic-handler")||f.handler).split(",");e.forEach(function(W){var O;V.forEach(function(X){O=L(a,b,Object.assign({},c,O,{type:W,handler:X.trim()}))})})}else A(a,b)}}function A(a,b,c){c=Object.assign({},c);handlers=(c.oldHandler||b.getAttribute("data-magic-handler")||f.handler).split(",");var d;handlers.forEach(function(e){d=L(a,b,Object.assign({},c,d,{type:"DataMagicUnload",handler:e.trim()}))})}
function P(a){return!a.querySelectorAll(".HYPE_element, .HYPE_element_container").length}function Q(a,b){q[a.documentId()]||(q[a.documentId()]={changeObserver:new MutationObserver(function(c){c.forEach(function(d){if("attributes"==d.type){var e=d.target,g=d.attributeName,h=M(e.getAttribute(g));d=d.oldValue;if(h!=d)switch(g){case "data-magic-key":h?E(a,e):A(a,e);break;case "data-magic-source":case "data-magic-branch":A(a,e);k(a,e);break;case "data-magic-handler":A(a,e,{oldHandler:d});k(a,e);break;
case "data-magic-prefix":case "data-magic-append":k(a,e)}}})}),options:{subtree:!0,attributes:!0,attributeFilter:"data-magic-key data-magic-source data-magic-branch data-magic-handler data-magic-prefix data-magic-append".split(" "),attributeOldValue:!0},enable:function(){this.changeObserver.observe(b,this.options)},disable:function(){this.changeObserver.disconnect()}})}function B(a){q[a.documentId()]&&q[a.documentId()].enable()}function G(a){q[a.documentId()]&&q[a.documentId()].disable()}function R(a,
b,c){b&&b.querySelectorAll("[data-magic-key]").forEach(function(d){E(a,d,c);(f.forceRedrawElement||!n&&f.forceRedrawElementNonIDE)&&H(d)})}function v(a,b,c){b&&(E(a,b,c),(1==f.forceRedrawElement||!n&&f.forceRedrawElementNonIDE)&&H(b))}function k(a,b,c){b&&(v(a,b,c),R(a,b,c),(1==f.forceRedrawDocument||!n&&f.forceRedrawDocumentNonIDE)&&H(b))}function F(a,b){if(f.sourceRedirect[a])return r[f.sourceRedirect[a]]||null;a||(a=f.source);var c=r[a]||null;return c&&b?p(c,b):c}function S(a){a&&a.hasOwnProperty("refresh")?
a.refresh():window.hasOwnProperty("HYPE")&&Object.values(window.HYPE.documents).forEach(function(b){b.hasOwnProperty("refresh")&&b.refresh()})}function M(a){if("string"==typeof a)return a.trim()}function C(a){if(Array.isArray(a))return a.reduce(function(b,c){return b.concat(C(c))},[]);if("string"==typeof a)return a=a.replace(/\[(\d+)\]/g,function(b,c){return"."+parseInt(c)}),a=a.replace(/^\./,""),a.split(".")}function p(a,b){if("object"==typeof a){for(var c=C(b),d=a,e=0;void 0!==d&&e<c.length;)d=
d[c[e]],f.allowDataFunctions&&"function"===typeof d&&(d=d()),e++;return d}}function u(a,b,c){"object"===typeof a?(c||(a=I(a)),Object.keys(a).forEach(function(d){a[d]=u(a[d],b,!0)})):"function"===typeof a?a[key]=u(a[key](),b,!0):"string"===typeof a&&(a=x(a,b));return a}function x(a,b){if("string"===typeof a){var c=a.match(/\${.*?}|%{.*?}|\u2728{.*?}/g);c&&c.forEach(function(d){var e=d.replace(/\$\{|%\{|\u2728\{|\}|\(\)/g,"").split(":"),g=e[1]?e[1]:e[0];e=e[1]?F(e[0].trim()):null;g=p(e||b,g);a=a.replace(d,
g)})}return a}function I(a){if(null==a||"object"!=typeof a)return a;var b=a.constructor(),c;for(c in a)a.hasOwnProperty(c)&&(b[c]=I(a[c]));return b}function z(a,b,c,d){if(!a)return null;for(var e="";null!==a;){var g=a.getAttribute(b);if(null!==g)if(c&&0==g.indexOf("+"))e=g.slice(1)+(e?"."+e:"");else return g+(e?"."+e:"");(a=a.parentNode.closest("["+b+"]"))&&d&&!d.contains(a)&&(a=null)}}function K(a){var b=Object.assign({},a.dataset);if(f.allowMagicSets&&b.magicSets)for(var c=b.magicSets.split(","),
d=0;d<c.length;){var e=c[d].trim();switch(e){case "parent":(e=a.parentElement.parentElement)&&D(b,e.dataset);break;case "parents":for(e=a.parentElement;e&&!e.classList.contains("HYPE_scene");)D(b,e.dataset),e=e.parentElement;break;default:0===e.indexOf("closest(")&&e.indexOf(")")===e.length-1?(e=e.substring(8,e.length-1),(e=a.closest(e))&&D(b,e.dataset)):document.querySelectorAll(e).forEach(function(g){D(b,g.dataset)})}d++}return b}function D(a,b){for(var c in b)a.hasOwnProperty(c)||(a[c]=b[c])}function t(a){return function(){if(!a.timeout){var b=
arguments;a.timeout=requestAnimationFrame(function(){a.apply(this,b);a.timeout=null}.bind(this))}}}function T(a,b,c){a.refresh=function(d){k(this,d||document.getElementById(this.currentSceneId()))};a.refreshDebounced=t(a.refresh);a.refreshDescendants=function(d){R(this,d||document.getElementById(this.currentSceneId()))};a.refreshDescendantsDebounced=t(a.refreshDescendants);a.refreshElement=function(d){v(this,d)};a.refreshElementDebounced=t(a.refreshElement);a.disableChangeObserver=function(){G(this)};
a.enableChangeObserver=function(){B(this)};a.setContentIfNecessary=function(d,e){d.innerHTML!=e&&(d.innerHTML=e)};f.customData&&(a.customData=Object.assign(a.customData,f.customData));n||(Q(a,b),B(a))}var n=-1!=window.location.href.indexOf("/Hype/Scratch/HypeScratch."),r={},q={},f={source:"shared",fallbackImage:function(){return"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="},handler:"text",variables:null,handlerMixin:{},sourceRedirect:{},
refreshOnSetData:!0,refreshOnCustomData:!0,forceRedrawElement:!1,forceRedrawElementNonIDE:!1,forceRedrawDocument:!1,forceRedrawDocumentNonIDE:!1,highlightDataMagic:!0,allowDataFunctions:!0,allowVariables:!0,allowDatasets:!0,allowMagicSets:!0,autoVariables:!0,resourcesFolderNameForPreview:""},H=function(a){var b=a.style.display;a.style.display="none";void 0!=a.offsetHeight;a.style.display=b},l={text:{DataMagicPrepareForDisplay:function(a,b,c){b.innerHTML!=c.data&&P(b)&&(!f.autoVariables&&f.allowVariables&&
(a=y(a,b),c.data=w(c.data,a)),b.innerHTML=c.data)},DataMagicUnload:function(a,b,c){P(b)&&(b.innerHTML="")}},variables:{DataMagicPrepareForDisplay:function(a,b,c){f.allowVariables&&(c.data=x(c.data,f.variables||a.customData));return c}},dataset:{DataMagicPrepareForDisplay:function(a,b,c){b&&f.allowDatasets&&(c.data=u(c.data,{dataset:K(b)}));return c}},image:{DataMagicPrepareForDisplay:function(a,b,c){"string"==typeof c.data&&(c.data={src:c.data});c.data.src||(c.data.src=b.dataset.fallbackImage||f.fallbackImage());
if(!f.autoVariables&&f.allowVariables){var d=y(a,b);c.data=w(c.data,d)}b.innerHTML="";a.getElementProperty(b,"background-image")!=c.data.src&&(b.style.backgroundRepeat="no-repeat",b.style.backgroundPosition=c.data.backgroundPosition||b.dataset.backgroundPosition||"center center",b.style.backgroundSize=c.data.backgroundSize||b.dataset.backgroundSize||"contain",a.setElementProperty(b,"background-image",c.data.src))},DataMagicUnload:function(a,b,c){a.setElementProperty(b,"background-image","");b.style.backgroundRepeat=
b.style.backgroundPosition=""}}};t(k);var U=t(S);!1==="HYPE_eventListeners"in window&&(window.HYPE_eventListeners=[]);window.HYPE_eventListeners.push({type:"HypeDocumentLoad",callback:T});window.HYPE_eventListeners.push({type:"HypeScenePrepareForDisplay",callback:function(a,b,c){G(a);k(a,b,c)}});window.HYPE_eventListeners.push({type:"HypeSceneLoad",callback:function(a,b,c){k(a,b,c);B(a)}});window.HYPE_eventListeners.push({type:"HypeSceneUnload",callback:function(a,b,c){G(a)}});if(n){var m=new Proxy({getElementProperty:function(a,
b){return a.style.getPropertyValue(b)||null},setElementProperty:function(a,b,c){if(null!==c)switch(b){case "background-image":c="url("+c+")"}a.style.setProperty(b,c,"important")},documentId:function(){return"HypeSceneEditor"},resourcesFolderURL:function(){return f.resourcesFolderNameForPreview||window.location.href.replace(/\/$/,"")}},{get:function(a,b){return"customData"===b?f.customDataForPreview||f.customData||{}:a[b]}});T(m,document.documentElement);(new MutationObserver(function(a){a.forEach(function(b){var c=
b.target;"attributes"===b.type&&c.parentNode&&"contenteditable"==b.attributeName&&(c.parentNode.hasAttribute("data-magic-key")?"false"==c.getAttribute("contenteditable")&&"true"==b.oldValue?setTimeout(function(){c.parentNode.removeAttribute("magic-edit");v(m,c.parentNode)},1):(c.parentNode.setAttribute("magic-edit","preview"),setTimeout(function(){var d=z(c.parentNode,"data-magic-branch",!0),e="\x3c!-- Hype Data magic: This is only a preview placeholder and edits are ignored!";d&&(e+=' This key resides on the branch "'+
d+'"');c.innerHTML=e+" --\x3e\n"+c.parentNode.getAttribute("data-magic-key")},1)):c.parentNode.querySelectorAll("[data-magic-key]").length&&("false"==c.getAttribute("contenteditable")&&"true"==b.oldValue?setTimeout(function(){c.parentNode.removeAttribute("magic-edit");c.parentNode.querySelectorAll("[data-magic-key]").forEach(function(d){v(m,d)})},1):(c.parentNode.setAttribute("magic-edit","innerHTML"),setTimeout(function(){c.parentNode.querySelectorAll("[data-magic-key]").forEach(function(d){d.innerHTML=
d.getAttribute("data-magic-key")})},1))))})})).observe(document.documentElement,{attributes:!0,attributeOldValue:!0,attributeFilter:["contenteditable"],subtree:!0});var J;(new MutationObserver(function(a){J||2==a.length&&"HypeMainContentDiv"==a[0].target.id&&(J=setTimeout(function(){J=null;k(m,document.documentElement)},1))})).observe(document.documentElement,{childList:!0,subtree:!0});_updates={};(new MutationObserver(function(a){a.forEach(function(b){var c=b.target.getAttribute(b.attributeName);
if(c&&b.oldValue==c&&-1!=c.indexOf("data-magic")){var d=b.target.getAttribute("hypeobjectid");_updates[d]||(_updates[d]=setTimeout(function(){v(m,b.target);delete _updates[d]},1))}})})).observe(document.documentElement,{attributes:!0,attributeOldValue:!0,attributeFilter:["hypeattributelastkeysplist"],subtree:!0});window.addEventListener("DOMContentLoaded",function(a){document.addEventListener("focusout",function(b){b.target.parentNode.hasAttribute("magic-edit")&&b.target.hasAttribute("contenteditable")&&
(b.target.parentNode.hasAttribute("data-magic-key")?b.target&&(b.target.innerHTML=""):"innerHTML"==b.target.parentNode.getAttribute("magic-edit")&&(b=b.target.querySelectorAll("[data-magic-key]"),b.length&&b.forEach(function(c){c.innerHTML=""})))});document.addEventListener("visibilitychange",function(b){document.hidden||k(m,document.documentElement)},!1);setTimeout(function(){k(m,document.documentElement);Q(m,document.documentElement);B(m)},1);a=['[contenteditable="true"] [data-magic-key], [magic-edit="preview"] [contenteditable="true"] {opacity:0.5}',
(f.highlightDataMagic?"[data-magic-key]::before":"[magic-edit]::before")+" {position: absolute; content: attr(data-magic-key); z-index: 10; height: 16px; line-height:16px; padding: 3px 5px 3px 5px; top: -16px; left: 0px; text-align: center; font: 9px Arial; color: white; background: #75A4EA; border-top-right-radius: 0.2rem; border-top-left-radius: 0.2rem; box-sizing: border-box;}"];f.highlightDataMagic&&a.push('[data-magic-key]:after {content: " "; position: absolute; z-index: -1; top: 0px; left: 0px; right: 0px; bottom: 0px; border: 1px solid #75A4EA;}');
a.forEach(function(b){return document.styleSheets[0].insertRule(b,0)});window.getSelection().removeAllRanges()})}return{version:"1.4.1",setData:function(a,b,c){b=b||f.source;if(c){r[b]||(r[b]={});var d=C(c);c=d.pop();(b=p(r[b],d))&&(b[c]=a)}else r[b]=a;1==f.refreshOnSetData&&U()},getData:F,refresh:S,refreshDebounced:U,setDefault:function(a,b){f[a]=b},getDefault:function(a){return f[a]},addDataHandler:function(a,b){if("string"!=!typeof a)switch(typeof b){case "object":l[a]=Object.assign({},f.handlerMixin,
b);break;case "function":l[a]=Object.assign({},f.handlerMixin,l.text),l[a].DataMagicPrepareForDisplay=b}},resolveObjectByKey:p,resolveKeyToArray:C,resolveVariablesInString:x,resolveVariablesInObject:u,cloneObject:I,findAttribute:z,resolveVariables:w,constructVariablesContext:y,debounceByRequestFrame:t}}());