Repository navigation
Expand file tree
/
Copy pathsentencestoplottertime.plug.js
More file actions
3 lines (3 loc) · 7.26 KB
/
Copy pathsentencestoplottertime.plug.js
File metadata and controls
3 lines (3 loc) · 7.26 KB
1
2
3
var T=Object.defineProperty;var P=(e,t)=>{for(var n in t)T(e,n,{get:t[n],enumerable:!0})};var u=e=>{throw new Error("Not initialized yet")},y=typeof window>"u"&&typeof globalThis.WebSocketPair>"u";typeof Deno>"u"&&(self.Deno={args:[],build:{arch:"x86_64"},env:{get(){}}});var f=new Map,g=0;y&&(globalThis.syscall=async(e,...t)=>await new Promise((n,o)=>{g++,f.set(g,{resolve:n,reject:o}),u({type:"sys",id:g,name:e,args:t})}));function h(e,t,n){y&&(u=n,self.addEventListener("message",o=>{(async()=>{let i=o.data;switch(i.type){case"inv":{let s=e[i.name];if(!s)throw new Error(`Function not loaded: ${i.name}`);try{let a=await Promise.resolve(s(...i.args||[]));u({type:"invr",id:i.id,result:a})}catch(a){console.error("An exception was thrown as a result of invoking function",i.name,"error:",a.message),u({type:"invr",id:i.id,error:a.message})}}break;case"sysr":{let s=i.id,a=f.get(s);if(!a)throw Error("Invalid request id");f.delete(s),i.error?a.reject(new Error(i.error)):a.resolve(i.result)}break}})().catch(console.error)}),u({type:"manifest",manifest:t}))}function F(e){let t=atob(e),n=t.length,o=new Uint8Array(n);for(let i=0;i<n;i++)o[i]=t.charCodeAt(i);return o}function v(e){typeof e=="string"&&(e=new TextEncoder().encode(e));let t="",n=e.byteLength;for(let o=0;o<n;o++)t+=String.fromCharCode(e[o]);return btoa(t)}async function M(e,t){if(typeof e!="string"){let n=new Uint8Array(await e.arrayBuffer()),o=n.length>0?v(n):void 0;t={method:e.method,headers:Object.fromEntries(e.headers.entries()),base64Body:o},e=e.url}return syscall("sandboxFetch.fetch",e,t)}globalThis.nativeFetch=globalThis.fetch;function _(){globalThis.fetch=async function(e,t){let n=t&&t.body?v(new Uint8Array(await new Response(t.body).arrayBuffer())):void 0,o=await M(e,t&&{method:t.method,headers:t.headers,base64Body:n});return new Response(o.base64Body?F(o.base64Body):null,{status:o.status,headers:o.headers})}}y&&_();var l={};P(l,{confirm:()=>oe,copyToClipboard:()=>fe,deleteLine:()=>ye,dispatch:()=>re,downloadFile:()=>I,filterBox:()=>G,flashNotification:()=>H,fold:()=>ae,foldAll:()=>ue,getCurrentPage:()=>R,getCursor:()=>k,getSelection:()=>K,getText:()=>E,getUiOption:()=>ie,goHistory:()=>$,hidePanel:()=>Y,insertAtCursor:()=>te,insertAtPos:()=>J,moveCursor:()=>Z,moveCursorToLine:()=>ee,navigate:()=>W,openCommandPalette:()=>B,openPageNavigator:()=>q,openSearchPanel:()=>ge,openUrl:()=>Q,prompt:()=>ne,redo:()=>pe,reloadConfigAndCommands:()=>N,reloadPage:()=>O,reloadUI:()=>j,replaceRange:()=>X,save:()=>L,setSelection:()=>D,setText:()=>U,setUiOption:()=>se,showPanel:()=>z,toggleFold:()=>le,undo:()=>de,unfold:()=>ce,unfoldAll:()=>me,uploadFile:()=>V,vimEx:()=>xe});typeof self>"u"&&(self={syscall:()=>{throw new Error("Not implemented here")}});function r(e,...t){return globalThis.syscall(e,...t)}function R(){return r("editor.getCurrentPage")}function E(){return r("editor.getText")}function U(e){return r("editor.setText",e)}function k(){return r("editor.getCursor")}function K(){return r("editor.getSelection")}function D(e,t){return r("editor.setSelection",e,t)}function L(){return r("editor.save")}function W(e,t=!1,n=!1){return r("editor.navigate",e,t,n)}function q(e="page"){return r("editor.openPageNavigator",e)}function B(){return r("editor.openCommandPalette")}function O(){return r("editor.reloadPage")}function j(){return r("editor.reloadUI")}function N(){return r("editor.reloadConfigAndCommands")}function Q(e,t=!1){return r("editor.openUrl",e,t)}function $(e){return r("editor.goHistory",e)}function I(e,t){return r("editor.downloadFile",e,t)}function V(e,t){return r("editor.uploadFile",e,t)}function H(e,t="info"){return r("editor.flashNotification",e,t)}function G(e,t,n="",o=""){return r("editor.filterBox",e,t,n,o)}function z(e,t,n,o=""){return r("editor.showPanel",e,t,n,o)}function Y(e){return r("editor.hidePanel",e)}function J(e,t){return r("editor.insertAtPos",e,t)}function X(e,t,n){return r("editor.replaceRange",e,t,n)}function Z(e,t=!1){return r("editor.moveCursor",e,t)}function ee(e,t=1,n=!1){return r("editor.moveCursorToLine",e,t,n)}function te(e){return r("editor.insertAtCursor",e)}function re(e){return r("editor.dispatch",e)}function ne(e,t=""){return r("editor.prompt",e,t)}function oe(e){return r("editor.confirm",e)}function ie(e){return r("editor.getUiOption",e)}function se(e,t){return r("editor.setUiOption",e,t)}function ae(){return r("editor.fold")}function ce(){return r("editor.unfold")}function le(){return r("editor.toggleFold")}function ue(){return r("editor.foldAll")}function me(){return r("editor.unfoldAll")}function de(){return r("editor.undo")}function pe(){return r("editor.redo")}function ge(){return r("editor.openSearchPanel")}function fe(e){return r("editor.copyToClipboard",e)}function ye(){return r("editor.deleteLine")}function xe(e){return r("editor.vimEx",e)}var d={};P(d,{applyAttributeExtractors:()=>Ce,getEnv:()=>_e,getMode:()=>Re,getSpaceConfig:()=>Te,getVersion:()=>Ee,invokeCommand:()=>be,invokeFunction:()=>ve,invokeSpaceFunction:()=>Ae,listCommands:()=>we,listSyscalls:()=>Se,reloadConfig:()=>Me,reloadPlugs:()=>Fe});function ve(e,...t){return r("system.invokeFunction",e,...t)}function be(e,t){return r("system.invokeCommand",e,t)}function we(){return r("system.listCommands")}function Se(){return r("system.listSyscalls")}function Ae(e,...t){return r("system.invokeSpaceFunction",e,...t)}function Ce(e,t,n){return r("system.applyAttributeExtractors",e,t,n)}async function Te(e,t){return await r("system.getSpaceConfig",e)??t}function Fe(){return r("system.reloadPlugs")}function Me(){return r("system.reloadConfig")}function _e(){return r("system.getEnv")}function Re(){return r("system.getMode")}function Ee(){return r("system.getVersion")}var b={seconds_per_sentence:20,milliseconds_per_word:2e3,ignore_headings:!0,ignore_lists:!0,start_regex:"",end_regex:""};async function S(){let e=await Ie(),t=await He(e),n=await Ge(e),o=await p(),i=t*o.seconds_per_sentence,s=n*o.milliseconds_per_word;await l.flashNotification("Plotter runtime: "+w(i,!1)+" min [by sentence] or "+w(s,!0)+" min [by words].","info")}async function Ie(){let e=await p(),t=await l.getSelection(),n=await l.getText();return t.from!=t.to?n=n.slice(t.from,t.to):n=Ve(n,e.start_regex,e.end_regex),n}function Ve(e,t,n){let o=0,s=new RegExp(t,"g").exec(e),a="";s&&(o=s.index,a=s[0]);let c=e.length,x=new RegExp(n,"gm").exec(e.slice(o+a.length));return x&&(c=o+a.length+x.index),e.slice(o,c)}async function p(){return{...b,...await d.getSpaceConfig("sentencesToPlotterTime",b)}}async function He(e){let t=0,n=/[^.!?]+[.!?]*/g,o="",i=await p();i.ignore_headings&&(o+="#"),i.ignore_lists&&(o+="*-");let s=new RegExp(`^s*[${o}]`);return e.split(`
`).forEach(a=>{let c=a.trim();if(!s.test(c)){let m=c.match(n)||[];t+=m.length}}),t}async function Ge(e){let t=0,n=/\b\w+\b/g,o="",i=await p();i.ignore_headings&&(o+="#"),i.ignore_lists&&(o+="*-");let s=new RegExp(`^s*[${o}]`);return e.split(`
`).forEach(a=>{let c=a.trim();if(!s.test(c)){let m=c.match(n)||[];t+=m.length}}),t}function w(e,t){t&&(e=e/1e3);let n=Math.floor(e/60),o=e%60,i=String(n),s=String(o).padStart(2,"0");return`${i}:${s}`}var A={showPlotterTime:S},C={name:"sentencestoplottertime",functions:{showPlotterTime:{path:"sentencestoplottertime.ts:showPlotterTime",command:{name:"Show plotter time for selected text or whole document",key:"Alt-l"}}},assets:{}},Ct={manifest:C,functionMapping:A};h(A,C,self.postMessage);export{Ct as plug};