svgedit/dist/editor/system/extensions/ext-imagelib/ext-imagelib.js

10 lines
8.1 KiB
JavaScript

System.register([],(function(e,t){"use strict";return{execute:function(){function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function asyncGeneratorStep(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function _asyncToGenerator(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function _next(e){asyncGeneratorStep(o,n,i,_next,_throw,"next",e)}function _throw(e){asyncGeneratorStep(o,n,i,_next,_throw,"throw",e)}_next(void 0)}))}}
/**
* @file ext-imagelib.js
*
* @license MIT
*
* @copyright 2010 Alexis Deveria
*
*/e("default",{name:"imagelib",init:function init(e){var r=this;return _asyncToGenerator(regeneratorRuntime.mark((function _callee2(){var n,i,o,a,s,c,p,l,u,d,b,m,g,h,f,v,x,w,_,y,k,S;return regeneratorRuntime.wrap((function _callee2$(T){for(;;)switch(T.prev=T.next){case 0:return k=function _showBrowser(){var e=n("#imgbrowse");if(e.length)n("#imgbrowse_holder").show();else{n("<div id=imgbrowse_holder><div id=imgbrowse class=toolbar_button></div></div>").insertAfter("#svg_docprops"),e=n("#imgbrowse");var t=c.select_lib,r=n("<ul id=imglib_opts>").appendTo(e),i=n('<iframe src="javascript:0"/>').prependTo(e).hide().wrap("<div id=lib_framewrap>"),o=n("<h1>").prependTo(e).text(t).css({position:"absolute",top:0,left:0,width:"100%"}),a=n("<button>"+p.common.cancel+"</button>").appendTo(e).on("click touchend",(function(){n("#imgbrowse_holder").hide()})).css({position:"absolute",top:5,right:-10}),s=n("<span>").css({position:"absolute",top:5,left:10}).appendTo(e),l=n("<button hidden>"+c.show_list+"</button>").appendTo(s).on("click touchend",(function(){i.attr("src","about:blank").hide(),r.show(),o.text(t),l.hide()})).css({"margin-right":5}).hide();n("<select><option value=s>"+c.import_single+"</option><option value=m>"+c.import_multi+"</option><option value=o>"+c.open+"</option></select>").appendTo(s).change((function(){switch(g=n(this).val()){case"s":case"o":y(!1);break;case"m":y(!0)}})).css({"margin-top":10}),a.prepend(n.getSvgIcon("cancel",!0)),l.prepend(n.getSvgIcon("tool_imagelib",!0)),c.imgLibs.forEach((function(e){var t=e.name,a=e.url,s=e.description;n("<li>").appendTo(r).text(t).on("click touchend",(function(){i.attr("src",a).show(),o.text(t),r.hide(),l.show()})).append("<span>".concat(s,"</span>"))}))}},y=function _toggleMulti(e){n("#lib_framewrap, #imglib_opts").css({right:e?200:10}),v||(v=n("<div id=imglib_preview>").css({position:"absolute",top:45,right:10,width:180,bottom:45,background:"#fff",overflow:"auto"}).insertAfter("#lib_framewrap"),x=n("<button disabled>Import selected</button>").appendTo("#imgbrowse").on("click touchend",(function(){n.each(h,(function(e){var t=this[0],r=this[1];"svg"===t?l.importSvgString(r):b(r),l.moveSelectedElements(20*e,20*e,!1)})),v.empty(),h=[],n("#imgbrowse_holder").hide()})).css({position:"absolute",bottom:10,right:-10})),v.toggle(e),x.toggle(e)},_=function _onMessage3(){return(_=_asyncToGenerator(regeneratorRuntime.mark((function _callee(e){var t,r,s,c,w,_,y,k,S,T,A,E,B,G,I,L,R;return regeneratorRuntime.wrap((function _callee$(j){for(;;)switch(j.prev=j.next){case 0:if(t=e.origin,(r=e.data)&&["string","object"].includes(_typeof(r))){j.next=3;break}return j.abrupt("return");case 3:if(j.prev=3,"imagelib"===(r="object"===_typeof(r)?r:JSON.parse(r)).namespace){j.next=7;break}return j.abrupt("return");case 7:if(u.includes("*")||u.includes(t)){j.next=10;break}return console.log("Origin ".concat(t," not whitelisted for posting to ").concat(window.origin)),j.abrupt("return");case 10:if(_="href"in r,(w="name"in r)||!f){j.next=15;break}return f=!1,j.abrupt("return");case 15:_&&(s=r.href,r=r.data),n("#dialog_box").hide(),c=w?"meta":r.charAt(0),j.next=28;break;case 20:if(j.prev=20,j.t0=j.catch(3),"string"!=typeof r){j.next=28;break}if("{"===(y=r.charAt(0))||!f){j.next=27;break}return f=!1,j.abrupt("return");case 27:"|"===y&&(k=r.indexOf("|",1),s=r.substr(1,k-1),r=r.substr(k+1),c=r.charAt(0));case 28:j.t1=c,j.next="meta"===j.t1?31:"<"===j.t1?47:"d"===j.t1?49:60;break;case 31:if(f=!1,m[(T=r).id]=T,B=T.name||"file",G=p.notification.retrieving.replace("%s",B),"m"===g){j.next=43;break}return j.next=39,n.process_cancel(G);case 39:f=!0,n("#dialog_box").hide(),j.next=46;break;case 43:S=n("<div>").text(G).data("id",T.id),v.append(S),T.entry=S;case 46:return j.abrupt("return");case 47:return A=!0,j.abrupt("break",62);case 49:if(!r.startsWith("data:image/svg+xml")){j.next=57;break}return"data:image/svg+xml;base64,",I=r.substring("data:image/svg+xml;base64,".length),r=i(I),A=!0,j.abrupt("break",62);case 57:if(!r.startsWith("data:image/")){j.next=60;break}return E=!0,j.abrupt("break",62);case 60:return"m"!==g?d():m[s].entry.remove(),j.abrupt("return");case 62:j.t2=g,j.next="s"===j.t2?65:"m"===j.t2?68:"o"===j.t2?72:83;break;case 65:return A?l.importSvgString(r):E&&b(r),d(),j.abrupt("break",83);case 68:return h.push([A?"svg":"img",r]),T=m[s],A?(T&&T.name?L=T.name:(R=(new DOMParser).parseFromString(o(r),"text/xml").documentElement,L=n(R).children("title").first().text()||"(SVG #"+r.length+")"),T?v.children().each((function(){n(this).data("id")===s&&(T.preview_url?n(this).html(n("<span>").append(n("<img>").attr("src",T.preview_url),L)):n(this).text(L),x.removeAttr("disabled"))})):(v.append(n("<div>").text(L)),x.removeAttr("disabled"))):(T&&T.preview_url?(L=T.name||"",S=n("<span>").append(n("<img>").attr("src",T.preview_url),L)):S=n("<img>").attr("src",r),T?v.children().each((function(){n(this).data("id")===s&&(n(this).html(S),x.removeAttr("disabled"))})):(v.append(n("<div>").append(S)),x.removeAttr("disabled"))),j.abrupt("break",83);case 72:if(A){j.next=74;break}return j.abrupt("break",83);case 74:return d(),j.next=77,a.openPrep();case 77:if(j.sent){j.next=80;break}return j.abrupt("return");case 80:return l.clear(),l.setSvgString(r),j.abrupt("break",83);case 83:case"end":return j.stop()}}),_callee,null,[[3,20]])})))).apply(this,arguments)},w=function _onMessage2(e){return _.apply(this,arguments)},b=function _importImage(e){var t=l.addSVGElementFromJson({element:"image",attr:{x:0,y:0,width:0,height:0,id:l.getNextId(),style:"pointer-events:inherit"}});l.clearSelection(),l.addToSelection([t]),l.setImageURL(e)},d=function _closeBrowser(){n("#imgbrowse_holder").hide(),document.activeElement.blur()},n=e.$,i=e.decode64,o=e.dropXMLInternalSubset,a=r,T.next=10,t.import("./locale/".concat(a.curPrefs.lang,".js"));case 10:return s=T.sent,c=s.default,p=a.uiStrings,l=a.canvas,u=c.imgLibs.map((function(e){var t=e.url;try{return new URL(t).origin}catch(e){return location.origin}})),m={},g="s",h=[],f=!1,window.addEventListener("message",w,!0),S=[{id:"tool_imagelib",type:"app_menu",icon:"imagelib.png",position:4,events:{mouseup:k}}],T.abrupt("return",{svgicons:"ext-imagelib.xml",buttons:c.buttons.map((function(e,t){return Object.assign(S[t],e)})),callback:function callback(){n("<style>").text("#imgbrowse_holder {position: absolute;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, .5);z-index: 5;}#imgbrowse {position: absolute;top: 25px;left: 25px;right: 25px;bottom: 25px;min-width: 300px;min-height: 200px;background: #B0B0B0;border: 1px outset #777;}#imgbrowse h1 {font-size: 20px;margin: .4em;text-align: center;}#lib_framewrap,#imgbrowse > ul {position: absolute;top: 45px;left: 10px;right: 10px;bottom: 10px;background: white;margin: 0;padding: 0;}#imgbrowse > ul {overflow: auto;}#imgbrowse > div {border: 1px solid #666;}#imglib_preview > div {padding: 5px;font-size: 12px;}#imglib_preview img {display: block;margin: 0 auto;max-height: 100px;}#imgbrowse li {list-style: none;padding: .5em;background: #E8E8E8;border-bottom: 1px solid #B0B0B0;line-height: 1.2em;font-style: sans-serif;}#imgbrowse li > span {color: #666;font-size: 15px;display: block;}#imgbrowse li:hover {background: #FFC;cursor: pointer;}#imgbrowse iframe {width: 100%;height: 100%;border: 0;}").appendTo("head")}});case 21:case"end":return T.stop()}}),_callee2)})))()}})}}}));