@prosekit/extensions 0.7.12 → 0.7.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ import { bundledLanguagesInfo } from 'shiki';
8
8
  import { BundledTheme } from 'shiki';
9
9
  import { BundledThemeInfo } from 'shiki';
10
10
  import { bundledThemesInfo } from 'shiki';
11
+ import { CellSelection } from 'prosemirror-tables';
11
12
  import { Command } from '@prosekit/pm/state';
12
13
  import { CursorAwareness } from 'loro-prosemirror';
13
14
  import type { DecorationAttrs } from '@prosekit/pm/view';
@@ -19,6 +20,7 @@ import { Extension } from '@prosekit/core';
19
20
  import { ExtensionTyping } from '@prosekit/core';
20
21
  import { ExtractMarkActions } from '@prosekit/core';
21
22
  import { ExtractNodeActions } from '@prosekit/core';
23
+ import { FindParentNodeResult } from '@prosekit/core';
22
24
  import { GapCursor } from 'prosemirror-gapcursor';
23
25
  import { getSingletonHighlighter } from 'shiki/bundle/full';
24
26
  import type { Highlighter } from 'shiki';
@@ -44,6 +46,7 @@ import { PlainExtension } from '@prosekit/core';
44
46
  import { Plugin as Plugin_2 } from '@prosekit/pm/state';
45
47
  import { PluginKey } from '@prosekit/pm/state';
46
48
  import { ProseMirrorNode } from '@prosekit/pm/model';
49
+ import type { ResolvedPos } from '@prosekit/pm/model';
47
50
  import type { Selection as Selection_2 } from '@prosekit/pm/state';
48
51
  import { SimplifyDeeper } from '@prosekit/core';
49
52
  import type { SpecialLanguage } from 'shiki';
@@ -811,7 +814,9 @@ export declare function defineStrikeSpec(): StrikeSpecExtension;
811
814
  /**
812
815
  * @public
813
816
  */
814
- export declare function defineTable(): TableExtension;
817
+ declare function defineTable(): TableExtension;
818
+ export { defineTable }
819
+ export { defineTable as defineTable_alias_1 }
815
820
 
816
821
  /**
817
822
  * @internal
@@ -1086,7 +1091,27 @@ export declare type EnterRuleOptions = {
1086
1091
  *
1087
1092
  * @public
1088
1093
  */
1089
- export declare const exitTable: Command;
1094
+ declare const exitTable: Command;
1095
+ export { exitTable }
1096
+ export { exitTable as exitTable_alias_1 }
1097
+
1098
+ /**
1099
+ * Try to find a resolved pos of a cell by using the given pos as a hit point.
1100
+ *
1101
+ * @internal
1102
+ */
1103
+ export declare function findCellPos(doc: ProseMirrorNode, pos: number): ResolvedPos | undefined;
1104
+
1105
+ /**
1106
+ * Try to find the anchor and head cell in the same table by using the given
1107
+ * anchor and head as hit points, or fallback to the selection's anchor and
1108
+ * head.
1109
+ *
1110
+ * @internal
1111
+ */
1112
+ export declare function findCellRange(selection: Selection_2, anchorHit?: number, headHit?: number): [ResolvedPos, ResolvedPos] | undefined;
1113
+
1114
+ export declare function findTable($pos: ResolvedPos): FindParentNodeResult | undefined;
1090
1115
 
1091
1116
  export { GapCursor }
1092
1117
 
@@ -1196,22 +1221,39 @@ export declare type ImageSpecExtension = Extension<{
1196
1221
 
1197
1222
  export { IndentListOptions }
1198
1223
 
1224
+ export declare function inputText(input: string): Promise<void>;
1225
+
1199
1226
  /**
1200
1227
  * Insert a table node with the given number of rows and columns, and optionally
1201
1228
  * a header row.
1202
1229
  *
1230
+ * @param options
1231
+ *
1203
1232
  * @public
1204
1233
  */
1205
- export declare function insertTable({ row, col, header }: InsertTableOptions): Command;
1234
+ declare function insertTable(options: InsertTableOptions): Command;
1235
+ export { insertTable }
1236
+ export { insertTable as insertTable_alias_1 }
1206
1237
 
1207
1238
  /**
1208
1239
  * @public
1209
1240
  */
1210
- export declare interface InsertTableOptions {
1241
+ declare interface InsertTableOptions {
1211
1242
  row: number;
1212
1243
  col: number;
1213
1244
  header: boolean;
1214
1245
  }
1246
+ export { InsertTableOptions }
1247
+ export { InsertTableOptions as InsertTableOptions_alias_1 }
1248
+
1249
+ /**
1250
+ * Checks if the given object is a `CellSelection` instance.
1251
+ *
1252
+ * @public
1253
+ */
1254
+ declare function isCellSelection(value: unknown): value is CellSelection;
1255
+ export { isCellSelection }
1256
+ export { isCellSelection as isCellSelection_alias_1 }
1215
1257
 
1216
1258
  /**
1217
1259
  * @internal
@@ -1551,6 +1593,96 @@ export declare interface SearchQueryOptions {
1551
1593
  wholeWord?: boolean;
1552
1594
  }
1553
1595
 
1596
+ /**
1597
+ * @public
1598
+ */
1599
+ declare function selectTable(options?: SelectTableOptions): Command;
1600
+ export { selectTable }
1601
+ export { selectTable as selectTable_alias_1 }
1602
+
1603
+ /**
1604
+ * @public
1605
+ */
1606
+ declare function selectTableCell(options?: SelectTableCellOptions): Command;
1607
+ export { selectTableCell }
1608
+ export { selectTableCell as selectTableCell_alias_1 }
1609
+
1610
+ /**
1611
+ * @public
1612
+ */
1613
+ declare interface SelectTableCellOptions {
1614
+ /**
1615
+ * A hit position of the table cell to select from. By default, the selection
1616
+ * anchor will be used.
1617
+ */
1618
+ pos?: number;
1619
+ }
1620
+ export { SelectTableCellOptions }
1621
+ export { SelectTableCellOptions as SelectTableCellOptions_alias_1 }
1622
+
1623
+ /**
1624
+ * @public
1625
+ */
1626
+ declare function selectTableColumn(options?: SelectTableColumnOptions): Command;
1627
+ export { selectTableColumn }
1628
+ export { selectTableColumn as selectTableColumn_alias_1 }
1629
+
1630
+ /**
1631
+ * @public
1632
+ */
1633
+ declare interface SelectTableColumnOptions {
1634
+ /**
1635
+ * A hit position of the table cell to select from. By default, the selection
1636
+ * anchor will be used.
1637
+ */
1638
+ anchor?: number;
1639
+ /**
1640
+ * A hit position of the table cell to select to. By default, the selection
1641
+ * head will be used.
1642
+ */
1643
+ head?: number;
1644
+ }
1645
+ export { SelectTableColumnOptions }
1646
+ export { SelectTableColumnOptions as SelectTableColumnOptions_alias_1 }
1647
+
1648
+ /**
1649
+ * @public
1650
+ */
1651
+ declare interface SelectTableOptions {
1652
+ /**
1653
+ * A hit position of the table to select from. By default, the selection
1654
+ * anchor will be used.
1655
+ */
1656
+ pos?: number;
1657
+ }
1658
+ export { SelectTableOptions }
1659
+ export { SelectTableOptions as SelectTableOptions_alias_1 }
1660
+
1661
+ /**
1662
+ * @public
1663
+ */
1664
+ declare function selectTableRow(options?: SelectTableRowOptions): Command;
1665
+ export { selectTableRow }
1666
+ export { selectTableRow as selectTableRow_alias_1 }
1667
+
1668
+ /**
1669
+ * @public
1670
+ */
1671
+ declare interface SelectTableRowOptions {
1672
+ /**
1673
+ * A hit position of the table cell to select from. By default, the selection
1674
+ * anchor will be used.
1675
+ */
1676
+ anchor?: number;
1677
+ /**
1678
+ * A hit position of the table cell to select to. By default, the selection
1679
+ * head will be used.
1680
+ */
1681
+ head?: number;
1682
+ }
1683
+ export { SelectTableRowOptions }
1684
+ export { SelectTableRowOptions as SelectTableRowOptions_alias_1 }
1685
+
1554
1686
  /**
1555
1687
  * @internal
1556
1688
  */
@@ -1702,6 +1834,20 @@ declare type TableCommandsExtension = Extension<{
1702
1834
  Commands: {
1703
1835
  insertTable: [InsertTableOptions];
1704
1836
  exitTable: [];
1837
+ selectTable: [options?: SelectTableOptions];
1838
+ selectTableCell: [options?: SelectTableCellOptions];
1839
+ selectTableColumn: [options?: SelectTableColumnOptions];
1840
+ selectTableRow: [options?: SelectTableRowOptions];
1841
+ addTableColumnBefore: [];
1842
+ addTableColumnAfter: [];
1843
+ addTableRowAbove: [];
1844
+ addTableRowBelow: [];
1845
+ deleteTable: [];
1846
+ deleteTableColumn: [];
1847
+ deleteTableRow: [];
1848
+ deleteCellSelection: [];
1849
+ mergeTableCells: [];
1850
+ splitTableCell: [];
1705
1851
  };
1706
1852
  }>;
1707
1853
  export { TableCommandsExtension }
@@ -1710,13 +1856,15 @@ export { TableCommandsExtension as TableCommandsExtension_alias_1 }
1710
1856
  /**
1711
1857
  * @internal
1712
1858
  */
1713
- export declare type TableExtension = Union<[
1859
+ declare type TableExtension = Union<[
1714
1860
  TableSpecExtension,
1715
1861
  TableRowSpecExtension,
1716
1862
  TableCellSpecExtension,
1717
1863
  TableHeaderCellSpecExtension,
1718
1864
  TableCommandsExtension
1719
1865
  ]>;
1866
+ export { TableExtension }
1867
+ export { TableExtension as TableExtension_alias_1 }
1720
1868
 
1721
1869
  /**
1722
1870
  * @internal
@@ -1,13 +1,25 @@
1
1
  export { defineTable } from './_tsup-dts-rollup';
2
2
  export { TableExtension } from './_tsup-dts-rollup';
3
- export { defineTableCellSpec } from './_tsup-dts-rollup';
4
3
  export { defineTableCommands } from './_tsup-dts-rollup';
5
- export { defineTableHeaderCellSpec } from './_tsup-dts-rollup';
4
+ export { exitTable } from './_tsup-dts-rollup';
5
+ export { insertTable } from './_tsup-dts-rollup';
6
+ export { selectTable } from './_tsup-dts-rollup';
7
+ export { selectTableCell } from './_tsup-dts-rollup';
8
+ export { selectTableColumn } from './_tsup-dts-rollup';
9
+ export { selectTableRow } from './_tsup-dts-rollup';
10
+ export { InsertTableOptions } from './_tsup-dts-rollup';
11
+ export { SelectTableCellOptions } from './_tsup-dts-rollup';
12
+ export { SelectTableColumnOptions } from './_tsup-dts-rollup';
13
+ export { SelectTableOptions } from './_tsup-dts-rollup';
14
+ export { SelectTableRowOptions } from './_tsup-dts-rollup';
15
+ export { TableCommandsExtension } from './_tsup-dts-rollup';
6
16
  export { defineTablePlugins } from './_tsup-dts-rollup';
17
+ export { defineTableCellSpec } from './_tsup-dts-rollup';
18
+ export { defineTableHeaderCellSpec } from './_tsup-dts-rollup';
7
19
  export { defineTableRowSpec } from './_tsup-dts-rollup';
8
20
  export { defineTableSpec } from './_tsup-dts-rollup';
9
21
  export { TableCellSpecExtension } from './_tsup-dts-rollup';
10
- export { TableCommandsExtension } from './_tsup-dts-rollup';
11
22
  export { TableHeaderCellSpecExtension } from './_tsup-dts-rollup';
12
23
  export { TableRowSpecExtension } from './_tsup-dts-rollup';
13
24
  export { TableSpecExtension } from './_tsup-dts-rollup';
25
+ export { isCellSelection } from './_tsup-dts-rollup';
@@ -1,4 +1,4 @@
1
- // src/table/index.ts
1
+ // src/table/table.ts
2
2
  import { union } from "@prosekit/core";
3
3
 
4
4
  // src/table/table-commands.ts
@@ -9,31 +9,83 @@ import {
9
9
  insertNode
10
10
  } from "@prosekit/core";
11
11
  import { TextSelection } from "@prosekit/pm/state";
12
+ import {
13
+ addColumnAfter,
14
+ addColumnBefore,
15
+ addRowAfter,
16
+ addRowBefore,
17
+ CellSelection as CellSelection2,
18
+ deleteCellSelection,
19
+ deleteColumn,
20
+ deleteRow,
21
+ deleteTable,
22
+ mergeCells,
23
+ splitCell,
24
+ TableMap
25
+ } from "prosemirror-tables";
26
+
27
+ // src/table/table-utils.ts
28
+ import { findParentNode } from "@prosekit/core";
29
+ import {
30
+ cellAround,
31
+ cellNear,
32
+ CellSelection,
33
+ inSameTable
34
+ } from "prosemirror-tables";
35
+ function isCellSelection(value) {
36
+ return value instanceof CellSelection;
37
+ }
38
+ function findTable($pos) {
39
+ return findParentNode((node) => node.type.spec.tableRole === "table", $pos);
40
+ }
41
+ function findCellRange(selection, anchorHit, headHit) {
42
+ var _a, _b;
43
+ if (anchorHit == null && headHit == null && isCellSelection(selection)) {
44
+ return [selection.$anchorCell, selection.$headCell];
45
+ }
46
+ const anchor = (_a = anchorHit != null ? anchorHit : headHit) != null ? _a : selection.anchor;
47
+ const head = (_b = headHit != null ? headHit : anchorHit) != null ? _b : selection.head;
48
+ const doc = selection.$head.doc;
49
+ const $anchorCell = findCellPos(doc, anchor);
50
+ const $headCell = findCellPos(doc, head);
51
+ if ($anchorCell && $headCell && inSameTable($anchorCell, $headCell)) {
52
+ return [$anchorCell, $headCell];
53
+ }
54
+ }
55
+ function findCellPos(doc, pos) {
56
+ const $pos = doc.resolve(pos);
57
+ return cellAround($pos) || cellNear($pos);
58
+ }
59
+
60
+ // src/table/table-commands.ts
12
61
  function createEmptyTable(schema, row, col, header) {
13
- const table = getNodeType(schema, "table");
14
- const tableRow = getNodeType(schema, "tableRow");
15
- const tableCell = getNodeType(schema, "tableCell");
16
- const tableHeaderCell = getNodeType(schema, "tableHeaderCell");
17
- const createHeaderRow = () => {
18
- return tableRow.createAndFill(
19
- null,
20
- Array.from({ length: col }, () => tableHeaderCell.createAndFill())
21
- );
22
- };
23
- const createBodyRow = () => {
24
- return tableRow.createAndFill(
25
- null,
26
- Array.from({ length: col }, () => tableCell.createAndFill())
27
- );
28
- };
29
- const rows = [
30
- ...Array.from({ length: header ? 1 : 0 }, createHeaderRow),
31
- ...Array.from({ length: header ? row - 1 : row }, createBodyRow)
32
- ];
33
- return table.createAndFill(null, rows);
62
+ const tableType = getNodeType(schema, "table");
63
+ const tableRowType = getNodeType(schema, "tableRow");
64
+ const tableCellType = getNodeType(schema, "tableCell");
65
+ const tableHeaderCellType = getNodeType(schema, "tableHeaderCell");
66
+ if (header) {
67
+ const headerCell = tableHeaderCellType.createAndFill();
68
+ const headerCells = repeat(headerCell, col);
69
+ const headerRow = tableRowType.createAndFill(null, headerCells);
70
+ const bodyCell = tableCellType.createAndFill();
71
+ const bodyCells = repeat(bodyCell, col);
72
+ const bodyRow = tableRowType.createAndFill(null, bodyCells);
73
+ const bodyRows = repeat(bodyRow, row - 1);
74
+ return tableType.createAndFill(null, [headerRow, ...bodyRows]);
75
+ } else {
76
+ const bodyCell = tableCellType.createAndFill();
77
+ const bodyCells = repeat(bodyCell, col);
78
+ const bodyRow = tableRowType.createAndFill(null, bodyCells);
79
+ const bodyRows = repeat(bodyRow, row);
80
+ return tableType.createAndFill(null, bodyRows);
81
+ }
34
82
  }
35
- function insertTable({ row, col, header }) {
83
+ function repeat(node, length) {
84
+ return Array(length).fill(node);
85
+ }
86
+ function insertTable(options) {
36
87
  return (state, dispatch, view) => {
88
+ const { row, col, header } = options;
37
89
  const table = createEmptyTable(state.schema, row, col, header);
38
90
  return insertNode({ node: table })(state, dispatch, view);
39
91
  };
@@ -70,10 +122,95 @@ var exitTable = (state, dispatch) => {
70
122
  }
71
123
  return true;
72
124
  };
125
+ function selectTableColumn(options) {
126
+ return (state, dispatch) => {
127
+ const range = findCellRange(state.selection, options == null ? void 0 : options.anchor, options == null ? void 0 : options.head);
128
+ if (!range) {
129
+ return false;
130
+ }
131
+ if (dispatch) {
132
+ const [$anchorCell, $headCell] = range;
133
+ const selection = CellSelection2.colSelection($anchorCell, $headCell);
134
+ dispatch(state.tr.setSelection(selection));
135
+ }
136
+ return true;
137
+ };
138
+ }
139
+ function selectTableRow(options) {
140
+ return (state, dispatch) => {
141
+ const range = findCellRange(state.selection, options == null ? void 0 : options.anchor, options == null ? void 0 : options.head);
142
+ if (!range) {
143
+ return false;
144
+ }
145
+ if (dispatch) {
146
+ const [$anchorCell, $headCell] = range;
147
+ const selection = CellSelection2.rowSelection($anchorCell, $headCell);
148
+ dispatch(state.tr.setSelection(selection));
149
+ }
150
+ return true;
151
+ };
152
+ }
153
+ function selectTableCell(options) {
154
+ return (state, dispatch) => {
155
+ var _a;
156
+ const $cellPos = findCellPos(
157
+ state.doc,
158
+ (_a = options == null ? void 0 : options.pos) != null ? _a : state.selection.anchor
159
+ );
160
+ if (!$cellPos) {
161
+ return false;
162
+ }
163
+ if (dispatch) {
164
+ const selection = new CellSelection2($cellPos);
165
+ dispatch(state.tr.setSelection(selection));
166
+ }
167
+ return true;
168
+ };
169
+ }
170
+ function selectTable(options) {
171
+ return (state, dispatch) => {
172
+ const $pos = (options == null ? void 0 : options.pos) ? state.doc.resolve(options.pos) : state.selection.$anchor;
173
+ const table = findTable($pos);
174
+ if (!table) {
175
+ return false;
176
+ }
177
+ const map = TableMap.get(table.node);
178
+ if (map.map.length === 0) {
179
+ return false;
180
+ }
181
+ if (dispatch) {
182
+ let tr = state.tr;
183
+ const firstCellPosInTable = map.map[0];
184
+ const lastCellPosInTable = map.map[map.map.length - 1];
185
+ const firstCellPos = table.pos + firstCellPosInTable + 1;
186
+ const lastCellPos = table.pos + lastCellPosInTable + 1;
187
+ const $firstCellPos = tr.doc.resolve(firstCellPos);
188
+ const $lastCellPos = tr.doc.resolve(lastCellPos);
189
+ const selection = new CellSelection2($firstCellPos, $lastCellPos);
190
+ tr = tr.setSelection(selection);
191
+ dispatch == null ? void 0 : dispatch(tr);
192
+ }
193
+ return true;
194
+ };
195
+ }
73
196
  function defineTableCommands() {
74
197
  return defineCommands({
75
198
  insertTable,
76
- exitTable: () => exitTable
199
+ exitTable: () => exitTable,
200
+ selectTable,
201
+ selectTableCell,
202
+ selectTableColumn,
203
+ selectTableRow,
204
+ addTableColumnBefore: () => addColumnBefore,
205
+ addTableColumnAfter: () => addColumnAfter,
206
+ addTableRowAbove: () => addRowBefore,
207
+ addTableRowBelow: () => addRowAfter,
208
+ deleteTable: () => deleteTable,
209
+ deleteTableColumn: () => deleteColumn,
210
+ deleteTableRow: () => deleteRow,
211
+ deleteCellSelection: () => deleteCellSelection,
212
+ mergeTableCells: () => mergeCells,
213
+ splitTableCell: () => splitCell
77
214
  });
78
215
  }
79
216
 
@@ -170,7 +307,7 @@ function defineTableHeaderCellSpec() {
170
307
  });
171
308
  }
172
309
 
173
- // src/table/index.ts
310
+ // src/table/table.ts
174
311
  function defineTable() {
175
312
  return union(
176
313
  defineTableSpec(),
@@ -188,5 +325,12 @@ export {
188
325
  defineTableHeaderCellSpec,
189
326
  defineTablePlugins,
190
327
  defineTableRowSpec,
191
- defineTableSpec
328
+ defineTableSpec,
329
+ exitTable,
330
+ insertTable,
331
+ isCellSelection,
332
+ selectTable,
333
+ selectTableCell,
334
+ selectTableColumn,
335
+ selectTableRow
192
336
  };
@@ -30,5 +30,10 @@
30
30
  cursor: col-resize;
31
31
  }
32
32
  .ProseMirror .selectedCell {
33
- background-color: Highlight;
33
+ --color:
34
+ 210,
35
+ 100%,
36
+ 56%;
37
+ background-color: hsla(var(--color), 20%);
38
+ border: 1px double hsl(var(--color));
34
39
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/extensions",
3
3
  "type": "module",
4
- "version": "0.7.12",
4
+ "version": "0.7.13",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -208,7 +208,7 @@
208
208
  "prosemirror-gapcursor": "^1.3.2",
209
209
  "prosemirror-highlight": "^0.8.0",
210
210
  "prosemirror-search": "^1.0.0",
211
- "prosemirror-tables": "^1.4.0",
211
+ "prosemirror-tables": "^1.5.0",
212
212
  "shiki": "^1.14.1",
213
213
  "@prosekit/core": "^0.7.9",
214
214
  "@prosekit/pm": "^0.1.8"