Skip to content

Commit

Permalink
feat: add readonly support for table (#1416)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone authored Jul 11, 2024
1 parent d16a763 commit d05d957
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './code-block'
export * from './link-tooltip'
export * from './image-inline'
export * from './table-block'
export { html } from 'atomico'
5 changes: 4 additions & 1 deletion packages/components/src/table-block/view/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Node } from '@milkdown/prose/model'
import type { EditorView } from '@milkdown/prose/view'
import type { Ctx } from '@milkdown/ctx'

import clsx from 'clsx'
import type { TableBlockConfig } from '../config'
import { useDragHandlers } from './drag'
import type { CellIndex, DragInfo, Refs } from './types'
Expand Down Expand Up @@ -62,7 +63,8 @@ export const tableComponent: Component<TableComponentProps> = ({
if (contentDOM)
current.appendChild(contentDOM)

recoveryStateBetweenUpdate(refs, ctx, node)
if (view?.editable)
recoveryStateBetweenUpdate(refs, ctx, node)
}, [])

const { pointerLeave, pointerMove } = usePointerHandlers(refs, view)
Expand All @@ -78,6 +80,7 @@ export const tableComponent: Component<TableComponentProps> = ({

return html`
<host
class=${clsx(!view?.editable && 'readonly')}
ondragstart=${(e: DragEvent) => e.preventDefault()}
ondragover=${(e: DragEvent) => e.preventDefault()}
ondragleave=${(e: DragEvent) => e.preventDefault()}
Expand Down
20 changes: 12 additions & 8 deletions packages/components/src/table-block/view/drag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import throttle from 'lodash.throttle'
import { computePosition, offset } from '@floating-ui/dom'
import { useEffect, useHost, useMemo } from 'atomico'
import { commandsCtx } from '@milkdown/core'
import { commandsCtx, editorViewCtx } from '@milkdown/core'
import { moveColCommand, moveRowCommand, selectColCommand, selectRowCommand } from '@milkdown/preset-gfm'
import type { Ctx } from '@milkdown/ctx'
import { computeColHandlePositionByIndex, computeRowHandlePositionByIndex, getRelatedDOM } from './utils'
Expand Down Expand Up @@ -61,7 +61,11 @@ function prepareDndContext(refs: Refs): DragContext | undefined {
return context
}

function handleDrag(refs: Refs, event: DragEvent, fn: (context: DragContext) => void) {
function handleDrag(refs: Refs, event: DragEvent, ctx: Ctx | undefined, fn: (context: DragContext) => void) {
const view = ctx?.get(editorViewCtx)
if (!view?.editable)
return

event.stopPropagation()
if (event.dataTransfer)
event.dataTransfer.effectAllowed = 'move'
Expand All @@ -78,9 +82,9 @@ function handleDrag(refs: Refs, event: DragEvent, fn: (context: DragContext) =>
})
}

export function createDragRowHandler(refs: Refs) {
export function createDragRowHandler(refs: Refs, ctx?: Ctx) {
return (event: DragEvent) => {
handleDrag(refs, event, ({
handleDrag(refs, event, ctx, ({
preview,
content,
previewRoot,
Expand Down Expand Up @@ -127,9 +131,9 @@ export function createDragRowHandler(refs: Refs) {
}
}

export function createDragColHandler(refs: Refs) {
export function createDragColHandler(refs: Refs, ctx?: Ctx) {
return (event: DragEvent) => {
handleDrag(refs, event, ({
handleDrag(refs, event, ctx, ({
preview,
content,
previewRoot,
Expand Down Expand Up @@ -351,8 +355,8 @@ export function useDragHandlers(
const host = useHost()
const root = useMemo(() => host.current.getRootNode() as HTMLElement, [host])

const dragRow = useMemo(() => createDragRowHandler(refs), [refs])
const dragCol = useMemo(() => createDragColHandler(refs), [refs])
const dragRow = useMemo(() => createDragRowHandler(refs, ctx), [refs])
const dragCol = useMemo(() => createDragColHandler(refs, ctx), [refs])

useEffect(() => {
const onDragEnd = () => {
Expand Down
16 changes: 15 additions & 1 deletion packages/components/src/table-block/view/operation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback } from 'atomico'
import { commandsCtx } from '@milkdown/core'
import { commandsCtx, editorViewCtx } from '@milkdown/core'
import {
addColAfterCommand,
addColBeforeCommand,
Expand Down Expand Up @@ -34,6 +34,9 @@ export function useOperation(refs: Refs, ctx?: Ctx, getPos?: () => number | unde
if (rowIndex < 0)
return

if (!ctx.get(editorViewCtx).editable)
return

const rows = Array.from(contentWrapperRef.current?.querySelectorAll('tr') ?? [])
const commands = ctx.get(commandsCtx)
const pos = (getPos?.() ?? 0) + 1
Expand Down Expand Up @@ -61,6 +64,9 @@ export function useOperation(refs: Refs, ctx?: Ctx, getPos?: () => number | unde
if (colIndex < 0)
return

if (!ctx.get(editorViewCtx).editable)
return

const cols = Array.from(contentWrapperRef.current?.querySelector('tr')?.children ?? [])
const commands = ctx.get(commandsCtx)

Expand Down Expand Up @@ -104,6 +110,10 @@ export function useOperation(refs: Refs, ctx?: Ctx, getPos?: () => number | unde
const deleteSelected = useCallback((e: PointerEvent) => {
if (!ctx)
return

if (!ctx.get(editorViewCtx).editable)
return

e.preventDefault()
e.stopPropagation()
const commands = ctx.get(commandsCtx)
Expand All @@ -114,6 +124,10 @@ export function useOperation(refs: Refs, ctx?: Ctx, getPos?: () => number | unde
(e: PointerEvent) => {
if (!ctx)
return

if (!ctx.get(editorViewCtx).editable)
return

e.preventDefault()
e.stopPropagation()
const commands = ctx.get(commandsCtx)
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/table-block/view/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {

export function createPointerMoveHandler(refs: Refs, view?: EditorView): (e: PointerEvent) => void {
return throttle((e: PointerEvent) => {
if (!view?.editable)
return
const {
contentWrapperRef,
yLineHandleRef,
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/table-block/view/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class TableNodeView implements NodeView {

#handleClick(event: PointerEvent) {
const view = this.view
if (!view.editable)
return false

const { state, dispatch } = view
const pos = view.posAtCoords({ left: event.clientX, top: event.clientY })

Expand Down
11 changes: 10 additions & 1 deletion packages/crepe/src/theme/common/table.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

.cell-handle {
z-index: 50;
left: -999px;
top: -999px;
cursor: grab;
background-color: var(--crepe-color-surface);
color: var(--crepe-color-outline);
Expand Down Expand Up @@ -128,15 +130,18 @@
.line-handle {
z-index: 20;
background-color: var(--crepe-color-primary);
&:hover{
opacity: 1;
}
.add-button {
cursor: pointer;
background-color: var(--crepe-color-surface);
color: var(--crepe-color-outline);
border-radius: 100px;
box-shadow: var(--crepe-shadow-1);
transition: background-color 0.2s ease-in-out;

&:hover {
transition: background-color 0.2s ease-in-out;
background-color: var(--crepe-color-hover);
}
}
Expand Down Expand Up @@ -166,5 +171,9 @@
}
}
}

&.readonly .handle {
display: none;
}
}
}

0 comments on commit d05d957

Please sign in to comment.