16 lines
6.3 MiB
JavaScript
16 lines
6.3 MiB
JavaScript
|
"use strict";var e=require("obsidian");
|
||
|
/*! *****************************************************************************
|
||
|
Copyright (c) Microsoft Corporation.
|
||
|
|
||
|
Permission to use, copy, modify, and/or distribute this software for any
|
||
|
purpose with or without fee is hereby granted.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||
|
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||
|
PERFORMANCE OF THIS SOFTWARE.
|
||
|
***************************************************************************** */function t(e,t,n,i){return new(n||(n=Promise))((function(a,r){function l(e){try{s(i.next(e))}catch(e){r(e)}}function o(e){try{s(i.throw(e))}catch(e){r(e)}}function s(e){var t;e.done?a(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(l,o)}s((i=i.apply(e,t||[])).next())}))}class n extends e.EditableFileView{constructor(e,t){super(e),this.plugin=t}onMoreOptionsMenu(e){super.onMoreOptionsMenu(e),e.addItem((e=>{e.onClick((e=>{this.saveAsPng()})),e.setIcon("image-file"),e.setTitle("Export as png")}))}readBlob(e){const t=new FileReader;return new Promise((n=>{t.onload=e=>{n(e.target.result)},t.readAsDataURL(e)}))}loadImage(e){const t=new Image;return new Promise((n=>{t.onload=()=>n(t),t.src=e}))}saveAsPng(){return t(this,void 0,void 0,(function*(){const e=yield this.app.vault.read(this.file),t=document.createElement("canvas"),n=t.getContext("2d"),i=new Blob([e],{type:"image/svg+xml"}),a=yield this.readBlob(i),r=yield this.loadImage(a);t.height=r.naturalHeight,t.width=r.naturalWidth,n.fillStyle="#fff",n.fillRect(0,0,t.width,t.height),n.drawImage(r,0,0);const l=t.toDataURL("image/png"),o=document.createElement("a");o.setAttribute("href",l),o.setAttribute("download",this.file.basename+".png"),document.body.appendChild(o),o.click(),document.body.removeChild(o)}))}}var i,a;!function(e){e.Script="script",e.Stylesheet="stylesheet",e.Css="css",e.FrameConfig="frame-config",e.Load="load",e.ToggleBodyClass="toggle-body-class"}(i||(i={})),function(e){e.Change="change",e.Iframe="iframe",e.Init="init",e.Load="load",e.FocusIn="focusin",e.FocusOut="focusout"}(a||(a={}));class r{constructor(e,t){this.awaitedMessages=new Set,this.targetFactory=e,this.listener=n=>{const i=e();if(n.source!==i)return;const a=JSON.parse(n.data);t&&t(a);Array.from(this.awaitedMessages.values()).forEach((e=>{e.filter(a)&&(this.awaitedMessages.delete(e),e.callback(a))}))},window.addEventListener("message",this.listener)}validateTarget(e){if("object"!=typeof e||null===e)throw Error("Target window is not an object");if("function"!=typeof e.postMessage)throw Error("Target window does not have a postMessage function")}waitForMessage(e,t){return new Promise(((n,i)=>{const a=setTimeout((()=>{this.awaitedMessages.delete(r),i("Timeout waiting for message")}),t),r={filter:e,callback:e=>{this.awaitedMessages.delete(r),clearTimeout(a),n(e)}};this.awaitedMessages.add(r)}))}sendMessageAndWait(e,t,n){const i=this.targetFactory();this.validateTarget(i);const a=this.waitForMessage(t,n);return i.postMessage(JSON.stringify(e),"*"),a}sendMessage(e){const t=this.targetFactory();this.validateTarget(t),t.postMessage(JSON.stringify(e),"*")}destroy(){window.removeEventListener("message",this.listener)}}class l extends Event{constructor(e){super("fileChange"),this.data=e}}class o extends Event{constructor(e){super("fileLoad"),this.data=e}}class s extends Event{constructor(e){super("stateChange"),this.initialized=e}}class c{constructor(e,t){this.iframeElement=null,this.contentEl=e,this.settings=t,this.file=null,this.isInitialized=!1,this.iframeElement=this.createFrameElement(),this.frameMessenger=new r((()=>this.iframeElement.contentWindow),this.handleMessage.bind(this)),this.contentEl.appendChild(this.iframeElement)}destroy(){this.iframeElement&&(this.iframeElement.parentElement&&this.iframeElement.parentElement.removeChild(this.iframeElement),this.iframeElement=null),this.frameMessenger&&this.frameMessenger.destroy()}loadFile(e){return t(this,void 0,void 0,(function*(){this.file=null,this.isInitialized||(yield this.waitForInit()),this.frameMessenger.sendMessage({action:i.Load,xml:e});const t="undefined"!==(yield this.frameMessenger.waitForMessage((e=>"load"===e.event),5e3)).xml;return t&&(this.file={data:e}),t}))}addEventListener(e,t,n){return this.iframeElement.addEventListener.call(this.iframeElement,e,t,n)}dispatchEvent(e){return this.iframeElement.dispatchEvent.call(this.iframeElement,e)}removeEventListener(e,t,n){return this.iframeElement.removeEventListener.call(this.iframeElement,e,t,n
|