@prosekit/extensions 0.4.1 → 0.4.2
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.
@@ -5,6 +5,7 @@ import { bundledLanguagesInfo } from 'shiki';
|
|
5
5
|
import { BundledTheme } from 'shiki';
|
6
6
|
import { BundledThemeInfo } from 'shiki';
|
7
7
|
import { bundledThemesInfo } from 'shiki';
|
8
|
+
import { Command } from '@prosekit/pm/state';
|
8
9
|
import { CommandArgs } from '@prosekit/core';
|
9
10
|
import { DedentListOptions } from 'prosemirror-flat-list';
|
10
11
|
import { EditorState } from '@prosekit/pm/state';
|
@@ -64,6 +65,12 @@ export { BundledThemeInfo as ShikiBundledThemeInfo_alias_1 }
|
|
64
65
|
export { bundledThemesInfo as shikiBundledThemesInfo }
|
65
66
|
export { bundledThemesInfo as shikiBundledThemesInfo_alias_1 }
|
66
67
|
|
68
|
+
export declare interface CellAttrs {
|
69
|
+
colspan: number;
|
70
|
+
rowspan: number;
|
71
|
+
colwidth: number[] | null;
|
72
|
+
}
|
73
|
+
|
67
74
|
/**
|
68
75
|
* The attributes for the `codeBlock` node.
|
69
76
|
*
|
@@ -516,6 +523,70 @@ export declare function defineStrikeSpec(): Extension< {
|
|
516
523
|
MARKS: "strike";
|
517
524
|
}>;
|
518
525
|
|
526
|
+
/**
|
527
|
+
* @public
|
528
|
+
*/
|
529
|
+
export declare function defineTable(): Extension< {
|
530
|
+
NODES: "table" | "tableRow" | "tableCell" | "tableHeaderCell";
|
531
|
+
COMMAND_ARGS: {
|
532
|
+
insertTable: [{
|
533
|
+
row: number;
|
534
|
+
col: number;
|
535
|
+
header: boolean;
|
536
|
+
}];
|
537
|
+
exitTable: [];
|
538
|
+
};
|
539
|
+
}>;
|
540
|
+
|
541
|
+
declare function defineTableCellSpec(): Extension< {
|
542
|
+
NODES: "tableCell";
|
543
|
+
}>;
|
544
|
+
export { defineTableCellSpec }
|
545
|
+
export { defineTableCellSpec as defineTableCellSpec_alias_1 }
|
546
|
+
|
547
|
+
/**
|
548
|
+
* Adds commands for working with `table` nodes.
|
549
|
+
*
|
550
|
+
* @public
|
551
|
+
*/
|
552
|
+
declare function defineTableCommands(): Extension< {
|
553
|
+
COMMAND_ARGS: {
|
554
|
+
insertTable: [{
|
555
|
+
row: number;
|
556
|
+
col: number;
|
557
|
+
header: boolean;
|
558
|
+
}];
|
559
|
+
exitTable: [];
|
560
|
+
};
|
561
|
+
}>;
|
562
|
+
export { defineTableCommands }
|
563
|
+
export { defineTableCommands as defineTableCommands_alias_1 }
|
564
|
+
|
565
|
+
declare function defineTableHeaderCellSpec(): Extension< {
|
566
|
+
NODES: "tableHeaderCell";
|
567
|
+
}>;
|
568
|
+
export { defineTableHeaderCellSpec }
|
569
|
+
export { defineTableHeaderCellSpec as defineTableHeaderCellSpec_alias_1 }
|
570
|
+
|
571
|
+
/**
|
572
|
+
* @public
|
573
|
+
*/
|
574
|
+
declare function defineTablePlugins(): Extension<ExtensionTyping<string, string, CommandArgs>>;
|
575
|
+
export { defineTablePlugins }
|
576
|
+
export { defineTablePlugins as defineTablePlugins_alias_1 }
|
577
|
+
|
578
|
+
declare function defineTableRowSpec(): Extension< {
|
579
|
+
NODES: "tableRow";
|
580
|
+
}>;
|
581
|
+
export { defineTableRowSpec }
|
582
|
+
export { defineTableRowSpec as defineTableRowSpec_alias_1 }
|
583
|
+
|
584
|
+
declare function defineTableSpec(): Extension< {
|
585
|
+
NODES: "table";
|
586
|
+
}>;
|
587
|
+
export { defineTableSpec }
|
588
|
+
export { defineTableSpec as defineTableSpec_alias_1 }
|
589
|
+
|
519
590
|
/**
|
520
591
|
* Defines an enter rule that replaces the matched text with a block node.
|
521
592
|
*
|
@@ -679,6 +750,14 @@ export declare type EnterRuleOptions = {
|
|
679
750
|
stop?: boolean;
|
680
751
|
};
|
681
752
|
|
753
|
+
/**
|
754
|
+
* When the selection is in a table node, create a default block after the table
|
755
|
+
* table, and move the cursor there.
|
756
|
+
*
|
757
|
+
* @public
|
758
|
+
*/
|
759
|
+
export declare const exitTable: Command;
|
760
|
+
|
682
761
|
export { getHighlighter }
|
683
762
|
|
684
763
|
export declare function getPluginState(state: EditorState): PredictionPluginState | undefined;
|
@@ -708,6 +787,18 @@ export declare interface ImageAttrs {
|
|
708
787
|
src?: string | null;
|
709
788
|
}
|
710
789
|
|
790
|
+
/**
|
791
|
+
* Insert a table node with the given number of rows and columns, and optionally
|
792
|
+
* a header row.
|
793
|
+
*
|
794
|
+
* @public
|
795
|
+
*/
|
796
|
+
export declare function insertTable({ row, col, header, }: {
|
797
|
+
row: number;
|
798
|
+
col: number;
|
799
|
+
header: boolean;
|
800
|
+
}): Command;
|
801
|
+
|
711
802
|
export declare const LINK_RE: RegExp;
|
712
803
|
|
713
804
|
export declare const LINK_SPACE_RE: RegExp;
|
@@ -0,0 +1,7 @@
|
|
1
|
+
export { defineTable } from './_tsup-dts-rollup';
|
2
|
+
export { defineTableCellSpec } from './_tsup-dts-rollup';
|
3
|
+
export { defineTableCommands } from './_tsup-dts-rollup';
|
4
|
+
export { defineTableHeaderCellSpec } from './_tsup-dts-rollup';
|
5
|
+
export { defineTablePlugins } from './_tsup-dts-rollup';
|
6
|
+
export { defineTableRowSpec } from './_tsup-dts-rollup';
|
7
|
+
export { defineTableSpec } from './_tsup-dts-rollup';
|
@@ -0,0 +1,197 @@
|
|
1
|
+
// src/table/index.ts
|
2
|
+
import { union } from "@prosekit/core";
|
3
|
+
|
4
|
+
// src/table/table-commands.ts
|
5
|
+
import {
|
6
|
+
defaultBlockAt,
|
7
|
+
defineCommands,
|
8
|
+
getNodeType,
|
9
|
+
insertNode
|
10
|
+
} from "@prosekit/core";
|
11
|
+
import { TextSelection } from "@prosekit/pm/state";
|
12
|
+
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);
|
34
|
+
}
|
35
|
+
function insertTable({
|
36
|
+
row,
|
37
|
+
col,
|
38
|
+
header
|
39
|
+
}) {
|
40
|
+
return (state, dispatch, view) => {
|
41
|
+
const table = createEmptyTable(state.schema, row, col, header);
|
42
|
+
return insertNode({ node: table })(state, dispatch, view);
|
43
|
+
};
|
44
|
+
}
|
45
|
+
var exitTable = (state, dispatch) => {
|
46
|
+
const { $head, $anchor } = state.selection;
|
47
|
+
if (!$head.sameParent($anchor)) {
|
48
|
+
return false;
|
49
|
+
}
|
50
|
+
let tableStart = -1;
|
51
|
+
let tableDepth = -1;
|
52
|
+
for (let depth = $head.depth; depth >= 0; depth--) {
|
53
|
+
const node2 = $head.node(depth);
|
54
|
+
if (node2.type.spec.tableRole === "table") {
|
55
|
+
tableStart = $head.before(depth);
|
56
|
+
tableDepth = depth;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
if (tableStart < 0 || tableDepth <= 0) {
|
60
|
+
return false;
|
61
|
+
}
|
62
|
+
const above = $head.node(tableDepth - 1);
|
63
|
+
const after = $head.indexAfter(tableDepth - 1);
|
64
|
+
const type = defaultBlockAt(above.contentMatchAt(after));
|
65
|
+
const node = type == null ? void 0 : type.createAndFill();
|
66
|
+
if (!type || !node || !above.canReplaceWith(after, after, type)) {
|
67
|
+
return false;
|
68
|
+
}
|
69
|
+
if (dispatch) {
|
70
|
+
const pos = $head.after(tableDepth);
|
71
|
+
const tr = state.tr.replaceWith(pos, pos, node);
|
72
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(pos), 1));
|
73
|
+
dispatch(tr.scrollIntoView());
|
74
|
+
}
|
75
|
+
return true;
|
76
|
+
};
|
77
|
+
function defineTableCommands() {
|
78
|
+
return defineCommands({
|
79
|
+
insertTable,
|
80
|
+
exitTable: () => exitTable
|
81
|
+
});
|
82
|
+
}
|
83
|
+
|
84
|
+
// src/table/table-plugins.ts
|
85
|
+
import { definePlugin } from "@prosekit/core";
|
86
|
+
import { tableEditing, columnResizing } from "prosemirror-tables";
|
87
|
+
function defineTablePlugins() {
|
88
|
+
return definePlugin([tableEditing(), columnResizing()]);
|
89
|
+
}
|
90
|
+
|
91
|
+
// src/table/table-spec.ts
|
92
|
+
import { defineNodeSpec } from "@prosekit/core";
|
93
|
+
import "prosemirror-tables";
|
94
|
+
var cellAttrs = {
|
95
|
+
colspan: { default: 1 },
|
96
|
+
rowspan: { default: 1 },
|
97
|
+
colwidth: { default: null }
|
98
|
+
};
|
99
|
+
var cellContent = "block+";
|
100
|
+
function getCellAttrs(dom) {
|
101
|
+
if (typeof dom === "string") {
|
102
|
+
return {};
|
103
|
+
}
|
104
|
+
const widthAttr = dom.getAttribute("data-colwidth");
|
105
|
+
const widths = widthAttr && /^\d+(,\d+)*$/.test(widthAttr) ? widthAttr.split(",").map((s) => Number(s)) : null;
|
106
|
+
const colspan = Number(dom.getAttribute("colspan") || 1);
|
107
|
+
return {
|
108
|
+
colspan,
|
109
|
+
rowspan: Number(dom.getAttribute("rowspan") || 1),
|
110
|
+
colwidth: widths && widths.length == colspan ? widths : null
|
111
|
+
};
|
112
|
+
}
|
113
|
+
function setCellAttrs(node) {
|
114
|
+
const pmAttrs = node.attrs;
|
115
|
+
const domAttrs = {};
|
116
|
+
if (pmAttrs.colspan !== 1) {
|
117
|
+
domAttrs.colspan = pmAttrs.colspan;
|
118
|
+
}
|
119
|
+
if (pmAttrs.rowspan !== 1) {
|
120
|
+
domAttrs.rowspan = pmAttrs.rowspan;
|
121
|
+
}
|
122
|
+
if (pmAttrs.colwidth) {
|
123
|
+
domAttrs["data-colwidth"] = pmAttrs.colwidth.join(",");
|
124
|
+
}
|
125
|
+
return domAttrs;
|
126
|
+
}
|
127
|
+
function defineTableSpec() {
|
128
|
+
return defineNodeSpec({
|
129
|
+
name: "table",
|
130
|
+
tableRole: "table",
|
131
|
+
content: "tableRow+",
|
132
|
+
isolating: true,
|
133
|
+
group: "block",
|
134
|
+
parseDOM: [{ tag: "table" }],
|
135
|
+
toDOM() {
|
136
|
+
return ["table", ["tbody", 0]];
|
137
|
+
}
|
138
|
+
});
|
139
|
+
}
|
140
|
+
function defineTableRowSpec() {
|
141
|
+
return defineNodeSpec({
|
142
|
+
name: "tableRow",
|
143
|
+
tableRole: "row",
|
144
|
+
content: "(tableCell | tableHeaderCell)*",
|
145
|
+
parseDOM: [{ tag: "tr" }],
|
146
|
+
toDOM() {
|
147
|
+
return ["tr", 0];
|
148
|
+
}
|
149
|
+
});
|
150
|
+
}
|
151
|
+
function defineTableCellSpec() {
|
152
|
+
return defineNodeSpec({
|
153
|
+
name: "tableCell",
|
154
|
+
tableRole: "cell",
|
155
|
+
content: cellContent,
|
156
|
+
attrs: cellAttrs,
|
157
|
+
isolating: true,
|
158
|
+
parseDOM: [{ tag: "td", getAttrs: (dom) => getCellAttrs(dom) }],
|
159
|
+
toDOM(node) {
|
160
|
+
return ["td", setCellAttrs(node), 0];
|
161
|
+
}
|
162
|
+
});
|
163
|
+
}
|
164
|
+
function defineTableHeaderCellSpec() {
|
165
|
+
return defineNodeSpec({
|
166
|
+
name: "tableHeaderCell",
|
167
|
+
tableRole: "header_cell",
|
168
|
+
content: cellContent,
|
169
|
+
attrs: cellAttrs,
|
170
|
+
isolating: true,
|
171
|
+
parseDOM: [{ tag: "th", getAttrs: (dom) => getCellAttrs(dom) }],
|
172
|
+
toDOM(node) {
|
173
|
+
return ["th", setCellAttrs(node), 0];
|
174
|
+
}
|
175
|
+
});
|
176
|
+
}
|
177
|
+
|
178
|
+
// src/table/index.ts
|
179
|
+
function defineTable() {
|
180
|
+
return union([
|
181
|
+
defineTableSpec(),
|
182
|
+
defineTableRowSpec(),
|
183
|
+
defineTableCellSpec(),
|
184
|
+
defineTableHeaderCellSpec(),
|
185
|
+
defineTablePlugins(),
|
186
|
+
defineTableCommands()
|
187
|
+
]);
|
188
|
+
}
|
189
|
+
export {
|
190
|
+
defineTable,
|
191
|
+
defineTableCellSpec,
|
192
|
+
defineTableCommands,
|
193
|
+
defineTableHeaderCellSpec,
|
194
|
+
defineTablePlugins,
|
195
|
+
defineTableRowSpec,
|
196
|
+
defineTableSpec
|
197
|
+
};
|
@@ -0,0 +1,34 @@
|
|
1
|
+
/* src/table/style.css */
|
2
|
+
.ProseMirror .tableWrapper {
|
3
|
+
overflow-x: auto;
|
4
|
+
}
|
5
|
+
.ProseMirror table {
|
6
|
+
border-collapse: collapse;
|
7
|
+
table-layout: fixed;
|
8
|
+
width: 100%;
|
9
|
+
overflow: hidden;
|
10
|
+
}
|
11
|
+
.ProseMirror td,
|
12
|
+
.ProseMirror th {
|
13
|
+
vertical-align: top;
|
14
|
+
box-sizing: border-box;
|
15
|
+
position: relative;
|
16
|
+
border-width: 1px;
|
17
|
+
}
|
18
|
+
.ProseMirror .column-resize-handle {
|
19
|
+
position: absolute;
|
20
|
+
right: -2px;
|
21
|
+
top: 0;
|
22
|
+
bottom: 0;
|
23
|
+
width: 4px;
|
24
|
+
z-index: 20;
|
25
|
+
background-color: HighlightText;
|
26
|
+
pointer-events: none;
|
27
|
+
}
|
28
|
+
.ProseMirror.resize-cursor {
|
29
|
+
cursor: ew-resize;
|
30
|
+
cursor: col-resize;
|
31
|
+
}
|
32
|
+
.ProseMirror .selectedCell {
|
33
|
+
background-color: Highlight;
|
34
|
+
}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@prosekit/extensions",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.4.
|
4
|
+
"version": "0.4.2",
|
5
5
|
"private": false,
|
6
6
|
"author": {
|
7
7
|
"name": "ocavue",
|
@@ -121,6 +121,14 @@
|
|
121
121
|
"import": "./dist/prosekit-extensions-strike.js",
|
122
122
|
"default": "./dist/prosekit-extensions-strike.js"
|
123
123
|
},
|
124
|
+
"./table": {
|
125
|
+
"types": "./dist/prosekit-extensions-table.d.ts",
|
126
|
+
"import": "./dist/prosekit-extensions-table.js",
|
127
|
+
"default": "./dist/prosekit-extensions-table.js"
|
128
|
+
},
|
129
|
+
"./table/style.css": {
|
130
|
+
"default": "./dist/table/style.css"
|
131
|
+
},
|
124
132
|
"./underline": {
|
125
133
|
"types": "./dist/prosekit-extensions-underline.d.ts",
|
126
134
|
"import": "./dist/prosekit-extensions-underline.js",
|
@@ -144,13 +152,14 @@
|
|
144
152
|
"prosemirror-dropcursor": "^1.8.1",
|
145
153
|
"prosemirror-flat-list": "^0.4.6",
|
146
154
|
"prosemirror-highlight": "^0.5.0",
|
147
|
-
"
|
155
|
+
"prosemirror-tables": "^1.3.7",
|
156
|
+
"shiki": "^1.1.7"
|
148
157
|
},
|
149
158
|
"devDependencies": {
|
150
159
|
"@prosekit/dev": "*",
|
151
160
|
"tsup": "^8.0.2",
|
152
161
|
"typescript": "^5.3.3",
|
153
|
-
"vitest": "^1.
|
162
|
+
"vitest": "^1.3.1"
|
154
163
|
},
|
155
164
|
"scripts": {
|
156
165
|
"build:tsup": "tsup",
|
@@ -213,6 +222,9 @@
|
|
213
222
|
"strike": [
|
214
223
|
"./dist/prosekit-extensions-strike.d.ts"
|
215
224
|
],
|
225
|
+
"table": [
|
226
|
+
"./dist/prosekit-extensions-table.d.ts"
|
227
|
+
],
|
216
228
|
"underline": [
|
217
229
|
"./dist/prosekit-extensions-underline.d.ts"
|
218
230
|
],
|