Skip to content

Commit 96f56aa

Browse files
authored
feat: 🎸 allow options to floating ui (#1578)
* feat: 🎸 allow options for floating ui ✅ Closes: #1436 * chore: f
1 parent 25e847f commit 96f56aa

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

Diff for: packages/plugins/plugin-block/src/block-provider.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import type { Ctx } from '@milkdown/ctx'
22
import type { EditorState } from '@milkdown/prose/state'
33
import type { EditorView } from '@milkdown/prose/view'
44

5-
import type { Placement, VirtualElement } from '@floating-ui/dom'
5+
import type {
6+
ComputePositionConfig,
7+
Middleware,
8+
Placement,
9+
VirtualElement,
10+
} from '@floating-ui/dom'
611
import { computePosition, flip, offset } from '@floating-ui/dom'
712

813
import { editorViewCtx } from '@milkdown/core'
@@ -38,6 +43,10 @@ export interface BlockProviderOptions {
3843
getPosition?: (deriveContext: DeriveContext) => Omit<DOMRect, 'toJSON'>
3944
/// The function to get the placement of the block. Default is 'left'.
4045
getPlacement?: (deriveContext: DeriveContext) => Placement
46+
/// Other middlewares for floating ui. This will be added after the internal middlewares.
47+
middleware?: Middleware[]
48+
/// Options for floating ui. If you pass `middleware` or `placement`, it will override the internal settings.
49+
floatingUIOptions?: Partial<ComputePositionConfig>
4150
}
4251

4352
/// A provider for creating block.
@@ -57,6 +66,12 @@ export class BlockProvider {
5766
/// @internal
5867
#initialized = false
5968

69+
/// @internal
70+
readonly #middleware: Middleware[]
71+
72+
/// @internal
73+
readonly #floatingUIOptions: Partial<ComputePositionConfig>
74+
6075
/// @internal
6176
readonly #getOffset?: (deriveContext: DeriveContext) =>
6277
| number
@@ -85,6 +100,8 @@ export class BlockProvider {
85100
this.#getOffset = options.getOffset
86101
this.#getPosition = options.getPosition
87102
this.#getPlacement = options.getPlacement
103+
this.#middleware = options.middleware ?? []
104+
this.#floatingUIOptions = options.floatingUIOptions ?? {}
88105
this.hide()
89106
}
90107

@@ -159,7 +176,8 @@ export class BlockProvider {
159176
placement: this.#getPlacement
160177
? this.#getPlacement(deriveContext)
161178
: 'left',
162-
middleware,
179+
middleware: [...middleware, ...this.#middleware],
180+
...this.#floatingUIOptions,
163181
}).then(({ x, y }) => {
164182
Object.assign(this.#element.style, {
165183
left: `${x}px`,

Diff for: packages/plugins/plugin-slash/src/slash-provider.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type { Node } from '@milkdown/prose/model'
44
import { TextSelection } from '@milkdown/prose/state'
55
import type { EditorView } from '@milkdown/prose/view'
66
import debounce from 'lodash.debounce'
7-
import type { VirtualElement } from '@floating-ui/dom'
7+
import type {
8+
ComputePositionConfig,
9+
Middleware,
10+
VirtualElement,
11+
} from '@floating-ui/dom'
812
import { computePosition, flip, offset } from '@floating-ui/dom'
913

1014
/// Options for slash provider.
@@ -25,6 +29,10 @@ export interface SlashProviderOptions {
2529
crossAxis?: number
2630
alignmentAxis?: number | null
2731
}
32+
/// Other middlewares for floating ui. This will be added after the internal middlewares.
33+
middleware?: Middleware[]
34+
/// Options for floating ui. If you pass `middleware` or `placement`, it will override the internal settings.
35+
floatingUIOptions?: Partial<ComputePositionConfig>
2836
}
2937

3038
/// A provider for creating slash.
@@ -35,6 +43,12 @@ export class SlashProvider {
3543
/// @internal
3644
#initialized = false
3745

46+
/// @internal
47+
readonly #middleware: Middleware[]
48+
49+
/// @internal
50+
readonly #floatingUIOptions: Partial<ComputePositionConfig>
51+
3852
/// @internal
3953
readonly #debounce: number
4054

@@ -65,6 +79,8 @@ export class SlashProvider {
6579
this.#shouldShow = options.shouldShow ?? this.#_shouldShow
6680
this.#trigger = options.trigger ?? '/'
6781
this.#offset = options.offset
82+
this.#middleware = options.middleware ?? []
83+
this.#floatingUIOptions = options.floatingUIOptions ?? {}
6884
}
6985

7086
/// @internal
@@ -94,7 +110,8 @@ export class SlashProvider {
94110
}
95111
computePosition(virtualEl, this.element, {
96112
placement: 'bottom-start',
97-
middleware: [flip(), offset(this.#offset)],
113+
middleware: [flip(), offset(this.#offset), ...this.#middleware],
114+
...this.#floatingUIOptions,
98115
}).then(({ x, y }) => {
99116
Object.assign(this.element.style, {
100117
left: `${x}px`,

Diff for: packages/plugins/plugin-tooltip/src/tooltip-provider.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import type { EditorState } from '@milkdown/prose/state'
22
import { TextSelection } from '@milkdown/prose/state'
33
import type { EditorView } from '@milkdown/prose/view'
44
import debounce from 'lodash.debounce'
5-
import type { VirtualElement } from '@floating-ui/dom'
5+
import type {
6+
ComputePositionConfig,
7+
Middleware,
8+
VirtualElement,
9+
} from '@floating-ui/dom'
610
import { computePosition, flip, offset, shift } from '@floating-ui/dom'
711
import { posToDOMRect } from '@milkdown/prose'
812

@@ -22,6 +26,10 @@ export interface TooltipProviderOptions {
2226
crossAxis?: number
2327
alignmentAxis?: number | null
2428
}
29+
/// Other middlewares for floating ui. This will be added after the internal middlewares.
30+
middleware?: Middleware[]
31+
/// Options for floating ui. If you pass `middleware` or `placement`, it will override the internal settings.
32+
floatingUIOptions?: Partial<ComputePositionConfig>
2533
}
2634

2735
/// A provider for creating tooltip.
@@ -32,6 +40,12 @@ export class TooltipProvider {
3240
/// @internal
3341
readonly #shouldShow: (view: EditorView, prevState?: EditorState) => boolean
3442

43+
/// @internal
44+
readonly #middleware: Middleware[]
45+
46+
/// @internal
47+
readonly #floatingUIOptions: Partial<ComputePositionConfig>
48+
3549
/// @internal
3650
#initialized = false
3751

@@ -58,6 +72,8 @@ export class TooltipProvider {
5872
this.#debounce = options.debounce ?? 200
5973
this.#shouldShow = options.shouldShow ?? this.#_shouldShow
6074
this.#offset = options.offset
75+
this.#middleware = options.middleware ?? []
76+
this.#floatingUIOptions = options.floatingUIOptions ?? {}
6177
this.element.dataset.show = 'false'
6278
}
6379

@@ -88,7 +104,7 @@ export class TooltipProvider {
88104
}
89105
computePosition(virtualEl, this.element, {
90106
placement: 'top',
91-
middleware: [flip(), offset(this.#offset), shift()],
107+
middleware: [flip(), offset(this.#offset), shift(), ...this.#middleware],
92108
}).then(({ x, y }) => {
93109
Object.assign(this.element.style, {
94110
left: `${x}px`,
@@ -137,6 +153,7 @@ export class TooltipProvider {
137153
computePosition(virtualElement, this.element, {
138154
placement: 'top',
139155
middleware: [flip(), offset(this.#offset)],
156+
...this.#floatingUIOptions,
140157
}).then(({ x, y }) => {
141158
Object.assign(this.element.style, {
142159
left: `${x}px`,

0 commit comments

Comments
 (0)