Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@milkdown/monorepo",
"version": "0.0.0",
"private": true,
"packageManager": "pnpm@10.25.0",
"packageManager": "pnpm@10.26.0",
"engines": {
"node": ">=22"
},
Expand Down
7 changes: 4 additions & 3 deletions packages/components/src/link-tooltip/edit/edit-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ export class LinkEditTooltip implements PluginView {
view.dispatch(
view.state.tr.setSelection(TextSelection.create(view.state.doc, from, to))
)
this.#provider.show({
getBoundingClientRect: () => posToDOMRect(view, from, to),
})
this.#provider.show(
{ getBoundingClientRect: () => posToDOMRect(view, from, to) },
view
)
requestAnimationFrame(() => {
this.#content.querySelector('input')?.focus()
})
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/link-tooltip/preview/preview-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export class LinkPreviewTooltip implements PluginView {
#onEdit = ref(() => {})
#onRemove = ref(() => {})
#app: App
#editorView: EditorView

#hovering = false

constructor(
readonly ctx: Ctx,
view: EditorView
) {
this.#editorView = view
this.#config = ref(this.ctx.get(linkTooltipConfig.key))
this.#app = createApp(PreviewLink, {
config: this.#config,
Expand Down Expand Up @@ -77,9 +79,7 @@ export class LinkPreviewTooltip implements PluginView {
this.#hide()
}

this.#provider.show({
getBoundingClientRect: () => rect,
})
this.#provider.show({ getBoundingClientRect: () => rect }, this.#editorView)
this.#provider.element.addEventListener('mouseenter', this.#onMouseEnter)
this.#provider.element.addEventListener('mouseleave', this.#onMouseLeave)
}
Expand Down
9 changes: 9 additions & 0 deletions packages/plugins/plugin-block/src/block-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ export class BlockService {
dom.addEventListener('mousedown', this.#handleMouseDown)
dom.addEventListener('mouseup', this.#handleMouseUp)
dom.addEventListener('dragstart', this.#handleDragStart)
dom.addEventListener('dragend', this.#handleDragEnd)
}

/// Remove mouse event to the dom.
removeEvent = (dom: HTMLElement) => {
dom.removeEventListener('mousedown', this.#handleMouseDown)
dom.removeEventListener('mouseup', this.#handleMouseUp)
dom.removeEventListener('dragstart', this.#handleDragStart)
dom.removeEventListener('dragend', this.#handleDragEnd)
}

/// Unbind the notify function.
Expand Down Expand Up @@ -171,6 +173,13 @@ export class BlockService {
}
}

/// @internal
#handleDragEnd = () => {
if (this.#view) {
this.#dragEnd(this.#view)
}
}

/// @internal
keydownCallback = (view: EditorView) => {
this.#hide()
Expand Down
97 changes: 62 additions & 35 deletions packages/plugins/plugin-tooltip/src/tooltip-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import type {
import type { EditorState } from '@milkdown/prose/state'
import type { EditorView } from '@milkdown/prose/view'

import { computePosition, flip, offset, shift } from '@floating-ui/dom'
import {
autoUpdate,
computePosition,
flip,
offset,
shift,
} from '@floating-ui/dom'
import { posToDOMRect } from '@milkdown/prose'
import { TextSelection } from '@milkdown/prose/state'
import { throttle } from 'lodash-es'
Expand Down Expand Up @@ -53,6 +59,9 @@ export class TooltipProvider {
/// @internal
#initialized = false

/// @internal
#cleanupAutoUpdate?: () => void

/// @internal
readonly #offset?: OffsetOptions

Expand Down Expand Up @@ -87,6 +96,32 @@ export class TooltipProvider {
this.#updater = throttle(this.#onUpdate, this.#debounce)
}

/// @internel
#updatePosition = (reference: VirtualElement) => {
computePosition(reference, this.element, {
placement: this.#floatingUIOptions.placement ?? 'top',
middleware: [
flip(),
offset(this.#offset),
shift(this.#shift),
...this.#middleware,
],
...this.#floatingUIOptions,
})
.then(({ x, y }) => {
Object.assign(this.element.style, {
left: `${x}px`,
top: `${y}px`,
})
})
.catch(console.error)
}

/// @internal
#shouldAutoUpdate = (editorView: EditorView) => {
return this.#root !== editorView.dom.parentElement
}

/// @internal
#onUpdate = (view: EditorView, prevState?: EditorState): void => {
const { state, composing } = view
Expand All @@ -105,30 +140,26 @@ export class TooltipProvider {

if (composing || isSame) return

this.#cleanupAutoUpdate?.()
this.#cleanupAutoUpdate = void 0

if (!this.#shouldShow(view, prevState)) {
this.hide()
return
}

const virtualEl: VirtualElement = {
getBoundingClientRect: () => posToDOMRect(view, from, to),
contextElement: view.dom,
}

if (this.#shouldAutoUpdate(view)) {
this.#cleanupAutoUpdate = autoUpdate(virtualEl, this.element, () =>
this.#updatePosition(virtualEl)
)
} else {
this.#updatePosition(virtualEl)
}
computePosition(virtualEl, this.element, {
placement: this.#floatingUIOptions.placement ?? 'top',
middleware: [
flip(),
offset(this.#offset),
shift(this.#shift),
...this.#middleware,
],
})
.then(({ x, y }) => {
Object.assign(this.element.style, {
left: `${x}px`,
top: `${y}px`,
})
})
.catch(console.error)

this.show()
}
Expand Down Expand Up @@ -160,31 +191,27 @@ export class TooltipProvider {

/// Destroy the tooltip.
destroy = () => {
this.#cleanupAutoUpdate?.()
this.#updater.cancel()
}

/// Show the tooltip.
show = (virtualElement?: VirtualElement) => {
show = (virtualElement?: VirtualElement, editorView?: EditorView) => {
this.element.dataset.show = 'true'

if (virtualElement) {
computePosition(virtualElement, this.element, {
placement: 'top',
middleware: [
flip(),
offset(this.#offset),
shift(this.#shift),
...this.#middleware,
],
...this.#floatingUIOptions,
})
.then(({ x, y }) => {
Object.assign(this.element.style, {
left: `${x}px`,
top: `${y}px`,
})
})
.catch(console.error)
this.#cleanupAutoUpdate?.()
this.#cleanupAutoUpdate = void 0

const reference = { ...virtualElement, contextElement: editorView?.dom }

if (editorView && this.#shouldAutoUpdate(editorView)) {
this.#cleanupAutoUpdate = autoUpdate(reference, this.element, () =>
this.#updatePosition(reference)
)
} else {
this.#updatePosition(reference)
}
}

this.onShow()
Expand Down
14 changes: 13 additions & 1 deletion packages/plugins/preset-gfm/src/node/table/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,19 @@ export const tableHeaderRowSchema = $nodeSchema('table_header_row', () => ({
...originalSchema.table_row,
disableDropCursor: true,
content: '(table_header)*',
parseDOM: [{ tag: 'tr[data-is-header]' }],
parseDOM: [
{ tag: 'tr[data-is-header]' },
{
tag: 'tr',
getAttrs: (dom: HTMLElement) => {
if (dom instanceof HTMLElement) {
const hasHeader = dom.querySelector('th')
return hasHeader ? {} : false
}
return false
},
},
],
toDOM() {
return ['tr', { 'data-is-header': true }, 0]
},
Expand Down
Loading