|
| 1 | +import { EditorSelection } from "@codemirror/state"; |
| 2 | +import { |
| 3 | + EditorView, |
| 4 | + type PluginValue, |
| 5 | + ViewPlugin, |
| 6 | + type ViewUpdate, |
| 7 | +} from "@codemirror/view"; |
| 8 | + |
| 9 | +import { findEditorScrollContainer } from "@/features/editor/lib/view-utils"; |
| 10 | + |
| 11 | +type ScrollPastEndOptions = { |
| 12 | + /** Content-to-viewport ratio at which padding begins (default: 0.5 = 50%). */ |
| 13 | + startRatio?: number; |
| 14 | + /** Step size: padding increases every this fraction of growth (default: 0.05 = 5%). */ |
| 15 | + step?: number; |
| 16 | + /** Maximum padding as a fraction of viewport height (default: 0.7 = 70%). */ |
| 17 | + maxPaddingRatio?: number; |
| 18 | +}; |
| 19 | + |
| 20 | +class ScrollPastEndPlugin implements PluginValue { |
| 21 | + private currentHeight = 0; |
| 22 | + private spacer: HTMLDivElement | null = null; |
| 23 | + |
| 24 | + constructor( |
| 25 | + private readonly view: EditorView, |
| 26 | + private readonly startRatio: number, |
| 27 | + private readonly step: number, |
| 28 | + private readonly maxPaddingRatio: number, |
| 29 | + ) { |
| 30 | + this.measure(); |
| 31 | + } |
| 32 | + |
| 33 | + update(update: ViewUpdate): void { |
| 34 | + if (update.geometryChanged || update.docChanged) { |
| 35 | + this.measure(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private ensureSpacer(): HTMLDivElement | null { |
| 40 | + if (this.spacer) { |
| 41 | + return this.spacer; |
| 42 | + } |
| 43 | + |
| 44 | + const scrollContainer = findEditorScrollContainer(this.view); |
| 45 | + if (!scrollContainer) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + this.spacer = document.createElement("div"); |
| 50 | + this.spacer.setAttribute("aria-hidden", "true"); |
| 51 | + this.spacer.style.cursor = "text"; |
| 52 | + this.spacer.addEventListener("mousedown", this.handleSpacerClick); |
| 53 | + scrollContainer.append(this.spacer); |
| 54 | + return this.spacer; |
| 55 | + } |
| 56 | + |
| 57 | + private measure(): void { |
| 58 | + const scrollContainer = findEditorScrollContainer(this.view); |
| 59 | + if (!scrollContainer) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const viewportHeight = scrollContainer.clientHeight; |
| 64 | + if (viewportHeight === 0) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const contentHeight = this.view.contentHeight; |
| 69 | + const ratio = contentHeight / viewportHeight; |
| 70 | + |
| 71 | + let height = 0; |
| 72 | + if (ratio > this.startRatio) { |
| 73 | + const excess = ratio - this.startRatio; |
| 74 | + const steps = Math.floor(excess / this.step); |
| 75 | + const maxHeight = viewportHeight * this.maxPaddingRatio; |
| 76 | + height = Math.min(steps * (maxHeight / 20), maxHeight); |
| 77 | + } |
| 78 | + |
| 79 | + if (height !== this.currentHeight) { |
| 80 | + this.currentHeight = height; |
| 81 | + if (height > 0) { |
| 82 | + const spacer = this.ensureSpacer(); |
| 83 | + if (spacer) { |
| 84 | + spacer.style.height = `${height}px`; |
| 85 | + } |
| 86 | + } else if (this.spacer) { |
| 87 | + this.spacer.style.height = "0"; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private handleSpacerClick = (event: MouseEvent): void => { |
| 93 | + event.preventDefault(); |
| 94 | + this.view.dispatch({ |
| 95 | + selection: EditorSelection.cursor(this.view.state.doc.length), |
| 96 | + scrollIntoView: false, |
| 97 | + }); |
| 98 | + this.view.focus(); |
| 99 | + }; |
| 100 | + |
| 101 | + destroy(): void { |
| 102 | + this.spacer?.removeEventListener("mousedown", this.handleSpacerClick); |
| 103 | + this.spacer?.remove(); |
| 104 | + this.spacer = null; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +export function scrollPastEnd({ |
| 109 | + startRatio = 0.3, |
| 110 | + step = 0.05, |
| 111 | + maxPaddingRatio = 0.7, |
| 112 | +}: ScrollPastEndOptions = {}) { |
| 113 | + return ViewPlugin.fromClass( |
| 114 | + class extends ScrollPastEndPlugin { |
| 115 | + constructor(view: EditorView) { |
| 116 | + super(view, startRatio, step, maxPaddingRatio); |
| 117 | + } |
| 118 | + }, |
| 119 | + ); |
| 120 | +} |
0 commit comments