{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-eventsourcing/src/components/widgets/lively-script.js"],"names":["Morph","boundEval","_","generateUuid","uuid","waitForDeepProperty","stripErrorString","currentScriptPromises","moduleMap","Map","LivelyScript","initialize","evaluated","Promise","resolve","resolveEvaluated","attachedCallback","lastParentElement","parentElement","src","textContent","get","innerHTML","id","result","isError","value","replace","then","HTMLElement","appendChild","undefined","console","log","cleanModuleURL","url","toString","moduleFor","obj","moduleName","markdown","lively","query","getAttribute","container","livelyScriptContainerId","getURL","lively4url","set","str","worldContext","findWorldContext","warn","targetModule","resolveMe","length","last","myPromise","push","myPromisedResult","first","shift","Error","livelyExample"],"mappings":";;;;;;;AAAOA,W;;AACAC,e;;AACAC,O;;AACUC,kB,UAARC,I;AAAsBC,yB,UAAAA,mB;;AAEvBC,sB,2CAAAA,gB;;;;;;;;;;;AALDN,gD;;;;;;;;;;;;;AACAC,oD;;;;;;;;;;;;;AACAC,4C;;;;;;;;;;;;;AACwBG,8D;;;;;;;;;;;;;AAAdF,uD;;;;;;;;;;;;;AAETG,2D;;;;;;;;;AAGR;;;;;;;;;AASO,UAAIC,wBAAwB,EAA5B;;;;;;;;;;2CAAIA,sD;;;;;;;;AAEJ,UAAIC,YAAY,IAAIC,GAAJ,EAAhB;;;;;;;;;;+BAAID,0C;;;;;;;;AAGI,YAAME,YAAN,SAA2BV,KAA3B,CAAiC;;AAG9C,cAAMW,UAAN,GAAmB;AACjB,eAAKC,SAAL,GAAiB,IAAIC,OAAJ,CAAYC,WAAW;AACtC,iBAAKC,gBAAL,GAAwBD,OAAxB;AACD,WAFgB,CAAjB;AAGD;;AAGD;AACA,cAAME,gBAAN,GAAyB;;AAEvB;AACA,cAAI,KAAKC,iBAAL,IAA0B,KAAKC,aAAnC,EAAkD;AAClD,eAAKD,iBAAL,GAA0B,KAAKC,aAA/B;AACA;;;AAGA;AACA,cAAIC,MAAM,KAAKC,WAAf;AACA;;AAEA,eAAKC,GAAL,CAAS,SAAT,EAA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CAA6B;;AAEvD;;AAF0B,WAA1B,CAIA,IAAII,SAAJ;AACA,cAAI/C,sBAAsBgD,MAAtB,GAA+B,CAAnC,EAAsC;AACpC;AACA,gBAAIC,OAAOtD,EAAEsD,IAAF,CAAOjD,qBAAP,CAAX,CAFoC,CAEM;AAC3C;AACD,cAAIkD,YAAY,IAAI5C,OAAJ,CAAYC,WAAW;AACrCwC,wBAAYxC,OAAZ;AACD,WAFe,CAAhB;AAGAP,gCAAsBmD,IAAtB,CAA2BD,SAA3B;AACA,cAAID,IAAJ,EAAU;AACR;AACA,kBAAMA,IAAN;AACD;AACD;AACA,cAAIG,mBAAmB1D,UAAUgD,GAAV,EAAe,IAAf,EAAqBI,YAArB,CAAvB;AACAM,2BAAiB/B,IAAjB,CAAsB,MAAM;AAC1B,gBAAIgC,QAAQrD,sBAAsBsD,KAAtB,EAAZ;AACA,gBAAID,UAAUH,SAAd,EAAyB;AACvB,0FAAyB,EAAzB;AACA,oBAAM,IAAIK,KAAJ,CAAU,sDAAV,CAAN;AACD;AACDR;AACD,WAPD;AAQA,iBAAOK,gBAAP;AACD;;AAEDvB,mBAAW;AACT,iBAAO,gBAAP;AACD;;AAED2B,wBAAgB;AACd,eAAK3C,WAAL,GAAmB,OAAnB;AACA,eAAKT,UAAL;AACD;;AAxI6C;;yBAA3BD,Y;;;;;;;;6BAAAA,6C","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    this.evaluated = new Promise(resolve => {\n      this.resolveEvaluated = resolve\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    \n    // we get exectuted to often?\n    if (this.lastParentElement == this.parentElement) return\n    this.lastParentElement =  this.parentElement\n    // lively.notify(\"execute \" + this.textContent)\n    \n    \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    if (this.resolveEvaluated) {\n      // lively.notify(\"[script] evaluated \" + this)\n      this.resolveEvaluated()\n      this.resolveEvaluated = null // don't do it twice...\n    } else {\n      console.log(\"[script] evaluated AGAIN\" )\n    }\n  }\n  \n  cleanModuleURL(url) {\n    return url.toString().replace(/[^A-Za-z0-9\\-_:\\/.]/g, \"\") // #TODO this is important, maybe we should blacklist instead of whitelist\n  }\n\n  async moduleFor(obj) {\n    var moduleName  = moduleMap.get(obj)\n    if (!moduleName) {\n      // first check if we are part of a markdonw \n      var markdown = lively.query(this, \"lively-markdown\")\n      if (markdown) {\n        var url = markdown.getAttribute(\"url\") || markdown.getAttribute(\"src\") \n        if (url) {\n          moduleName = url.replace(/[^/]*$/,\"livelyscript_\" + generateUuid())\n        }\n      } \n      // check if for a container...\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 = (this.cleanModuleURL(container.getURL()) || lively4url).toString() + \"_\" +container.livelyScriptContainerId\n        } \n      }\n      \n      // no markdown, no container, so we assume lively4 as root\n      if (!moduleName) {\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(\"[lively-script] \" + 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"]}