@milkdown/plugin-tooltip 7.17.3 → 7.18.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@milkdown/plugin-tooltip",
3
3
  "type": "module",
4
- "version": "7.17.3",
4
+ "version": "7.18.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -28,9 +28,9 @@
28
28
  "@floating-ui/dom": "^1.5.1",
29
29
  "@types/lodash-es": "^4.17.12",
30
30
  "lodash-es": "^4.17.21",
31
- "@milkdown/ctx": "7.17.3",
32
- "@milkdown/prose": "7.17.3",
33
- "@milkdown/utils": "7.17.3"
31
+ "@milkdown/ctx": "7.18.0",
32
+ "@milkdown/prose": "7.18.0",
33
+ "@milkdown/utils": "7.18.0"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "vite build"
@@ -8,7 +8,13 @@ import type {
8
8
  import type { EditorState } from '@milkdown/prose/state'
9
9
  import type { EditorView } from '@milkdown/prose/view'
10
10
 
11
- import { computePosition, flip, offset, shift } from '@floating-ui/dom'
11
+ import {
12
+ autoUpdate,
13
+ computePosition,
14
+ flip,
15
+ offset,
16
+ shift,
17
+ } from '@floating-ui/dom'
12
18
  import { posToDOMRect } from '@milkdown/prose'
13
19
  import { TextSelection } from '@milkdown/prose/state'
14
20
  import { throttle } from 'lodash-es'
@@ -53,6 +59,9 @@ export class TooltipProvider {
53
59
  /// @internal
54
60
  #initialized = false
55
61
 
62
+ /// @internal
63
+ #cleanupAutoUpdate?: () => void
64
+
56
65
  /// @internal
57
66
  readonly #offset?: OffsetOptions
58
67
 
@@ -87,6 +96,32 @@ export class TooltipProvider {
87
96
  this.#updater = throttle(this.#onUpdate, this.#debounce)
88
97
  }
89
98
 
99
+ /// @internel
100
+ #updatePosition = (reference: VirtualElement) => {
101
+ computePosition(reference, this.element, {
102
+ placement: this.#floatingUIOptions.placement ?? 'top',
103
+ middleware: [
104
+ flip(),
105
+ offset(this.#offset),
106
+ shift(this.#shift),
107
+ ...this.#middleware,
108
+ ],
109
+ ...this.#floatingUIOptions,
110
+ })
111
+ .then(({ x, y }) => {
112
+ Object.assign(this.element.style, {
113
+ left: `${x}px`,
114
+ top: `${y}px`,
115
+ })
116
+ })
117
+ .catch(console.error)
118
+ }
119
+
120
+ /// @internal
121
+ #shouldAutoUpdate = (editorView: EditorView) => {
122
+ return this.#root !== editorView.dom.parentElement
123
+ }
124
+
90
125
  /// @internal
91
126
  #onUpdate = (view: EditorView, prevState?: EditorState): void => {
92
127
  const { state, composing } = view
@@ -105,6 +140,9 @@ export class TooltipProvider {
105
140
 
106
141
  if (composing || isSame) return
107
142
 
143
+ this.#cleanupAutoUpdate?.()
144
+ this.#cleanupAutoUpdate = void 0
145
+
108
146
  if (!this.#shouldShow(view, prevState)) {
109
147
  this.hide()
110
148
  return
@@ -112,23 +150,16 @@ export class TooltipProvider {
112
150
 
113
151
  const virtualEl: VirtualElement = {
114
152
  getBoundingClientRect: () => posToDOMRect(view, from, to),
153
+ contextElement: view.dom,
154
+ }
155
+
156
+ if (this.#shouldAutoUpdate(view)) {
157
+ this.#cleanupAutoUpdate = autoUpdate(virtualEl, this.element, () =>
158
+ this.#updatePosition(virtualEl)
159
+ )
160
+ } else {
161
+ this.#updatePosition(virtualEl)
115
162
  }
116
- computePosition(virtualEl, this.element, {
117
- placement: this.#floatingUIOptions.placement ?? 'top',
118
- middleware: [
119
- flip(),
120
- offset(this.#offset),
121
- shift(this.#shift),
122
- ...this.#middleware,
123
- ],
124
- })
125
- .then(({ x, y }) => {
126
- Object.assign(this.element.style, {
127
- left: `${x}px`,
128
- top: `${y}px`,
129
- })
130
- })
131
- .catch(console.error)
132
163
 
133
164
  this.show()
134
165
  }
@@ -160,31 +191,27 @@ export class TooltipProvider {
160
191
 
161
192
  /// Destroy the tooltip.
162
193
  destroy = () => {
194
+ this.#cleanupAutoUpdate?.()
163
195
  this.#updater.cancel()
164
196
  }
165
197
 
166
198
  /// Show the tooltip.
167
- show = (virtualElement?: VirtualElement) => {
199
+ show = (virtualElement?: VirtualElement, editorView?: EditorView) => {
168
200
  this.element.dataset.show = 'true'
169
201
 
170
202
  if (virtualElement) {
171
- computePosition(virtualElement, this.element, {
172
- placement: 'top',
173
- middleware: [
174
- flip(),
175
- offset(this.#offset),
176
- shift(this.#shift),
177
- ...this.#middleware,
178
- ],
179
- ...this.#floatingUIOptions,
180
- })
181
- .then(({ x, y }) => {
182
- Object.assign(this.element.style, {
183
- left: `${x}px`,
184
- top: `${y}px`,
185
- })
186
- })
187
- .catch(console.error)
203
+ this.#cleanupAutoUpdate?.()
204
+ this.#cleanupAutoUpdate = void 0
205
+
206
+ const reference = { ...virtualElement, contextElement: editorView?.dom }
207
+
208
+ if (editorView && this.#shouldAutoUpdate(editorView)) {
209
+ this.#cleanupAutoUpdate = autoUpdate(reference, this.element, () =>
210
+ this.#updatePosition(reference)
211
+ )
212
+ } else {
213
+ this.#updatePosition(reference)
214
+ }
188
215
  }
189
216
 
190
217
  this.onShow()