text: define(["exports","../../../../@polymer/polymer/polymer-element.js","../../../responsive-utility/responsive-utility.js","../../rich-text-editor.js","../rich-text-editor-styles.js","../singletons/rich-text-editor-selection.js","../buttons/rich-text-editor-button.js","../buttons/rich-text-editor-more-button.js","../buttons/rich-text-editor-heading-picker.js","../buttons/rich-text-editor-symbol-picker.js","../buttons/rich-text-editor-underline.js","../buttons/rich-text-editor-image.js","../buttons/rich-text-editor-link.js","../buttons/rich-text-editor-button-styles.js","../../../../@polymer/iron-icons/iron-icons.js","../../../../@polymer/iron-icons/editor-icons.js","../../../../@polymer/iron-icons/image-icons.js","../../../md-extra-icons/md-extra-icons.js"],(function(t,e,o,i,r,l,n,a,s,d,c,p,u,b,h,m,g,y){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.RichTextEditorToolbar=void 0;
  /**
     * Copyright 2019 Penn State University
     * @license Apache-2.0, see License.md for full text.
     */
  class RichTextEditorToolbar extends e.PolymerElement{static get stickyTemplate(){return e.html`
        <style>
          :host([sticky]) {
            position: sticky;
            top: 0;
          }
        </style>
      `}static get styleTemplate(){return e.html`
        <style include="rich-text-editor-styles rich-text-editor-button-styles">
          :host([hidden]) {
            display: none;
          }
          :host #toolbar {
            display: flex;
            opacity: 1;
            z-index: 1;
            margin: 0;
            align-items: stretch;
            flex-wrap: wrap;
            justify-content: flex-start;
            background-color: var(--rich-text-editor-bg);
            border: var(--rich-text-editor-border);
            font-size: 12px;
            transition: all 0.5s;
            @apply --rich-text-editor-toolbar;
          }
          :host #toolbar[aria-hidden] {
            visibility: hidden;
            opacity: 0;
            height: 0;
          }
          :host #toolbar .group {
            display: flex;
            flex-wrap: nowrap;
            justify-content: space-evenly;
            align-items: stretch;
            padding: 0 3px;
            @apply --rich-text-editor-toolbar-group;
          }
          :host #toolbar .group:not(:last-of-type) {
            margin-right: 3px;
            border-right: var(--rich-text-editor-border);
            @apply --rich-text-editor-toolbar-divider;
          }
          :host #toolbar .button {
            display: flex;
            flex: 0 0 auto;
            align-items: stretch;
          }
          :host #toolbar #morebutton {
            flex: 1 0 auto;
            justify-content: flex-end;
          }
          /* hide the more button if all the buttons are displayed */
          :host([responsive-size="xs"]) #morebutton[collapse-max="xs"],
          :host([responsive-size="sm"]) #morebutton[collapse-max*="s"],
          :host([responsive-size="md"]) #morebutton:not([collapse-max*="l"]),
          :host([responsive-size="lg"]) #morebutton:not([collapse-max="xl"]),
          :host([responsive-size="xl"]) #morebutton,
          /* hide the buttons if they should be collaped until */
          :host([responsive-size="xs"]) #toolbar[collapsed] *[collapsed-until*="m"],
          :host([responsive-size="xs"]) #toolbar[collapsed] *[collapsed-until*="l"],
          :host([responsive-size="sm"]) #toolbar[collapsed] *[collapsed-until="md"],
          :host([responsive-size="sm"]) #toolbar[collapsed] *[collapsed-until*="l"],
          :host([responsive-size="md"]) #toolbar[collapsed] *[collapsed-until*="l"],
          :host([responsive-size="lg"]) #toolbar[collapsed] *[collapsed-until="xl"] {
            display: none;
          }
        </style>
      `}static get toolbarTemplate(){return e.html`
        <div
          id="toolbar"
          aria-live="polite"
          aria-hidden$="[[!controls]]"
          collapsed$="[[collapsed]]"
        >
          <rich-text-editor-more-button
            id="morebutton"
            class="button"
            controls="toolbar"
            icon$="[[moreIcon]]"
            label$="[[moreLabel]]"
            show-text-label$="[[moreShowTextLabel]]"
            label-toggled$="[[moreLabelToggled]]"
            toggled$="[[!collapsed]]"
            on-click="_toggleMore"
          >
          </rich-text-editor-more-button>
        </div>
      `}static get template(){return e.html`
        ${this.styleTemplate} ${this.stickyTemplate} ${this.toolbarTemplate}
      `}static get properties(){return{buttons:{name:"buttons",type:Array,computed:"_getButtons(config)"},canceled:{name:"canceled",type:Object,value:!0},collapsed:{name:"collapsed",type:Boolean,value:!0},config:{name:"config",type:Object,value:[{label:"History",type:"button-group",buttons:[{command:"undo",icon:"undo",label:"Undo",shortcutKeys:"ctrl+z",type:"rich-text-editor-button"},{command:"redo",icon:"redo",label:"Redo",shortcutKeys:"ctrl+shift+z",type:"rich-text-editor-button"}]},{label:"Basic Inline Operations",type:"button-group",buttons:[{label:"Format",type:"rich-text-editor-heading-picker"},{command:"bold",icon:"editor:format-bold",label:"Bold",shortcutKeys:"ctrl+b",toggles:!0,type:"rich-text-editor-button"},{command:"italic",icon:"editor:format-italic",label:"Italics",shortcutKeys:"ctrl+i",toggles:!0,type:"rich-text-editor-button"},{command:"removeFormat",icon:"editor:format-clear",label:"Erase Format",type:"rich-text-editor-button"}]},{label:"Links",type:"button-group",buttons:[{icon:"link",label:"Link",shortcutKeys:"ctrl+k",type:"rich-text-editor-link"}]},{label:"Clipboard Operations",type:"button-group",buttons:[{command:"cut",icon:"content-cut",label:"Cut",shortcutKeys:"ctrl+x",type:"rich-text-editor-button"},{command:"copy",icon:"content-copy",label:"Copy",shortcutKeys:"ctrl+c",type:"rich-text-editor-button"},{command:"paste",icon:"content-paste",label:"Paste",shortcutKeys:"ctrl+v",type:"rich-text-editor-button"}]},{collapsedUntil:"md",label:"Subscript and Superscript",type:"button-group",buttons:[{command:"subscript",icon:"mdextra:subscript",label:"Subscript",toggles:!0,type:"rich-text-editor-button"},{command:"superscript",icon:"mdextra:superscript",label:"Superscript",toggles:!0,type:"rich-text-editor-button"}]},{collapsedUntil:"sm",icon:"editor:functions",label:"Insert Symbol",symbolTypes:["symbols"],type:"rich-text-editor-symbol-picker"},{collapsedUntil:"sm",label:"Lists and Indents",type:"button-group",buttons:[{command:"insertOrderedList",icon:"editor:format-list-numbered",label:"Ordered List",toggles:!0,type:"rich-text-editor-button"},{command:"insertUnorderedList",icon:"editor:format-list-bulleted",label:"Unordered List",toggles:!0,type:"rich-text-editor-button"},{collapsedUntil:"lg",command:"formatBlock",commandVal:"blockquote",label:"Blockquote",icon:"editor:format-quote",shortcutKeys:"ctrl+'",type:"rich-text-editor-button"},{command:"indent",icon:"editor:format-indent-increase",event:"text-indent",label:"Increase Indent",shortcutKeys:"ctrl+]",type:"rich-text-editor-button"},{command:"outdent",event:"text-outdent",icon:"editor:format-indent-decrease",label:"Decrease Indent",shortcutKeys:"ctrl+[",type:"rich-text-editor-button"}]}]},controls:{name:"controls",type:String,value:null},editor:{name:"editor",type:Object,value:null},moreIcon:{name:"moreIcon",type:String,value:"more-vert"},moreLabel:{name:"moreLabel",type:String,value:"More Buttons"},moreLabelToggled:{name:"moreLabelToggled",type:String,value:"Fewer Buttons"},moreShowTextLabel:{name:"moreShowTextLabel",type:Boolean,value:!1},responsiveSize:{name:"responsiveSize",type:String,value:"xs",reflectToAttribute:!0},savedSelection:{name:"savedSelection",type:Object,value:null},range:{name:"range",type:Object,value:null,observer:"_rangeChange"},sticky:{name:"sticky",type:Boolean,value:!1,reflectToAttribute:!0,observer:"_stickyChanged"},__inlineWidgets:{name:"__inlineWidgets",type:Array,value:[]},__shortcutKeys:{name:"__shortcutKeys",type:Array,value:[]}}}static get tag(){return"rich-text-editor-toolbar"}connectedCallback(){super.connectedCallback();let t=this;this.__clipboard=document.createElement("textarea"),this.__clipboard.setAttribute("aria-hidden",!0),this.__clipboard.style.position="absolute",this.__clipboard.style.left="-9999px",this.__clipboard.style.top="0px",this.__clipboard.style.width="0px",this.__clipboard.style.height="0px",document.body.appendChild(this.__clipboard),window.addEventListener("paste",t._handlePaste.bind(t)),navigator.clipboard&&this.addEventListener("paste-button",t._handlePasteButton.bind(t)),window.RichTextEditorSelection.requestAvailability(),window.ResponsiveUtility.requestAvailability(),window.dispatchEvent(new CustomEvent("responsive-element",{detail:{element:t,attribute:"responsive-size",relativeToParent:!0}}))}disconnectedCallback(){super.disconnectedCallback();this.dispatchEvent(new CustomEvent("deselect-rich-text-editor-editor",{bubbles:!0,cancelable:!0,composed:!0,detail:{toolbar:this,editor:this.editor}}))}addEditableRegion(t){let e=this;t.addEventListener("mousedown",o=>{e.editTarget(t)}),t.addEventListener("focus",o=>{e.editTarget(t)}),t.addEventListener("keydown",o=>{e._handleShortcutKeys(t,o)}),t.addEventListener("blur",t=>{null!==t.relatedTarget&&"rich-text-editor"!==!t.relatedTarget.startsWith||e.editTarget(null)})}cancel(){this.editor.innerHTML=this.canceled,this.editTarget(null)}editTarget(t){let e=this;e.editor!==t&&(null!==e.editor&&(e.editor.contentEditable=!1,e.editor=null),e.dispatchEvent(new CustomEvent("select-rich-text-editor-editor",{bubbles:!0,cancelable:!0,composed:!0,detail:{toolbar:e,editor:e.editor}})),e.editor=t,t?(t.parentNode.insertBefore(e,t),e.canceled=t.innerHTML,e.editor.contentEditable=!0,e.controls=t.getAttribute("id")):e.controls=null,e.buttons.forEach(o=>{o.target=t,o.controls=e.controls}))}getRange(){let t=window.getSelection();return t.getRangeAt&&t.rangeCount?t.getRangeAt(0):t||void 0}getSanitizeClipboard(t){let e="<body(.*\n)*>(.*\n)*</body>";return t.match(e)&&t.match(e).length>0&&(t=t.match(e)[0].replace(/<\?body(.*\n)*\>/i)),t}makeEditableRegion(t){let e=document.createElement("rich-text-editor");t.parentNode.insertBefore(e,t),e.appendChild(t),this.addEditableRegion(e)}pasteIntoRange(t,e){console.log("pasteIntoRange",t,e);let o=document.createElement("div"),i=(window.getSelection(),t.commonAncestorContainer.parentNode.closest("[contenteditable=true]:not([disabled]),input:not([disabled]),textarea:not([disabled])"));if(this.editor=i){for(o.innerHTML=e,t&&t.extractContents&&t.extractContents(),t.insertNode(o);o.firstChild;)o.parentNode.insertBefore(o.firstChild,o);o.parentNode.removeChild(o)}}removeEditableRegion(t){let e=this;t.removeEventListener("mouseout",t=>{e.getUpdatedSelection()}),t.removeEventListener("focus",o=>{e.editTarget(t)}),t.removeEventListener("mousedown",o=>{e.editTarget(t)}),t.removeEventListener("keydown",o=>{e._handleShortcutKeys(t,o)}),t.removeEventListener("blur",t=>{null!==t.relatedTarget&&"rich-text-editor"!==!t.relatedTarget.startsWith||e.editTarget(null),e.getUpdatedSelection()})}_addButton(t,e){let o=this,i=document.createElement(t.type),r=i.shortcutKeys?i.shortcutKeys.replace(/ctrl\+[xcv]/g,""):"";for(var l in this.set(`__shortcutKeys.${r}`,i),t)i[l]=t[l];return i.setAttribute("class","button"),i.addEventListener("deselect",t=>{o.range&&o.range.collapse&&o.range.collapse(!1)}),i.inlineWidget&&o.push("__inlineWidgets",i.tag),e.appendChild(i),i}_handleKeyboardShortcuts(t){console.log("_handleKeyboardShortcuts",t)}_getButtons(t){let e=this,o=e.shadowRoot.querySelector("#toolbar"),i=this.shadowRoot.querySelector("#morebutton"),r=0,l=["xs","sm","md","lg","xl"],n=[];return o.innerHTML="",this.set("__shortcutKeys",[]),t.forEach(t=>{if("button-group"===t.type){let i=document.createElement("div");i.setAttribute("class","group"),void 0!==t.collapsedUntil&&null!==t.collapsedUntil&&i.setAttribute("collapsed-until",t.collapsedUntil),r=Math.max(r,l.indexOf(t.collapsedUntil)),t.buttons.forEach(t=>{r=Math.max(r,l.indexOf(t.collapsedUntil)),(navigator.clipboard||"paste"!==t.command)&&n.push(e._addButton(t,i))}),o.appendChild(i)}else r=Math.max(r,l.indexOf(t.collapsedUntil)),(navigator.clipboard||"paste"!==t.command)&&n.push(e._addButton(t,o));o.appendChild(i),i.collapseMax=l[r]}),n}_handleShortcutKeys(t,e){if(t.contentEditable){let t=e.key;e.shiftKey&&(t="shift+"+t),e.altKey&&(t="alt+"+t),("MacIntel"===window.navigator.platform&&e.metaKey||e.ctrlKey)&&(t="ctrl+"+t),this.__shortcutKeys[t]&&this.__shortcutKeys[t]._keysPressed(e)}}_handlePaste(t){console.log("_handlePaste",t);let e="";t&&(t.clipboardData||t.originalEvent.clipboardData)?e=(t.originalEvent||t).clipboardData.getData("text/html"):window.clipboardData&&(e=window.clipboardData.getData("Text")),console.log("clipboardData"),this.pasteIntoRange(this.getRange(),this.getSanitizeClipboard(e)),console.log("pasteIntoRange"),t.preventDefault()}_handlePasteButton(t){console.log("_handlePasteButton",t),setTimeout(async()=>{let e=t.detail.range,o=window.getSelection(),i=await navigator.clipboard.readText();this.__clipboard.value=i,this.__clipboard.focus(),this.__clipboard.select(),document.execCommand("paste"),o.removeAllRanges(),o.addRange(e),this.pasteIntoRange(e,this.getSanitizeClipboard(this.__clipboard.value))},2e3),t.preventDefault()}_rangeChange(t){let e=this;e.buttons.forEach(t=>{t.range=null,t.range=e.range})}_stickyChanged(t,e){this.__breadcrumbs&&(this.__breadcrumbs.sticky=this.sticky)}_toggleMore(t){this.collapsed=!this.collapsed}}t.RichTextEditorToolbar=RichTextEditorToolbar,window.customElements.define(RichTextEditorToolbar.tag,RichTextEditorToolbar)}));