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

UBERF-8555: cleaning up broken tables #7848

Merged
merged 1 commit into from
Jan 31, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
// limitations under the License.
//

import textEditor, { type ActionContext } from '@hcengineering/text-editor'
import { getEventPositionElement, SelectPopup, showPopup } from '@hcengineering/ui'
import { type Editor } from '@tiptap/core'
import TiptapTable from '@tiptap/extension-table'
import { CellSelection } from '@tiptap/pm/tables'
import { getEventPositionElement, SelectPopup, showPopup } from '@hcengineering/ui'
import textEditor, { type ActionContext } from '@hcengineering/text-editor'
import { CellSelection, TableMap } from '@tiptap/pm/tables'

import { SvelteNodeViewRenderer } from '../../node-view'
import TableNodeView from './TableNodeView.svelte'
import { findTable, isTableSelected, selectTable as selectTableNode } from './utils'
import { Plugin, type Transaction } from '@tiptap/pm/state'
import { Decoration, DecorationSet } from '@tiptap/pm/view'
import AddColAfter from '../../icons/table/AddColAfter.svelte'
import AddColBefore from '../../icons/table/AddColBefore.svelte'
import AddRowAfter from '../../icons/table/AddRowAfter.svelte'
import AddRowBefore from '../../icons/table/AddRowBefore.svelte'
import DeleteCol from '../../icons/table/DeleteCol.svelte'
import DeleteRow from '../../icons/table/DeleteRow.svelte'
import DeleteTable from '../../icons/table/DeleteTable.svelte'
import { Plugin } from '@tiptap/pm/state'
import { SvelteNodeViewRenderer } from '../../node-view'
import TableNodeView from './TableNodeView.svelte'
import { TableSelection } from './types'
import { Decoration, DecorationSet } from '@tiptap/pm/view'
import { findTable, isTableSelected, selectTable as selectTableNode } from './utils'

export const Table = TiptapTable.extend({
draggable: true,
Expand All @@ -48,7 +48,7 @@ export const Table = TiptapTable.extend({
return SvelteNodeViewRenderer(TableNodeView, {})
},
addProseMirrorPlugins () {
return [...(this.parent?.() ?? []), tableSelectionHighlight()]
return [...(this.parent?.() ?? []), tableSelectionHighlight(), cleanupBrokenTables()]
}
})

Expand Down Expand Up @@ -87,6 +87,32 @@ export const tableSelectionHighlight = (): Plugin<DecorationSet> => {
})
}

export const cleanupBrokenTables = (): Plugin<DecorationSet> => {
return new Plugin<DecorationSet>({
appendTransaction: (transactions, oldState, newState) => {
const lastTx = transactions[0]
if (!lastTx?.docChanged) return

let tr: Transaction | undefined
newState.doc.descendants((node, pos) => {
if (node.type.name !== 'table') return

const map = TableMap.get(node)

const isBroken = map.width === 0 || map.height === 0
if (!isBroken) return

tr = tr ?? newState.tr
const mpos = tr.mapping.map(pos)

tr = tr.delete(mpos, mpos + node.nodeSize)
})

return tr
}
})
}

function handleModDelete (editor: Editor): boolean {
const { selection } = editor.state.tr
if (selection instanceof CellSelection) {
Expand Down