UNPKG

google-docs-utils

Version:
6 lines (5 loc) 9.1 kB
/** * @license MIT * @see https://github.com/Amaimersion/google-docs-utils */ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t=["#docs-editor-container"],e=["#docs-editor",...t],n=["iframe.docs-texteventtarget-iframe",".docs-texteventtarget-iframe"],o=[".kix-page",".docs-page"],r=[".kix-lineview",".kix-paragraphrenderer"],c=[".kix-lineview-text-block"],i=[".kix-wordhtmlgenerator-word-node"],s=[".kix-selection-overlay"],l=[".kix-cursor"],u=[".docs-text-ui-cursor-blink"],f=[".kix-cursor-caret"];function a(t,e=document){if(null==e)throw new Error("Passed root element does not exists");let n=null;for(const o of t)if(n=e.querySelector(o),n)break;return n}function d(t,e=document){if(null==e)throw new Error("Passed root element does not exists");let n=null;for(const o of t)if(n=e.querySelectorAll(o),n.length>0)break;return n?Array.from(n):[]}function y(){return a(e)}function p(t,e){const n=document.createElement("span");n.textContent=t,n.style.cssText=e,n.style.whiteSpace="pre",n.style.position="absolute",n.style.top="-100px",document.body.appendChild(n);const o=n.getBoundingClientRect();return n.remove(),o}function h(){const t=a(n);return function(t){return"iframe"===t.nodeName.toLowerCase()}(t)?t.contentDocument:t}function x(){const t=y();return d(o,t)}function K(){const t=x();let e=[];for(const n of t){const t=d(r,n);e=[...e,...t]}return e}function g(){const t=K(),e=[];for(const n of t){const t=a(c,n);e.push(t)}return e}function m(t){return t=function(t){return t.replace(/\u00A0/g,"")}(t=t.replace(/\u200C/g,""))}function w(){const t=g(),e=[];for(const n of t){let t=n.textContent;t=m(t),t=v(t),e.push(t)}return e}function v(t){return t.trim()}function E(){const t=K(),e=[];for(const n of t){const t=d(i,n);e.push(t)}return e}function C(){const t=K(),e=[];for(const n of t){const t=a(s,n);e.push(t)}return e}function b(t,e,n,o){let r=n.left,c=!1,i=0,s=t.length;for(let n=0;n!==t.length;n++){if(o.left<=r&&r<o.right)c||(i=n,c=!0);else if(c){s=n;break}r+=p(t[n],e).width}return c?{start:i,end:s}:null}function T(){const t=y();return a(l,t)}function A(){const t=y();return a(u,t)}function k(){const t=T();return t?a(f,t):null}function L(){const t=k();if(!t)return null;const e=E();if(!e.length)return null;const n=t.getBoundingClientRect(),o={element:null,wordElement:null,lineIndex:null,positionIndexRelativeToWord:null};let r=!1;for(let s=0;s!==e.length;s++){const l=e[s];for(let e=0;e!==l.length;e++){const u=l[e],f=u.getBoundingClientRect();if(i=f,(c=n).left<=i.right&&c.right>=i.left&&c.top<=i.bottom&&c.bottom>=i.top){o.element=t,o.wordElement=u,o.lineIndex=s,o.positionIndexRelativeToWord=P(f,n,u.textContent,u.style.cssText),r=!0;break}}if(r)break}var c,i;return o}function P(t,e,n,o){let r=t.left-e.width,c=0;for(const t of n){const n=p(t,o);if(0!==n.width){if(r+=n.width,r>=e.left)break;c+=1}}return c}function R(t,e,n){let o,r,c;n?(o=t=>t<=0,r=t=>t-1,c=t=>t+1):(o=t=>t>=e.length,r=t=>t+1,c=t=>t-1);let i=t,s=e[i];if(n&&_(s)&&!o(i)&&(i=r(i),s=e[i],_(s)))return t;for(;!_(s)&&!o(i);)i=r(i),s=e[i];return n&&!o(i)&&(i=c(i)),i}function _(t){return null==t||!((e=t).match(/[\w]/)||e.toLowerCase()!==e.toUpperCase());var e}function S(t,e,n,o,r,c){return null==o&&(o="Key"+n.toUpperCase()),null==r&&(r=n.codePointAt(0)),new KeyboardEvent(t,{repeat:!1,isComposing:!1,bubbles:!0,cancelable:!0,ctrlKey:!1,shiftKey:!1,altKey:!1,metaKey:!1,target:e,currentTarget:e,key:n,code:o,keyCode:r,charCode:r,which:r,...c})}function D(t,e,n=null,o=null,r={}){const c=S("keypress",t,e,n,o,r);t.dispatchEvent(c)}function W(t,e,n=null,o=null,r={}){const c=S("keydown",t,e,n,o,r);t.dispatchEvent(c)}function B(t,{ctrlKey:e=!1,shiftKey:n=!1}={}){e||n?W(h(),t,null,null,{ctrlKey:e,shiftKey:n}):D(h(),t)}function N({ctrlKey:t=!1}={}){W(h(),"Backspace","Backspace",8,{ctrlKey:t})}function I({ctrlKey:t=!1,shiftKey:e=!1}={}){W(h(),"End","End",35,{ctrlKey:t,shiftKey:e})}function O({ctrlKey:t=!1,shiftKey:e=!1}={}){W(h(),"Home","Home",36,{ctrlKey:t,shiftKey:e})}function U({ctrlKey:t=!1,shiftKey:e=!1}={}){W(h(),"ArrowLeft","ArrowLeft",37,{ctrlKey:t,shiftKey:e})}function j({ctrlKey:t=!1,shiftKey:e=!1}={}){W(h(),"ArrowUp","ArrowUp",38,{ctrlKey:t,shiftKey:e})}function z({ctrlKey:t=!1,shiftKey:e=!1}={}){W(h(),"ArrowRight","ArrowRight",39,{ctrlKey:t,shiftKey:e})}function M({ctrlKey:t=!1,shiftKey:e=!1}={}){W(h(),"ArrowDown","ArrowDown",40,{ctrlKey:t,shiftKey:e})}function H({ctrlKey:t=!1}={}){W(h(),"Delete","Delete",46,{ctrlKey:t})}function q(){B("z",{ctrlKey:!0})}var F=Object.freeze({__proto__:null,Character:B,Backspace:N,Tab:function(){W(h(),"Tab","Tab",9)},Enter:function(){W(h(),"Enter","Enter",13)},Space:function(){D(h()," ","Space",32)},End:I,Home:O,ArrowLeft:U,ArrowUp:j,ArrowRight:z,ArrowDown:M,Delete:H,Undo:q,Redo:function(){B("y",{ctrlKey:!0})},PrintDialog:function(){B("p",{ctrlKey:!0})},Bold:function(){B("b",{ctrlKey:!0})},Italic:function(){B("i",{ctrlKey:!0})},Underline:function(){B("u",{ctrlKey:!0})}});function G(){return C().some((t=>!!t))}function J(){return!!A()}function Q(){if(J())return!1;const t=G();return B("?"),t?q():N(),!0}var V=Object.freeze({__proto__:null,PrevCharacter:function(){U()},NextCharacter:function(){z()},PrevLine:function(){j()},NextLine:function(){M()},PrevWord:function(){U({ctrlKey:!0})},NextWord:function(){z({ctrlKey:!0})},PrevParagraph:function(){j({ctrlKey:!0})},NextParagraph:function(){M({ctrlKey:!0})},LineStart:function(){Q(),O()},LineEnd:function(){Q(),I()},DocumentStart:function(){O({ctrlKey:!0})},DocumentEnd:function(){I({ctrlKey:!0})}});var X=Object.freeze({__proto__:null,PrevWord:function(){N({ctrlKey:!0})},NextWord:function(){H({ctrlKey:!0})},Selection:function(){return!!G()&&(H(),!0)}});var Y=Object.freeze({__proto__:null,All:function(){B("a",{ctrlKey:!0})},PrevCharacter:function(){U({shiftKey:!0})},NextCharacter:function(){z({shiftKey:!0})},PrevWord:function(){U({shiftKey:!0,ctrlKey:!0})},NextWord:function(){z({shiftKey:!0,ctrlKey:!0})},PrevLine:function(){Q(),j({shiftKey:!0})},NextLine:function(){Q(),M({shiftKey:!0})},PrevParagraph:function(){j({shiftKey:!0,ctrlKey:!0})},NextParagraph:function(){M({shiftKey:!0,ctrlKey:!0})},TextBetweenCursorAndLineStart:function(){Q(),O({shiftKey:!0})},TextBetweenCursorAndLineEnd:function(){Q(),I({shiftKey:!0})},TextBetweenCursorAndDocumentStart:function(){O({shiftKey:!0,ctrlKey:!0})},TextBetweenCursorAndDocumentEnd:function(){I({shiftKey:!0,ctrlKey:!0})}});const Z="selectionchange",$={},tt=function(t){const e=[];for(let n of t){n.startsWith(".")||(n=n.slice(n.indexOf("."))),n=n.slice(1);const t=n.split(".");for(const n of t)n&&e.push(n)}return e}(s);function et(){const e=a(t);if(null==e)throw new Error("Unable to observe missing docsEditorContainer");new MutationObserver(nt).observe(e,{subtree:!0,childList:!0,attributes:!1,characterData:!1})}function nt(t){let e=!1;for(const n of t){for(const t of n.addedNodes){const n=Array.from(t.classList||[]);e=e||tt.some((t=>n.includes(t)))}for(const t of n.removedNodes){const n=Array.from(t.classList||[]);e=e||tt.some((t=>n.includes(t)))}}e&&function(t){const e=$[t];if(!e)return;for(const n of e)try{n({type:t})}catch(t){console.error(t)}}(Z)}!function(t){const e=()=>{t()};"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()}(et),exports.addEventListener=function(t,e){$[t]||($[t]=[]),$[t].push(e)},exports.clearTextContent=m,exports.focusDocument=Q,exports.getActiveCursorElement=A,exports.getCaret=L,exports.getCaretElement=k,exports.getCaretWord=function(){const t=L();if(!t)return null;const e=m(t.wordElement.textContent),n={word:"",text:e,indexStart:t.positionIndexRelativeToWord,indexEnd:t.positionIndexRelativeToWord};if(t.positionIndexRelativeToWord>e.length)return n;const o=R(t.positionIndexRelativeToWord,e,!0),r=R(t.positionIndexRelativeToWord,e,!1);return n.indexStart=o,n.indexEnd=r,n.word=e.substring(o,r),n},exports.getCursorElement=T,exports.getEditorElement=y,exports.getLineText=function(t,e,n){const o=w();if(t>=o.length)return null;const r=o[t];return null==e&&(e=0),null==n&&(n=r.length),r.substring(e,n)},exports.getLinesElements=K,exports.getLinesText=w,exports.getLinesTextElements=g,exports.getPagesElements=x,exports.getSelection=function(){const t=C(),e=E();if(t.length!==e.length)throw new Error("Unable to map selection elements and word elements");const n=Math.min(t.length,e.length),o=[],r=null;for(let c=0;c!==n;c++){const n=t[c];if(!n){o.push(r);continue}const i=e[c],s=[];for(const t of i){if(!t){s.push(r);continue}const e=m(t.textContent),o=t.style.cssText,c=t.getBoundingClientRect(),i=n.getBoundingClientRect(),l=b(e,o,c,i);if(!l){s.push(r);continue}const u=e.substring(l.start,l.end);s.push({text:e,selectedText:u,selectionStart:l.start,selectionEnd:l.end,textRect:c,selectionRect:i,textElement:t,selectionElement:n})}o.push(s)}return o},exports.getSelectionOverlayElements=C,exports.getTextEventTarget=h,exports.getWordElements=E,exports.isDocumentActive=J,exports.isTextSelected=G,exports.moveCursorTo=V,exports.pressOn=F,exports.remove=X,exports.select=Y,exports.typeText=function(t){!function(t){for(const e of t)B(e)}(t)};