@tiptap/extensions 3.27.2 → 3.27.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,137 +0,0 @@
1
- import type { EditorState, PluginView, StateField, Transaction } from '@tiptap/pm/state'
2
- import type { EditorView } from '@tiptap/pm/view'
3
-
4
- import { PLUGIN_KEY } from '../constants.js'
5
- import type { ViewportState } from '../types.js'
6
- import { findScrollParent } from './findScrollParent.js'
7
- import { getViewportBoundaryPositions } from './getViewportBoundaryPositions.js'
8
-
9
- /**
10
- * The plugin `state` config that tracks the visible viewport boundaries so the
11
- * decoration callback only scans the nodes currently on screen.
12
- */
13
- export const viewportPluginState: StateField<ViewportState> = {
14
- /**
15
- * Initialises the viewport state with no known positions.
16
- * @returns The initial viewport state.
17
- */
18
- init(): ViewportState {
19
- return { topPos: null, bottomPos: null }
20
- },
21
-
22
- /**
23
- * Updates the viewport state from incoming transactions.
24
- * @param tr - The transaction being applied.
25
- * @param prev - The previous viewport state.
26
- * @returns The next viewport state.
27
- */
28
- apply(tr: Transaction, prev: ViewportState): ViewportState {
29
- const meta = tr.getMeta(PLUGIN_KEY) as
30
- | { positions?: { top: number; bottom: number } }
31
- | undefined
32
-
33
- if (meta?.positions) {
34
- return { topPos: meta.positions.top, bottomPos: meta.positions.bottom }
35
- }
36
-
37
- if (!tr.docChanged) {
38
- return prev
39
- }
40
-
41
- // Preserve last known viewport positions across transactions.
42
- // Without this, every keystroke resets back to a full document
43
- // scan, defeating the viewport optimisation.
44
- // Only map when we have actual positions — null means "no viewport
45
- // info yet" and should stay null to fall back to full doc scan.
46
- return {
47
- topPos: prev.topPos !== null ? tr.mapping.map(prev.topPos) : null,
48
- bottomPos: prev.bottomPos !== null ? tr.mapping.map(prev.bottomPos) : null,
49
- }
50
- },
51
- }
52
-
53
- /**
54
- * Creates the plugin `view` that recomputes the visible viewport on scroll and
55
- * document size changes, dispatching the result into the plugin state.
56
- * @param view - The editor view the plugin is attached to.
57
- * @returns A plugin view with `update` and `destroy` handlers.
58
- */
59
- export function createViewportPluginView(view: EditorView): PluginView {
60
- const scrollContainer = findScrollParent(view.dom)
61
-
62
- const computeAndDispatch = () => {
63
- const positions = getViewportBoundaryPositions({
64
- view,
65
- scrollContainer,
66
- })
67
-
68
- // Not measurable, keep the last good window frozen
69
- if (positions === null) {
70
- return
71
- }
72
-
73
- const prev = PLUGIN_KEY.getState(view.state)
74
- if (prev?.topPos === positions.top && prev?.bottomPos === positions.bottom) {
75
- return
76
- }
77
-
78
- const tr = view.state.tr.setMeta(PLUGIN_KEY, { positions })
79
- view.dispatch(tr)
80
- }
81
-
82
- // rAF-based scheduler with interval guard (150ms → ~6–7 Hz) used by
83
- // scroll events and update(). The overscan margin hides the extra
84
- // latency. When the guard blocks, the callback reschedules itself so
85
- // the trailing position is always captured.
86
- let frame: number | null = null
87
- let lastCompute = 0
88
- const MIN_SCROLL_INTERVAL = 150
89
-
90
- const scheduleFrame = () => {
91
- if (frame !== null) return
92
- frame = requestAnimationFrame(() => {
93
- frame = null
94
- const now = performance.now()
95
- if (now - lastCompute >= MIN_SCROLL_INTERVAL) {
96
- lastCompute = now
97
- computeAndDispatch()
98
- } else {
99
- scheduleFrame()
100
- }
101
- })
102
- }
103
-
104
- scrollContainer.addEventListener('scroll', scheduleFrame, { passive: true })
105
-
106
- // Recover a frozen window once the editor becomes usable again (e.g. a modal
107
- // closes): re-measure on size/visibility changes and when focus returns.
108
- const resizeObserver =
109
- typeof ResizeObserver !== 'undefined' ? new ResizeObserver(scheduleFrame) : null
110
- resizeObserver?.observe(view.dom)
111
-
112
- const intersectionObserver =
113
- typeof IntersectionObserver !== 'undefined' ? new IntersectionObserver(scheduleFrame) : null
114
- intersectionObserver?.observe(view.dom)
115
-
116
- view.dom.addEventListener('focus', scheduleFrame)
117
-
118
- // Fire once to populate initial viewport
119
- computeAndDispatch()
120
-
121
- return {
122
- update(_view: EditorView, prevState: EditorState) {
123
- if (view.state.doc.content.size !== prevState.doc.content.size) {
124
- scheduleFrame()
125
- }
126
- },
127
- destroy: () => {
128
- if (frame !== null) {
129
- cancelAnimationFrame(frame)
130
- }
131
- scrollContainer.removeEventListener('scroll', scheduleFrame)
132
- resizeObserver?.disconnect()
133
- intersectionObserver?.disconnect()
134
- view.dom.removeEventListener('focus', scheduleFrame)
135
- },
136
- }
137
- }