prosemirror-utils
Advanced tools
Comparing version
{ | ||
"name": "prosemirror-utils", | ||
"version": "0.7.7", | ||
"version": "0.8.0", | ||
"description": "Utils library for ProseMirror", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
297
README.md
@@ -331,2 +331,253 @@ # Utils library for ProseMirror | ||
* **`moveRow`**`(originRowIndex: number, targetRowIndex: targetColumnIndex, options: ?MovementOptions) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that moves the origin row to the target index; | ||
by default "tryToFit" is false, that means if you try to move a row to a place | ||
where we will need to split a row with merged cells it'll throw an exception, for example: | ||
____________________________ | ||
| | | | | ||
0 | A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
| | | | | ||
1 | A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
2 | A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
if you try to move the row 0 to the row index 1 with tryToFit false, | ||
it'll throw an exception since you can't split the row 1; | ||
but if "tryToFit" is true, it'll move the row using the current direction. | ||
We defined current direction using the target and origin values | ||
if the origin is greater than the target, that means the course is `bottom-to-top`, | ||
so the `tryToFit` logic will use this direction to determine | ||
if we should move the column to the right or the left. | ||
for example, if you call the function using `moveRow(0, 1, { tryToFit: true })` | ||
the result will be: | ||
____________________________ | ||
| | | | | ||
0 | A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
1 | A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
| | | | | ||
2 | A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
since we could put the row zero on index one, | ||
we pushed to the best place to fit the row index 0, | ||
in this case, row index 2. | ||
-------- HOW TO OVERRIDE DIRECTION -------- | ||
If you set "tryToFit" to "true", it will try to figure out the best direction | ||
place to fit using the origin and target index, for example: | ||
____________________________ | ||
| | | | | ||
0 | A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
| | | | | ||
1 | A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
2 | A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
| | | | | ||
3 | A4 | B4 | | | ||
|______|______ ______| | | ||
| | | | D2 | | ||
4 | A5 | B5 | C3 | | | ||
|______|______|______|______| | ||
If you try to move the row 0 to row index 4 with "tryToFit" enabled, by default, | ||
the code will put it on after the merged rows, | ||
but you can override it using the "direction" option. | ||
-1: Always put the origin before the target | ||
____________________________ | ||
| | | | | ||
0 | A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
1 | A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
| | | | | ||
2 | A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
| | | | | ||
3 | A4 | B4 | | | ||
|______|______ ______| | | ||
| | | | D2 | | ||
4 | A5 | B5 | C3 | | | ||
|______|______|______|______| | ||
0: Automatically decide the best place to fit | ||
____________________________ | ||
| | | | | ||
0 | A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
1 | A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
| | | | | ||
2 | A4 | B4 | | | ||
|______|______ ______| | | ||
| | | | D2 | | ||
3 | A5 | B5 | C3 | | | ||
|______|______|______|______| | ||
| | | | | ||
4 | A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
1: Always put the origin after the target | ||
____________________________ | ||
| | | | | ||
0 | A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
1 | A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
| | | | | ||
2 | A4 | B4 | | | ||
|______|______ ______| | | ||
| | | | D2 | | ||
3 | A5 | B5 | C3 | | | ||
|______|______|______|______| | ||
| | | | | ||
4 | A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
```javascript | ||
dispatch( | ||
moveRow(x, y, options)(state.tr) | ||
); | ||
``` | ||
* **`moveColumn`**`(originColumnIndex: number, targetColumnIndex: targetColumnIndex, options: ?MovementOptions) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that moves the origin column to the target index; | ||
by default "tryToFit" is false, that means if you try to move a column to a place | ||
where we will need to split a column with merged cells it'll throw an exception, for example: | ||
0 1 2 | ||
____________________________ | ||
| | | | | ||
| A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
| | | | | ||
| A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
| A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
if you try to move the column 0 to the column index 1 with tryToFit false, | ||
it'll throw an exception since you can't split the column 1; | ||
but if "tryToFit" is true, it'll move the column using the current direction. | ||
We defined current direction using the target and origin values | ||
if the origin is greater than the target, that means the course is `right-to-left`, | ||
so the `tryToFit` logic will use this direction to determine | ||
if we should move the column to the right or the left. | ||
for example, if you call the function using `moveColumn(0, 1, { tryToFit: true })` | ||
the result will be: | ||
0 1 2 | ||
_____________________ _______ | ||
| | | | | ||
| B1 | C1 | A1 | | ||
|______|______ ______|______| | ||
| | | | | ||
| B2 | | A2 | | ||
|______ ______| |______| | ||
| | | D1 | | | ||
| B3 | C2 | | A3 | | ||
|______|______|______|______| | ||
since we could put the column zero on index one, | ||
we pushed to the best place to fit the column 0, in this case, column index 2. | ||
-------- HOW TO OVERRIDE DIRECTION -------- | ||
If you set "tryToFit" to "true", it will try to figure out the best direction | ||
place to fit using the origin and target index, for example: | ||
0 1 2 3 4 5 6 | ||
_________________________________________________ | ||
| | | | | | | ||
| A1 | B1 | C1 | E1 | F1 | | ||
|______|______|______ ______|______|______ ______| | ||
| | | | | | | ||
| A2 | B2 | | E2 | | | ||
|______|______ ______| |______ ______| | | ||
| | | | D1 | | | G2 | | ||
| A3 | B3 | C3 | | E3 | F3 | | | ||
|______|______|______|______|______|______|______| | ||
If you try to move the column 0 to column index 5 with "tryToFit" enabled, by default, | ||
the code will put it on after the merged columns, | ||
but you can override it using the "direction" option. | ||
-1: Always put the origin before the target | ||
0 1 2 3 4 5 6 | ||
_________________________________________________ | ||
| | | | | | | ||
| B1 | C1 | A1 | E1 | F1 | | ||
|______|______ ______|______|______|______ ______| | ||
| | | | | | | ||
| B2 | | A2 | E2 | | | ||
|______ ______| |______|______ ______| | | ||
| | | D1 | | | | G2 | | ||
| B3 | C3 | | A3 | E3 | F3 | | | ||
|______|______|______|______|______|______|______| | ||
0: Automatically decide the best place to fit | ||
0 1 2 3 4 5 6 | ||
_________________________________________________ | ||
| | | | | | | ||
| B1 | C1 | E1 | F1 | A1 | | ||
|______|______ ______|______|______ ______|______| | ||
| | | | | | | ||
| B2 | | E2 | | A2 | | ||
|______ ______| |______ ______| |______| | ||
| | | D1 | | | G2 | | | ||
| B3 | C3 | | E3 | F3 | | A3 | | ||
|______|______|______|______|______|______|______| | ||
1: Always put the origin after the target | ||
0 1 2 3 4 5 6 | ||
_________________________________________________ | ||
| | | | | | | ||
| B1 | C1 | E1 | F1 | A1 | | ||
|______|______ ______|______|______ ______|______| | ||
| | | | | | | ||
| B2 | | E2 | | A2 | | ||
|______ ______| |______ ______| |______| | ||
| | | D1 | | | G2 | | | ||
| B3 | C3 | | E3 | F3 | | A3 | | ||
|______|______|______|______|______|______|______| | ||
```javascript | ||
dispatch( | ||
moveColumn(x, y, options)(state.tr) | ||
); | ||
``` | ||
* **`addRowAt`**`(rowIndex: number, clonePreviousRow: ?boolean) → fn(tr: Transaction) → Transaction`\ | ||
@@ -629,2 +880,48 @@ Returns a new transaction that adds a new row at index `rowIndex`. Optionally clone the previous row. | ||
* **`convertTableNodeToArrayOfRows`**`(tableNode: Node) → [Node]`\ | ||
This function will transform the table node | ||
into a matrix of rows and columns respecting merged cells, | ||
for example this table will be convert to the below: | ||
____________________________ | ||
| | | | | ||
| A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
| | | | | ||
| A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
| A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
array = [ | ||
[A1, B1, C1, null], | ||
[A2, B2, null, D1], | ||
[A3. B3, C2, null], | ||
] | ||
* **`convertArrayOfRowsToTableNode`**`(tableNode: Node, tableArray: [Node]) → Node`\ | ||
This function will transform a matrix of nodes | ||
into table node respecting merged cells and rows configurations, | ||
for example this array will be convert to the table below: | ||
array = [ | ||
[A1, B1, C1, null], | ||
[A2, B2, null, D1], | ||
[A3. B3, C2, null], | ||
] | ||
____________________________ | ||
| | | | | ||
| A1 | B1 | C1 | | ||
|______|______|______ ______| | ||
| | | | | ||
| A2 | B2 | | | ||
|______|______ ______| | | ||
| | | | D1 | | ||
| A3 | B3 | C2 | | | ||
|______|______|______|______| | ||
## License | ||
@@ -631,0 +928,0 @@ |
@@ -14,2 +14,4 @@ import { Node as ProsemirrorNode, Schema, NodeType, Mark, MarkType, ResolvedPos, Fragment } from 'prosemirror-model'; | ||
export type MovementOptions = { tryToFit: boolean, direction?: -1 | 0 | 1 }; | ||
// Selection | ||
@@ -86,2 +88,6 @@ export function findParentNode(predicate: Predicate): (selection: Selection) => ContentNodeWithPos | undefined; | ||
export function moveRow(originRowIndex: number, targetRowIndex: number, options?: MovementOptions): (tr: Transaction) => Transaction; | ||
export function moveColumn(originColumnIndex: number, targetColumnIndex: number, options?: MovementOptions): (tr: Transaction) => Transaction; | ||
export function addRowAt(rowIndex: number, clonePreviousRow?: boolean): (tr: Transaction) => Transaction; | ||
@@ -132,2 +138,6 @@ | ||
export function convertTableNodeToArrayOfRows(tableNode: ProsemirrorNode): ProsemirrorNode[]; | ||
export function convertArrayOfRowsToTableNode(tableNode: ProsemirrorNode, tableArray: ProsemirrorNode[]): ProsemirrorNode; | ||
export function canInsert($pos: ResolvedPos, node: ProsemirrorNode | Fragment): boolean; | ||
@@ -134,0 +144,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
229279
30.54%2057
28.64%929
46.99%