prosemirror-utils
Advanced tools
Comparing version 0.0.9 to 0.1.0
@@ -7,2 +7,3 @@ 'use strict'; | ||
var prosemirrorTables = require('prosemirror-tables'); | ||
var prosemirrorModel = require('prosemirror-model'); | ||
@@ -53,2 +54,19 @@ // :: (selection: Selection) → boolean | ||
// (schema: Schema) → {[key: string]: NodeType} | ||
// Returns a map where keys are tableRoles and values are NodeTypes. | ||
var tableNodeTypes = function tableNodeTypes(schema) { | ||
if (schema.cached.tableNodeTypes) { | ||
return schema.cached.tableNodeTypes; | ||
} | ||
var roles = {}; | ||
Object.keys(schema.nodes).forEach(function (type) { | ||
var nodeType = schema.nodes[type]; | ||
if (nodeType.spec.tableRole) { | ||
roles[nodeType.spec.tableRole] = nodeType; | ||
} | ||
}); | ||
schema.cached.tableNodeTypes = roles; | ||
return roles; | ||
}; | ||
// :: (predicate: (node: ProseMirrorNode) → boolean) → (selection: Selection) → ?{pos: number, node: ProseMirrorNode} | ||
@@ -226,3 +244,3 @@ // Iterates over parent nodes, returning the first node and its position `predicate` returns truthy for. | ||
return findParentNode(function (node) { | ||
return node.type.spec.tableRole && node.type.spec.tableRole === "table"; | ||
return node.type.spec.tableRole && node.type.spec.tableRole === 'table'; | ||
})(selection); | ||
@@ -292,12 +310,14 @@ }; | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
var cells = map.cellsInRect({ | ||
left: columnIndex, | ||
right: columnIndex + 1, | ||
top: 0, | ||
bottom: map.height | ||
}); | ||
return cells.map(function (pos) { | ||
var node = table.node.nodeAt(pos); | ||
return { pos: pos + table.pos, node: node }; | ||
}); | ||
if (columnIndex >= 0 && columnIndex <= map.width - 1) { | ||
var cells = map.cellsInRect({ | ||
left: columnIndex, | ||
right: columnIndex + 1, | ||
top: 0, | ||
bottom: map.height | ||
}); | ||
return cells.map(function (pos) { | ||
var node = table.node.nodeAt(pos); | ||
return { pos: pos + table.pos, node: node }; | ||
}); | ||
} | ||
} | ||
@@ -314,12 +334,14 @@ }; | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
var cells = map.cellsInRect({ | ||
left: 0, | ||
right: map.width, | ||
top: rowIndex, | ||
bottom: rowIndex + 1 | ||
}); | ||
return cells.map(function (pos) { | ||
var node = table.node.nodeAt(pos); | ||
return { pos: pos + table.pos, node: node }; | ||
}); | ||
if (rowIndex >= 0 && rowIndex <= map.height - 1) { | ||
var cells = map.cellsInRect({ | ||
left: 0, | ||
right: map.width, | ||
top: rowIndex, | ||
bottom: rowIndex + 1 | ||
}); | ||
return cells.map(function (pos) { | ||
var node = table.node.nodeAt(pos); | ||
return { pos: pos + table.pos, node: node }; | ||
}); | ||
} | ||
} | ||
@@ -348,2 +370,199 @@ }; | ||
// :: (columnIndex: number) → (tr: Transaction) → Transaction | ||
// Creates a CellSelection on a column at `columnIndex`. | ||
var selectColumn = function selectColumn(columnIndex) { | ||
return function (tr) { | ||
var cells = getCellsInColumn(columnIndex)(tr.selection); | ||
if (cells) { | ||
var $anchor = tr.doc.resolve(cells[0].pos); | ||
var $head = tr.doc.resolve(cells[cells.length - 1].pos); | ||
return cloneTr(tr.setSelection(new prosemirrorTables.CellSelection($anchor, $head))); | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (rowIndex: number) → (tr: Transaction) → Transaction | ||
// Creates a CellSelection on a row at `rowIndex`. | ||
var selectRow = function selectRow(rowIndex) { | ||
return function (tr) { | ||
var cells = getCellsInRow(rowIndex)(tr.selection); | ||
if (cells) { | ||
var $anchor = tr.doc.resolve(cells[0].pos); | ||
var $head = tr.doc.resolve(cells[cells.length - 1].pos); | ||
return cloneTr(tr.setSelection(new prosemirrorTables.CellSelection($anchor, $head))); | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (selection: Selection) → (tr: Transaction) → Transaction | ||
// Creates a CellSelection on the entire table. | ||
var selectTable = function selectTable(tr) { | ||
var cells = getCellsInTable(tr.selection); | ||
if (cells) { | ||
var $anchor = tr.doc.resolve(cells[0].pos); | ||
var $head = tr.doc.resolve(cells[cells.length - 1].pos); | ||
return cloneTr(tr.setSelection(new prosemirrorTables.CellSelection($anchor, $head))); | ||
} | ||
return tr; | ||
}; | ||
// :: (schema: Schema) → (tr: Transaction) → Transaction | ||
// Clears the content of selected cells. | ||
var emptySelectedCells = function emptySelectedCells(schema) { | ||
return function (tr) { | ||
if (isCellSelection(tr.selection)) { | ||
var emptyCell = tableNodeTypes(schema).cell.createAndFill().content; | ||
tr.selection.forEachCell(function (cell, pos) { | ||
if (!cell.content.eq(emptyCell)) { | ||
tr.replaceWith(tr.mapping.map(pos + 1), tr.mapping.map(pos + cell.nodeSize - 1), new prosemirrorModel.Slice(emptyCell, 0, 0)); | ||
} | ||
}); | ||
if (tr.docChanged) { | ||
return cloneTr(tr); | ||
} | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (columnIndex: number) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that adds a new column at `columnIndex`. | ||
var addColumnAt = function addColumnAt(columnIndex) { | ||
return function (tr) { | ||
var table = findTable(tr.selection); | ||
if (table) { | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
if (columnIndex >= 0 && columnIndex <= map.width) { | ||
return cloneTr(prosemirrorTables.addColumn(tr, { | ||
map: map, | ||
tableStart: table.pos, | ||
table: table.node | ||
}, columnIndex)); | ||
} | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (rowIndex: number) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that adds a new row at `rowIndex`. | ||
var addRowAt = function addRowAt(rowIndex) { | ||
return function (tr) { | ||
var table = findTable(tr.selection); | ||
if (table) { | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
if (rowIndex >= 0 && rowIndex <= map.height) { | ||
return cloneTr(prosemirrorTables.addRow(tr, { | ||
map: map, | ||
tableStart: table.pos, | ||
table: table.node | ||
}, rowIndex)); | ||
} | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (columnIndex: number) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that removes a column at `columnIndex`. | ||
var removeColumnAt = function removeColumnAt(columnIndex) { | ||
return function (tr) { | ||
var table = findTable(tr.selection); | ||
if (table) { | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
if (columnIndex >= 0 && columnIndex <= map.width) { | ||
prosemirrorTables.removeColumn(tr, { | ||
map: map, | ||
tableStart: table.pos, | ||
table: table.node | ||
}, columnIndex); | ||
return cloneTr(tr); | ||
} | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (rowIndex: number) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that removes a row at `rowIndex`. | ||
var removeRowAt = function removeRowAt(rowIndex) { | ||
return function (tr) { | ||
var table = findTable(tr.selection); | ||
if (table) { | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
if (rowIndex >= 0 && rowIndex <= map.height) { | ||
prosemirrorTables.removeRow(tr, { | ||
map: map, | ||
tableStart: table.pos, | ||
table: table.node | ||
}, rowIndex); | ||
return cloneTr(tr); | ||
} | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (tr: Transaction) → Transaction | ||
// Returns a new transaction that removes a table if the cursor is inside | ||
var removeTable = function removeTable(tr) { | ||
var $from = tr.selection.$from; | ||
for (var depth = $from.depth; depth > 0; depth--) { | ||
var node = $from.node(depth); | ||
if (node.type.spec.tableRole === 'table') { | ||
return cloneTr(tr.delete($from.before(depth), $from.after(depth))); | ||
} | ||
} | ||
return tr; | ||
}; | ||
// :: (tr: Transaction) → Transaction | ||
// Returns a new transaction that removes selected columns | ||
var removeSelectedColumns = function removeSelectedColumns(tr) { | ||
var _tr = tr, | ||
selection = _tr.selection; | ||
if (isTableSelected(selection)) { | ||
return removeTable(tr); | ||
} | ||
if (isCellSelection(selection) && selection.isColSelection()) { | ||
var table = findTable(selection); | ||
if (table) { | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
var rect = map.rectBetween(selection.$anchorCell.pos - table.pos, selection.$headCell.pos - table.pos); | ||
for (var i = rect.right - 1; i >= rect.left; i--) { | ||
tr = removeColumnAt(i)(tr); | ||
} | ||
return tr; | ||
} | ||
} | ||
return tr; | ||
}; | ||
// :: (tr: Transaction) → Transaction | ||
// Returns a new transaction that removes selected rows | ||
var removeSelectedRows = function removeSelectedRows(tr) { | ||
var _tr2 = tr, | ||
selection = _tr2.selection; | ||
if (isTableSelected(selection)) { | ||
return removeTable(tr); | ||
} | ||
if (isCellSelection(selection) && selection.isRowSelection()) { | ||
var table = findTable(selection); | ||
if (table) { | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
var rect = map.rectBetween(selection.$anchorCell.pos - table.pos, selection.$headCell.pos - table.pos); | ||
for (var i = rect.bottom - 1; i >= rect.top; i--) { | ||
tr = removeRowAt(i)(tr); | ||
} | ||
return tr; | ||
} | ||
} | ||
return tr; | ||
}; | ||
// :: (nodeType: union<NodeType, [NodeType]>) → (tr: Transaction) → Transaction | ||
@@ -354,3 +573,3 @@ // Returns a new transaction that removes a node of a given `nodeType`. | ||
return function (tr) { | ||
var parent = findParentNodeOfType(nodeType)(tr.curSelection); | ||
var parent = findParentNodeOfType(nodeType)(tr.selection); | ||
if (parent) { | ||
@@ -368,3 +587,3 @@ return removeNodeAtPos(parent.pos)(tr); | ||
return function (tr) { | ||
var parent = findParentNodeOfType(nodeType)(tr.curSelection); | ||
var parent = findParentNodeOfType(nodeType)(tr.selection); | ||
if (parent) { | ||
@@ -382,4 +601,4 @@ return replaceNodeAtPos(parent.pos, node)(tr); | ||
if (isNodeSelection(tr.selection)) { | ||
var from = tr.curSelection.$from.pos; | ||
var to = tr.curSelection.$to.pos; | ||
var from = tr.selection.$from.pos; | ||
var to = tr.selection.$to.pos; | ||
return cloneTr(tr.delete(from, to)); | ||
@@ -396,5 +615,5 @@ } | ||
if (isNodeSelection(tr.selection)) { | ||
var _tr$curSelection = tr.curSelection, | ||
$from = _tr$curSelection.$from, | ||
$to = _tr$curSelection.$to; | ||
var _tr$selection = tr.selection, | ||
$from = _tr$selection.$from, | ||
$to = _tr$selection.$to; | ||
@@ -414,3 +633,3 @@ if ($from.parent.canReplaceWith($from.index(), $from.indexAfter(), node.type)) { | ||
return function (tr) { | ||
var $from = tr.curSelection.$from; | ||
var $from = tr.selection.$from; | ||
@@ -441,3 +660,3 @@ var index = $from.index(); | ||
return function (tr) { | ||
var parent = findParentNodeOfType(nodeType)(tr.curSelection); | ||
var parent = findParentNodeOfType(nodeType)(tr.selection); | ||
if (parent) { | ||
@@ -455,3 +674,3 @@ return cloneTr(tr.setNodeMarkup(parent.pos - 1, type, Object.assign({}, parent.node.attrs, attrs), marks)); | ||
if (!isNodeSelection(tr.selection)) { | ||
var parent = findParentNodeOfType(nodeType)(tr.curSelection); | ||
var parent = findParentNodeOfType(nodeType)(tr.selection); | ||
if (parent) { | ||
@@ -490,2 +709,13 @@ return cloneTr(tr.setSelection(prosemirrorState.NodeSelection.create(tr.doc, parent.pos - 1))); | ||
exports.getCellsInTable = getCellsInTable; | ||
exports.selectColumn = selectColumn; | ||
exports.selectRow = selectRow; | ||
exports.selectTable = selectTable; | ||
exports.emptySelectedCells = emptySelectedCells; | ||
exports.addColumnAt = addColumnAt; | ||
exports.addRowAt = addRowAt; | ||
exports.removeColumnAt = removeColumnAt; | ||
exports.removeRowAt = removeRowAt; | ||
exports.removeTable = removeTable; | ||
exports.removeSelectedColumns = removeSelectedColumns; | ||
exports.removeSelectedRows = removeSelectedRows; | ||
exports.removeParentNodeOfType = removeParentNodeOfType; | ||
@@ -492,0 +722,0 @@ exports.replaceParentNodeOfType = replaceParentNodeOfType; |
{ | ||
"name": "prosemirror-utils", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"description": "Utils library for ProseMirror", | ||
"main": "dist/index.js", | ||
"author": { | ||
"name": "Eduard Shvedai", | ||
"email": "eshvedai@gmail.com", | ||
"url": "https://github.com/eshvedai/prosemirror-utils" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "Eduard Shvedai", | ||
"email": "eshvedai@gmail.com" | ||
"email": "eshvedai@atlassian.com" | ||
} | ||
], | ||
"license": "MIT", | ||
"license": "Apache-2.0", | ||
"repository": { | ||
@@ -37,2 +42,3 @@ "type": "git", | ||
"test": "NODE_ENV=testing jest", | ||
"test-ci": "NODE_ENV=testing jest --coverage && codecov", | ||
"prepare": "npm run build", | ||
@@ -42,4 +48,5 @@ "precommit": "lint-staged" | ||
"peerDependencies": { | ||
"prosemirror-model": "^1.0.0", | ||
"prosemirror-state": "^1.0.1", | ||
"prosemirror-tables": "^0.6.3" | ||
"prosemirror-tables": "^0.6.5" | ||
}, | ||
@@ -51,2 +58,3 @@ "devDependencies": { | ||
"builddocs": "^0.3.2", | ||
"codecov": "^3.0.0", | ||
"husky": "^0.14.3", | ||
@@ -56,6 +64,6 @@ "jest": "^22.4.2", | ||
"prettier": "^1.11.1", | ||
"prosemirror-model": "^1.1.0", | ||
"prosemirror-model": "^1.0.0", | ||
"prosemirror-schema-basic": "^1.0.0", | ||
"prosemirror-state": "^1.0.1", | ||
"prosemirror-tables": "^0.6.3", | ||
"prosemirror-tables": "^0.6.5", | ||
"prosemirror-test-builder": "^1.0.1", | ||
@@ -71,3 +79,7 @@ "prosemirror-view": "^1.1.1", | ||
] | ||
}, | ||
"prettier": { | ||
"singleQuote": true, | ||
"trailing-comma": "es5" | ||
} | ||
} |
# Utils library for ProseMirror | ||
[![npm](https://img.shields.io/npm/v/prosemirror-utils.svg?style=flat-square)](https://www.npmjs.com/package/prosemirror-utils) | ||
[![License](https://img.shields.io/npm/l/prosemirror-utils.svg?style=flat-square)](http://www.apache.org/licenses/LICENSE-2.0) | ||
[![CircleCI](https://img.shields.io/circleci/project/github/eshvedai/prosemirror-utils.svg?style=flat-square)](https://circleci.com/gh/eshvedai/prosemirror-utils) | ||
[![Codecov](https://img.shields.io/codecov/c/github/eshvedai/prosemirror-utils.svg?style=flat-square)](https://codecov.io/gh/eshvedai/prosemirror-utils) | ||
[![Github Issues](https://img.shields.io/github/issues/eshvedai/prosemirror-utils.svg?style=flat-square)](https://github.com/eshvedai/prosemirror-utils/issues) | ||
[![Downloads](https://img.shields.io/npm/dw/prosemirror-utils.svg?style=flat-square)](https://www.npmjs.com/package/prosemirror-utils) | ||
[![Code size](https://img.shields.io/github/languages/code-size/eshvedai/prosemirror-utils.svg?style=flat-square)](https://www.npmjs.com/package/prosemirror-utils) | ||
## Quick Start | ||
@@ -120,2 +128,46 @@ | ||
* **`selectColumn`**`(columnIndex: number) → fn(tr: Transaction) → Transaction`\ | ||
Creates a CellSelection on a column at `columnIndex`. | ||
* **`selectRow`**`(rowIndex: number) → fn(tr: Transaction) → Transaction`\ | ||
Creates a CellSelection on a row at `rowIndex`. | ||
* **`selectTable`**`(selection: Selection) → fn(tr: Transaction) → Transaction`\ | ||
Creates a CellSelection on the entire table. | ||
* **`emptySelectedCells`**`(schema: Schema) → fn(tr: Transaction) → Transaction`\ | ||
Clears the content of selected cells. | ||
* **`addColumnAt`**`(columnIndex: number) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that adds a new column at `columnIndex`. | ||
* **`addRowAt`**`(rowIndex: number) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that adds a new row at `rowIndex`. | ||
* **`removeColumnAt`**`(columnIndex: number) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that removes a column at `columnIndex`. | ||
* **`removeRowAt`**`(rowIndex: number) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that removes a row at `rowIndex`. | ||
* **`removeTable`**`(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that removes a table if the cursor is inside | ||
* **`removeSelectedColumns`**`(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that removes selected columns | ||
* **`removeSelectedRows`**`(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that removes selected rows | ||
### Transforms | ||
@@ -158,3 +210,3 @@ | ||
* **MIT** : http://opensource.org/licenses/MIT | ||
* **Apache 2.0** : http://www.apache.org/licenses/LICENSE-2.0 | ||
@@ -1,2 +0,2 @@ | ||
import { Node as ProsemirrorNode, NodeType, Mark, MarkType } from 'prosemirror-model'; | ||
import { Node as ProsemirrorNode, Schema, NodeType, Mark, MarkType } from 'prosemirror-model'; | ||
import { Selection, Transaction } from 'prosemirror-state'; | ||
@@ -44,3 +44,3 @@ | ||
// Tables | ||
// Table | ||
export function findTable(selection: Selection): {pos: number, node: ProsemirrorNode} | undefined; | ||
@@ -62,2 +62,22 @@ | ||
export function selectColumn(columnIndex: number): (tr: Transaction) => Transaction; | ||
export function selectRow(rowIndex: number): (tr: Transaction) => Transaction; | ||
export function selectTable(tr: Transaction): Transaction; | ||
export function emptySelectedCells(schema: Schema): (tr: Transaction) => Transaction; | ||
export function addColumnAt(columnIndex: number): (tr: Transaction) => Transaction; | ||
export function addRowAt(rowIndex: number): (tr: Transaction) => Transaction; | ||
export function removeColumnAt(columnIndex: number): (tr: Transaction) => Transaction; | ||
export function removeRowAt(rowIndex: number): (tr: Transaction) => Transaction; | ||
export function removeSelectedColumns(tr: Transaction): Transaction; | ||
export function removeSelectedRows(tr: Transaction): Transaction; | ||
// Transforms | ||
@@ -64,0 +84,0 @@ export function removeParentNodeOfType(nodeType: NodeType | NodeType[]): (tr: Transaction) => Transaction; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
83871
695
1
211
3
17