prosemirror-tables
Advanced tools
Comparing version 0.8.1 to 0.9.0
{ | ||
"name": "prosemirror-tables", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "ProseMirror's rowspan/colspan tables component", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,25 +9,6 @@ # ProseMirror table module | ||
Note that Firefox will, by default, add various kinds of controls to | ||
editable tables, even though those don't work in ProseMirror. The only | ||
way to turn these off is globally, which you might want to do with the | ||
following code: | ||
The top-level directory contains a `demo.js` and `index.html`, which | ||
can be built with `npm run build_demo` to show a simple demo of how the | ||
module can be used. | ||
```javascript | ||
document.execCommand("enableObjectResizing", false, "false") | ||
document.execCommand("enableInlineTableEditing", false, "false") | ||
``` | ||
## Getting started | ||
To see a demo comprised of **index.html** and **demo.js** running in a browser, follow these steps: | ||
``` | ||
git clone git@github.com:ProseMirror/prosemirror-tables.git | ||
cd prosemirror-tables | ||
npm install | ||
npm run build_demo | ||
``` | ||
Then open **index.html** with your browser. | ||
## Documentation | ||
@@ -71,3 +52,3 @@ | ||
* **`tableEditing`**`({allowTableNodeSelection: false}) → Plugin`\ | ||
* **`tableEditing`**`() → Plugin`\ | ||
Creates a [plugin](http://prosemirror.net/docs/ref/#state.Plugin) | ||
@@ -119,2 +100,6 @@ that, when added to an editor, enables cell-selection, handles | ||
* `static `**`colSelection`**`($anchorCell: ResolvedPos, $headCell: ?ResolvedPos = $anchorCell) → CellSelection`\ | ||
Returns the smallest column selection that covers the given anchor | ||
and head cell. | ||
* `static `**`rowSelection`**`($anchorCell: ResolvedPos, $headCell: ?ResolvedPos = $anchorCell) → CellSelection`\ | ||
@@ -124,6 +109,2 @@ Returns the smallest row selection that covers the given anchor | ||
* `static `**`colSelection`**`($anchorCell: ResolvedPos, $headCell: ?ResolvedPos = $anchorCell) → CellSelection`\ | ||
Returns the smallest column selection that covers the given anchor | ||
and head cell. | ||
* `static `**`create`**`(doc: Node, anchorCell: number, headCell: ?number = anchorCell) → CellSelection` | ||
@@ -168,5 +149,10 @@ | ||
Split a selected cell, whose rowpan or colspan is greater than one, | ||
into smaller cells. | ||
into smaller cells. Use the first cell type for the new cells. | ||
* **`splitCellWithType`**`(getType: fn({row: number, col: number, node: Node}) → NodeType) → fn(EditorState, dispatch: ?fn(tr: Transaction)) → bool`\ | ||
Split a selected cell, whose rowpan or colspan is greater than one, | ||
into smaller cells with the cell type (th, td) returned by getType function. | ||
* **`setCellAttr`**`(name: string, value: any) → fn(EditorState, dispatch: ?fn(tr: Transaction)) → bool`\ | ||
@@ -192,3 +178,3 @@ Returns a command that sets the given attribute to the given value, | ||
Toggles between row/column header and normal cells (Only applies to first row/column). | ||
For deprecated behavior pass useDeprecatedLogic in options with true. | ||
For deprecated behavior pass `useDeprecatedLogic` in options with true. | ||
@@ -195,0 +181,0 @@ |
@@ -273,29 +273,40 @@ // This file defines a number of table-related commands. | ||
} | ||
// :: (EditorState, dispatch: ?(tr: Transaction)) → bool | ||
// Split a selected cell, whose rowpan or colspan is greater than one, | ||
// into smaller cells. | ||
// into smaller cells. Use the first cell type for the new cells. | ||
export function splitCell(state, dispatch) { | ||
let sel = state.selection | ||
let cellNode, cellPos | ||
if (!(sel instanceof CellSelection)) { | ||
cellNode = cellWrapping(sel.$from) | ||
if (!cellNode) return false | ||
cellPos = cellAround(sel.$from).pos | ||
} else { | ||
if (sel.$anchorCell.pos != sel.$headCell.pos) return false | ||
cellNode = sel.$anchorCell.nodeAfter | ||
cellPos = sel.$anchorCell.pos | ||
} | ||
if (cellNode.attrs.colspan == 1 && cellNode.attrs.rowspan == 1) {return false} | ||
if (dispatch) { | ||
let baseAttrs = cellNode.attrs, attrs = [], colwidth = baseAttrs.colwidth | ||
if (baseAttrs.rowspan > 1) baseAttrs = setAttr(baseAttrs, "rowspan", 1) | ||
if (baseAttrs.colspan > 1) baseAttrs = setAttr(baseAttrs, "colspan", 1) | ||
let rect = selectedRect(state), tr = state.tr | ||
for (let i = 0; i < rect.right - rect.left; i++) | ||
attrs.push(colwidth ? setAttr(baseAttrs, "colwidth", colwidth && colwidth[i] ? [colwidth[i]] : null) : baseAttrs) | ||
let lastCell, cellType = tableNodeTypes(state.schema)[cellNode.type.spec.tableRole]; | ||
for (let row = 0; row < rect.bottom; row++) { | ||
if (row >= rect.top) { | ||
const nodeTypes = tableNodeTypes(state.schema); | ||
return splitCellWithType(({ | ||
node, | ||
}) => { | ||
return nodeTypes[node.type.spec.tableRole] | ||
})(state, dispatch) | ||
} | ||
// :: (getCellType: ({ row: number, col: number, node: Node}) → NodeType) → (EditorState, dispatch: ?(tr: Transaction)) → bool | ||
// Split a selected cell, whose rowpan or colspan is greater than one, | ||
// into smaller cells with the cell type (th, td) returned by getType function. | ||
export function splitCellWithType(getCellType) { | ||
return (state, dispatch) => { | ||
let sel = state.selection | ||
let cellNode, cellPos | ||
if (!(sel instanceof CellSelection)) { | ||
cellNode = cellWrapping(sel.$from) | ||
if (!cellNode) return false | ||
cellPos = cellAround(sel.$from).pos | ||
} else { | ||
if (sel.$anchorCell.pos != sel.$headCell.pos) return false | ||
cellNode = sel.$anchorCell.nodeAfter | ||
cellPos = sel.$anchorCell.pos | ||
} | ||
if (cellNode.attrs.colspan == 1 && cellNode.attrs.rowspan == 1) {return false} | ||
if (dispatch) { | ||
let baseAttrs = cellNode.attrs, attrs = [], colwidth = baseAttrs.colwidth | ||
if (baseAttrs.rowspan > 1) baseAttrs = setAttr(baseAttrs, "rowspan", 1) | ||
if (baseAttrs.colspan > 1) baseAttrs = setAttr(baseAttrs, "colspan", 1) | ||
let rect = selectedRect(state), tr = state.tr | ||
for (let i = 0; i < rect.right - rect.left; i++) | ||
attrs.push(colwidth ? setAttr(baseAttrs, "colwidth", colwidth && colwidth[i] ? [colwidth[i]] : null) : baseAttrs) | ||
let lastCell; | ||
for (let row = rect.top; row < rect.bottom; row++) { | ||
let pos = rect.map.positionAt(row, rect.left, rect.table) | ||
@@ -305,13 +316,13 @@ if (row == rect.top) pos += cellNode.nodeSize | ||
if (col == rect.left && row == rect.top) continue | ||
tr.insert(lastCell = tr.mapping.map(pos + rect.tableStart, 1), cellType.createAndFill(attrs[i])) | ||
tr.insert(lastCell = tr.mapping.map(pos + rect.tableStart, 1), getCellType({ node: cellNode, row, col}).createAndFill(attrs[i])) | ||
} | ||
} | ||
tr.setNodeMarkup(cellPos, getCellType({ node: cellNode, row: rect.top, col: rect.left}), attrs[0]) | ||
if (sel instanceof CellSelection) | ||
tr.setSelection(new CellSelection(tr.doc.resolve(sel.$anchorCell.pos), | ||
lastCell && tr.doc.resolve(lastCell))) | ||
dispatch(tr) | ||
} | ||
tr.setNodeMarkup(cellPos, null, attrs[0]) | ||
if (sel instanceof CellSelection) | ||
tr.setSelection(new CellSelection(tr.doc.resolve(sel.$anchorCell.pos), | ||
lastCell && tr.doc.resolve(lastCell))) | ||
dispatch(tr) | ||
return true | ||
} | ||
return true | ||
} | ||
@@ -318,0 +329,0 @@ |
@@ -46,2 +46,4 @@ # ProseMirror table module | ||
@splitCellWithType | ||
@setCellAttr | ||
@@ -48,0 +50,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1043325
19512
237