-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
2 lines (2 loc) · 15.3 KB
/
Copy pathmain.js
File metadata and controls
2 lines (2 loc) · 15.3 KB
1
2
"use strict";var H=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var W=Object.prototype.hasOwnProperty;var K=(r,s)=>{for(var t in s)H(r,t,{get:s[t],enumerable:!0})},B=(r,s,t,e)=>{if(s&&typeof s=="object"||typeof s=="function")for(let i of V(s))!W.call(r,i)&&i!==t&&H(r,i,{get:()=>s[i],enumerable:!(e=L(s,i))||e.enumerable});return r};var I=r=>B(H({},"__esModule",{value:!0}),r);var U={};K(U,{default:()=>T});module.exports=I(U);var o=require("obsidian"),$=require("@codemirror/view"),w={enabled:!0,respectReducedMotion:!0,shape:"beam",animationMode:"trail",trail:{length:12,decayMs:250,opacity:.75},smooth:{durationMs:180,easing:"ease-out"},pulse:{style:"pulse",periodMs:700,minOpacity:.2,maxOpacity:1},smear:{durationMs:120,opacity:.6,maxStretchPx:80},beamWidthPx:2,underlineHeightPx:2};function g(r,s,t){return Math.min(t,Math.max(s,r))}function G(){try{return window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches??!1}catch{return!1}}function k(r,s){let t=r.state.doc.length,e=r.coordsAtPos(s),i=r.coordsAtPos(Math.min(s+1,t));if(e&&i&&Number.isFinite(e.left)&&Number.isFinite(i.left)){let n=i.left-e.left;if(n>.5)return n}return r.defaultCharacterWidth}function F(r){return $.ViewPlugin.fromClass(class{constructor(t){this.view=t;this.lastHeadPos=t.state.selection.main.head,this.layerEl=document.createElement("div"),this.layerEl.className="kitty-cursor-trail-layer",this.smoothEl=document.createElement("div"),this.smoothEl.className="kitty-cursor-smooth",this.smoothEl.style.display="none",this.pulseEl=document.createElement("div"),this.pulseEl.className="kitty-cursor-pulse",this.pulseEl.style.display="none";let e=t.dom;window.getComputedStyle(e).position==="static"&&(e.style.position="relative"),e.appendChild(this.layerEl),this.layerEl.appendChild(this.smoothEl),this.layerEl.appendChild(this.pulseEl)}layerEl;ghosts=[];smears=[];lastHeadPos;smoothEl;pulseEl;smoothAnimation;lastSmoothRect;pulseInitialized=!1;lastMode="trail";update(t){let e=r(),i=t.state.selection.main.head;if(!e.enabled){this.hideAll(),this.lastHeadPos=i;return}if(e.respectReducedMotion&&G()){this.hideAll(),this.lastHeadPos=i;return}let n=e.animationMode!==this.lastMode;this.applyMode(e.animationMode);let d=i!==this.lastHeadPos,l=t.viewportChanged||t.geometryChanged||t.docChanged,a=e.animationMode==="smooth"&&!this.lastSmoothRect,m=e.animationMode==="pulse"&&!this.pulseInitialized,c=d||l||n||a||m;e.animationMode==="trail"?d&&this.spawnGhost(this.lastHeadPos,e):e.animationMode==="smooth"?c&&this.updateSmooth(i,e,d):e.animationMode==="pulse"?c&&this.updatePulse(i,e):e.animationMode==="smear"&&d&&this.spawnSmear(this.lastHeadPos,i,e),this.lastHeadPos=i}hideAll(){this.clearGhosts(),this.clearSmears(),this.smoothEl.style.display="none",this.pulseEl.style.display="none",this.smoothAnimation&&(this.smoothAnimation.cancel(),this.smoothAnimation=void 0),this.lastSmoothRect=void 0,this.pulseInitialized=!1,this.pulseEl.style.animationName="none"}applyMode(t){this.lastMode!==t&&(this.lastMode=t,t!=="trail"&&this.clearGhosts(),t!=="smear"&&this.clearSmears(),t!=="smooth"&&(this.smoothAnimation&&(this.smoothAnimation.cancel(),this.smoothAnimation=void 0),this.lastSmoothRect=void 0),t!=="pulse"&&(this.pulseEl.style.animationName="none",this.pulseInitialized=!1),this.smoothEl.style.display=t==="smooth"?"block":"none",this.pulseEl.style.display=t==="pulse"?"block":"none")}clearGhosts(){for(let t of this.ghosts)t.remove();this.ghosts=[]}clearSmears(){for(let t of this.smears)t.remove();this.smears=[]}applyRect(t,e){t.style.left=`${e.left}px`,t.style.top=`${e.top}px`,t.style.width=`${e.width}px`,t.style.height=`${e.height}px`}measureCursorRect(t,e,i){let n=this.view,d=e.shape,l=g(e.beamWidthPx,1,12),a=g(e.underlineHeightPx,1,12);n.requestMeasure({read:()=>{let m=n.state.doc.length,c=Math.min(Math.max(t,0),m),S=n.coordsAtPos(c);if(!S)return null;let y=n.defaultLineHeight,h=k(n,c),u=n.dom.getBoundingClientRect(),M=S.left-u.left,E=S.top-u.top,x=Math.max(1,h),f=y,b=E,P=M;return d==="beam"?(x=l,P=Math.max(0,M-l)):d==="underline"&&(f=a,b=E+y-a),{left:P,top:b,width:x,height:f}},write:m=>{m&&i(m)}})}updateSmooth(t,e,i){let n=g(e.smooth.durationMs,40,5e3),d=e.smooth.easing;this.measureCursorRect(t,e,l=>{if(!i||!this.lastSmoothRect){this.smoothAnimation&&(this.smoothAnimation.cancel(),this.smoothAnimation=void 0),this.applyRect(this.smoothEl,l),this.lastSmoothRect=l;return}this.smoothAnimation&&this.smoothAnimation.cancel();let a=this.lastSmoothRect,m=l.left-a.left,c=l.top-a.top,S=Math.hypot(m,c),y=g(S/40,0,.35),h=1,u=1;Math.abs(m)>=Math.abs(c)?(h=1+y,u=1-y*.4):(u=1+y,h=1-y*.4),this.applyRect(this.smoothEl,a),this.smoothAnimation=this.smoothEl.animate([{left:`${a.left}px`,top:`${a.top}px`,width:`${a.width}px`,height:`${a.height}px`,transform:"scale(1)"},{left:`${a.left+m*.5}px`,top:`${a.top+c*.5}px`,width:`${a.width+(l.width-a.width)*.5}px`,height:`${a.height+(l.height-a.height)*.5}px`,transform:`scale(${h}, ${u})`},{left:`${l.left}px`,top:`${l.top}px`,width:`${l.width}px`,height:`${l.height}px`,transform:"scale(1)"}],{duration:n,easing:d,fill:"forwards"}),this.smoothAnimation.onfinish=()=>{this.smoothAnimation=void 0,this.applyRect(this.smoothEl,l)},this.lastSmoothRect=l})}updatePulse(t,e){let i=g(e.pulse.periodMs,200,5e3),n=g(e.pulse.minOpacity,0,1),d=g(e.pulse.maxOpacity,0,1),l=Math.min(n,d),a=Math.max(n,d);this.measureCursorRect(t,e,m=>{this.applyRect(this.pulseEl,m),this.pulseEl.style.setProperty("--kitty-pulse-min",String(l)),this.pulseEl.style.setProperty("--kitty-pulse-max",String(a)),this.pulseEl.style.animationName=e.pulse.style==="blink"?"kitty-cursor-blink":"kitty-cursor-pulse",this.pulseEl.style.animationDuration=`${i}ms`,this.pulseEl.style.animationIterationCount="infinite",this.pulseEl.style.animationTimingFunction=e.pulse.style==="blink"?"steps(2, start)":"ease-in-out",this.pulseEl.style.animationFillMode="both",this.pulseInitialized=!0})}spawnSmear(t,e,i){let n=this.view,d=g(i.smear.opacity,.05,1),l=g(i.smear.durationMs,40,2e3),a=g(i.smear.maxStretchPx,0,400),m=i.shape,c=g(i.beamWidthPx,1,12),S=g(i.underlineHeightPx,1,12);n.requestMeasure({read:()=>{let y=M=>{let E=n.state.doc.length,x=Math.min(Math.max(M,0),E),f=n.coordsAtPos(x);if(!f)return null;let b=n.defaultLineHeight,P=k(n,x),v=n.dom.getBoundingClientRect(),D=f.left-v.left,p=f.top-v.top,R=Math.max(1,P),C=b,A=p,N=D;return m==="beam"?(R=c,N=Math.max(0,D-c)):m==="underline"&&(C=S,A=p+b-S),{left:N,top:A,width:R,height:C}},h=y(t),u=y(e);return!h||!u?null:{fromRect:h,toRect:u}},write:y=>{if(!y)return;let{fromRect:h,toRect:u}=y,M=u.left-h.left,E=u.top-h.top,x=Math.hypot(M,E);if(x<.5)return;let f=Math.max(h.width,u.width),b=Math.max(h.height,u.height),P=Math.min(x,a),v=Math.atan2(M,h.top-u.top)+Math.PI/2,D=Math.abs(Math.sin(v))*f+Math.abs(Math.cos(v))*b,p=document.createElement("div");for(p.className="kitty-cursor-smear",p.style.opacity=String(d),p.style.left="0px",p.style.top="0px",p.style.width=`${f}px`,p.style.height=`${b}px`,p.style.transform=`translate3d(${h.left}px, ${h.top}px, 0)`,p.style.setProperty("--kitty-smear-drag-width",`${P}px`),p.style.setProperty("--kitty-smear-drag-height",`${D}px`),p.style.setProperty("--kitty-smear-drag-angle",`${v}rad`),this.layerEl.appendChild(p),this.smears.push(p);this.smears.length>20;)this.smears.shift()?.remove();let R=p.animate([{transform:`translate3d(${h.left}px, ${h.top}px, 0)`,opacity:String(d)},{transform:`translate3d(${u.left}px, ${u.top}px, 0)`,opacity:"0"}],{duration:l,easing:"ease-out",fill:"forwards"});R.onfinish=()=>{let C=this.smears.indexOf(p);C>=0&&this.smears.splice(C,1),p.remove()}}})}spawnGhost(t,e){let i=g(e.trail.opacity,.05,1),n=g(e.trail.length,1,100),d=g(e.trail.decayMs,40,1e4);this.measureCursorRect(t,e,l=>{let a=document.createElement("div");for(a.className="kitty-cursor-ghost",a.style.opacity=String(i),this.applyRect(a,l),this.layerEl.appendChild(a),this.ghosts.push(a);this.ghosts.length>n;)this.ghosts.shift()?.remove();let m=a.animate([{opacity:a.style.opacity,transform:"scale(1)"},{opacity:"0",transform:"scale(0.98)"}],{duration:d,easing:"linear",fill:"forwards"});m.onfinish=()=>{let c=this.ghosts.indexOf(a);c>=0&&this.ghosts.splice(c,1),a.remove()}})}destroy(){this.hideAll(),this.layerEl.remove()}})}var O=class extends o.PluginSettingTab{plugin;constructor(s){super(s.app,s),this.plugin=s}display(){let{containerEl:s}=this;s.empty(),s.createEl("h3",{text:"General"}),new o.Setting(s).setName("Enable cursor animation").setDesc("Enables kitty-style cursor animation effects.").addToggle(t=>t.setValue(this.plugin.settings.enabled).onChange(async e=>{this.plugin.settings.enabled=e,await this.plugin.saveSettings()})),new o.Setting(s).setName("Respect reduced motion").setDesc("Disables the animation if your OS prefers reduced motion.").addToggle(t=>t.setValue(this.plugin.settings.respectReducedMotion).onChange(async e=>{this.plugin.settings.respectReducedMotion=e,await this.plugin.saveSettings()})),new o.Setting(s).setName("Animation version").setDesc("Choose the cursor animation style.").addDropdown(t=>{t.addOption("trail","Trail (ghosts)").addOption("smooth","Smooth glide").addOption("pulse","Blink/Pulse").addOption("smear","Smear (neovide)").setValue(this.plugin.settings.animationMode).onChange(async e=>{this.plugin.settings.animationMode=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Cursor shape").setDesc("Cursor shape to mimic kitty\u2019s cursor styles.").addDropdown(t=>{t.addOption("beam","Beam").addOption("block","Block").addOption("underline","Underline").setValue(this.plugin.settings.shape).onChange(async e=>{this.plugin.settings.shape=e,await this.plugin.saveSettings()})}),s.createEl("h3",{text:"Trail settings"}),new o.Setting(s).setName("Trail length").setDesc("How many ghosts can exist at once.").addSlider(t=>{t.setLimits(1,50,1).setValue(this.plugin.settings.trail.length).setDynamicTooltip().onChange(async e=>{this.plugin.settings.trail.length=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Fade out (ms)").setDesc("How long each ghost takes to fade out.").addSlider(t=>{t.setLimits(40,2e3,10).setValue(this.plugin.settings.trail.decayMs).setDynamicTooltip().onChange(async e=>{this.plugin.settings.trail.decayMs=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Opacity").setDesc("Starting opacity of each ghost.").addSlider(t=>{t.setLimits(5,100,1).setValue(Math.round(this.plugin.settings.trail.opacity*100)).setDynamicTooltip().onChange(async e=>{this.plugin.settings.trail.opacity=e/100,await this.plugin.saveSettings()})}),s.createEl("h3",{text:"Smooth glide settings"}),new o.Setting(s).setName("Duration (ms)").setDesc("Time to ease between cursor positions.").addSlider(t=>{t.setLimits(40,1e3,10).setValue(this.plugin.settings.smooth.durationMs).setDynamicTooltip().onChange(async e=>{this.plugin.settings.smooth.durationMs=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Easing").setDesc("Easing curve to use for smooth movement.").addDropdown(t=>{t.addOption("linear","Linear").addOption("ease","Ease").addOption("ease-in","Ease in").addOption("ease-out","Ease out").addOption("ease-in-out","Ease in/out").setValue(this.plugin.settings.smooth.easing).onChange(async e=>{this.plugin.settings.smooth.easing=e,await this.plugin.saveSettings()})}),s.createEl("h3",{text:"Blink/Pulse settings"}),new o.Setting(s).setName("Style").setDesc("Blink toggles visibility; Pulse eases opacity.").addDropdown(t=>{t.addOption("blink","Blink").addOption("pulse","Pulse").setValue(this.plugin.settings.pulse.style).onChange(async e=>{this.plugin.settings.pulse.style=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Period (ms)").setDesc("Time for a full blink/pulse cycle.").addSlider(t=>{t.setLimits(200,2e3,10).setValue(this.plugin.settings.pulse.periodMs).setDynamicTooltip().onChange(async e=>{this.plugin.settings.pulse.periodMs=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Min opacity (%)").setDesc("Lowest opacity during the cycle.").addSlider(t=>{t.setLimits(0,100,1).setValue(Math.round(this.plugin.settings.pulse.minOpacity*100)).setDynamicTooltip().onChange(async e=>{this.plugin.settings.pulse.minOpacity=e/100,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Max opacity (%)").setDesc("Highest opacity during the cycle.").addSlider(t=>{t.setLimits(0,100,1).setValue(Math.round(this.plugin.settings.pulse.maxOpacity*100)).setDynamicTooltip().onChange(async e=>{this.plugin.settings.pulse.maxOpacity=e/100,await this.plugin.saveSettings()})}),s.createEl("h3",{text:"Smear settings"}),new o.Setting(s).setName("Duration (ms)").setDesc("How long the smear lasts.").addSlider(t=>{t.setLimits(40,600,10).setValue(this.plugin.settings.smear.durationMs).setDynamicTooltip().onChange(async e=>{this.plugin.settings.smear.durationMs=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Opacity").setDesc("Opacity of the smear.").addSlider(t=>{t.setLimits(5,100,1).setValue(Math.round(this.plugin.settings.smear.opacity*100)).setDynamicTooltip().onChange(async e=>{this.plugin.settings.smear.opacity=e/100,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Max stretch (px)").setDesc("Cap how far the smear stretches in one move.").addSlider(t=>{t.setLimits(0,200,5).setValue(this.plugin.settings.smear.maxStretchPx).setDynamicTooltip().onChange(async e=>{this.plugin.settings.smear.maxStretchPx=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Beam width (px)").setDesc("Only used for the Beam shape.").addSlider(t=>{t.setLimits(1,8,1).setValue(this.plugin.settings.beamWidthPx).setDynamicTooltip().onChange(async e=>{this.plugin.settings.beamWidthPx=e,await this.plugin.saveSettings()})}),new o.Setting(s).setName("Underline height (px)").setDesc("Only used for the Underline shape.").addSlider(t=>{t.setLimits(1,8,1).setValue(this.plugin.settings.underlineHeightPx).setDynamicTooltip().onChange(async e=>{this.plugin.settings.underlineHeightPx=e,await this.plugin.saveSettings()})})}},T=class extends o.Plugin{settings={...w};async onload(){if(await this.loadSettings(),this.addSettingTab(new O(this)),typeof this.registerEditorExtension!="function"){new o.Notice("Kitty Cursor requires a newer Obsidian version that supports editor extensions.");return}this.registerEditorExtension(F(()=>this.settings))}onunload(){}async loadSettings(){let s=await this.loadData();if(!s||typeof s!="object"){this.settings={...w};return}let t=s;this.settings={...w,...t,trail:{...w.trail,...typeof t.trail=="object"&&t.trail!==null?t.trail:{}},smooth:{...w.smooth,...typeof t.smooth=="object"&&t.smooth!==null?t.smooth:{}},pulse:{...w.pulse,...typeof t.pulse=="object"&&t.pulse!==null?t.pulse:{}},smear:{...w.smear,...typeof t.smear=="object"&&t.smear!==null?t.smear:{}}},typeof t.trailLength=="number"&&(this.settings.trail.length=t.trailLength),typeof t.decayMs=="number"&&(this.settings.trail.decayMs=t.decayMs),typeof t.opacity=="number"&&(this.settings.trail.opacity=t.opacity),typeof t.beamWidthPx=="number"&&(this.settings.beamWidthPx=t.beamWidthPx),typeof t.underlineHeightPx=="number"&&(this.settings.underlineHeightPx=t.underlineHeightPx),typeof t.respectReducedMotion=="boolean"&&(this.settings.respectReducedMotion=t.respectReducedMotion),typeof t.enabled=="boolean"&&(this.settings.enabled=t.enabled),typeof t.shape=="string"&&(this.settings.shape=t.shape)}async saveSettings(){await this.saveData(this.settings)}};
if (module.exports && module.exports.default) { module.exports = module.exports.default; } if (module.exports) { module.exports.default = module.exports; }