smartdown
Version:
A library for translating, rendering and interacting with Smartdown documents. Smartdown is an extension of Markdown that provides richer media support and reactive programming capability.
1 lines • 5.07 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.Viz=n()}(this,(function(){"use strict";var e="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},n=function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")},r=function(){function e(e,n){for(var r=0;r<n.length;r++){var t=n[r];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(e,t.key,t)}}return function(n,r,t){return r&&e(n.prototype,r),t&&e(n,t),n}}(),t=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e},i=function(){function e(r){var t=this;n(this,e),this.worker=r,this.listeners=[],this.nextId=0,this.worker.addEventListener("message",(function(e){var n=e.data.id,r=e.data.error,i=e.data.result;t.listeners[n](r,i),delete t.listeners[n]}))}return r(e,[{key:"render",value:function(e,n){var r=this;return new Promise((function(t,i){var o=r.nextId++;r.listeners[o]=function(e,n){e?i(new Error(e.message,e.fileName,e.lineNumber)):t(n)},r.worker.postMessage({id:o,src:e,options:n})}))}}]),e}(),o=function e(r,t){n(this,e);var i=r();this.render=function(e,n){return new Promise((function(r,o){try{r(t(i,e,n))}catch(e){o(e)}}))}};function a(){return"devicePixelRatio"in window&&window.devicePixelRatio>1?window.devicePixelRatio:1}var d=function(){function d(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.workerURL,t=e.worker,a=e.Module,s=e.render;if(n(this,d),void 0!==r)this.wrapper=new i(new Worker(r));else if(void 0!==t)this.wrapper=new i(t);else if(void 0!==a&&void 0!==s)this.wrapper=new o(a,s);else{if(void 0===d.Module||void 0===d.render)throw new Error("Must specify workerURL or worker option, Module and render options, or include one of full.render.js or lite.render.js after viz.js.");this.wrapper=new o(d.Module,d.render)}}return r(d,[{key:"renderString",value:function(e){for(var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.format,t=void 0===r?"svg":r,i=n.engine,o=void 0===i?"dot":i,a=n.files,d=void 0===a?[]:a,s=n.images,u=void 0===s?[]:s,l=n.yInvert,c=void 0!==l&&l,f=n.nop,v=void 0===f?0:f,h=0;h<u.length;h++)d.push({path:u[h].path,data:'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<svg width="'+u[h].width+'" height="'+u[h].height+'"></svg>'});return this.wrapper.render(e,{format:t,engine:o,files:d,images:u,yInvert:c,nop:v})}},{key:"renderSVGElement",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.renderString(e,t({},n,{format:"svg"})).then((function(e){return(new DOMParser).parseFromString(e,"image/svg+xml").documentElement}))}},{key:"renderImageElement",value:function(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=r.scale,o=r.mimeType,d=r.quality;return this.renderString(n,t({},r,{format:"svg"})).then((function(n){return"object"===("undefined"==typeof fabric?"undefined":e(fabric))&&fabric.loadSVGFromString?function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.scale,t=void 0===r?a():r,i=n.mimeType,o=void 0===i?"image/png":i,d=n.quality,s=void 0===d?1:d,u=t,l=void 0;return"image/jpeg"==o?l="jpeg":"image/png"==o&&(l="png"),new Promise((function(n,r){fabric.loadSVGFromString(e,(function(e,t){0==e.length&&r(new Error("Error loading SVG with Fabric"));var i=document.createElement("canvas");i.width=t.width,i.height=t.height;var o=new fabric.Canvas(i,{enableRetinaScaling:!1}),a=fabric.util.groupSVGElements(e,t);o.add(a).renderAll();var d=new Image;d.src=o.toDataURL({format:l,multiplier:u,quality:s}),d.width=t.width,d.height=t.height,n(d)}))}))}(n,{scale:i,mimeType:o,quality:d}):function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.scale,t=void 0===r?a():r,i=n.mimeType,o=void 0===i?"image/png":i,d=n.quality,s=void 0===d?1:d;return new Promise((function(n,r){var i=new Image;i.onload=function(){var e=document.createElement("canvas");e.width=i.width*t,e.height=i.height*t,e.getContext("2d").drawImage(i,0,0,e.width,e.height),e.toBlob((function(e){var r=new Image;r.src=URL.createObjectURL(e),r.width=i.width,r.height=i.height,n(r)}),o,s)},i.onerror=function(e){var n;n="error"in e?e.error:new Error("Error loading SVG"),r(n)},i.src="data:image/svg+xml;base64,"+btoa(encodeURIComponent(e).replace(/%([0-9A-F]{2})/g,(function(e,n){return String.fromCharCode("0x"+n)})))}))}(n,{scale:i,mimeType:o,quality:d})}))}},{key:"renderJSONObject",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n.format;return"json"===r&&"json0"===r||(r="json"),this.renderString(e,t({},n,{format:r})).then((function(e){return JSON.parse(e)}))}}]),d}();return d}));