mirror of
https://github.com/hcengineering/platform.git
synced 2025-03-15 02:23:12 +00:00
UBERF-8555: cleaning up broken tables (#7848)
Signed-off-by: Victor Ilyushchenko <alt13ri@gmail.com>
This commit is contained in:
parent
f112f63d1c
commit
9071a57706
@ -13,15 +13,14 @@
|
|||||||
// limitations under the License.
|
// 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 { type Editor } from '@tiptap/core'
|
||||||
import TiptapTable from '@tiptap/extension-table'
|
import TiptapTable from '@tiptap/extension-table'
|
||||||
import { CellSelection } from '@tiptap/pm/tables'
|
import { CellSelection, TableMap } from '@tiptap/pm/tables'
|
||||||
import { getEventPositionElement, SelectPopup, showPopup } from '@hcengineering/ui'
|
|
||||||
import textEditor, { type ActionContext } from '@hcengineering/text-editor'
|
|
||||||
|
|
||||||
import { SvelteNodeViewRenderer } from '../../node-view'
|
import { Plugin, type Transaction } from '@tiptap/pm/state'
|
||||||
import TableNodeView from './TableNodeView.svelte'
|
import { Decoration, DecorationSet } from '@tiptap/pm/view'
|
||||||
import { findTable, isTableSelected, selectTable as selectTableNode } from './utils'
|
|
||||||
import AddColAfter from '../../icons/table/AddColAfter.svelte'
|
import AddColAfter from '../../icons/table/AddColAfter.svelte'
|
||||||
import AddColBefore from '../../icons/table/AddColBefore.svelte'
|
import AddColBefore from '../../icons/table/AddColBefore.svelte'
|
||||||
import AddRowAfter from '../../icons/table/AddRowAfter.svelte'
|
import AddRowAfter from '../../icons/table/AddRowAfter.svelte'
|
||||||
@ -29,9 +28,10 @@ import AddRowBefore from '../../icons/table/AddRowBefore.svelte'
|
|||||||
import DeleteCol from '../../icons/table/DeleteCol.svelte'
|
import DeleteCol from '../../icons/table/DeleteCol.svelte'
|
||||||
import DeleteRow from '../../icons/table/DeleteRow.svelte'
|
import DeleteRow from '../../icons/table/DeleteRow.svelte'
|
||||||
import DeleteTable from '../../icons/table/DeleteTable.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 { TableSelection } from './types'
|
||||||
import { Decoration, DecorationSet } from '@tiptap/pm/view'
|
import { findTable, isTableSelected, selectTable as selectTableNode } from './utils'
|
||||||
|
|
||||||
export const Table = TiptapTable.extend({
|
export const Table = TiptapTable.extend({
|
||||||
draggable: true,
|
draggable: true,
|
||||||
@ -48,7 +48,7 @@ export const Table = TiptapTable.extend({
|
|||||||
return SvelteNodeViewRenderer(TableNodeView, {})
|
return SvelteNodeViewRenderer(TableNodeView, {})
|
||||||
},
|
},
|
||||||
addProseMirrorPlugins () {
|
addProseMirrorPlugins () {
|
||||||
return [...(this.parent?.() ?? []), tableSelectionHighlight()]
|
return [...(this.parent?.() ?? []), tableSelectionHighlight(), cleanupBrokenTables()]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -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 {
|
function handleModDelete (editor: Editor): boolean {
|
||||||
const { selection } = editor.state.tr
|
const { selection } = editor.state.tr
|
||||||
if (selection instanceof CellSelection) {
|
if (selection instanceof CellSelection) {
|
||||||
|
Loading…
Reference in New Issue
Block a user