Skip to content

Commit

Permalink
fix: disable insert table when in table
Browse files Browse the repository at this point in the history
Fixes #686
  • Loading branch information
petyosi committed Feb 7, 2025
1 parent 11706e6 commit 3fd2df7
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/plugins/toolbar/components/InsertTable.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { ButtonWithTooltip } from '.././primitives/toolbar'
import React from 'react'
import { insertTable$ } from '../../table'
import { useCellValue, usePublisher } from '@mdxeditor/gurx'
import { iconComponentFor$, useTranslation } from '../../core'
import { Cell, map, useCellValue, usePublisher } from '@mdxeditor/gurx'
import { activeEditor$, iconComponentFor$, useTranslation } from '../../core'

const disableInsertTableButton$ = Cell<boolean>(false, (r) => {
r.link(
r.pipe(
activeEditor$,
map((editor) => ['td', 'th'].includes(editor?.getRootElement()?.parentNode?.nodeName.toLowerCase() ?? ''))
),
disableInsertTableButton$
)
})

/**
* A toolbar button that allows the user to insert a table.
Expand All @@ -14,12 +24,16 @@ export const InsertTable: React.FC = () => {
const insertTable = usePublisher(insertTable$)
const t = useTranslation()

// Do not allow inserting a table inside a table cell, markdown does not support it
const isDisabled = useCellValue(disableInsertTableButton$)

return (
<ButtonWithTooltip
title={t('toolbar.table', 'Insert Table')}
onClick={() => {
insertTable({ rows: 3, columns: 3 })
}}
{...(isDisabled ? { 'aria-disabled': true, 'data-disabled': true, disabled: true } : {})}
>
{iconComponentFor('table')}
</ButtonWithTooltip>
Expand Down

0 comments on commit 3fd2df7

Please sign in to comment.