A Wikiful of Hacks: Hacks.Wiki is an experiment to organise quick hacks, notes, bookmarks and tools into an easy-to-build-and-maintain “Digital Garden”.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
209 KiB

  1. import{Text,RangeSet,MapMode,RangeValue,Facet,StateEffect,ChangeSet,findClusterBreak,EditorSelection,EditorState,findColumn,CharCategory,Prec,Transaction,codePointAt,codePointSize,combineConfig,StateField,RangeSetBuilder,countColumn}from"@codemirror/state";import{StyleModule}from"style-mod";import{keyName,base,shift}from"w3c-keyname";function getSelection(root){let target;if(root.nodeType==11){target=root.getSelection?root:root.ownerDocument}else{target=root}return target.getSelection()}function contains(dom,node){return node?dom==node||dom.contains(node.nodeType!=1?node.parentNode:node):false}function deepActiveElement(doc){let elt=doc.activeElement;while(elt&&elt.shadowRoot)elt=elt.shadowRoot.activeElement;return elt}function hasSelection(dom,selection){if(!selection.anchorNode)return false;try{return contains(dom,selection.anchorNode)}catch(_){return false}}function clientRectsFor(dom){if(dom.nodeType==3)return textRange(dom,0,dom.nodeValue.length).getClientRects();else if(dom.nodeType==1)return dom.getClientRects();else return[]}function isEquivalentPosition(node,off,targetNode,targetOff){return targetNode?scanFor(node,off,targetNode,targetOff,-1)||scanFor(node,off,targetNode,targetOff,1):false}function domIndex(node){for(var index=0;;index++){node=node.previousSibling;if(!node)return index}}function scanFor(node,off,targetNode,targetOff,dir){for(;;){if(node==targetNode&&off==targetOff)return true;if(off==(dir<0?0:maxOffset(node))){if(node.nodeName=="DIV")return false;let parent=node.parentNode;if(!parent||parent.nodeType!=1)return false;off=domIndex(node)+(dir<0?0:1);node=parent}else if(node.nodeType==1){node=node.childNodes[off+(dir<0?-1:0)];if(node.nodeType==1&&node.contentEditable=="false")return false;off=dir<0?maxOffset(node):0}else{return false}}}function maxOffset(node){return node.nodeType==3?node.nodeValue.length:node.childNodes.length}const Rect0={left:0,right:0,top:0,bottom:0};function flattenRect(rect,left){let x=left?rect.left:rect.right;return{left:x,right:x,top:rect.top,bottom:rect.bottom}}function windowRect(win){return{left:0,right:win.innerWidth,top:0,bottom:win.innerHeight}}function scrollRectIntoView(dom,rect,side,x,y,xMargin,yMargin,ltr){let doc=dom.ownerDocument,win=doc.defaultView||window;for(let cur=dom;cur;){if(cur.nodeType==1){let bounding,top=cur==doc.body;if(top){bounding=windowRect(win)}else{if(cur.scrollHeight<=cur.clientHeight&&cur.scrollWidth<=cur.clientWidth){cur=cur.assignedSlot||cur.parentNode;continue}let rect=cur.getBoundingClientRect();bounding={left:rect.left,right:rect.left+cur.clientWidth,top:rect.top,bottom:rect.top+cur.clientHeight}}let moveX=0,moveY=0;if(y=="nearest"){if(rect.top<bounding.top){moveY=-(bounding.top-rect.top+yMargin);if(side>0&&rect.bottom>bounding.bottom+moveY)moveY=rect.bottom-bounding.bottom+moveY+yMargin}else if(rect.bottom>bounding.bottom){moveY=rect.bottom-bounding.bottom+yMargin;if(side<0&&rect.top-moveY<bounding.top)moveY=-(bounding.top+moveY-rect.top+yMargin)}}else{let rectHeight=rect.bottom-rect.top,boundingHeight=bounding.bottom-bounding.top;let targetTop=y=="center"&&rectHeight<=boundingHeight?rect.top+rectHeight/2-boundingHeight/2:y=="start"||y=="center"&&side<0?rect.top-yMargin:rect.bottom-boundingHeight+yMargin;moveY=targetTop-bounding.top}if(x=="nearest"){if(rect.left<bounding.left){moveX=-(bounding.left-rect.left+xMargin);if(side>0&&rect.right>bounding.right+moveX)moveX=rect.right-bounding.right+moveX+xMargin}else if(rect.right>bounding.right){moveX=rect.right-bounding.right+xMargin;if(side<0&&rect.left<bounding.left+moveX)moveX=-(bounding.left+moveX-rect.left+xMargin)}}else{let targetLeft=x=="center"?rect.left+(rect.right-rect.left)/2-(bounding.right-bounding.left)/2:x=="start"==ltr?rect.left-xMargin:rect.right-(bounding.right-bounding.left)+xMargin;moveX=targetLeft-bounding.left}if(moveX||moveY){if(top){win.scrollBy(moveX,moveY)}else{let movedX=0,movedY=0;if(moveY){let start=cur.scrollTop;cur.scrollTop+=moveY;movedY=cur.scrollTop-start}if(moveX){let start=cur.scrollLeft;cur.scrollLeft+=moveX;movedX=cur.scrollLeft-start}rect={left:rect.lef