Skip to content

Commit

Permalink
fix: 🐛 block plugin display and exception bugs (#1485)
Browse files Browse the repository at this point in the history
* fix: 🐛 block plugin display and exception bugs

* chore: optimize
  • Loading branch information
Saul-Mirone authored Aug 24, 2024
1 parent 53288c5 commit 705c263
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 61 deletions.
8 changes: 8 additions & 0 deletions .changeset/green-spies-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@milkdown/plugin-block": patch
"@milkdown/components": patch
"@milkdown/crepe": patch
"@milkdown/kit": patch
---

Fix plugin block bugs
46 changes: 26 additions & 20 deletions packages/components/src/table-block/view/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,37 @@ export function findNodeIndex(parent: Node, child: Node) {
export function findPointerIndex(event: PointerEvent, view?: EditorView): CellIndex | undefined {
if (!view)
return
const posAtCoords = view.posAtCoords({ left: event.clientX, top: event.clientY })
if (!posAtCoords)
return
const pos = posAtCoords?.inside
if (pos == null || pos < 0)
return

const $pos = view.state.doc.resolve(pos)
const node = view.state.doc.nodeAt(pos)
if (!node)
return
try {
const posAtCoords = view.posAtCoords({ left: event.clientX, top: event.clientY })
if (!posAtCoords)
return
const pos = posAtCoords?.inside
if (pos == null || pos < 0)
return

const cellType = ['table_cell', 'table_header']
const rowType = ['table_row', 'table_header_row']
const $pos = view.state.doc.resolve(pos)
const node = view.state.doc.nodeAt(pos)
if (!node)
return

const cell = cellType.includes(node.type.name) ? node : findParent(node => cellType.includes(node.type.name))($pos)?.node
const row = findParent(node => rowType.includes(node.type.name))($pos)?.node
const table = findParent(node => node.type.name === 'table')($pos)?.node
if (!cell || !row || !table)
return
const cellType = ['table_cell', 'table_header']
const rowType = ['table_row', 'table_header_row']

const cell = cellType.includes(node.type.name) ? node : findParent(node => cellType.includes(node.type.name))($pos)?.node
const row = findParent(node => rowType.includes(node.type.name))($pos)?.node
const table = findParent(node => node.type.name === 'table')($pos)?.node
if (!cell || !row || !table)
return

const columnIndex = findNodeIndex(row, cell)
const rowIndex = findNodeIndex(table, row)
const columnIndex = findNodeIndex(row, cell)
const rowIndex = findNodeIndex(table, row)

return [rowIndex, columnIndex]
return [rowIndex, columnIndex]
}
catch {
return undefined
}
}

export function getRelatedDOM(contentWrapperRef: Ref<HTMLDivElement>, [rowIndex, columnIndex]: CellIndex) {
Expand Down
17 changes: 15 additions & 2 deletions packages/crepe/src/feature/block-edit/handle/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { PluginView } from '@milkdown/kit/prose/state'
import { TextSelection } from '@milkdown/kit/prose/state'
import { BlockProvider, block } from '@milkdown/kit/plugin/block'
import { BlockProvider, block, blockConfig } from '@milkdown/kit/plugin/block'
import type { Ctx } from '@milkdown/kit/ctx'
import type { AtomicoThis } from 'atomico/types/dom'
import { editorViewCtx } from '@milkdown/kit/core'
import { paragraphSchema } from '@milkdown/kit/preset/commonmark'
import { findParent } from '@milkdown/kit/prose'
import { menuAPI } from '../menu'
import { defIfNotExists } from '../../../utils'
import type { BlockEditFeatureConfig } from '../index'
Expand All @@ -28,6 +29,9 @@ export class BlockHandleView implements PluginView {
content,
getOffset: () => 16,
getPlacement: ({ active, blockDom }) => {
if (active.node.type.name === 'heading')
return 'left'

let totalDescendant = 0
active.node.descendants((node) => {
totalDescendant += node.childCount
Expand All @@ -40,7 +44,7 @@ export class BlockHandleView implements PluginView {
const paddingBottom = Number.parseInt(style.paddingBottom, 10) || 0
const height = domRect.height - paddingTop - paddingBottom
const handleHeight = handleRect.height
return totalDescendant > 2 || handleHeight * 2 < height ? 'left-start' : 'left'
return totalDescendant > 2 || handleHeight < height ? 'left-start' : 'left'
},
})
this.update()
Expand Down Expand Up @@ -79,6 +83,15 @@ export class BlockHandleView implements PluginView {

defIfNotExists('milkdown-block-handle', BlockHandleElement)
export function configureBlockHandle(ctx: Ctx, config?: BlockEditFeatureConfig) {
ctx.set(blockConfig.key, {
filterNodes: (pos) => {
const filter = findParent(node => ['table', 'blockquote'].includes(node.type.name))(pos)
if (filter)
return false

return true
},
})
ctx.set(block.key, {
view: () => new BlockHandleView(ctx, config),
})
Expand Down
1 change: 1 addition & 0 deletions packages/crepe/src/theme/common/block-edit.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
milkdown-block-handle {
&[data-show='false'] {
opacity: 0;
pointer-events: none;
}
transition: all 0.2s;
position: absolute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,44 @@ export function selectRootNodeByDom(view: EditorView, coords: { x: number, y: nu
if (!root)
return null

const pos = view.posAtCoords({
left: coords.x,
top: coords.y,
})?.inside
if (pos == null || pos < 0)
return null

let $pos = view.state.doc.resolve(pos)
let node = view.state.doc.nodeAt(pos)
let element = view.nodeDOM(pos) as HTMLElement | null

const filter = (needLookup: boolean) => {
const checkDepth = $pos.depth >= 1 && $pos.index($pos.depth) === 0
const shouldLookUp = needLookup || checkDepth

if (!shouldLookUp)
return

const ancestorPos = $pos.before($pos.depth)
node = view.state.doc.nodeAt(ancestorPos)
element = view.nodeDOM(ancestorPos) as HTMLElement | null
$pos = view.state.doc.resolve(ancestorPos)

if (!filterNodes($pos, node!))
filter(true)
try {
const pos = view.posAtCoords({
left: coords.x,
top: coords.y,
})?.inside
if (pos == null || pos < 0)
return null

let $pos = view.state.doc.resolve(pos)
let node = view.state.doc.nodeAt(pos)
let element = view.nodeDOM(pos) as HTMLElement | null

const filter = (needLookup: boolean) => {
const checkDepth = $pos.depth >= 1 && $pos.index($pos.depth) === 0
const shouldLookUp = needLookup || checkDepth

if (!shouldLookUp)
return

const ancestorPos = $pos.before($pos.depth)
node = view.state.doc.nodeAt(ancestorPos)
element = view.nodeDOM(ancestorPos) as HTMLElement | null
$pos = view.state.doc.resolve(ancestorPos)

if (!filterNodes($pos, node!))
filter(true)
}

// If filterNodes returns false, we should look up the parent node.
const filterResult = filterNodes($pos, node!)
filter(!filterResult)

if (!element || !node)
return null

return { node, $pos, el: element }
}

// If filterNodes returns false, we should look up the parent node.
const filterResult = filterNodes($pos, node!)
filter(!filterResult)

if (!element || !node)
catch {
return null

return { node, $pos, el: element }
}
}
6 changes: 0 additions & 6 deletions packages/plugins/plugin-block/src/block-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,5 @@ export class BlockProvider {
/// Hide the block.
hide = () => {
this.#element.dataset.show = 'false'
setTimeout(() => {
Object.assign(this.#element.style, {
left: `-999px`,
top: `-999px`,
})
}, 200)
}
}
4 changes: 4 additions & 0 deletions storybook/stories/crepe/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ const crepe = new Crepe({
>
> No escape from [reality](https://en.wikipedia.org/wiki/Bohemian_Rhapsody).
Open your eyes, look up to the skies and see.
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low.
| Fruit | Animal | Vegetable |
| ----- | :----: | --------: |
| 🍎 | 🐱 | 🥕 |
Expand Down

0 comments on commit 705c263

Please sign in to comment.