@tiptap/extensions 3.23.5 → 3.24.0
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.
- package/dist/index.cjs +285 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -7
- package/dist/index.d.ts +24 -7
- package/dist/index.js +268 -47
- package/dist/index.js.map +1 -1
- package/dist/placeholder/index.cjs +265 -45
- package/dist/placeholder/index.cjs.map +1 -1
- package/dist/placeholder/index.d.cts +25 -8
- package/dist/placeholder/index.d.ts +25 -8
- package/dist/placeholder/index.js +264 -46
- package/dist/placeholder/index.js.map +1 -1
- package/dist/trailing-node/index.cjs +4 -1
- package/dist/trailing-node/index.cjs.map +1 -1
- package/dist/trailing-node/index.js +4 -1
- package/dist/trailing-node/index.js.map +1 -1
- package/package.json +19 -20
- package/src/placeholder/constants.ts +17 -0
- package/src/placeholder/index.ts +3 -0
- package/src/placeholder/placeholder.ts +5 -147
- package/src/placeholder/plugins/PlaceholderPlugin.ts +35 -0
- package/src/placeholder/types.ts +75 -0
- package/src/placeholder/utils/buildPlaceholderDecorations.ts +111 -0
- package/src/placeholder/utils/createPlaceholderDecoration.ts +61 -0
- package/src/placeholder/utils/findScrollParent.ts +38 -0
- package/src/placeholder/utils/getViewportBoundaryPositions.ts +49 -0
- package/src/placeholder/utils/index.ts +6 -0
- package/src/placeholder/utils/preparePlaceholderAttribute.ts +21 -0
- package/src/placeholder/utils/throttle.ts +28 -0
- package/src/placeholder/utils/viewportTracking.ts +118 -0
- package/src/trailing-node/trailing-node.ts +10 -2
|
@@ -0,0 +1,118 @@
|
|
|
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
|
+
doc: view.state.doc,
|
|
66
|
+
scrollContainer,
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const prev = PLUGIN_KEY.getState(view.state)
|
|
70
|
+
if (prev?.topPos === positions.top && prev?.bottomPos === positions.bottom) {
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const tr = view.state.tr.setMeta(PLUGIN_KEY, { positions })
|
|
75
|
+
view.dispatch(tr)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// rAF-based scheduler with interval guard (150ms → ~6–7 Hz) used by
|
|
79
|
+
// scroll events and update(). The overscan margin hides the extra
|
|
80
|
+
// latency. When the guard blocks, the callback reschedules itself so
|
|
81
|
+
// the trailing position is always captured.
|
|
82
|
+
let frame: number | null = null
|
|
83
|
+
let lastCompute = 0
|
|
84
|
+
const MIN_SCROLL_INTERVAL = 150
|
|
85
|
+
|
|
86
|
+
const scheduleFrame = () => {
|
|
87
|
+
if (frame !== null) return
|
|
88
|
+
frame = requestAnimationFrame(() => {
|
|
89
|
+
frame = null
|
|
90
|
+
const now = performance.now()
|
|
91
|
+
if (now - lastCompute >= MIN_SCROLL_INTERVAL) {
|
|
92
|
+
lastCompute = now
|
|
93
|
+
computeAndDispatch()
|
|
94
|
+
} else {
|
|
95
|
+
scheduleFrame()
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
scrollContainer.addEventListener('scroll', scheduleFrame, { passive: true })
|
|
101
|
+
|
|
102
|
+
// Fire once to populate initial viewport
|
|
103
|
+
computeAndDispatch()
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
update(_view: EditorView, prevState: EditorState) {
|
|
107
|
+
if (view.state.doc.content.size !== prevState.doc.content.size) {
|
|
108
|
+
scheduleFrame()
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
destroy: () => {
|
|
112
|
+
if (frame !== null) {
|
|
113
|
+
cancelAnimationFrame(frame)
|
|
114
|
+
}
|
|
115
|
+
scrollContainer.removeEventListener('scroll', scheduleFrame)
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -4,7 +4,13 @@ import { Plugin, PluginKey } from '@tiptap/pm/state'
|
|
|
4
4
|
|
|
5
5
|
export const skipTrailingNodeMeta = 'skipTrailingNode'
|
|
6
6
|
|
|
7
|
-
function nodeEqualsType({
|
|
7
|
+
function nodeEqualsType({
|
|
8
|
+
types,
|
|
9
|
+
node,
|
|
10
|
+
}: {
|
|
11
|
+
types: NodeType | NodeType[]
|
|
12
|
+
node: Node | null | undefined
|
|
13
|
+
}) {
|
|
8
14
|
return (node && Array.isArray(types) && types.includes(node.type)) || node?.type === types
|
|
9
15
|
}
|
|
10
16
|
|
|
@@ -46,7 +52,9 @@ export const TrailingNode = Extension.create<TrailingNodeOptions>({
|
|
|
46
52
|
addProseMirrorPlugins() {
|
|
47
53
|
const plugin = new PluginKey(this.name)
|
|
48
54
|
const defaultNode =
|
|
49
|
-
this.options.node ||
|
|
55
|
+
this.options.node ||
|
|
56
|
+
this.editor.schema.topNodeType.contentMatch.defaultType?.name ||
|
|
57
|
+
'paragraph'
|
|
50
58
|
|
|
51
59
|
const disabledNodes = Object.entries(this.editor.schema.nodes)
|
|
52
60
|
.map(([, value]) => value)
|