marktext/src/muya/lib/ui/tableTools/index.js
Ran Luo 289b17c015 Optimization of table block (#1456)
* Prepare for drag and drop row and column

* remove regexp th|td

* render drag button

* Feat: support drag and drop row and column of table

* Feat: table bar tools

* remove unnecessary codes

* Feat: support select multiple cells

* Do not show table drag bar when selected cells

* Feat: support delete selected cells content or remove row/column/table

* Feat: select one cell or table when press ctrl + a

* Support select all content

* Remove table tools in context menu

* Feat: support copy paste selected cells as sub table

* Fix: PR issue 1 press tab will not show the table drag bars

* Select one cell and press backspace will cause bug

* Fix: The table drag bar location error when there are tow tables in the editor

* Fix unable copy and paste 1* n or n * 1 table

* Drag any row to the top to editor will cause error.

* Update table resize icon

* Fix: table resize is not work in table tool bar

* Fix: No need to show left drag bar if only one row, and no need to show bottom drag bar if only one column.

* Fix: Create an empty table in source code mode, turn to preview mode, there are more than two drag bars in one table.

* Fix: resize table

* Opti: table is not 100% width now

* Fix drag in one row or column

* Change table delete icon

* Fix: backspace is not work

* Little style opti

* Fix: cmd + enter bug

* Update the table drag bar context menu text

* Handle delete key when select table cells

* remove all unnecessary debug codes

* Feat: support cut selected cells and copy/cut by context menu

* Fix typo

* Rename some methods name

* Fix an issue when drag and drop table drag bar

* fix do not handle cell selection when the context menu shown

* Do not handle select cells when mouse up outside table
2019-10-13 13:23:00 +02:00

87 lines
1.9 KiB
JavaScript

import BaseFloat from '../baseFloat'
import { patch, h } from '../../parser/render/snabbdom'
import { toolList } from './config'
import './index.css'
const defaultOptions = {
placement: 'right-start',
modifiers: {
offset: {
offset: '0, 5'
}
},
showArrow: false
}
class TableBarTools extends BaseFloat {
static pluginName = 'tableBarTools'
constructor (muya, options = {}) {
const name = 'ag-table-bar-tools'
const opts = Object.assign({}, defaultOptions, options)
super(muya, name, opts)
this.options = opts
this.oldVnode = null
this.tableInfo = null
this.floatBox.classList.add('ag-table-bar-tools')
const tableBarContainer = this.tableBarContainer = document.createElement('div')
this.container.appendChild(tableBarContainer)
this.listen()
}
listen () {
super.listen()
const { eventCenter } = this.muya
eventCenter.subscribe('muya-table-bar', ({ reference, tableInfo }) => {
if (reference) {
this.tableInfo = tableInfo
this.show(reference)
this.render()
} else {
this.hide()
}
})
}
render () {
const { tableInfo, oldVnode, tableBarContainer } = this
const renderArray = toolList[tableInfo.barType]
const children = renderArray.map((item) => {
const { label } = item
const selector = 'li.item'
return h(selector, {
dataset: {
label: item.action
},
on: {
click: event => {
this.selectItem(event, item)
}
}
}, label)
})
const vnode = h('ul', children)
if (oldVnode) {
patch(oldVnode, vnode)
} else {
patch(tableBarContainer, vnode)
}
this.oldVnode = vnode
}
selectItem (event, item) {
event.preventDefault()
event.stopPropagation()
const { contentState } = this.muya
contentState.editTable(item)
this.hide()
}
}
export default TableBarTools