@prosekit/extensions 0.9.2 → 0.10.0

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/extensions",
3
3
  "type": "module",
4
- "version": "0.9.2",
4
+ "version": "0.10.0",
5
5
  "private": false,
6
6
  "description": "A collection of common extensions for ProseKit",
7
7
  "author": {
@@ -206,9 +206,9 @@
206
206
  "prosemirror-highlight": "^0.13.0",
207
207
  "prosemirror-search": "^1.1.0",
208
208
  "prosemirror-tables": "^1.7.1",
209
- "shiki": "^3.6.0",
209
+ "shiki": "^3.7.0",
210
210
  "@prosekit/pm": "^0.1.11",
211
- "@prosekit/core": "^0.8.2"
211
+ "@prosekit/core": "^0.8.3"
212
212
  },
213
213
  "peerDependencies": {
214
214
  "loro-crdt": ">= 0.16.7",
@@ -231,20 +231,20 @@
231
231
  }
232
232
  },
233
233
  "devDependencies": {
234
- "@vitest/browser": "^3.2.3",
234
+ "@vitest/browser": "^3.2.4",
235
235
  "just-pick": "^4.2.0",
236
- "loro-crdt": "^1.5.8",
236
+ "loro-crdt": "^1.5.9",
237
237
  "loro-prosemirror": "^0.2.3",
238
- "prettier": "^3.5.3",
239
- "tsdown": "^0.12.7",
238
+ "prettier": "^3.6.0",
239
+ "tsdown": "^0.12.9",
240
240
  "type-fest": "^4.41.0",
241
241
  "typescript": "~5.8.3",
242
- "vitest": "^3.2.3",
243
- "y-prosemirror": "^1.3.5",
242
+ "vitest": "^3.2.4",
243
+ "y-prosemirror": "^1.3.6",
244
244
  "y-protocols": "^1.0.6",
245
245
  "yjs": "^13.6.27",
246
- "@prosekit/config-tsdown": "0.0.0",
247
- "@prosekit/config-vitest": "0.0.0"
246
+ "@prosekit/config-vitest": "0.0.0",
247
+ "@prosekit/config-tsdown": "0.0.0"
248
248
  },
249
249
  "publishConfig": {
250
250
  "dev": {}
@@ -1,287 +0,0 @@
1
- import { defaultBlockAt, defineCommands, defineNodeSpec, definePlugin, findParentNode, getNodeType, insertNode, union } from "@prosekit/core";
2
- import { TextSelection } from "@prosekit/pm/state";
3
- import { CellSelection, TableMap, addColumnAfter, addColumnBefore, addRowAfter, addRowBefore, cellAround, cellNear, columnResizing, deleteCellSelection, deleteColumn, deleteRow, deleteTable, inSameTable, mergeCells, splitCell, tableEditing, tableNodes } from "prosemirror-tables";
4
-
5
- //#region src/table/table-utils.ts
6
- /**
7
- * Checks if the given object is a `CellSelection` instance.
8
- *
9
- * @public
10
- */
11
- function isCellSelection(value) {
12
- return value instanceof CellSelection;
13
- }
14
- /**
15
- * Find the closest table node.
16
- *
17
- * @internal
18
- */
19
- function findTable($pos) {
20
- return findParentNode((node) => node.type.spec.tableRole === "table", $pos);
21
- }
22
- /**
23
- * Try to find the anchor and head cell in the same table by using the given
24
- * anchor and head as hit points, or fallback to the selection's anchor and
25
- * head.
26
- *
27
- * @internal
28
- */
29
- function findCellRange(selection, anchorHit, headHit) {
30
- if (anchorHit == null && headHit == null && isCellSelection(selection)) return [selection.$anchorCell, selection.$headCell];
31
- const anchor = anchorHit ?? headHit ?? selection.anchor;
32
- const head = headHit ?? anchorHit ?? selection.head;
33
- const doc = selection.$head.doc;
34
- const $anchorCell = findCellPos(doc, anchor);
35
- const $headCell = findCellPos(doc, head);
36
- if ($anchorCell && $headCell && inSameTable($anchorCell, $headCell)) return [$anchorCell, $headCell];
37
- }
38
- /**
39
- * Try to find a resolved pos of a cell by using the given pos as a hit point.
40
- *
41
- * @internal
42
- */
43
- function findCellPos(doc, pos) {
44
- const $pos = doc.resolve(pos);
45
- return cellAround($pos) || cellNear($pos);
46
- }
47
-
48
- //#endregion
49
- //#region src/table/table-commands.ts
50
- function createEmptyTable(schema, row, col, header) {
51
- const tableType = getNodeType(schema, "table");
52
- const tableRowType = getNodeType(schema, "tableRow");
53
- const tableCellType = getNodeType(schema, "tableCell");
54
- const tableHeaderCellType = getNodeType(schema, "tableHeaderCell");
55
- if (header) {
56
- const headerCell = tableHeaderCellType.createAndFill();
57
- const headerCells = repeat(headerCell, col);
58
- const headerRow = tableRowType.createAndFill(null, headerCells);
59
- const bodyCell = tableCellType.createAndFill();
60
- const bodyCells = repeat(bodyCell, col);
61
- const bodyRow = tableRowType.createAndFill(null, bodyCells);
62
- const bodyRows = repeat(bodyRow, row - 1);
63
- return tableType.createAndFill(null, [headerRow, ...bodyRows]);
64
- } else {
65
- const bodyCell = tableCellType.createAndFill();
66
- const bodyCells = repeat(bodyCell, col);
67
- const bodyRow = tableRowType.createAndFill(null, bodyCells);
68
- const bodyRows = repeat(bodyRow, row);
69
- return tableType.createAndFill(null, bodyRows);
70
- }
71
- }
72
- function repeat(node, length) {
73
- return Array(length).fill(node);
74
- }
75
- /**
76
- * Insert a table node with the given number of rows and columns, and optionally
77
- * a header row.
78
- *
79
- * @param options
80
- *
81
- * @public
82
- */
83
- function insertTable(options) {
84
- return (state, dispatch, view) => {
85
- const { row, col, header = false } = options;
86
- const table = createEmptyTable(state.schema, row, col, header);
87
- return insertNode({ node: table })(state, dispatch, view);
88
- };
89
- }
90
- /**
91
- * When the selection is in a table node, create a default block after the table
92
- * table, and move the cursor there.
93
- *
94
- * @public
95
- */
96
- const exitTable = (state, dispatch) => {
97
- const { $head, $anchor } = state.selection;
98
- if (!$head.sameParent($anchor)) return false;
99
- let tableStart = -1;
100
- let tableDepth = -1;
101
- for (let depth = $head.depth; depth >= 0; depth--) {
102
- const node$1 = $head.node(depth);
103
- if (node$1.type.spec.tableRole === "table") {
104
- tableStart = $head.before(depth);
105
- tableDepth = depth;
106
- }
107
- }
108
- if (tableStart < 0 || tableDepth <= 0) return false;
109
- const above = $head.node(tableDepth - 1);
110
- const after = $head.indexAfter(tableDepth - 1);
111
- const type = defaultBlockAt(above.contentMatchAt(after));
112
- const node = type?.createAndFill();
113
- if (!type || !node || !above.canReplaceWith(after, after, type)) return false;
114
- if (dispatch) {
115
- const pos = $head.after(tableDepth);
116
- const tr = state.tr.replaceWith(pos, pos, node);
117
- tr.setSelection(TextSelection.near(tr.doc.resolve(pos), 1));
118
- dispatch(tr.scrollIntoView());
119
- }
120
- return true;
121
- };
122
- /**
123
- * @public
124
- */
125
- function selectTableColumn(options) {
126
- return (state, dispatch) => {
127
- const range = findCellRange(state.selection, options?.anchor, options?.head);
128
- if (!range) return false;
129
- if (dispatch) {
130
- const [$anchorCell, $headCell] = range;
131
- const selection = CellSelection.colSelection($anchorCell, $headCell);
132
- dispatch(state.tr.setSelection(selection));
133
- }
134
- return true;
135
- };
136
- }
137
- /**
138
- * @public
139
- */
140
- function selectTableRow(options) {
141
- return (state, dispatch) => {
142
- const range = findCellRange(state.selection, options?.anchor, options?.head);
143
- if (!range) return false;
144
- if (dispatch) {
145
- const [$anchorCell, $headCell] = range;
146
- const selection = CellSelection.rowSelection($anchorCell, $headCell);
147
- dispatch(state.tr.setSelection(selection));
148
- }
149
- return true;
150
- };
151
- }
152
- /**
153
- * @public
154
- */
155
- function selectTableCell(options) {
156
- return (state, dispatch) => {
157
- const $cellPos = findCellPos(state.doc, options?.pos ?? state.selection.anchor);
158
- if (!$cellPos) return false;
159
- if (dispatch) {
160
- const selection = new CellSelection($cellPos);
161
- dispatch(state.tr.setSelection(selection));
162
- }
163
- return true;
164
- };
165
- }
166
- /**
167
- * @public
168
- */
169
- function selectTable(options) {
170
- return (state, dispatch) => {
171
- const $pos = options?.pos ? state.doc.resolve(options.pos) : state.selection.$anchor;
172
- const table = findTable($pos);
173
- if (!table) return false;
174
- const map = TableMap.get(table.node);
175
- if (map.map.length === 0) return false;
176
- if (dispatch) {
177
- let tr = state.tr;
178
- const firstCellPosInTable = map.map[0];
179
- const lastCellPosInTable = map.map[map.map.length - 1];
180
- const firstCellPos = table.pos + firstCellPosInTable + 1;
181
- const lastCellPos = table.pos + lastCellPosInTable + 1;
182
- const $firstCellPos = tr.doc.resolve(firstCellPos);
183
- const $lastCellPos = tr.doc.resolve(lastCellPos);
184
- const selection = new CellSelection($firstCellPos, $lastCellPos);
185
- tr = tr.setSelection(selection);
186
- dispatch?.(tr);
187
- }
188
- return true;
189
- };
190
- }
191
- /**
192
- * Adds commands for working with `table` nodes.
193
- *
194
- * @public
195
- */
196
- function defineTableCommands() {
197
- return defineCommands({
198
- insertTable,
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
214
- });
215
- }
216
-
217
- //#endregion
218
- //#region src/table/table-plugins.ts
219
- /**
220
- * @public
221
- */
222
- function defineTablePlugins() {
223
- return definePlugin([tableEditing(), columnResizing()]);
224
- }
225
-
226
- //#endregion
227
- //#region src/table/table-spec.ts
228
- const cellContent = "block+";
229
- const cellAttrs = {
230
- colspan: { default: 1 },
231
- rowspan: { default: 1 },
232
- colwidth: { default: null }
233
- };
234
- const specs = tableNodes({
235
- tableGroup: "block",
236
- cellContent,
237
- cellAttributes: {}
238
- });
239
- /**
240
- * @internal
241
- */
242
- function defineTableSpec() {
243
- return defineNodeSpec({
244
- ...specs["table"],
245
- content: "tableRow+",
246
- name: "table"
247
- });
248
- }
249
- /**
250
- * @internal
251
- */
252
- function defineTableRowSpec() {
253
- return defineNodeSpec({
254
- ...specs["table_row"],
255
- content: "(tableCell | tableHeaderCell)*",
256
- name: "tableRow"
257
- });
258
- }
259
- /**
260
- * @internal
261
- */
262
- function defineTableCellSpec() {
263
- return defineNodeSpec({
264
- ...specs["table_cell"],
265
- name: "tableCell",
266
- attrs: cellAttrs
267
- });
268
- }
269
- function defineTableHeaderCellSpec() {
270
- return defineNodeSpec({
271
- ...specs["table_header"],
272
- name: "tableHeaderCell",
273
- attrs: cellAttrs
274
- });
275
- }
276
-
277
- //#endregion
278
- //#region src/table/table.ts
279
- /**
280
- * @public
281
- */
282
- function defineTable() {
283
- return union(defineTableSpec(), defineTableRowSpec(), defineTableCellSpec(), defineTableHeaderCellSpec(), defineTablePlugins(), defineTableCommands());
284
- }
285
-
286
- //#endregion
287
- export { defineTable, defineTableCellSpec, defineTableCommands, defineTableHeaderCellSpec, defineTablePlugins, defineTableRowSpec, defineTableSpec, exitTable, findTable, insertTable, isCellSelection, selectTable, selectTableCell, selectTableColumn, selectTableRow };