{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-livelyenergy/src/components/widgets/lively-script.js"],"names":["Morph","boundEval","_","generateUuid","uuid","waitForDeepProperty","stripErrorString","currentScriptPromises","moduleMap","Map","LivelyScript","initialize","attachedCallback","src","textContent","get","innerHTML","id","result","isError","value","replace","then","HTMLElement","appendChild","undefined","moduleFor","obj","moduleName","container","lively","query","livelyScriptContainerId","getURL","lively4url","toString","set","str","worldContext","findWorldContext","console","warn","targetModule","resolveMe","length","last","myPromise","Promise","resolve","push","log","myPromisedResult","first","shift","Error","livelyExample"],"mappings":";;;;;;;AAAOA,W;;AACAC,e;;AACAC,O;;AACUC,kB,UAARC,I;AAAsBC,yB,UAAAA,mB;;AAEvBC,sB,2CAAAA,gB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGR;;;;;;;;;AASO,UAAIC,wBAAwB,EAA5B;;;;;;;;;;;;;;;;;;AAEA,UAAIC,YAAY,IAAIC,GAAJ,EAAhB;;;;;;;;;;;;;;;;;;AAGQ,YAAMC,YAAN,SAA2BV,KAA3B,CAAiC;;AAG9C,cAAMW,UAAN,GAAmB,CAElB;;AAGD;AACA,cAAMC,gBAAN,GAAyB;AACvB;AACA,cAAIC,MAAM,KAAKC,WAAf;AACA;;AAEA,eAAKC,GAAL,CAAS,SAAT,EAAoBC,SAApB,GAAiC,uBAAsB,KAAKC,EAAL,GAAU,MAAM,KAAKA,EAArB,GAAyB,EAAG,GAAnF;AACA,cAAIC,SAAS,MAAM,KAAKjB,SAAL,CAAeY,GAAf,CAAnB;AACA,cAAIK,OAAOC,OAAX,EAAoB;AAClB,iBAAKJ,GAAL,CAAS,SAAT,EAAoBC,SAApB,GAAgC,wBAAwBV,iBAAiBY,OAAOE,KAAxB,EAA+BC,OAA/B,CAAuC,IAAvC,EAA4C,MAA5C,CAAxB,GAA8E,uBAA9G;AACA;AACD;;AAED,c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ACA,cAAIC,eAAeR,OAAOS,gBAAP,CAAwB,IAAxB,CAAnB;;AAGA,cAAID,YAAJ,EAAkB;AAChB;AACAE,oBAAQC,IAAR,CAAa,+CAAb,EAA+D,IAA/D;AACD;;AAED,cAAIC,eAAgB,MAAM,KAAKhB,SAAL,CAAeY,YAAf,CAA6B;;AAEvD;;AAF0B,WAA1B,CAIA,IAAIK,SAAJ;AACA,cAAIpC,sBAAsBqC,MAAtB,GAA+B,CAAnC,EAAsC;AACpC;AACA,gBAAIC,OAAO3C,EAAE2C,IAAF,CAAOtC,qBAAP,CAAX,CAFoC,CAEM;AAC3C;AACD,cAAIuC,YAAY,IAAIC,OAAJ,CAAYC,WAAW;AACrCL,wBAAYK,OAAZ;AACD,WAFe,CAAhB;AAGAzC,gCAAsB0C,IAAtB,CAA2BH,SAA3B;AACA,cAAID,IAAJ,EAAU;AACR;AACA,kBAAMA,IAAN;AACD;AACDL,kBAAQU,GAAR,CAAY,KAAK,KAAKjC,EAAV,GAAe,cAAf,GAAgC,gBAAhC,GAAmDyB,YAAnD,GAAkE,aAAlE,GAAkFL,GAAlF,GAAwF,GAApG;AACA,cAAIc,mBAAmBlD,UAAUoC,GAAV,EAAe,IAAf,EAAqBK,YAArB,CAAvB;AACAS,2BAAiB7B,IAAjB,CAAsB,MAAM;AAC1B,gBAAI8B,QAAQ7C,sBAAsB8C,KAAtB,EAAZ;AACA,gBAAID,UAAUN,SAAd,EAAyB;AACvB,0FAAyB,EAAzB;AACA,oBAAM,IAAIQ,KAAJ,CAAU,sDAAV,CAAN;AACD;AACDX;AACD,WAPD;AAQA,iBAAOQ,gBAAP;AACD;;AAEDhB,mBAAW;AACT,iBAAO,gBAAP;AACD;;AAEDoB,wBAAgB;AACd,eAAKzC,WAAL,GAAmB,OAAnB;AACA,eAAKH,UAAL;AACD;;AAxG6C;;yBAA3BD,Y","file":"lively-script.js","sourcesContent":["import Morph from 'src/components/widgets/lively-morph.js';\nimport boundEval from 'src/client/bound-eval.js';\nimport _ from 'src/external/lodash/lodash.js'\nimport { uuid as generateUuid, waitForDeepProperty } from 'utils'\n\nimport {stripErrorString} from \"src/components/widgets/lively-code-mirror.js\"\n\n\n/* Replacement for \"script\" tag, that supports\n *  - import module statements\n *  - extended syntax \n * by going through or own \"boundEval\" that uses babel incl. our plugins\n *\n * #TODO coordinate evaluation of multiple script tags\n *\n */\n\nexport var currentScriptPromises = []\n\nexport var moduleMap = new Map()\n\n\nexport default class LivelyScript extends Morph {\n\n  \n  async initialize() {\n    \n  }\n  \n  \n  // evalute script not when the component is initialized, but when it is actually in the DOM\n  async attachedCallback() {\n    // console.log(\"SCRIPT attached \", lively.findWorldContext(this))\n    var src = this.textContent\n    // console.log(\"LivelyScript>>initialize \" + src)\n\n    this.get(\"#result\").innerHTML = `[pending exec script${this.id ? \" \" + this.id :\"\"}]`\n    var result = await this.boundEval(src)\n    if (result.isError) {\n      this.get(\"#result\").innerHTML = \"<lively-error><pre>\" + stripErrorString(result.value).replace(/</g,\"&lt;\") + \"</pre></lively-error>\" \n      return \n    }\n    \n    if (result.value && result.value.then) {\n      result.value = await result.value\n    }\n    \n    \n    if (result.value instanceof HTMLElement && result.value !== this) {\n      this.get(\"#result\").innerHTML = \"\"\n      this.get(\"#result\").appendChild(result.value)\n    } else if (result.value !== undefined) {\n      this.get(\"#result\").innerHTML = \"\" + result.value \n    } else {\n      this.get(\"#result\").innerHTML = \"\"\n    }\n  }\n  \n\n  async moduleFor(obj) {\n    var moduleName  = moduleMap.get(obj)\n    if (!moduleName) {\n      var container = lively.query(this, \"lively-container\")\n      if (container) {\n        \n        await waitForDeepProperty(container, \"getURL\")\n        container.livelyScriptContainerId = container.livelyScriptContainerId || generateUuid()\n        \n        // all scripts in one container share the same module... but a second container will get a different module\n        moduleName = (container.getURL() || lively4url).toString() + \"_\" +container.livelyScriptContainerId\n      } else {\n        // no container, so we assume lively4 as root\n        moduleName = lively4url + \"/livelyscript_\" + generateUuid() // so that some relative urls work...\n      }\n      moduleMap.set(obj, moduleName)\n    }\n    return moduleName\n  }\n  \n  async boundEval(str) {\n    // console.log(\"\" + this.id + \">>boundEval \" + str )\n    var worldContext = lively.findWorldContext(this)\n    \n    \n    if (worldContext) {\n      // #Bug we are not yet in a body or shadow-root\n      console.warn(\"Executing lively-script without world context\" , this)\n    }\n    \n    var targetModule =  await this.moduleFor(worldContext) // all scripts in one container should share scope? \n    \n    //console.log(\"[lively-script] worldContext: \" + worldContext + \" targetModule: \", targetModule)\n    \n    var resolveMe\n    if (currentScriptPromises.length > 0) {\n      // console.log(\"script wait on \" + currentScriptPromise)\n      var last = _.last(currentScriptPromises); // wait on each other... scripts can only be executed sequencitally?\n    }\n    var myPromise = new Promise(resolve => {\n      resolveMe = resolve\n    })\n    currentScriptPromises.push(myPromise)\n    if (last) {\n      // console.log(\"wait on last: \" + last)\n      await last\n    }\n    console.log(\"\" + this.id + \">>boundEval \" + \"targetModule: \" + targetModule + \"\\n exec: \\\"\" + str + '\"', )\n    var myPromisedResult = boundEval(str, this, targetModule)\n    myPromisedResult.then(() => {\n      var first = currentScriptPromises.shift()\n      if (first !== myPromise) {\n        currentScriptPromises  = []\n        throw new Error(\"LivelyScript execution order broken, resetting it...\")\n      }\n      resolveMe()\n    })\n    return myPromisedResult\n  }\n  \n  toString() {\n    return \"[LivelyScript]\"\n  }\n  \n  livelyExample() {\n    this.textContent = \"3 + 4\"\n    this.initialize()\n  }\n  \n  \n}\n"]}