UNPKG

google-docs-utils

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