@blocknote/core 0.54.0 → 0.54.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.
- package/dist/blocknote.cjs +1 -1
- package/dist/blocknote.js +6 -6
- package/dist/{blocks-CzkqclGj.cjs → blocks-CTB_5Vyj.cjs} +4 -4
- package/dist/blocks-CTB_5Vyj.cjs.map +1 -0
- package/dist/{blocks-CzQLehlc.js → blocks-Dol85hpH.js} +337 -330
- package/dist/blocks-Dol85hpH.js.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +2 -2
- package/dist/extensions-B7Rsp_Ft.cjs +58 -0
- package/dist/extensions-B7Rsp_Ft.cjs.map +1 -0
- package/dist/{extensions-DGuhtOgB.js → extensions-D6JcJCwd.js} +389 -346
- package/dist/extensions-D6JcJCwd.js.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +3 -3
- package/dist/{src-BkfZVfaO.js → src-Buuo5l7X.js} +521 -516
- package/dist/{src-BkfZVfaO.js.map → src-Buuo5l7X.js.map} +1 -1
- package/dist/{src-DM3syMdf.cjs → src-C54Ok9E0.cjs} +10 -10
- package/dist/{src-DM3syMdf.cjs.map → src-C54Ok9E0.cjs.map} +1 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/y.cjs +1 -1
- package/dist/y.cjs.map +1 -1
- package/dist/y.js +205 -205
- package/dist/y.js.map +1 -1
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.js +21 -21
- package/package.json +1 -1
- package/src/api/getBlocksChangedByTransaction.test.ts +211 -0
- package/src/api/getBlocksChangedByTransaction.ts +48 -12
- package/src/api/getChangedRange.ts +54 -0
- package/src/blocks/index.ts +0 -1
- package/src/editor/performance.test.ts +45 -0
- package/src/exporter/Exporter.test.ts +148 -10
- package/src/exporter/Exporter.ts +20 -2
- package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +21 -0
- package/src/extensions/PreviousBlockType/PreviousBlockType.ts +5 -3
- package/src/extensions/TableHandles/TableHandles.browser.test.ts +305 -0
- package/src/extensions/TableHandles/TableHandles.ts +82 -41
- package/src/fonts/inter.css +9 -9
- package/src/schema/blocks/createSpec.ts +4 -5
- package/src/schema/inlineContent/createSpec.ts +4 -5
- package/src/schema/nodeViewMutations.test.ts +81 -56
- package/src/schema/nodeViewMutations.ts +30 -27
- package/src/schema/schema.test.ts +110 -0
- package/src/schema/schema.ts +13 -4
- package/src/y/extensions/AttributionExtension.test.ts +8 -9
- package/src/y/versioning/__test__/yhub.test.ts +92 -59
- package/src/y/versioning/yhub.ts +35 -29
- package/types/src/api/getChangedRange.d.ts +14 -0
- package/types/src/blocks/index.d.ts +0 -1
- package/types/src/exporter/Exporter.d.ts +9 -0
- package/types/src/extensions/TableHandles/TableHandles.browser.test.d.ts +1 -0
- package/types/src/extensions/TableHandles/TableHandles.d.ts +3 -0
- package/types/src/schema/nodeViewMutations.d.ts +2 -2
- package/types/src/schema/schema.test.d.ts +1 -0
- package/types/src/y/versioning/yhub.d.ts +3 -2
- package/dist/blocks-CzQLehlc.js.map +0 -1
- package/dist/blocks-CzkqclGj.cjs.map +0 -1
- package/dist/extensions-CSB9ZvuL.cjs +0 -58
- package/dist/extensions-CSB9ZvuL.cjs.map +0 -1
- package/dist/extensions-DGuhtOgB.js.map +0 -1
- package/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.ts +0 -20
- package/types/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.d.ts +0 -6
|
@@ -1,15 +1,151 @@
|
|
|
1
1
|
import { describe, expect, it } from "vite-plus/test";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import {
|
|
3
|
+
BlockNoteSchema,
|
|
4
|
+
BlockSchema,
|
|
5
|
+
COLORS_DEFAULT,
|
|
6
|
+
createBlockSpec,
|
|
7
|
+
createInlineContentSpec,
|
|
8
|
+
createStyleSpec,
|
|
9
|
+
defaultBlockSpecs,
|
|
10
|
+
defaultInlineContentSpecs,
|
|
11
|
+
defaultStyleSpecs,
|
|
12
|
+
InlineContentSchema,
|
|
13
|
+
StyledText,
|
|
14
|
+
StyleSchema,
|
|
15
|
+
} from "../index.js";
|
|
6
16
|
import { Exporter } from "./Exporter.js";
|
|
17
|
+
import { BlockMapping, InlineContentMapping, StyleMapping } from "./mapping.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Type-level tests for the base `Exporter`: the constructor's `mappings`
|
|
21
|
+
* argument must cover every block / inline-content / style type in the schema.
|
|
22
|
+
* This enforcement lives entirely in the generic base class, so the tests live
|
|
23
|
+
* here rather than being duplicated in each concrete exporter.
|
|
24
|
+
*
|
|
25
|
+
* A minimal concrete exporter with `string` result types exercises the typing;
|
|
26
|
+
* the tests pass as long as the file type-checks (the `@ts-expect-error`
|
|
27
|
+
* directives assert that the omissions are compile errors).
|
|
28
|
+
*/
|
|
29
|
+
class TestExporter<
|
|
30
|
+
B extends BlockSchema,
|
|
31
|
+
I extends InlineContentSchema,
|
|
32
|
+
S extends StyleSchema,
|
|
33
|
+
> extends Exporter<B, I, S, string, string, string, string> {
|
|
34
|
+
public constructor(
|
|
35
|
+
schema: BlockNoteSchema<B, I, S>,
|
|
36
|
+
// NoInfer so B/I/S come from the schema only (matching the real exporters);
|
|
37
|
+
// the mappings are then type-checked against it.
|
|
38
|
+
mappings: Exporter<
|
|
39
|
+
NoInfer<B>,
|
|
40
|
+
NoInfer<I>,
|
|
41
|
+
NoInfer<S>,
|
|
42
|
+
string,
|
|
43
|
+
string,
|
|
44
|
+
string,
|
|
45
|
+
string
|
|
46
|
+
>["mappings"],
|
|
47
|
+
) {
|
|
48
|
+
super(schema, mappings, { colors: COLORS_DEFAULT });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public transformStyledText(_styledText: StyledText<S>): string {
|
|
52
|
+
return "";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Mappings covering the default schema, used as the baseline. These tests are
|
|
57
|
+
// purely type-level (nothing is ever invoked), so only the declared types
|
|
58
|
+
// matter - plain casts, no runtime construction.
|
|
59
|
+
const defaultSchema = BlockNoteSchema.create();
|
|
60
|
+
const blockMapping = {} as BlockMapping<
|
|
61
|
+
typeof defaultSchema.blockSchema,
|
|
62
|
+
typeof defaultSchema.inlineContentSchema,
|
|
63
|
+
typeof defaultSchema.styleSchema,
|
|
64
|
+
string,
|
|
65
|
+
string
|
|
66
|
+
>;
|
|
67
|
+
const inlineContentMapping = {} as InlineContentMapping<
|
|
68
|
+
typeof defaultSchema.inlineContentSchema,
|
|
69
|
+
typeof defaultSchema.styleSchema,
|
|
70
|
+
string,
|
|
71
|
+
string
|
|
72
|
+
>;
|
|
73
|
+
const styleMapping = {} as StyleMapping<
|
|
74
|
+
typeof defaultSchema.styleSchema,
|
|
75
|
+
string
|
|
76
|
+
>;
|
|
77
|
+
const defaultMappings = { blockMapping, inlineContentMapping, styleMapping };
|
|
78
|
+
|
|
79
|
+
describe("Exporter mapping typing", () => {
|
|
80
|
+
it("typescript: a schema with an extra block requires a mapping for it", () => {
|
|
81
|
+
const schema = BlockNoteSchema.create({
|
|
82
|
+
blockSpecs: {
|
|
83
|
+
...defaultBlockSpecs,
|
|
84
|
+
extraBlock: createBlockSpec(
|
|
85
|
+
{ content: "none", type: "extraBlock", propSchema: {} },
|
|
86
|
+
{} as any,
|
|
87
|
+
)(),
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// @ts-expect-error - extraBlock has no mapping
|
|
92
|
+
new TestExporter(schema, defaultMappings);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("typescript: a schema with extra inline content requires a mapping for it", () => {
|
|
96
|
+
const schema = BlockNoteSchema.create({
|
|
97
|
+
inlineContentSpecs: {
|
|
98
|
+
...defaultInlineContentSpecs,
|
|
99
|
+
extraInlineContent: createInlineContentSpec(
|
|
100
|
+
{ type: "extraInlineContent", content: "styled", propSchema: {} },
|
|
101
|
+
{} as any,
|
|
102
|
+
),
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// @ts-expect-error - extraInlineContent has no mapping
|
|
107
|
+
new TestExporter(schema, defaultMappings);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("typescript: a schema with an extra style requires a mapping for it", () => {
|
|
111
|
+
const schema = BlockNoteSchema.create({
|
|
112
|
+
styleSpecs: {
|
|
113
|
+
...defaultStyleSpecs,
|
|
114
|
+
extraStyle: createStyleSpec(
|
|
115
|
+
{ type: "extraStyle", propSchema: "boolean" },
|
|
116
|
+
{} as any,
|
|
117
|
+
),
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// @ts-expect-error - extraStyle has no mapping
|
|
122
|
+
new TestExporter(schema, defaultMappings);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("typescript: a mapping covering more block types than the schema is allowed", () => {
|
|
126
|
+
// Only a subset of the default blocks; the default mappings are a superset.
|
|
127
|
+
const schema = BlockNoteSchema.create({
|
|
128
|
+
blockSpecs: { paragraph: defaultBlockSpecs.paragraph },
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// no error — extra mappings are fine
|
|
132
|
+
new TestExporter(schema, defaultMappings);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
7
135
|
|
|
8
136
|
// A minimal concrete exporter with empty mappings, to exercise the
|
|
9
137
|
// missing-mapping errors thrown when a document contains block types the
|
|
10
138
|
// mappings don't cover (e.g. blocks from separate packages, like math,
|
|
11
139
|
// without their exporter mappings spread in).
|
|
12
|
-
class
|
|
140
|
+
class EmptyMappingsExporter extends Exporter<
|
|
141
|
+
any,
|
|
142
|
+
any,
|
|
143
|
+
any,
|
|
144
|
+
void,
|
|
145
|
+
void,
|
|
146
|
+
void,
|
|
147
|
+
void
|
|
148
|
+
> {
|
|
13
149
|
constructor() {
|
|
14
150
|
super(
|
|
15
151
|
BlockNoteSchema.create(),
|
|
@@ -26,7 +162,7 @@ class TestExporter extends Exporter<any, any, any, void, void, void, void> {
|
|
|
26
162
|
describe("Exporter missing mappings", () => {
|
|
27
163
|
it("throws a descriptive error for an unmapped block type", async () => {
|
|
28
164
|
await expect(
|
|
29
|
-
new
|
|
165
|
+
new EmptyMappingsExporter().mapBlock({ type: "math" } as any, 0, 0),
|
|
30
166
|
).rejects.toThrow(
|
|
31
167
|
'missing a block mapping for block type "math". If this block comes from a separate package, spread that package\'s exporter mappings',
|
|
32
168
|
);
|
|
@@ -34,15 +170,17 @@ describe("Exporter missing mappings", () => {
|
|
|
34
170
|
|
|
35
171
|
it("throws a descriptive error for an unmapped inline content type", () => {
|
|
36
172
|
expect(() =>
|
|
37
|
-
new
|
|
173
|
+
new EmptyMappingsExporter().mapInlineContent({
|
|
174
|
+
type: "inlineMath",
|
|
175
|
+
} as any),
|
|
38
176
|
).toThrow(
|
|
39
177
|
'missing an inline content mapping for inline content type "inlineMath"',
|
|
40
178
|
);
|
|
41
179
|
});
|
|
42
180
|
|
|
43
181
|
it("throws a descriptive error for an unmapped style", () => {
|
|
44
|
-
expect(() =>
|
|
45
|
-
|
|
46
|
-
);
|
|
182
|
+
expect(() =>
|
|
183
|
+
new EmptyMappingsExporter().mapStyles({ bold: true } as any),
|
|
184
|
+
).toThrow('missing a style mapping for style "bold"');
|
|
47
185
|
});
|
|
48
186
|
});
|
package/src/exporter/Exporter.ts
CHANGED
|
@@ -81,13 +81,31 @@ export abstract class Exporter<
|
|
|
81
81
|
|
|
82
82
|
public async resolveFile(url: string) {
|
|
83
83
|
if (!this.options?.resolveFileUrl) {
|
|
84
|
-
return (
|
|
84
|
+
return this.fetchBlob(url);
|
|
85
85
|
}
|
|
86
86
|
const ret = await this.options.resolveFileUrl(url);
|
|
87
87
|
if (ret instanceof Blob) {
|
|
88
88
|
return ret;
|
|
89
89
|
}
|
|
90
|
-
return (
|
|
90
|
+
return this.fetchBlob(ret);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Fetch a URL as a Blob, throwing a clear error on a non-OK response. Without
|
|
95
|
+
* this an error page (404 / rate-limit / proxy failure) would be returned as
|
|
96
|
+
* the file's bytes and silently embedded as a corrupt image. An unreachable
|
|
97
|
+
* file is an environment failure, not expected input, so it fails the export
|
|
98
|
+
* loudly (see the error-handling conventions in AGENTS.md) rather than
|
|
99
|
+
* degrading the document silently.
|
|
100
|
+
*/
|
|
101
|
+
private async fetchBlob(url: string): Promise<Blob> {
|
|
102
|
+
const res = await fetch(url);
|
|
103
|
+
if (!res.ok) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Failed to fetch file "${url}": ${res.status} ${res.statusText}`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return res.blob();
|
|
91
109
|
}
|
|
92
110
|
|
|
93
111
|
public mapStyles(styles: Styles<S>) {
|
|
@@ -96,4 +96,25 @@ describe("PreviousBlockType: scoped traversal", () => {
|
|
|
96
96
|
).length;
|
|
97
97
|
expect(trackedBlockCount).toBeLessThan(10);
|
|
98
98
|
});
|
|
99
|
+
|
|
100
|
+
it("detects attribute-only changes (heading level) that emit an AttrStep", () => {
|
|
101
|
+
const editor = createEditorWithBlocks(100, "heading");
|
|
102
|
+
|
|
103
|
+
// Changing only a heading's level (type stays "heading") emits an AttrStep,
|
|
104
|
+
// whose StepMap is empty — so ProseMirror's changedRange() returns null and
|
|
105
|
+
// the change would be missed unless the scoped range also covers AttrSteps.
|
|
106
|
+
const firstBlock = editor.document[0];
|
|
107
|
+
editor.updateBlock(firstBlock, { props: { level: 2 } } as any);
|
|
108
|
+
|
|
109
|
+
const state = getPreviousBlockTypePluginState(editor);
|
|
110
|
+
|
|
111
|
+
expect(state.updatedBlocks.size).toBe(1);
|
|
112
|
+
expect(state.updatedBlocks.has(firstBlock.id)).toBe(true);
|
|
113
|
+
|
|
114
|
+
// Still scoped to the changed range, not all 100 blocks.
|
|
115
|
+
const trackedBlockCount = Object.keys(
|
|
116
|
+
state.currentTransactionOldBlockAttrs,
|
|
117
|
+
).length;
|
|
118
|
+
expect(trackedBlockCount).toBeLessThan(10);
|
|
119
|
+
});
|
|
99
120
|
});
|
|
@@ -2,6 +2,7 @@ import { findChildrenInRange } from "@tiptap/core";
|
|
|
2
2
|
import { Plugin, PluginKey } from "prosemirror-state";
|
|
3
3
|
import { Decoration, DecorationSet } from "prosemirror-view";
|
|
4
4
|
import { getNodeId } from "../../api/getBlockInfoFromPos.js";
|
|
5
|
+
import { getChangedRange } from "../../api/getChangedRange.js";
|
|
5
6
|
import { createExtension } from "../../editor/BlockNoteExtension.js";
|
|
6
7
|
|
|
7
8
|
const PLUGIN_KEY = new PluginKey(`previous-blocks`);
|
|
@@ -72,9 +73,10 @@ export const PreviousBlockTypeExtension = createExtension(() => {
|
|
|
72
73
|
return prev;
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
// Only check nodes
|
|
76
|
-
//
|
|
77
|
-
|
|
76
|
+
// Only check nodes in the changed range, not the whole document.
|
|
77
|
+
// getChangedRange() also covers attribute-only steps (AttrStep), so a
|
|
78
|
+
// block whose `level`/`index` changes with no content edit is caught.
|
|
79
|
+
const newRange = getChangedRange(transaction);
|
|
78
80
|
if (!newRange) {
|
|
79
81
|
return prev;
|
|
80
82
|
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
|
|
2
|
+
|
|
3
|
+
import { BlockNoteSchema } from "../../blocks/BlockNoteSchema.js";
|
|
4
|
+
import { defaultBlockSpecs } from "../../blocks/defaultBlocks.js";
|
|
5
|
+
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
6
|
+
import { createBlockSpec } from "../../schema/index.js";
|
|
7
|
+
import { TableHandlesExtension } from "./TableHandles.js";
|
|
8
|
+
|
|
9
|
+
// Unit tests for the mouse handling in `TableHandlesView`, which decides
|
|
10
|
+
// whether the row/column handles should be attached to the cell under the
|
|
11
|
+
// cursor. It reads the DOM (`getBoundingClientRect`, node views, hit testing),
|
|
12
|
+
// so it runs in the browser suite rather than as a node unit test.
|
|
13
|
+
|
|
14
|
+
// A custom block which renders a real `<table>` of its own, without having any
|
|
15
|
+
// table content in the document. Table cells like these are indistinguishable
|
|
16
|
+
// from a table block's cells when only looking at the DOM.
|
|
17
|
+
const fakeTableBlock = createBlockSpec(
|
|
18
|
+
{
|
|
19
|
+
type: "fakeTable",
|
|
20
|
+
propSchema: {},
|
|
21
|
+
content: "none",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
render: () => {
|
|
25
|
+
const dom = document.createElement("div");
|
|
26
|
+
dom.className = "fake-table";
|
|
27
|
+
dom.innerHTML =
|
|
28
|
+
"<table><tbody><tr><td>Not a real cell</td></tr></tbody></table>";
|
|
29
|
+
|
|
30
|
+
return { dom };
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
// A custom block which embeds a second editor, itself containing a table. The
|
|
36
|
+
// nested editor's cells live inside the outer editor's DOM, so they reach the
|
|
37
|
+
// outer editor's mouse handlers, but its block IDs are unknown to the outer
|
|
38
|
+
// document.
|
|
39
|
+
const nestedEditorBlock = createBlockSpec(
|
|
40
|
+
{
|
|
41
|
+
type: "nestedEditor",
|
|
42
|
+
propSchema: {},
|
|
43
|
+
content: "none",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
render: () => {
|
|
47
|
+
const dom = document.createElement("div");
|
|
48
|
+
dom.className = "nested-editor";
|
|
49
|
+
dom.contentEditable = "false";
|
|
50
|
+
|
|
51
|
+
const nestedEditor = BlockNoteEditor.create({
|
|
52
|
+
initialContent: [
|
|
53
|
+
{
|
|
54
|
+
id: "nested-table",
|
|
55
|
+
type: "table",
|
|
56
|
+
content: {
|
|
57
|
+
type: "tableContent",
|
|
58
|
+
rows: [{ cells: ["Nested cell"] }],
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
nestedEditor.mount(dom, { portalTarget: document.body });
|
|
64
|
+
|
|
65
|
+
return { dom, destroy: () => nestedEditor.unmount() };
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const schema = BlockNoteSchema.create({
|
|
71
|
+
blockSpecs: {
|
|
72
|
+
...defaultBlockSpecs,
|
|
73
|
+
fakeTable: fakeTableBlock(),
|
|
74
|
+
nestedEditor: nestedEditorBlock(),
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe("TableHandlesView mouse handling", () => {
|
|
79
|
+
let editor: BlockNoteEditor<any, any, any>;
|
|
80
|
+
let mountPoint: HTMLElement;
|
|
81
|
+
|
|
82
|
+
beforeEach(() => {
|
|
83
|
+
mountPoint = document.createElement("div");
|
|
84
|
+
document.body.appendChild(mountPoint);
|
|
85
|
+
|
|
86
|
+
editor = BlockNoteEditor.create({
|
|
87
|
+
schema,
|
|
88
|
+
initialContent: [
|
|
89
|
+
{
|
|
90
|
+
id: "table-0",
|
|
91
|
+
type: "table",
|
|
92
|
+
content: {
|
|
93
|
+
type: "tableContent",
|
|
94
|
+
rows: [
|
|
95
|
+
{ cells: ["Cell 1", "Cell 2", "Cell 3"] },
|
|
96
|
+
{ cells: ["Cell 4", "Cell 5", "Cell 6"] },
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{ id: "paragraph-0", type: "paragraph", content: "Paragraph" },
|
|
101
|
+
{ id: "fake-table-0", type: "fakeTable" },
|
|
102
|
+
{ id: "nested-editor-0", type: "nestedEditor" },
|
|
103
|
+
],
|
|
104
|
+
}) as BlockNoteEditor<any, any, any>;
|
|
105
|
+
editor.mount(mountPoint);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
afterEach(() => {
|
|
109
|
+
editor.unmount();
|
|
110
|
+
editor._tiptapEditor.destroy();
|
|
111
|
+
mountPoint.remove();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
function tableHandlesState() {
|
|
115
|
+
return editor.getExtension(TableHandlesExtension)!.store.state;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function queryElement(selector: string) {
|
|
119
|
+
const element = mountPoint.querySelector<HTMLElement>(selector);
|
|
120
|
+
if (!element) {
|
|
121
|
+
throw new Error(`No element matching "${selector}"`);
|
|
122
|
+
}
|
|
123
|
+
return element;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// The cell in the (only) real table block, at the given row and column.
|
|
127
|
+
function tableCell(rowIndex: number, colIndex: number) {
|
|
128
|
+
const cell = queryElement(
|
|
129
|
+
`[data-id="table-0"] tbody tr:nth-child(${rowIndex + 1})`,
|
|
130
|
+
).children[colIndex];
|
|
131
|
+
if (!(cell instanceof HTMLElement)) {
|
|
132
|
+
throw new Error(`No cell at row ${rowIndex}, column ${colIndex}`);
|
|
133
|
+
}
|
|
134
|
+
return cell;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function dispatchMouseEvent(
|
|
138
|
+
type: "mousemove" | "mousedown",
|
|
139
|
+
el: HTMLElement,
|
|
140
|
+
) {
|
|
141
|
+
const rect = el.getBoundingClientRect();
|
|
142
|
+
el.dispatchEvent(
|
|
143
|
+
new MouseEvent(type, {
|
|
144
|
+
bubbles: true,
|
|
145
|
+
clientX: rect.left + rect.width / 2,
|
|
146
|
+
clientY: rect.top + rect.height / 2,
|
|
147
|
+
}),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Moves the mouse over `el` and asserts the handler survived it. An event
|
|
152
|
+
// listener that throws doesn't propagate the error to `dispatchEvent`'s
|
|
153
|
+
// caller - the browser reports it as an `error` event on `window` instead -
|
|
154
|
+
// so the error has to be picked up from there to be visible to the test.
|
|
155
|
+
function moveMouseOver(el: HTMLElement) {
|
|
156
|
+
const errors: unknown[] = [];
|
|
157
|
+
const collectError = (event: ErrorEvent) => {
|
|
158
|
+
errors.push(event.error ?? event.message);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
window.addEventListener("error", collectError);
|
|
162
|
+
try {
|
|
163
|
+
dispatchMouseEvent("mousemove", el);
|
|
164
|
+
} finally {
|
|
165
|
+
window.removeEventListener("error", collectError);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
expect(errors).toEqual([]);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
it("attaches the handles to the hovered cell", () => {
|
|
172
|
+
moveMouseOver(tableCell(1, 1));
|
|
173
|
+
|
|
174
|
+
expect(tableHandlesState()).toMatchObject({
|
|
175
|
+
show: true,
|
|
176
|
+
rowIndex: 1,
|
|
177
|
+
colIndex: 1,
|
|
178
|
+
block: { id: "table-0" },
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("only offers the add/remove buttons on the last row and column", () => {
|
|
183
|
+
moveMouseOver(tableCell(0, 0));
|
|
184
|
+
|
|
185
|
+
expect(tableHandlesState()).toMatchObject({
|
|
186
|
+
showAddOrRemoveRowsButton: false,
|
|
187
|
+
showAddOrRemoveColumnsButton: false,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
moveMouseOver(tableCell(1, 2));
|
|
191
|
+
|
|
192
|
+
expect(tableHandlesState()).toMatchObject({
|
|
193
|
+
showAddOrRemoveRowsButton: true,
|
|
194
|
+
showAddOrRemoveColumnsButton: true,
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("hides the handles when the mouse leaves the table", () => {
|
|
199
|
+
moveMouseOver(tableCell(0, 0));
|
|
200
|
+
expect(tableHandlesState()?.show).toBe(true);
|
|
201
|
+
|
|
202
|
+
moveMouseOver(queryElement('[data-id="paragraph-0"]'));
|
|
203
|
+
|
|
204
|
+
expect(tableHandlesState()?.show).toBe(false);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("hides the handles while text is being selected with the mouse", () => {
|
|
208
|
+
moveMouseOver(tableCell(0, 0));
|
|
209
|
+
expect(tableHandlesState()?.show).toBe(true);
|
|
210
|
+
|
|
211
|
+
dispatchMouseEvent("mousedown", tableCell(0, 0));
|
|
212
|
+
moveMouseOver(tableCell(0, 1));
|
|
213
|
+
|
|
214
|
+
expect(tableHandlesState()?.show).toBe(false);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("keeps the handles on the same cell while frozen", () => {
|
|
218
|
+
moveMouseOver(tableCell(0, 0));
|
|
219
|
+
editor.getExtension(TableHandlesExtension)!.freezeHandles();
|
|
220
|
+
|
|
221
|
+
moveMouseOver(tableCell(1, 2));
|
|
222
|
+
|
|
223
|
+
expect(tableHandlesState()).toMatchObject({
|
|
224
|
+
show: true,
|
|
225
|
+
rowIndex: 0,
|
|
226
|
+
colIndex: 0,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
editor.getExtension(TableHandlesExtension)!.unfreezeHandles();
|
|
230
|
+
moveMouseOver(tableCell(1, 2));
|
|
231
|
+
|
|
232
|
+
expect(tableHandlesState()).toMatchObject({
|
|
233
|
+
show: true,
|
|
234
|
+
rowIndex: 1,
|
|
235
|
+
colIndex: 2,
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("does not show the handles when the editor is not editable", () => {
|
|
240
|
+
editor.isEditable = false;
|
|
241
|
+
|
|
242
|
+
moveMouseOver(tableCell(0, 0));
|
|
243
|
+
|
|
244
|
+
expect(tableHandlesState()?.show).toBeFalsy();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
// Regression test for https://github.com/TypeCellOS/BlockNote/issues/2964.
|
|
248
|
+
// The hovered block used to be treated as a table as soon as the schema had
|
|
249
|
+
// a table block in it, so a custom block rendering a `<table>` ended up on
|
|
250
|
+
// the table path and crashed on its (absent) table content.
|
|
251
|
+
it("ignores cells of a custom block that renders its own table", () => {
|
|
252
|
+
moveMouseOver(queryElement(".fake-table td"));
|
|
253
|
+
|
|
254
|
+
expect(tableHandlesState()?.show).toBeFalsy();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("hides the handles when moving from a table onto a custom block's table", () => {
|
|
258
|
+
moveMouseOver(tableCell(0, 0));
|
|
259
|
+
expect(tableHandlesState()?.show).toBe(true);
|
|
260
|
+
|
|
261
|
+
moveMouseOver(queryElement(".fake-table td"));
|
|
262
|
+
|
|
263
|
+
expect(tableHandlesState()?.show).toBe(false);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// The other half of #2964: the hovered cell belongs to a nested editor, so
|
|
267
|
+
// its block ID cannot be resolved in the outer editor's document. That used
|
|
268
|
+
// to throw `Block with ID <id> not found` on every mouse move.
|
|
269
|
+
it("ignores table cells belonging to a nested editor", () => {
|
|
270
|
+
moveMouseOver(queryElement(".nested-editor td"));
|
|
271
|
+
|
|
272
|
+
expect(tableHandlesState()?.show).toBeFalsy();
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("hides the handles when moving from a table onto a nested editor's table", () => {
|
|
276
|
+
moveMouseOver(tableCell(0, 0));
|
|
277
|
+
expect(tableHandlesState()?.show).toBe(true);
|
|
278
|
+
|
|
279
|
+
moveMouseOver(queryElement(".nested-editor td"));
|
|
280
|
+
|
|
281
|
+
expect(tableHandlesState()?.show).toBe(false);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// The table's position is captured when the handles attach to a cell, but
|
|
285
|
+
// the table shifts whenever content before it changes - a collaborator or an
|
|
286
|
+
// extension editing while a handle menu sits open, say. Acting on the handles
|
|
287
|
+
// afterwards used to resolve the stale position, landing in the wrong node.
|
|
288
|
+
it("acts on the hovered table after content is inserted before it", () => {
|
|
289
|
+
moveMouseOver(tableCell(0, 0));
|
|
290
|
+
|
|
291
|
+
editor.insertBlocks(
|
|
292
|
+
[{ type: "paragraph", content: "Inserted" }],
|
|
293
|
+
"table-0",
|
|
294
|
+
"before",
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
editor
|
|
298
|
+
.getExtension(TableHandlesExtension)!
|
|
299
|
+
.addRowOrColumn(0, { orientation: "row", side: "below" });
|
|
300
|
+
|
|
301
|
+
expect(
|
|
302
|
+
mountPoint.querySelectorAll('[data-id="table-0"] tbody tr'),
|
|
303
|
+
).toHaveLength(3);
|
|
304
|
+
});
|
|
305
|
+
});
|