text: Object.defineProperty(exports,"__esModule",{value:!0}),exports.MonacoElement=void 0;var e=require("lit");function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function _typeof(e){return typeof e}:function _typeof(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _templateObject2_c9789270012611ec9676957c409166bc(){var e=_taggedTemplateLiteral([' <iframe id="iframe" frameborder="0"></iframe>']);return _templateObject2_c9789270012611ec9676957c409166bc=function _templateObject2_c9789270012611ec9676957c409166bc(){return e},e}function _templateObject_c9789270012611ec9676957c409166bc(){var e=_taggedTemplateLiteral(["\n        :host {\n          display: block;\n        }\n        iframe {\n          border: none;\n          width: 100%;\n          height: 100%;\n          padding: 0;\n          margin: 0;\n          resize: vertical;\n        }\n      "]);return _templateObject_c9789270012611ec9676957c409166bc=function _templateObject_c9789270012611ec9676957c409166bc(){return e},e}function _taggedTemplateLiteral(e,t){return t||(t=e.slice(0)),Object.freeze(Object.defineProperties(e,{raw:{value:Object.freeze(t)}}))}function _possibleConstructorReturn(e,t){return!t||"object"!==_typeof(t)&&"function"!=typeof t?function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function _get(e,t,n){return(_get="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function _get(e,t,n){var a=function _superPropBase(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=_getPrototypeOf(e)););return e}(e,t);if(a){var o=Object.getOwnPropertyDescriptor(a,t);return o.get?o.get.call(n):o.value}})(e,t,n||e)}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _defineProperties(e,t){for(var n=0;n<t.length;n++){var a=t[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}function _createClass(e,t,n){return t&&_defineProperties(e.prototype,t),n&&_defineProperties(e,n),e}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(e,t){return e.__proto__=t,e})(e,t)}var t=function(t){function MonacoElement(){var e;return function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,MonacoElement),(e=_possibleConstructorReturn(this,_getPrototypeOf(MonacoElement).call(this))).iframe=null,e.value="",e.fontSize=16,e.wordWrap=!1,e.tabSize=2,e.readOnly=!1,e.eventTypes={ready:"ready",focus:"focus",blur:"blur",valueChanged:"valueChanged",languageChanged:"languageChanged",themeChanged:"themeChanged"},e.language="javascript",e.theme="vs-dark",e.libPath="node_modules/monaco-editor/min/vs",e.autofocus=!1,e.hideLineNumbers=!1,e.editorReference=e.generateUUID(),e}return function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}(MonacoElement,e.LitElement),_createClass(MonacoElement,null,[{key:"styles",get:function get(){return[(0,e.css)(_templateObject_c9789270012611ec9676957c409166bc())]}}]),_createClass(MonacoElement,[{key:"render",value:function render(){return(0,e.html)(_templateObject2_c9789270012611ec9676957c409166bc())}},{key:"generateUUID",value:function generateUUID(){return"ss-s-s-s-sss".replace(/s/g,this._uuidPart)}},{key:"_uuidPart",value:function _uuidPart(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}},{key:"updated",value:function updated(e){var t=this;e.forEach((function(e,n){"value"==n&&t.monacoValueChanged(t[n]),"language"==n&&t.monacoLanguageChanged(t[n]),"theme"==n&&t.monacoThemeChanged(t[n])}))}},{key:"firstUpdated",value:function firstUpdated(e){var t=this;_get(_getPrototypeOf(MonacoElement.prototype),"firstUpdated",this).call(this,e),this.__init||setTimeout((function(){t.initIFrame()}),500)}},{key:"connectedCallback",value:function connectedCallback(){var e=this;_get(_getPrototypeOf(MonacoElement.prototype),"connectedCallback",this).call(this),window.addEventListener("message",(function(t){e.handleMessage(t)}))}},{key:"disconnectedCallback",value:function disconnectedCallback(){var e=this;window.removeEventListener("message",(function(t){e.handleMessage(t)})),this.__init=!1,_get(_getPrototypeOf(MonacoElement.prototype),"disconnectedCallback",this).call(this)}},{key:"initIFrame",value:function initIFrame(){var e=this;if(this.iframe=this.shadowRoot.querySelector("#iframe"),this.document&&!this.__init){this.__init=!0;var t=document.createElement("div");t.id="container",this.document.body.appendChild(t);var n="\n  var eventTypes = {\n    ready: 'ready',\n    focus: 'focus',\n    blur: \"blur\",\n    valueChanged: 'valueChanged',\n    languageChanged: 'languageChanged',\n    themeChanged: 'themeChanged',\n  };\n\n  class MonacoEditor {\n    constructor(editorReference) {\n      this._editorReference_ = editorReference;\n      this.language = 'javascript';\n      this.value = '';\n      this.editor = null;\n      this.setupEventListener('message', this.handleMessage.bind(this));\n      this.setupEditor();\n    }\n\n    setupEditor() {\n      require.config({ paths: { vs: '".concat(this.libPath,"' } });\n      require(['vs/editor/editor.main'], () => {\n        this.editor = monaco.editor.create(document.getElementById('container'), {\n          value: this.value,\n          language: '").concat(this.language,"',\n          scrollBeyondLastLine: false,\n          automaticLayout: true,\n          ").concat(this.hideLineNumbers?"lineNumbers: 'false',":"","\n          fontSize: ").concat(this.fontSize,",\n          wordWrap: ").concat(this.wordWrap,",\n          readOnly: ").concat(this.readOnly,",\n          automaticLayout: true,\n          minimap: {\n            enabled: true\n          },\n          tabSize: ").concat(this.tabSize,",\n          autoIndent: true,\n        });\n        const model = this.editor.getModel();\n        model.onDidChangeContent(() => {\n          const value = model.getValue();\n          this.onValueChanged(value);\n        });\n        this.ready();\n      });\n    }\n\n    ready() {\n      setTimeout(root => {\n        this.postMessage(eventTypes.ready, null);\n        if(").concat(this.autofocus,') this.editor.focus();\n        this.setupEventListener(\n          eventTypes.valueChanged,\n          this.onValueChanged.bind(this)\n        );\n        this.editor.onDidFocusEditorText(e=>{\n          this.postMessage(eventTypes.focus, null);\n        });\n        this.editor.onDidBlurEditorText(e=>{\n          this.postMessage(eventTypes.blur, null);\n        });\n        this.editor.onDidFocusEditorWidget(e=>{\n          this.postMessage(eventTypes.focus, null);\n        });\n        this.editor.onDidBlurEditorWidget(e=>{\n          this.postMessage(eventTypes.blur, null);\n        });\n      }, 100);\n    }\n\n    _handleMessage(data) {\n      switch (data.event) {\n        case eventTypes.valueChanged:\n          this.onInputValueChanged(data.payload);\n          break;\n        case eventTypes.languageChanged:\n          this.onLanguageChanged(data.payload);\n          break;\n        case eventTypes.themeChanged:\n          this.onThemeChanged(data.payload);\n          break;\n        default:\n          break;\n      }\n    }\n\n    handleMessage(message) {\n      try {\n        const data = JSON.parse(message.data);\n        this._handleMessage(data);\n      } catch (error) {\n        console.warn(error);\n        return;\n      }\n    }\n\n    postMessage(event, payload) {\n      var msg = {\n        event: event,\n        payload: payload,\n        editorReference: this._editorReference_\n      }\n      window.parent.postMessage(msg, window.parent.location.href);\n    }\n\n    setupEventListener(type, callback) {\n      window.addEventListener(type, data => {\n        callback(data);\n      });\n    }\n\n    onInputValueChanged(newValue) {\n      if (newValue !== this.value) {\n        this.value = newValue;\n        this.editor.getModel().setValue(newValue);\n        this.postMessage(eventTypes.valueChanged, newValue);\n      }\n    } \n\n    onValueChanged(newValue) {\n      if (newValue !== this.value) {\n        this.value = newValue;\n        this.postMessage(eventTypes.valueChanged, newValue);\n      }\n    }\n\n    onLanguageChanged(newLang) {\n      monaco.editor.setModelLanguage(this.editor.getModel(), newLang);\n    }\n\n    onThemeChanged(newValue) {\n      monaco.editor.setTheme(newValue);\n    }\n  }\n\n  new MonacoEditor("').concat(this.editorReference,'");');this.insertScriptElement({src:"".concat(this.libPath,"/loader.js"),onload:function onload(){e.insertScriptElement({text:n}),e.insertStyle()}})}this.autofocus&&this.iframe.focus()}},{key:"handleMessage",value:function handleMessage(e){try{var t=e.data;"string"==typeof e.data&&(t=JSON.parse(e.data)),this._handleMessage(t)}catch(e){return void console.warn("[monaco-element] Error while parsing message:",e)}}},{key:"_handleMessage",value:function _handleMessage(e){if(e.editorReference===this.editorReference)if(e.event===this.eventTypes.valueChanged){var t=new CustomEvent("value-changed",{bubbles:!0,cancelable:!0,detail:e.payload});this.dispatchEvent(t)}else e.event===this.eventTypes.ready?this.onIFrameReady():e.event===this.eventTypes.focus?this.onIFrameFocus():e.event===this.eventTypes.blur&&this.onIFrameBlur()}},{key:"onIFrameFocus",value:function onIFrameFocus(){this.dispatchEvent(new CustomEvent("code-editor-focus",{bubbles:!0,composed:!0,cancelable:!0,detail:!0}))}},{key:"onIFrameBlur",value:function onIFrameBlur(){this.dispatchEvent(new CustomEvent("code-editor-blur",{bubbles:!0,composed:!0,cancelable:!0,detail:!0}))}},{key:"onIFrameReady",value:function onIFrameReady(){var e=this;this.monacoValueChanged(this.value),this.monacoLanguageChanged(this.language),this.monacoThemeChanged(this.theme),setTimeout((function(){e.dispatchEvent(new CustomEvent("monaco-element-ready",{bubbles:!0,composed:!0,cancelable:!0,detail:!0}))}),10)}},{key:"monacoValueChanged",value:function monacoValueChanged(e){this.postMessage(this.eventTypes.valueChanged,e)}},{key:"monacoLanguageChanged",value:function monacoLanguageChanged(e){this.postMessage(this.eventTypes.languageChanged,e)}},{key:"monacoThemeChanged",value:function monacoThemeChanged(e){this.postMessage(this.eventTypes.themeChanged,e)}},{key:"postMessage",value:function postMessage(e,t){this.iframe&&null!=this.iframe.contentWindow&&this.iframe.contentWindow.postMessage(JSON.stringify({event:e,payload:t}),window.location.href)}},{key:"insertScriptElement",value:function insertScriptElement(e){var t=e.src,n=e.text,a=e.onload,o=this.document.createElement("script");t&&(o.src=t),n&&(o.text=n),a&&(o.onload=a),this.document.head.appendChild(o)}},{key:"insertStyle",value:function insertStyle(){var e="\n    body {\n      height: 100vh;\n      overflow: hidden;\n      margin: 0;\n    }    \n    #container {\n      width: 100%;\n      height: 100%;\n    }\n    .debug-red {\n      background : red;\n    }\n    .debug-green {\n      background : green;\n    }\n    html,body {\n      margin : 0px;\n    }",t=this.document.head,n=this.document.createElement("style");n.type="text/css",n.styleSheet?n.styleSheet.cssText=e:n.appendChild(this.document.createTextNode(e)),t.appendChild(n)}},{key:"document",get:function get(){if(this.iframe.contentWindow)return this.iframe.contentWindow.document}}],[{key:"properties",get:function get(){return{value:{type:String},fontSize:{type:Number,attribute:"font-size"},wordWrap:{type:Boolean,attribute:"word-wrap"},readOnly:{type:Boolean,attribute:"read-only"},uniqueKey:{type:String,attribute:"unique-key"},eventTypes:{type:Object},language:{type:String},theme:{type:String},libPath:{type:String,attribute:"lib-path"},editorReference:{type:String,reflect:!0,attribute:"editor-reference"},autofocus:{type:Boolean,reflect:!0},hideLineNumbers:{type:Boolean,attribute:"hide-line-numbers"},tabSize:{type:Number,attribute:"tab-size"}}}}]),MonacoElement}();exports.MonacoElement=t,window.customElements.define("monaco-element",t),window.MonacoData=window.MonacoData||{};