@prosekit/extensions 0.4.0 → 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
  *
@@ -639,14 +710,26 @@ export declare interface DropCursorOptions {
639
710
  * @public
640
711
  */
641
712
  export declare type EnterRuleHandler = (options: {
713
+ /**
714
+ * The current editor state.
715
+ */
642
716
  state: EditorState;
717
+ /**
718
+ * The start position of the matched text.
719
+ */
643
720
  from: number;
721
+ /**
722
+ * The end position of the matched text.
723
+ */
644
724
  to: number;
725
+ /**
726
+ * The matched result from the regular expression.
727
+ */
645
728
  match: RegExpExecArray;
646
729
  }) => Transaction | null;
647
730
 
648
731
  /**
649
- * Options for {@link createEnterRule}.
732
+ * Options for {@link defineEnterRule}.
650
733
  *
651
734
  * @public
652
735
  */
@@ -656,7 +739,7 @@ export declare type EnterRuleOptions = {
656
739
  */
657
740
  regex: RegExp;
658
741
  /**
659
- * A handler function to be called when an enter rule is triggered.
742
+ * A function to be called when an enter rule is triggered.
660
743
  */
661
744
  handler: EnterRuleHandler;
662
745
  /**
@@ -667,6 +750,14 @@ export declare type EnterRuleOptions = {
667
750
  stop?: boolean;
668
751
  };
669
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
+
670
761
  export { getHighlighter }
671
762
 
672
763
  export declare function getPluginState(state: EditorState): PredictionPluginState | undefined;
@@ -696,6 +787,18 @@ export declare interface ImageAttrs {
696
787
  src?: string | null;
697
788
  }
698
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
+
699
802
  export declare const LINK_RE: RegExp;
700
803
 
701
804
  export declare const LINK_SPACE_RE: RegExp;
@@ -757,7 +860,7 @@ export declare interface PredictionPluginState {
757
860
  export declare function setTrMeta(tr: Transaction, meta: PredictionPluginState): Transaction;
758
861
 
759
862
  /**
760
- * Options for {@link createTextBlockEnterRule}.
863
+ * Options for {@link defineTextBlockEnterRule}.
761
864
  *
762
865
  * @public
763
866
  */
@@ -771,7 +874,8 @@ export declare type TextBlockEnterRuleOptions = {
771
874
  */
772
875
  type: string | NodeType;
773
876
  /**
774
- * Attributes to set on the node.
877
+ * Attributes to set on the node. If a function is provided, it will be called
878
+ * with the matched result from the regular expression.
775
879
  */
776
880
  attrs?: Attrs | null | ((match: RegExpMatchArray) => Attrs | null);
777
881
  /**
@@ -1,9 +1,9 @@
1
- import {
2
- defineTextBlockEnterRule
3
- } from "./chunk-6DQPSMHC.js";
4
1
  import {
5
2
  defineTextBlockInputRule
6
3
  } from "./chunk-DYFRBXUX.js";
4
+ import {
5
+ defineTextBlockEnterRule
6
+ } from "./chunk-ASTUC4KT.js";
7
7
 
8
8
  // src/code-block/code-block.ts
9
9
  import { union } from "@prosekit/core";
@@ -0,0 +1,5 @@
1
+ export { defineEnterRule } from './_tsup-dts-rollup';
2
+ export { defineTextBlockEnterRule } from './_tsup-dts-rollup';
3
+ export { EnterRuleHandler } from './_tsup-dts-rollup';
4
+ export { EnterRuleOptions } from './_tsup-dts-rollup';
5
+ export { TextBlockEnterRuleOptions } from './_tsup-dts-rollup';
@@ -0,0 +1,8 @@
1
+ import {
2
+ defineEnterRule,
3
+ defineTextBlockEnterRule
4
+ } from "./chunk-ASTUC4KT.js";
5
+ export {
6
+ defineEnterRule,
7
+ defineTextBlockEnterRule
8
+ };
@@ -1,9 +1,9 @@
1
- import {
2
- defineEnterRule
3
- } from "./chunk-6DQPSMHC.js";
4
1
  import {
5
2
  defineInputRule
6
3
  } from "./chunk-DYFRBXUX.js";
4
+ import {
5
+ defineEnterRule
6
+ } from "./chunk-ASTUC4KT.js";
7
7
 
8
8
  // src/link/index.ts
9
9
  import {
@@ -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.0",
4
+ "version": "0.4.2",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -60,6 +60,11 @@
60
60
  "import": "./dist/prosekit-extensions-drop-cursor.js",
61
61
  "default": "./dist/prosekit-extensions-drop-cursor.js"
62
62
  },
63
+ "./enter-rule": {
64
+ "types": "./dist/prosekit-extensions-enter-rule.d.ts",
65
+ "import": "./dist/prosekit-extensions-enter-rule.js",
66
+ "default": "./dist/prosekit-extensions-enter-rule.js"
67
+ },
63
68
  "./heading": {
64
69
  "types": "./dist/prosekit-extensions-heading.d.ts",
65
70
  "import": "./dist/prosekit-extensions-heading.js",
@@ -116,6 +121,14 @@
116
121
  "import": "./dist/prosekit-extensions-strike.js",
117
122
  "default": "./dist/prosekit-extensions-strike.js"
118
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
+ },
119
132
  "./underline": {
120
133
  "types": "./dist/prosekit-extensions-underline.d.ts",
121
134
  "import": "./dist/prosekit-extensions-underline.js",
@@ -135,17 +148,18 @@
135
148
  ],
136
149
  "dependencies": {
137
150
  "@prosekit/core": "^0.4.0",
138
- "@prosekit/pm": "^0.1.1",
151
+ "@prosekit/pm": "^0.1.2",
139
152
  "prosemirror-dropcursor": "^1.8.1",
140
153
  "prosemirror-flat-list": "^0.4.6",
141
154
  "prosemirror-highlight": "^0.5.0",
142
- "shiki": "^1.1.2"
155
+ "prosemirror-tables": "^1.3.7",
156
+ "shiki": "^1.1.7"
143
157
  },
144
158
  "devDependencies": {
145
159
  "@prosekit/dev": "*",
146
160
  "tsup": "^8.0.2",
147
161
  "typescript": "^5.3.3",
148
- "vitest": "^1.2.2"
162
+ "vitest": "^1.3.1"
149
163
  },
150
164
  "scripts": {
151
165
  "build:tsup": "tsup",
@@ -175,6 +189,9 @@
175
189
  "drop-cursor": [
176
190
  "./dist/prosekit-extensions-drop-cursor.d.ts"
177
191
  ],
192
+ "enter-rule": [
193
+ "./dist/prosekit-extensions-enter-rule.d.ts"
194
+ ],
178
195
  "heading": [
179
196
  "./dist/prosekit-extensions-heading.d.ts"
180
197
  ],
@@ -205,6 +222,9 @@
205
222
  "strike": [
206
223
  "./dist/prosekit-extensions-strike.d.ts"
207
224
  ],
225
+ "table": [
226
+ "./dist/prosekit-extensions-table.d.ts"
227
+ ],
208
228
  "underline": [
209
229
  "./dist/prosekit-extensions-underline.d.ts"
210
230
  ],
File without changes