Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 allow options to floating ui #1578

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/plugins/plugin-block/src/block-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import type { Ctx } from '@milkdown/ctx'
import type { EditorState } from '@milkdown/prose/state'
import type { EditorView } from '@milkdown/prose/view'

import type { Placement, VirtualElement } from '@floating-ui/dom'
import type {
ComputePositionConfig,
Middleware,
Placement,
VirtualElement,
} from '@floating-ui/dom'
import { computePosition, flip, offset } from '@floating-ui/dom'

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

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

/// @internal
readonly #middleware: Middleware[]

/// @internal
readonly #floatingUIOptions: Partial<ComputePositionConfig>

/// @internal
readonly #getOffset?: (deriveContext: DeriveContext) =>
| number
Expand Down Expand Up @@ -85,6 +100,8 @@ export class BlockProvider {
this.#getOffset = options.getOffset
this.#getPosition = options.getPosition
this.#getPlacement = options.getPlacement
this.#middleware = options.middleware ?? []
this.#floatingUIOptions = options.floatingUIOptions ?? {}
this.hide()
}

Expand Down Expand Up @@ -159,7 +176,8 @@ export class BlockProvider {
placement: this.#getPlacement
? this.#getPlacement(deriveContext)
: 'left',
middleware,
middleware: [...middleware, ...this.#middleware],
...this.#floatingUIOptions,
}).then(({ x, y }) => {
Object.assign(this.#element.style, {
left: `${x}px`,
Expand Down
21 changes: 19 additions & 2 deletions packages/plugins/plugin-slash/src/slash-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type { Node } from '@milkdown/prose/model'
import { TextSelection } from '@milkdown/prose/state'
import type { EditorView } from '@milkdown/prose/view'
import debounce from 'lodash.debounce'
import type { VirtualElement } from '@floating-ui/dom'
import type {
ComputePositionConfig,
Middleware,
VirtualElement,
} from '@floating-ui/dom'
import { computePosition, flip, offset } from '@floating-ui/dom'

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

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

/// @internal
readonly #middleware: Middleware[]

/// @internal
readonly #floatingUIOptions: Partial<ComputePositionConfig>

/// @internal
readonly #debounce: number

Expand Down Expand Up @@ -65,6 +79,8 @@ export class SlashProvider {
this.#shouldShow = options.shouldShow ?? this.#_shouldShow
this.#trigger = options.trigger ?? '/'
this.#offset = options.offset
this.#middleware = options.middleware ?? []
this.#floatingUIOptions = options.floatingUIOptions ?? {}
}

/// @internal
Expand Down Expand Up @@ -94,7 +110,8 @@ export class SlashProvider {
}
computePosition(virtualEl, this.element, {
placement: 'bottom-start',
middleware: [flip(), offset(this.#offset)],
middleware: [flip(), offset(this.#offset), ...this.#middleware],
...this.#floatingUIOptions,
}).then(({ x, y }) => {
Object.assign(this.element.style, {
left: `${x}px`,
Expand Down
21 changes: 19 additions & 2 deletions packages/plugins/plugin-tooltip/src/tooltip-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import type { EditorState } from '@milkdown/prose/state'
import { TextSelection } from '@milkdown/prose/state'
import type { EditorView } from '@milkdown/prose/view'
import debounce from 'lodash.debounce'
import type { VirtualElement } from '@floating-ui/dom'
import type {
ComputePositionConfig,
Middleware,
VirtualElement,
} from '@floating-ui/dom'
import { computePosition, flip, offset, shift } from '@floating-ui/dom'
import { posToDOMRect } from '@milkdown/prose'

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

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

/// @internal
readonly #middleware: Middleware[]

/// @internal
readonly #floatingUIOptions: Partial<ComputePositionConfig>

/// @internal
#initialized = false

Expand All @@ -58,6 +72,8 @@ export class TooltipProvider {
this.#debounce = options.debounce ?? 200
this.#shouldShow = options.shouldShow ?? this.#_shouldShow
this.#offset = options.offset
this.#middleware = options.middleware ?? []
this.#floatingUIOptions = options.floatingUIOptions ?? {}
this.element.dataset.show = 'false'
}

Expand Down Expand Up @@ -88,7 +104,7 @@ export class TooltipProvider {
}
computePosition(virtualEl, this.element, {
placement: 'top',
middleware: [flip(), offset(this.#offset), shift()],
middleware: [flip(), offset(this.#offset), shift(), ...this.#middleware],
}).then(({ x, y }) => {
Object.assign(this.element.style, {
left: `${x}px`,
Expand Down Expand Up @@ -137,6 +153,7 @@ export class TooltipProvider {
computePosition(virtualElement, this.element, {
placement: 'top',
middleware: [flip(), offset(this.#offset)],
...this.#floatingUIOptions,
}).then(({ x, y }) => {
Object.assign(this.element.style, {
left: `${x}px`,
Expand Down
Loading