@pie-lib/editable-html-tip-tap 2.1.2-next.0 → 2.1.2-next.104
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/CHANGELOG.json +32 -0
- package/CHANGELOG.md +2532 -0
- package/LICENSE.md +5 -0
- package/lib/components/CharacterPicker.js +226 -0
- package/lib/components/CharacterPicker.js.map +1 -0
- package/lib/components/EditableHtml.js +388 -0
- package/lib/components/EditableHtml.js.map +1 -0
- package/lib/components/MenuBar.js +702 -0
- package/lib/components/MenuBar.js.map +1 -0
- package/lib/components/TiptapContainer.js +242 -0
- package/lib/components/TiptapContainer.js.map +1 -0
- package/lib/components/characters/characterUtils.js +378 -0
- package/lib/components/characters/characterUtils.js.map +1 -0
- package/lib/components/characters/custom-popper.js +44 -0
- package/lib/components/characters/custom-popper.js.map +1 -0
- package/lib/components/common/done-button.js +34 -0
- package/lib/components/common/done-button.js.map +1 -0
- package/lib/components/common/toolbar-buttons.js +149 -0
- package/lib/components/common/toolbar-buttons.js.map +1 -0
- package/lib/components/icons/CssIcon.js +25 -0
- package/lib/components/icons/CssIcon.js.map +1 -0
- package/lib/components/icons/RespArea.js +72 -0
- package/lib/components/icons/RespArea.js.map +1 -0
- package/lib/components/icons/TableIcons.js +53 -0
- package/lib/components/icons/TableIcons.js.map +1 -0
- package/lib/components/icons/TextAlign.js +157 -0
- package/lib/components/icons/TextAlign.js.map +1 -0
- package/lib/components/image/AltDialog.js +98 -0
- package/lib/components/image/AltDialog.js.map +1 -0
- package/lib/components/image/ImageToolbar.js +137 -0
- package/lib/components/image/ImageToolbar.js.map +1 -0
- package/lib/components/image/InsertImageHandler.js +135 -0
- package/lib/components/image/InsertImageHandler.js.map +1 -0
- package/lib/components/media/MediaDialog.js +594 -0
- package/lib/components/media/MediaDialog.js.map +1 -0
- package/lib/components/media/MediaToolbar.js +74 -0
- package/lib/components/media/MediaToolbar.js.map +1 -0
- package/lib/components/media/MediaWrapper.js +67 -0
- package/lib/components/media/MediaWrapper.js.map +1 -0
- package/lib/components/respArea/DragInTheBlank/DragInTheBlank.js +84 -0
- package/lib/components/respArea/DragInTheBlank/DragInTheBlank.js.map +1 -0
- package/lib/components/respArea/DragInTheBlank/choice.js +253 -0
- package/lib/components/respArea/DragInTheBlank/choice.js.map +1 -0
- package/lib/components/respArea/ExplicitConstructedResponse.js +137 -0
- package/lib/components/respArea/ExplicitConstructedResponse.js.map +1 -0
- package/lib/components/respArea/InlineDropdown.js +223 -0
- package/lib/components/respArea/InlineDropdown.js.map +1 -0
- package/lib/components/respArea/MathTemplated.js +130 -0
- package/lib/components/respArea/MathTemplated.js.map +1 -0
- package/lib/components/respArea/ToolbarIcon.js +81 -0
- package/lib/components/respArea/ToolbarIcon.js.map +1 -0
- package/lib/components/respArea/inlineDropdownUtils.js +67 -0
- package/lib/components/respArea/inlineDropdownUtils.js.map +1 -0
- package/lib/constants.js +11 -0
- package/lib/constants.js.map +1 -0
- package/lib/extensions/css.js +227 -0
- package/lib/extensions/css.js.map +1 -0
- package/lib/extensions/custom-toolbar-wrapper.js +92 -0
- package/lib/extensions/custom-toolbar-wrapper.js.map +1 -0
- package/lib/extensions/div-node.js +83 -0
- package/lib/extensions/div-node.js.map +1 -0
- package/lib/extensions/ensure-empty-root-div.js +48 -0
- package/lib/extensions/ensure-empty-root-div.js.map +1 -0
- package/lib/extensions/ensure-list-item-content-is-div.js +64 -0
- package/lib/extensions/ensure-list-item-content-is-div.js.map +1 -0
- package/lib/extensions/extended-list-item.js +15 -0
- package/lib/extensions/extended-list-item.js.map +1 -0
- package/lib/extensions/extended-table-cell.js +22 -0
- package/lib/extensions/extended-table-cell.js.map +1 -0
- package/lib/extensions/extended-table.js +75 -0
- package/lib/extensions/extended-table.js.map +1 -0
- package/lib/extensions/heading-paragraph.js +61 -0
- package/lib/extensions/heading-paragraph.js.map +1 -0
- package/lib/extensions/image-component.js +351 -0
- package/lib/extensions/image-component.js.map +1 -0
- package/lib/extensions/image.js +134 -0
- package/lib/extensions/image.js.map +1 -0
- package/lib/extensions/index.js +46 -0
- package/lib/extensions/index.js.map +1 -0
- package/lib/extensions/math.js +451 -0
- package/lib/extensions/math.js.map +1 -0
- package/lib/extensions/media.js +243 -0
- package/lib/extensions/media.js.map +1 -0
- package/lib/extensions/responseArea.js +452 -0
- package/lib/extensions/responseArea.js.map +1 -0
- package/lib/index.js +37 -0
- package/lib/index.js.map +1 -0
- package/lib/styles/editorContainerStyles.js +137 -0
- package/lib/styles/editorContainerStyles.js.map +1 -0
- package/lib/theme.js +8 -0
- package/lib/theme.js.map +1 -0
- package/lib/utils/helper.js +73 -0
- package/lib/utils/helper.js.map +1 -0
- package/lib/utils/size.js +26 -0
- package/lib/utils/size.js.map +1 -0
- package/lib/utils/toolbar.js +19 -0
- package/lib/utils/toolbar.js.map +1 -0
- package/package.json +22 -35
- package/src/__tests__/EditableHtml.test.jsx +588 -0
- package/src/__tests__/constants.test.js +19 -0
- package/src/__tests__/div-to-paragraph-conversion.test.jsx +125 -0
- package/src/__tests__/extensions.test.js +208 -0
- package/src/__tests__/index.test.jsx +154 -0
- package/src/__tests__/size-utils.test.js +64 -0
- package/src/__tests__/theme.test.js +17 -0
- package/src/components/CharacterPicker.jsx +237 -0
- package/src/components/EditableHtml.jsx +458 -0
- package/src/components/MenuBar.jsx +561 -0
- package/src/components/TiptapContainer.jsx +235 -0
- package/src/components/__tests__/AltDialog.test.jsx +147 -0
- package/src/components/__tests__/CharacterPicker.test.jsx +270 -0
- package/src/components/__tests__/CssIcon.test.jsx +46 -0
- package/src/components/__tests__/DragInTheBlank.test.jsx +255 -0
- package/src/components/__tests__/ExplicitConstructedResponse.test.jsx +209 -0
- package/src/components/__tests__/ImageToolbar.test.jsx +128 -0
- package/src/components/__tests__/InlineDropdown.test.jsx +557 -0
- package/src/components/__tests__/InsertImageHandler.test.js +161 -0
- package/src/components/__tests__/MediaDialog.test.jsx +293 -0
- package/src/components/__tests__/MediaToolbar.test.jsx +74 -0
- package/src/components/__tests__/MediaWrapper.test.jsx +81 -0
- package/src/components/__tests__/MenuBar.test.jsx +250 -0
- package/src/components/__tests__/RespArea.test.jsx +122 -0
- package/src/components/__tests__/TableIcons.test.jsx +149 -0
- package/src/components/__tests__/TextAlign.test.jsx +167 -0
- package/src/components/__tests__/TiptapContainer.test.jsx +138 -0
- package/src/components/__tests__/characterUtils.test.js +166 -0
- package/src/components/__tests__/choice.test.jsx +171 -0
- package/src/components/__tests__/custom-popper.test.jsx +82 -0
- package/src/components/__tests__/done-button.test.jsx +54 -0
- package/src/components/__tests__/toolbar-buttons.test.jsx +234 -0
- package/src/components/characters/characterUtils.js +447 -0
- package/src/components/characters/custom-popper.js +38 -0
- package/src/components/common/done-button.jsx +27 -0
- package/src/components/common/toolbar-buttons.jsx +127 -0
- package/src/components/icons/CssIcon.jsx +15 -0
- package/src/components/icons/RespArea.jsx +71 -0
- package/src/components/icons/TableIcons.jsx +52 -0
- package/src/components/icons/TextAlign.jsx +114 -0
- package/src/components/image/AltDialog.jsx +82 -0
- package/src/components/image/ImageToolbar.jsx +99 -0
- package/src/components/image/InsertImageHandler.js +107 -0
- package/src/components/media/MediaDialog.jsx +596 -0
- package/src/components/media/MediaToolbar.jsx +49 -0
- package/src/components/media/MediaWrapper.jsx +39 -0
- package/src/components/respArea/DragInTheBlank/DragInTheBlank.jsx +76 -0
- package/src/components/respArea/DragInTheBlank/__tests__/choice.test.jsx +72 -0
- package/src/components/respArea/DragInTheBlank/choice.jsx +259 -0
- package/src/components/respArea/ExplicitConstructedResponse.jsx +136 -0
- package/src/components/respArea/InlineDropdown.jsx +240 -0
- package/src/components/respArea/MathTemplated.jsx +124 -0
- package/src/components/respArea/ToolbarIcon.jsx +66 -0
- package/src/components/respArea/__tests__/MathTemplated.test.jsx +210 -0
- package/src/components/respArea/inlineDropdownUtils.js +79 -0
- package/src/constants.js +5 -0
- package/src/extensions/__tests__/css.test.js +196 -0
- package/src/extensions/__tests__/custom-toolbar-wrapper.test.jsx +180 -0
- package/src/extensions/__tests__/divNode.test.js +87 -0
- package/src/extensions/__tests__/ensure-empty-root-div.test.js +57 -0
- package/src/extensions/__tests__/ensure-list-item-content-is-div.test.js +44 -0
- package/src/extensions/__tests__/extended-list-item.test.js +13 -0
- package/src/extensions/__tests__/extended-table-cell.test.js +22 -0
- package/src/extensions/__tests__/extended-table.test.js +183 -0
- package/src/extensions/__tests__/image-component.test.jsx +346 -0
- package/src/extensions/__tests__/image.test.js +237 -0
- package/src/extensions/__tests__/math.test.js +898 -0
- package/src/extensions/__tests__/media-node-view.test.jsx +298 -0
- package/src/extensions/__tests__/media.test.js +271 -0
- package/src/extensions/__tests__/responseArea.test.js +655 -0
- package/src/extensions/css.js +233 -0
- package/src/extensions/custom-toolbar-wrapper.jsx +78 -0
- package/src/extensions/div-node.js +86 -0
- package/src/extensions/ensure-empty-root-div.js +47 -0
- package/src/extensions/ensure-list-item-content-is-div.js +62 -0
- package/src/extensions/extended-list-item.js +10 -0
- package/src/extensions/extended-table-cell.js +19 -0
- package/src/extensions/extended-table.js +60 -0
- package/src/extensions/heading-paragraph.js +53 -0
- package/src/extensions/image-component.jsx +340 -0
- package/src/extensions/image.js +109 -0
- package/src/extensions/index.js +81 -0
- package/src/extensions/math.js +446 -0
- package/src/extensions/media.js +188 -0
- package/src/extensions/responseArea.js +407 -0
- package/src/index.jsx +5 -0
- package/src/styles/editorContainerStyles.js +145 -0
- package/src/theme.js +1 -0
- package/src/utils/__tests__/helper.test.js +126 -0
- package/src/utils/__tests__/toolbar.test.js +43 -0
- package/src/utils/helper.js +69 -0
- package/src/utils/size.js +32 -0
- package/src/utils/toolbar.js +15 -0
- package/dist/components/CharacterPicker.d.ts +0 -31
- package/dist/components/CharacterPicker.js +0 -131
- package/dist/components/EditableHtml.d.ts +0 -11
- package/dist/components/EditableHtml.js +0 -291
- package/dist/components/MenuBar.d.ts +0 -11
- package/dist/components/MenuBar.js +0 -462
- package/dist/components/TiptapContainer.d.ts +0 -11
- package/dist/components/TiptapContainer.js +0 -154
- package/dist/components/characters/characterUtils.d.ts +0 -35
- package/dist/components/characters/characterUtils.js +0 -465
- package/dist/components/characters/custom-popper.d.ts +0 -14
- package/dist/components/characters/custom-popper.js +0 -32
- package/dist/components/common/done-button.d.ts +0 -30
- package/dist/components/common/done-button.js +0 -26
- package/dist/components/common/toolbar-buttons.d.ts +0 -38
- package/dist/components/common/toolbar-buttons.js +0 -91
- package/dist/components/icons/CssIcon.d.ts +0 -11
- package/dist/components/icons/CssIcon.js +0 -14
- package/dist/components/icons/RespArea.d.ts +0 -26
- package/dist/components/icons/RespArea.js +0 -42
- package/dist/components/icons/TableIcons.d.ts +0 -14
- package/dist/components/icons/TableIcons.js +0 -32
- package/dist/components/icons/TextAlign.d.ts +0 -18
- package/dist/components/icons/TextAlign.js +0 -134
- package/dist/components/image/AltDialog.d.ts +0 -22
- package/dist/components/image/AltDialog.js +0 -61
- package/dist/components/image/ImageToolbar.d.ts +0 -24
- package/dist/components/image/ImageToolbar.js +0 -80
- package/dist/components/image/InsertImageHandler.d.ts +0 -32
- package/dist/components/image/InsertImageHandler.js +0 -53
- package/dist/components/media/MediaDialog.d.ts +0 -43
- package/dist/components/media/MediaDialog.js +0 -389
- package/dist/components/media/MediaToolbar.d.ts +0 -19
- package/dist/components/media/MediaToolbar.js +0 -41
- package/dist/components/media/MediaWrapper.d.ts +0 -19
- package/dist/components/respArea/DragInTheBlank/DragInTheBlank.d.ts +0 -23
- package/dist/components/respArea/DragInTheBlank/DragInTheBlank.js +0 -58
- package/dist/components/respArea/DragInTheBlank/choice.d.ts +0 -56
- package/dist/components/respArea/DragInTheBlank/choice.js +0 -156
- package/dist/components/respArea/ExplicitConstructedResponse.d.ts +0 -20
- package/dist/components/respArea/ExplicitConstructedResponse.js +0 -83
- package/dist/components/respArea/InlineDropdown.d.ts +0 -18
- package/dist/components/respArea/InlineDropdown.js +0 -119
- package/dist/components/respArea/MathTemplated.d.ts +0 -19
- package/dist/components/respArea/MathTemplated.js +0 -97
- package/dist/components/respArea/ToolbarIcon.d.ts +0 -14
- package/dist/components/respArea/ToolbarIcon.js +0 -17
- package/dist/components/respArea/inlineDropdownUtils.d.ts +0 -15
- package/dist/components/respArea/inlineDropdownUtils.js +0 -15
- package/dist/constants.d.ts +0 -13
- package/dist/constants.js +0 -4
- package/dist/extensions/css.d.ts +0 -11
- package/dist/extensions/css.js +0 -115
- package/dist/extensions/custom-toolbar-wrapper.d.ts +0 -11
- package/dist/extensions/custom-toolbar-wrapper.js +0 -61
- package/dist/extensions/div-node.d.ts +0 -10
- package/dist/extensions/div-node.js +0 -42
- package/dist/extensions/ensure-empty-root-div.d.ts +0 -14
- package/dist/extensions/ensure-empty-root-div.js +0 -24
- package/dist/extensions/ensure-list-item-content-is-div.d.ts +0 -15
- package/dist/extensions/ensure-list-item-content-is-div.js +0 -31
- package/dist/extensions/extended-list-item.d.ts +0 -13
- package/dist/extensions/extended-list-item.js +0 -5
- package/dist/extensions/extended-table-cell.d.ts +0 -10
- package/dist/extensions/extended-table-cell.js +0 -6
- package/dist/extensions/extended-table.d.ts +0 -17
- package/dist/extensions/extended-table.js +0 -34
- package/dist/extensions/heading-paragraph.d.ts +0 -17
- package/dist/extensions/heading-paragraph.js +0 -30
- package/dist/extensions/image-component.d.ts +0 -22
- package/dist/extensions/image-component.js +0 -220
- package/dist/extensions/image.d.ts +0 -10
- package/dist/extensions/image.js +0 -68
- package/dist/extensions/index.d.ts +0 -16
- package/dist/extensions/index.js +0 -65
- package/dist/extensions/math.d.ts +0 -15
- package/dist/extensions/math.js +0 -158
- package/dist/extensions/media.d.ts +0 -19
- package/dist/extensions/media.js +0 -149
- package/dist/extensions/responseArea.d.ts +0 -27
- package/dist/extensions/responseArea.js +0 -259
- package/dist/index.d.ts +0 -13
- package/dist/index.js +0 -7
- package/dist/styles/editorContainerStyles.d.ts +0 -134
- package/dist/theme.d.ts +0 -9
- package/dist/utils/helper.d.ts +0 -9
- package/dist/utils/helper.js +0 -27
- package/dist/utils/size.d.ts +0 -9
- package/dist/utils/size.js +0 -14
|
@@ -0,0 +1,898 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, waitFor, fireEvent, act } from '@testing-library/react';
|
|
3
|
+
import { EnsureTextAfterMathPlugin, MathNode, MathNodeView, ZeroWidthSpaceHandlingPlugin } from '../math';
|
|
4
|
+
import * as toolbarUtils from '../../utils/toolbar';
|
|
5
|
+
|
|
6
|
+
jest.mock('@tiptap/react', () => ({
|
|
7
|
+
NodeViewWrapper: ({ children, ...props }) => (
|
|
8
|
+
<div data-testid="node-view-wrapper" {...props}>
|
|
9
|
+
{children}
|
|
10
|
+
</div>
|
|
11
|
+
),
|
|
12
|
+
ReactNodeViewRenderer: jest.fn((component) => component),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
const mockCreatePortal = jest.fn((node) => node);
|
|
16
|
+
jest.mock('react-dom', () => ({
|
|
17
|
+
...jest.requireActual('react-dom'),
|
|
18
|
+
createPortal: (...args) => mockCreatePortal(...args),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
jest.mock('@pie-lib/math-toolbar', () => {
|
|
22
|
+
const React = require('react');
|
|
23
|
+
return {
|
|
24
|
+
MathPreview: ({ latex }) => <div data-testid="math-preview">{latex}</div>,
|
|
25
|
+
MathToolbar: ({ latex, onChange, onDone }) => {
|
|
26
|
+
const [localLatex, setLocalLatex] = React.useState(latex);
|
|
27
|
+
return (
|
|
28
|
+
<div data-testid="math-toolbar">
|
|
29
|
+
<input
|
|
30
|
+
data-testid="math-input"
|
|
31
|
+
value={localLatex}
|
|
32
|
+
onChange={(e) => {
|
|
33
|
+
setLocalLatex(e.target.value);
|
|
34
|
+
onChange(e.target.value);
|
|
35
|
+
}}
|
|
36
|
+
/>
|
|
37
|
+
<button data-testid="done-button" onClick={() => onDone(localLatex)}>
|
|
38
|
+
Done
|
|
39
|
+
</button>
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
jest.mock('@pie-lib/math-rendering', () => ({
|
|
47
|
+
wrapMath: (latex, wrapper) => latex,
|
|
48
|
+
}));
|
|
49
|
+
|
|
50
|
+
jest.mock('@tiptap/core', () => ({
|
|
51
|
+
Node: {
|
|
52
|
+
create: jest.fn((config) => config),
|
|
53
|
+
},
|
|
54
|
+
}));
|
|
55
|
+
|
|
56
|
+
jest.mock('prosemirror-state', () => ({
|
|
57
|
+
Plugin: jest.fn(function (config) {
|
|
58
|
+
return config;
|
|
59
|
+
}),
|
|
60
|
+
PluginKey: jest.fn(function (key) {
|
|
61
|
+
this.key = key;
|
|
62
|
+
}),
|
|
63
|
+
TextSelection: {
|
|
64
|
+
create: jest.fn((doc, pos) => ({ type: 'text', pos })),
|
|
65
|
+
},
|
|
66
|
+
NodeSelection: {
|
|
67
|
+
create: jest.fn((doc, pos) => ({ type: 'node', pos })),
|
|
68
|
+
},
|
|
69
|
+
}));
|
|
70
|
+
|
|
71
|
+
describe('MathNode', () => {
|
|
72
|
+
describe('configuration', () => {
|
|
73
|
+
it('has correct name', () => {
|
|
74
|
+
expect(MathNode.name).toBe('math');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('is inline', () => {
|
|
78
|
+
expect(MathNode.inline).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('is in inline group', () => {
|
|
82
|
+
expect(MathNode.group).toBe('inline');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('is atomic', () => {
|
|
86
|
+
expect(MathNode.atom).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('addAttributes', () => {
|
|
91
|
+
it('returns required attributes', () => {
|
|
92
|
+
const attributes = MathNode.addAttributes();
|
|
93
|
+
|
|
94
|
+
expect(attributes).toHaveProperty('latex');
|
|
95
|
+
expect(attributes).toHaveProperty('wrapper');
|
|
96
|
+
expect(attributes).toHaveProperty('html');
|
|
97
|
+
|
|
98
|
+
expect(attributes.latex).toEqual({ default: '' });
|
|
99
|
+
expect(attributes.wrapper).toEqual({ default: null });
|
|
100
|
+
expect(attributes.html).toEqual({ default: null });
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('parseHTML', () => {
|
|
105
|
+
it('returns parsing rules for latex', () => {
|
|
106
|
+
const rules = MathNode.parseHTML();
|
|
107
|
+
|
|
108
|
+
expect(Array.isArray(rules)).toBe(true);
|
|
109
|
+
expect(rules).toHaveLength(2);
|
|
110
|
+
expect(rules[0]).toHaveProperty('tag', 'span[data-latex]');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('returns parsing rules for mathml', () => {
|
|
114
|
+
const rules = MathNode.parseHTML();
|
|
115
|
+
expect(rules[1]).toHaveProperty('tag', 'span[data-type="mathml"]');
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('renderHTML', () => {
|
|
120
|
+
it('renders mathml when html attribute is present', () => {
|
|
121
|
+
const result = MathNode.renderHTML({
|
|
122
|
+
HTMLAttributes: {
|
|
123
|
+
html: '<math><mi>x</mi></math>',
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
expect(result[0]).toBe('span');
|
|
128
|
+
expect(result[1]).toHaveProperty('data-type', 'mathml');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('renders latex when html attribute is not present', () => {
|
|
132
|
+
const result = MathNode.renderHTML({
|
|
133
|
+
HTMLAttributes: {
|
|
134
|
+
latex: 'x^2',
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
expect(result[0]).toBe('span');
|
|
139
|
+
expect(result[1]).toHaveProperty('data-latex', '');
|
|
140
|
+
expect(result[1]).toHaveProperty('data-raw', 'x^2');
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe('addCommands', () => {
|
|
145
|
+
it('returns insertMath command', () => {
|
|
146
|
+
const commands = MathNode.addCommands();
|
|
147
|
+
|
|
148
|
+
expect(commands).toHaveProperty('insertMath');
|
|
149
|
+
expect(typeof commands.insertMath).toBe('function');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('insertMath opens the toolbar after inserting a math node', () => {
|
|
153
|
+
const setToolbarOpenedSpy = jest.spyOn(toolbarUtils, 'setToolbarOpened');
|
|
154
|
+
const mathNode = { type: { name: 'math' }, nodeSize: 1 };
|
|
155
|
+
const tr = {
|
|
156
|
+
insert: jest.fn().mockReturnThis(),
|
|
157
|
+
setSelection: jest.fn().mockReturnThis(),
|
|
158
|
+
doc: {},
|
|
159
|
+
};
|
|
160
|
+
const editor = {
|
|
161
|
+
view: {
|
|
162
|
+
state: {
|
|
163
|
+
schema: { nodes: { math: { create: jest.fn(() => mathNode) } } },
|
|
164
|
+
selection: { $from: { pos: 1 } },
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
const dispatch = jest.fn();
|
|
169
|
+
const insertMath = MathNode.addCommands().insertMath('x^2');
|
|
170
|
+
|
|
171
|
+
insertMath({ tr, editor, dispatch });
|
|
172
|
+
|
|
173
|
+
expect(tr.insert).toHaveBeenCalledWith(1, mathNode);
|
|
174
|
+
expect(tr.setSelection).toHaveBeenCalled();
|
|
175
|
+
expect(dispatch).toHaveBeenCalledWith(tr);
|
|
176
|
+
expect(setToolbarOpenedSpy).toHaveBeenCalledWith(editor, true);
|
|
177
|
+
|
|
178
|
+
setToolbarOpenedSpy.mockRestore();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe('addNodeView', () => {
|
|
183
|
+
it('returns ReactNodeViewRenderer result', () => {
|
|
184
|
+
const result = MathNode.addNodeView();
|
|
185
|
+
|
|
186
|
+
expect(result).toBeDefined();
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('addProseMirrorPlugins', () => {
|
|
191
|
+
it('registers ensure-text-after-math and zero-width-space plugins', () => {
|
|
192
|
+
const plugins = MathNode.addProseMirrorPlugins();
|
|
193
|
+
|
|
194
|
+
expect(plugins).toHaveLength(2);
|
|
195
|
+
expect(plugins[0].appendTransaction).toBeDefined();
|
|
196
|
+
expect(plugins[1].props.handleKeyDown).toBeDefined();
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe('EnsureTextAfterMathPlugin', () => {
|
|
202
|
+
it('inserts a zero-width space after a math node when no text follows', () => {
|
|
203
|
+
const plugin = EnsureTextAfterMathPlugin('math');
|
|
204
|
+
const textNode = { type: { name: 'text' } };
|
|
205
|
+
const mathNode = { type: { name: 'math' }, nodeSize: 3 };
|
|
206
|
+
const tr = { insert: jest.fn() };
|
|
207
|
+
|
|
208
|
+
const newState = {
|
|
209
|
+
schema: { text: jest.fn((value) => ({ type: textNode.type, text: value })) },
|
|
210
|
+
tr,
|
|
211
|
+
doc: {
|
|
212
|
+
descendants: (cb) => cb(mathNode, 5),
|
|
213
|
+
nodeAt: jest.fn(() => null),
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const result = plugin.appendTransaction([{ docChanged: true }], {}, newState);
|
|
218
|
+
|
|
219
|
+
expect(tr.insert).toHaveBeenCalledWith(8, expect.anything());
|
|
220
|
+
expect(result).toBe(tr);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('does not insert when text already follows the math node', () => {
|
|
224
|
+
const plugin = EnsureTextAfterMathPlugin('math');
|
|
225
|
+
const tr = { insert: jest.fn() };
|
|
226
|
+
const mathNode = { type: { name: 'math' }, nodeSize: 3 };
|
|
227
|
+
|
|
228
|
+
const newState = {
|
|
229
|
+
schema: { text: jest.fn() },
|
|
230
|
+
tr,
|
|
231
|
+
doc: {
|
|
232
|
+
descendants: (cb) => cb(mathNode, 5),
|
|
233
|
+
nodeAt: jest.fn(() => ({ type: { name: 'text' } })),
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const result = plugin.appendTransaction([{ docChanged: true }], {}, newState);
|
|
238
|
+
|
|
239
|
+
expect(tr.insert).not.toHaveBeenCalled();
|
|
240
|
+
expect(result).toBeNull();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('returns null when the document did not change', () => {
|
|
244
|
+
const plugin = EnsureTextAfterMathPlugin('math');
|
|
245
|
+
|
|
246
|
+
const result = plugin.appendTransaction([{ docChanged: false }], {}, {});
|
|
247
|
+
expect(result).toBeNull();
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
describe('ZeroWidthSpaceHandlingPlugin', () => {
|
|
252
|
+
const createDocWithMathAndZwsp = (resolveOverrides = {}) => ({
|
|
253
|
+
resolve: jest.fn((pos) => ({
|
|
254
|
+
nodeAfter: null,
|
|
255
|
+
nodeBefore: null,
|
|
256
|
+
pos,
|
|
257
|
+
...resolveOverrides[pos],
|
|
258
|
+
})),
|
|
259
|
+
nodeAt: jest.fn((pos) => {
|
|
260
|
+
if (pos === 1) {
|
|
261
|
+
return { type: { name: 'text' }, textContent: '\u200b' };
|
|
262
|
+
}
|
|
263
|
+
if (pos === 0) {
|
|
264
|
+
return { type: { name: 'math' }, nodeSize: 1 };
|
|
265
|
+
}
|
|
266
|
+
return null;
|
|
267
|
+
}),
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const createDocWithRegularTextBeforeCursor = () => ({
|
|
271
|
+
resolve: jest.fn((pos) => ({ nodeAfter: null, nodeBefore: null, pos })),
|
|
272
|
+
nodeAt: jest.fn((pos) => {
|
|
273
|
+
if (pos === 1) {
|
|
274
|
+
return { type: { name: 'text' }, textContent: 'a' };
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
}),
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const createView = ({ state: stateOverrides = {} } = {}) => {
|
|
281
|
+
const dispatch = jest.fn();
|
|
282
|
+
const tr = {
|
|
283
|
+
delete: jest.fn().mockReturnThis(),
|
|
284
|
+
setSelection: jest.fn().mockReturnThis(),
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
return {
|
|
288
|
+
state: {
|
|
289
|
+
selection: { from: 2, empty: true },
|
|
290
|
+
doc: createDocWithMathAndZwsp(),
|
|
291
|
+
tr,
|
|
292
|
+
...stateOverrides,
|
|
293
|
+
},
|
|
294
|
+
dispatch,
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
describe('Backspace', () => {
|
|
299
|
+
it('deletes the inline node and zero-width space before the cursor', () => {
|
|
300
|
+
const view = createView();
|
|
301
|
+
const handled = ZeroWidthSpaceHandlingPlugin.props.handleKeyDown(view, { key: 'Backspace' });
|
|
302
|
+
|
|
303
|
+
expect(handled).toBe(true);
|
|
304
|
+
expect(view.state.tr.delete).toHaveBeenCalledWith(0, 2);
|
|
305
|
+
expect(view.dispatch).toHaveBeenCalledWith(view.state.tr);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('returns false when regular text precedes the cursor', () => {
|
|
309
|
+
const view = createView({
|
|
310
|
+
state: {
|
|
311
|
+
doc: createDocWithRegularTextBeforeCursor(),
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
const handled = ZeroWidthSpaceHandlingPlugin.props.handleKeyDown(view, { key: 'Backspace' });
|
|
316
|
+
|
|
317
|
+
expect(handled).toBe(false);
|
|
318
|
+
expect(view.state.tr.delete).not.toHaveBeenCalled();
|
|
319
|
+
expect(view.dispatch).not.toHaveBeenCalled();
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
describe('ArrowLeft', () => {
|
|
324
|
+
it('selects the inline node before a zero-width space', () => {
|
|
325
|
+
const mathNode = { nodeSize: 3 };
|
|
326
|
+
const view = createView({
|
|
327
|
+
state: {
|
|
328
|
+
doc: createDocWithMathAndZwsp({
|
|
329
|
+
0: { nodeAfter: mathNode, nodeBefore: null, pos: 0 },
|
|
330
|
+
}),
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
const { NodeSelection } = require('prosemirror-state');
|
|
334
|
+
|
|
335
|
+
const handled = ZeroWidthSpaceHandlingPlugin.props.handleKeyDown(view, { key: 'ArrowLeft' });
|
|
336
|
+
|
|
337
|
+
expect(handled).toBe(true);
|
|
338
|
+
expect(view.state.doc.resolve).toHaveBeenCalledWith(0);
|
|
339
|
+
expect(NodeSelection.create).toHaveBeenCalledWith(view.state.doc, 0);
|
|
340
|
+
expect(view.dispatch).toHaveBeenCalled();
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it('moves the text cursor before the zero-width space when no inline node precedes it', () => {
|
|
344
|
+
const view = createView();
|
|
345
|
+
const { TextSelection } = require('prosemirror-state');
|
|
346
|
+
|
|
347
|
+
const handled = ZeroWidthSpaceHandlingPlugin.props.handleKeyDown(view, { key: 'ArrowLeft' });
|
|
348
|
+
|
|
349
|
+
expect(handled).toBe(true);
|
|
350
|
+
expect(view.state.doc.resolve).toHaveBeenCalledWith(0);
|
|
351
|
+
expect(TextSelection.create).toHaveBeenCalledWith(view.state.doc, 0);
|
|
352
|
+
expect(view.dispatch).toHaveBeenCalled();
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it('returns false when regular text precedes the cursor', () => {
|
|
356
|
+
const view = createView({
|
|
357
|
+
state: {
|
|
358
|
+
doc: createDocWithRegularTextBeforeCursor(),
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
const handled = ZeroWidthSpaceHandlingPlugin.props.handleKeyDown(view, { key: 'ArrowLeft' });
|
|
363
|
+
|
|
364
|
+
expect(handled).toBe(false);
|
|
365
|
+
expect(view.state.tr.setSelection).not.toHaveBeenCalled();
|
|
366
|
+
expect(view.dispatch).not.toHaveBeenCalled();
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it('returns false for unrelated keys', () => {
|
|
371
|
+
const view = createView();
|
|
372
|
+
|
|
373
|
+
const handled = ZeroWidthSpaceHandlingPlugin.props.handleKeyDown(view, { key: 'Enter' });
|
|
374
|
+
expect(handled).toBe(false);
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
describe('MathNodeView', () => {
|
|
379
|
+
const createEditorElement = (rect = { top: 0, left: 0, width: 600, height: 400 }) => {
|
|
380
|
+
const element = document.createElement('div');
|
|
381
|
+
Object.defineProperty(element, 'getBoundingClientRect', {
|
|
382
|
+
value: jest.fn(() => rect),
|
|
383
|
+
configurable: true,
|
|
384
|
+
});
|
|
385
|
+
return element;
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const createMockEditor = () => ({
|
|
389
|
+
state: {
|
|
390
|
+
selection: {
|
|
391
|
+
from: 0,
|
|
392
|
+
to: 1,
|
|
393
|
+
node: { type: { name: 'math' } },
|
|
394
|
+
},
|
|
395
|
+
tr: {
|
|
396
|
+
setSelection: jest.fn().mockReturnThis(),
|
|
397
|
+
setMeta: jest.fn().mockReturnThis(),
|
|
398
|
+
},
|
|
399
|
+
doc: {},
|
|
400
|
+
},
|
|
401
|
+
view: {
|
|
402
|
+
coordsAtPos: jest.fn(() => ({ top: 100, left: 50 })),
|
|
403
|
+
dispatch: jest.fn(),
|
|
404
|
+
},
|
|
405
|
+
options: {
|
|
406
|
+
element: createEditorElement(),
|
|
407
|
+
},
|
|
408
|
+
commands: {
|
|
409
|
+
focus: jest.fn(),
|
|
410
|
+
},
|
|
411
|
+
instanceId: 'editor-123',
|
|
412
|
+
_toolbarOpened: false,
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
const mockNode = {
|
|
416
|
+
attrs: {
|
|
417
|
+
latex: 'x^2',
|
|
418
|
+
},
|
|
419
|
+
nodeSize: 1,
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
let defaultProps;
|
|
423
|
+
|
|
424
|
+
beforeEach(() => {
|
|
425
|
+
jest.clearAllMocks();
|
|
426
|
+
mockCreatePortal.mockImplementation((node) => node);
|
|
427
|
+
defaultProps = {
|
|
428
|
+
node: mockNode,
|
|
429
|
+
updateAttributes: jest.fn(),
|
|
430
|
+
editor: createMockEditor(),
|
|
431
|
+
selected: false,
|
|
432
|
+
options: {},
|
|
433
|
+
getPos: jest.fn(() => 0),
|
|
434
|
+
};
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('renders without crashing', () => {
|
|
438
|
+
const { container } = render(<MathNodeView {...defaultProps} />);
|
|
439
|
+
expect(container).toBeInTheDocument();
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it('renders NodeViewWrapper', () => {
|
|
443
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} />);
|
|
444
|
+
expect(getByTestId('node-view-wrapper')).toBeInTheDocument();
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it('displays math preview', () => {
|
|
448
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} />);
|
|
449
|
+
expect(getByTestId('math-preview')).toBeInTheDocument();
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it('shows toolbar when selected', async () => {
|
|
453
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
454
|
+
await waitFor(() => {
|
|
455
|
+
expect(getByTestId('math-toolbar')).toBeInTheDocument();
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it('does not show toolbar when not selected', () => {
|
|
460
|
+
const { queryByTestId } = render(<MathNodeView {...defaultProps} selected={false} />);
|
|
461
|
+
expect(queryByTestId('math-toolbar')).not.toBeInTheDocument();
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('adds data-toolbar-for attribute with editor instanceId', async () => {
|
|
465
|
+
const { container } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
466
|
+
await waitFor(() => {
|
|
467
|
+
const toolbar = container.querySelector('[data-toolbar-for]');
|
|
468
|
+
expect(toolbar).toHaveAttribute('data-toolbar-for', 'editor-123');
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
describe('toolbar positioning', () => {
|
|
473
|
+
it('positions relative to portal container using coordsAtPos', async () => {
|
|
474
|
+
const { container } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
475
|
+
await waitFor(() => {
|
|
476
|
+
const toolbar = container.querySelector('[data-toolbar-for]');
|
|
477
|
+
expect(toolbar).toBeInTheDocument();
|
|
478
|
+
expect(toolbar.style.top).toBe('100px');
|
|
479
|
+
expect(toolbar.style.left).toBe('50px');
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it('offsets position by portal container getBoundingClientRect', async () => {
|
|
484
|
+
const containerEl = document.createElement('div');
|
|
485
|
+
containerEl.getBoundingClientRect = jest.fn(() => ({ top: 100, left: 50, width: 600, height: 400 }));
|
|
486
|
+
|
|
487
|
+
const editor = {
|
|
488
|
+
...defaultProps.editor,
|
|
489
|
+
_tiptapContainerEl: containerEl,
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const { container } = render(<MathNodeView {...defaultProps} editor={editor} selected={true} />);
|
|
493
|
+
await waitFor(() => {
|
|
494
|
+
const toolbar = container.querySelector('[data-toolbar-for]');
|
|
495
|
+
expect(toolbar).toBeInTheDocument();
|
|
496
|
+
expect(toolbar.style.top).toBe('0px');
|
|
497
|
+
expect(toolbar.style.left).toBe('0px');
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it('applies absolute positioning style to toolbar', async () => {
|
|
502
|
+
const { container } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
503
|
+
await waitFor(() => {
|
|
504
|
+
const toolbar = container.querySelector('[data-toolbar-for]');
|
|
505
|
+
expect(toolbar).toBeInTheDocument();
|
|
506
|
+
expect(toolbar.style.position).toBe('absolute');
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it('renders above other editor overlays with a high z-index', async () => {
|
|
511
|
+
const { container } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
512
|
+
await waitFor(() => {
|
|
513
|
+
const toolbar = container.querySelector('[data-toolbar-for]');
|
|
514
|
+
expect(toolbar).toBeInTheDocument();
|
|
515
|
+
expect(toolbar.style.zIndex).toBe('1000');
|
|
516
|
+
});
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('updates position from coordsAtPos when selection changes', async () => {
|
|
520
|
+
const editor = {
|
|
521
|
+
...defaultProps.editor,
|
|
522
|
+
view: {
|
|
523
|
+
...defaultProps.editor.view,
|
|
524
|
+
coordsAtPos: jest.fn(() => ({ top: 200, left: 150 })),
|
|
525
|
+
dispatch: jest.fn(),
|
|
526
|
+
},
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
const { container } = render(<MathNodeView {...defaultProps} editor={editor} selected={true} />);
|
|
530
|
+
await waitFor(() => {
|
|
531
|
+
const toolbar = container.querySelector('[data-toolbar-for]');
|
|
532
|
+
expect(toolbar).toBeInTheDocument();
|
|
533
|
+
expect(toolbar.style.top).toBe('200px');
|
|
534
|
+
expect(toolbar.style.left).toBe('150px');
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it('clamps toolbar position to viewport margins', async () => {
|
|
539
|
+
const originalInnerHeight = window.innerHeight;
|
|
540
|
+
const originalInnerWidth = window.innerWidth;
|
|
541
|
+
|
|
542
|
+
Object.defineProperty(window, 'innerHeight', { configurable: true, writable: true, value: 200 });
|
|
543
|
+
Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: 300 });
|
|
544
|
+
|
|
545
|
+
const editor = {
|
|
546
|
+
...defaultProps.editor,
|
|
547
|
+
view: {
|
|
548
|
+
...defaultProps.editor.view,
|
|
549
|
+
coordsAtPos: jest.fn(() => ({ top: 190, left: 280, bottom: 195 })),
|
|
550
|
+
dispatch: jest.fn(),
|
|
551
|
+
},
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
const { container } = render(<MathNodeView {...defaultProps} editor={editor} selected={true} />);
|
|
555
|
+
|
|
556
|
+
let toolbar;
|
|
557
|
+
await waitFor(() => {
|
|
558
|
+
toolbar = container.querySelector('[data-toolbar-for]');
|
|
559
|
+
expect(toolbar).toBeInTheDocument();
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
Object.defineProperty(toolbar, 'offsetHeight', { configurable: true, value: 100 });
|
|
563
|
+
Object.defineProperty(toolbar, 'offsetWidth', { configurable: true, value: 150 });
|
|
564
|
+
|
|
565
|
+
await act(async () => {
|
|
566
|
+
window.dispatchEvent(new Event('resize'));
|
|
567
|
+
await new Promise((resolve) => requestAnimationFrame(resolve));
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
await waitFor(() => {
|
|
571
|
+
expect(parseInt(toolbar.style.top, 10)).toBeLessThanOrEqual(200 - 100 - 8);
|
|
572
|
+
expect(parseInt(toolbar.style.left, 10)).toBeLessThanOrEqual(300 - 150 - 8);
|
|
573
|
+
expect(parseInt(toolbar.style.top, 10)).toBeGreaterThanOrEqual(8);
|
|
574
|
+
expect(parseInt(toolbar.style.left, 10)).toBeGreaterThanOrEqual(8);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
Object.defineProperty(window, 'innerHeight', { configurable: true, writable: true, value: originalInnerHeight });
|
|
578
|
+
Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: originalInnerWidth });
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
it('attaches scroll and resize listeners while toolbar is open', async () => {
|
|
582
|
+
const addSpy = jest.spyOn(window, 'addEventListener');
|
|
583
|
+
render(<MathNodeView {...defaultProps} selected={true} />);
|
|
584
|
+
await waitFor(() => {
|
|
585
|
+
expect(addSpy).toHaveBeenCalledWith('scroll', expect.any(Function), true);
|
|
586
|
+
expect(addSpy).toHaveBeenCalledWith('resize', expect.any(Function));
|
|
587
|
+
});
|
|
588
|
+
addSpy.mockRestore();
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
it('portals toolbar into _tiptapContainerEl when available', async () => {
|
|
592
|
+
const containerEl = document.createElement('div');
|
|
593
|
+
containerEl.getBoundingClientRect = jest.fn(() => ({ top: 0, left: 0, width: 600, height: 400 }));
|
|
594
|
+
|
|
595
|
+
const editor = {
|
|
596
|
+
...defaultProps.editor,
|
|
597
|
+
_tiptapContainerEl: containerEl,
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
mockCreatePortal.mockClear();
|
|
601
|
+
render(<MathNodeView {...defaultProps} editor={editor} selected={true} />);
|
|
602
|
+
await waitFor(() => {
|
|
603
|
+
expect(mockCreatePortal).toHaveBeenCalled();
|
|
604
|
+
const lastCall = mockCreatePortal.mock.calls[mockCreatePortal.mock.calls.length - 1];
|
|
605
|
+
expect(lastCall[1]).toBe(containerEl);
|
|
606
|
+
});
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
it('portals toolbar into document.body when _tiptapContainerEl is not set', async () => {
|
|
610
|
+
const editor = {
|
|
611
|
+
...defaultProps.editor,
|
|
612
|
+
_tiptapContainerEl: undefined,
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
mockCreatePortal.mockClear();
|
|
616
|
+
render(<MathNodeView {...defaultProps} editor={editor} selected={true} />);
|
|
617
|
+
await waitFor(() => {
|
|
618
|
+
expect(mockCreatePortal).toHaveBeenCalled();
|
|
619
|
+
const lastCall = mockCreatePortal.mock.calls[mockCreatePortal.mock.calls.length - 1];
|
|
620
|
+
expect(lastCall[1]).toBe(document.body);
|
|
621
|
+
});
|
|
622
|
+
});
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
it('calls updateAttributes when latex changes', async () => {
|
|
626
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
627
|
+
await waitFor(() => {
|
|
628
|
+
const input = getByTestId('math-input');
|
|
629
|
+
fireEvent.change(input, { target: { value: 'y^2' } });
|
|
630
|
+
});
|
|
631
|
+
expect(defaultProps.updateAttributes).toHaveBeenCalledWith({ latex: 'y^2' });
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
it('closes toolbar and updates attributes when done', async () => {
|
|
635
|
+
const updateAttributes = jest.fn();
|
|
636
|
+
const { getByTestId } = render(
|
|
637
|
+
<MathNodeView {...defaultProps} updateAttributes={updateAttributes} selected={true} />,
|
|
638
|
+
);
|
|
639
|
+
|
|
640
|
+
await waitFor(() => {
|
|
641
|
+
expect(getByTestId('done-button')).toBeInTheDocument();
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
const doneButton = getByTestId('done-button');
|
|
645
|
+
fireEvent.click(doneButton);
|
|
646
|
+
|
|
647
|
+
await waitFor(() => {
|
|
648
|
+
expect(updateAttributes).toHaveBeenCalledWith({ latex: 'x^2' });
|
|
649
|
+
});
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
it('sets editor._toolbarOpened when toolbar is shown', async () => {
|
|
653
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
654
|
+
await waitFor(() => {
|
|
655
|
+
expect(getByTestId('math-toolbar')).toBeInTheDocument();
|
|
656
|
+
expect(defaultProps.editor._toolbarOpened).toBe(true);
|
|
657
|
+
});
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
it('unsets editor._toolbarOpened when toolbar is closed and node is not selected', async () => {
|
|
661
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} selected={false} />);
|
|
662
|
+
|
|
663
|
+
fireEvent.click(getByTestId('math-preview'));
|
|
664
|
+
|
|
665
|
+
await waitFor(() => {
|
|
666
|
+
expect(getByTestId('math-toolbar')).toBeInTheDocument();
|
|
667
|
+
expect(defaultProps.editor._toolbarOpened).toBe(true);
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
fireEvent.click(getByTestId('done-button'));
|
|
671
|
+
|
|
672
|
+
await waitFor(() => {
|
|
673
|
+
expect(defaultProps.editor._toolbarOpened).toBe(false);
|
|
674
|
+
});
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
it('keeps editor._toolbarOpened true while the math node remains selected', async () => {
|
|
678
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
679
|
+
|
|
680
|
+
await waitFor(() => {
|
|
681
|
+
expect(getByTestId('done-button')).toBeInTheDocument();
|
|
682
|
+
expect(defaultProps.editor._toolbarOpened).toBe(true);
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
fireEvent.click(getByTestId('done-button'));
|
|
686
|
+
|
|
687
|
+
await waitFor(() => {
|
|
688
|
+
expect(defaultProps.editor._toolbarOpened).toBe(true);
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
it('closes toolbar on outside click and runs handleDone', async () => {
|
|
693
|
+
const updateAttributes = jest.fn();
|
|
694
|
+
const editor = createMockEditor();
|
|
695
|
+
const { TextSelection } = require('prosemirror-state');
|
|
696
|
+
const { queryByTestId } = render(
|
|
697
|
+
<MathNodeView {...defaultProps} editor={editor} updateAttributes={updateAttributes} selected={true} />,
|
|
698
|
+
);
|
|
699
|
+
|
|
700
|
+
await waitFor(() => {
|
|
701
|
+
expect(queryByTestId('math-toolbar')).toBeInTheDocument();
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
fireEvent.click(document.body);
|
|
705
|
+
|
|
706
|
+
await waitFor(() => {
|
|
707
|
+
expect(queryByTestId('math-toolbar')).not.toBeInTheDocument();
|
|
708
|
+
expect(updateAttributes).toHaveBeenCalledWith({ latex: 'x^2' });
|
|
709
|
+
expect(TextSelection.create).toHaveBeenCalledWith(editor.state.doc, 1);
|
|
710
|
+
expect(editor.state.tr.setSelection).toHaveBeenCalled();
|
|
711
|
+
expect(editor.view.dispatch).toHaveBeenCalledWith(editor.state.tr);
|
|
712
|
+
expect(editor.commands.focus).toHaveBeenCalled();
|
|
713
|
+
expect(editor._toolbarOpened).toBe(true);
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
it('re-registers click listener when node changes', async () => {
|
|
718
|
+
const addEventListenerSpy = jest.spyOn(document, 'addEventListener');
|
|
719
|
+
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener');
|
|
720
|
+
const nodeA = { attrs: { latex: 'x^2' } };
|
|
721
|
+
const nodeB = { attrs: { latex: 'y^2' } };
|
|
722
|
+
|
|
723
|
+
const { rerender } = render(<MathNodeView {...defaultProps} node={nodeA} selected={true} />);
|
|
724
|
+
|
|
725
|
+
await waitFor(() => {
|
|
726
|
+
expect(addEventListenerSpy).toHaveBeenCalledWith('click', expect.any(Function));
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
const initialCallCount = addEventListenerSpy.mock.calls.length;
|
|
730
|
+
|
|
731
|
+
rerender(<MathNodeView {...defaultProps} node={nodeB} selected={true} />);
|
|
732
|
+
|
|
733
|
+
await waitFor(() => {
|
|
734
|
+
expect(removeEventListenerSpy).toHaveBeenCalled();
|
|
735
|
+
expect(addEventListenerSpy.mock.calls.length).toBeGreaterThan(initialCallCount);
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
addEventListenerSpy.mockRestore();
|
|
739
|
+
removeEventListenerSpy.mockRestore();
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
it('does not close toolbar when clicking the math node preview', async () => {
|
|
743
|
+
const { getByTestId, queryByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
744
|
+
|
|
745
|
+
await waitFor(() => {
|
|
746
|
+
expect(queryByTestId('math-toolbar')).toBeInTheDocument();
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
fireEvent.click(getByTestId('math-preview'));
|
|
750
|
+
|
|
751
|
+
await waitFor(() => {
|
|
752
|
+
expect(queryByTestId('math-toolbar')).toBeInTheDocument();
|
|
753
|
+
});
|
|
754
|
+
});
|
|
755
|
+
|
|
756
|
+
describe('unique math-node class per instance', () => {
|
|
757
|
+
let dateNowSpy;
|
|
758
|
+
|
|
759
|
+
beforeEach(() => {
|
|
760
|
+
let timestamp = 1000;
|
|
761
|
+
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => timestamp++);
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
afterEach(() => {
|
|
765
|
+
dateNowSpy.mockRestore();
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
it('assigns a timestamp-based class to NodeViewWrapper', () => {
|
|
769
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} />);
|
|
770
|
+
const wrapper = getByTestId('node-view-wrapper');
|
|
771
|
+
|
|
772
|
+
expect(wrapper.className).toMatch(/^math-node-\d+$/);
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
it('assigns a different class to each MathNodeView instance', () => {
|
|
776
|
+
const { getAllByTestId } = render(
|
|
777
|
+
<>
|
|
778
|
+
<MathNodeView {...defaultProps} />
|
|
779
|
+
<MathNodeView {...defaultProps} node={{ attrs: { latex: 'y^2' } }} />
|
|
780
|
+
</>,
|
|
781
|
+
);
|
|
782
|
+
|
|
783
|
+
const wrappers = getAllByTestId('node-view-wrapper');
|
|
784
|
+
expect(wrappers[0].className).toBe('math-node-1000');
|
|
785
|
+
expect(wrappers[1].className).toBe('math-node-1001');
|
|
786
|
+
});
|
|
787
|
+
|
|
788
|
+
it('closes toolbar when clicking a different math node', async () => {
|
|
789
|
+
const updateAttributes = jest.fn();
|
|
790
|
+
const editorA = createMockEditor();
|
|
791
|
+
const editorB = createMockEditor();
|
|
792
|
+
|
|
793
|
+
const { getAllByTestId, queryAllByTestId } = render(
|
|
794
|
+
<>
|
|
795
|
+
<MathNodeView {...defaultProps} editor={editorA} updateAttributes={updateAttributes} selected={false} />
|
|
796
|
+
<MathNodeView {...defaultProps} editor={editorB} node={{ attrs: { latex: 'y^2' } }} selected={false} />
|
|
797
|
+
</>,
|
|
798
|
+
);
|
|
799
|
+
|
|
800
|
+
fireEvent.click(getAllByTestId('math-preview')[0]);
|
|
801
|
+
|
|
802
|
+
await waitFor(() => {
|
|
803
|
+
expect(queryAllByTestId('math-toolbar')).toHaveLength(1);
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
const secondPreview = getAllByTestId('math-preview')[1];
|
|
807
|
+
fireEvent.click(secondPreview);
|
|
808
|
+
|
|
809
|
+
await waitFor(() => {
|
|
810
|
+
expect(queryAllByTestId('math-toolbar')).toHaveLength(1);
|
|
811
|
+
expect(updateAttributes).toHaveBeenCalledWith({ latex: 'x^2' });
|
|
812
|
+
expect(editorA._toolbarOpened).toBe(false);
|
|
813
|
+
expect(getAllByTestId('math-input')[0]).toHaveValue('y^2');
|
|
814
|
+
});
|
|
815
|
+
});
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
describe('selection-based toolbar guard', () => {
|
|
819
|
+
it('opens toolbar when selected transitions to true and the editor has a NodeSelection on math', async () => {
|
|
820
|
+
const { queryByTestId, rerender } = render(<MathNodeView {...defaultProps} selected={false} />);
|
|
821
|
+
expect(queryByTestId('math-toolbar')).not.toBeInTheDocument();
|
|
822
|
+
|
|
823
|
+
rerender(<MathNodeView {...defaultProps} selected={true} />);
|
|
824
|
+
await waitFor(() => {
|
|
825
|
+
expect(queryByTestId('math-toolbar')).toBeInTheDocument();
|
|
826
|
+
});
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
it('does not open toolbar when selected briefly becomes true but editor selection has no node (Cmd+A / drag case)', async () => {
|
|
830
|
+
const editor = {
|
|
831
|
+
...defaultProps.editor,
|
|
832
|
+
state: {
|
|
833
|
+
...defaultProps.editor.state,
|
|
834
|
+
selection: { from: 0, to: 100 }, // no .node — TextSelection / AllSelection shape
|
|
835
|
+
},
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
const { queryByTestId, rerender } = render(
|
|
839
|
+
<MathNodeView {...defaultProps} editor={editor} selected={false} />,
|
|
840
|
+
);
|
|
841
|
+
rerender(<MathNodeView {...defaultProps} editor={editor} selected={true} />);
|
|
842
|
+
|
|
843
|
+
await act(async () => {});
|
|
844
|
+
expect(queryByTestId('math-toolbar')).not.toBeInTheDocument();
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
it('does not open toolbar when selected briefly becomes true but NodeSelection targets a non-math node', async () => {
|
|
848
|
+
const editor = {
|
|
849
|
+
...defaultProps.editor,
|
|
850
|
+
state: {
|
|
851
|
+
...defaultProps.editor.state,
|
|
852
|
+
selection: { from: 0, to: 1, node: { type: { name: 'image' } } },
|
|
853
|
+
},
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
const { queryByTestId, rerender } = render(
|
|
857
|
+
<MathNodeView {...defaultProps} editor={editor} selected={false} />,
|
|
858
|
+
);
|
|
859
|
+
rerender(<MathNodeView {...defaultProps} editor={editor} selected={true} />);
|
|
860
|
+
|
|
861
|
+
await act(async () => {});
|
|
862
|
+
expect(queryByTestId('math-toolbar')).not.toBeInTheDocument();
|
|
863
|
+
});
|
|
864
|
+
});
|
|
865
|
+
|
|
866
|
+
it('does not close toolbar when clicking equation editor dropdown', async () => {
|
|
867
|
+
const { queryByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
|
|
868
|
+
|
|
869
|
+
await waitFor(() => {
|
|
870
|
+
expect(queryByTestId('math-toolbar')).toBeInTheDocument();
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
// Simulate MUI Select's portal dropdown container.
|
|
874
|
+
const dropdown = document.createElement('div');
|
|
875
|
+
dropdown.id = 'equation-editor-select-listbox';
|
|
876
|
+
document.body.appendChild(dropdown);
|
|
877
|
+
|
|
878
|
+
fireEvent.click(dropdown);
|
|
879
|
+
|
|
880
|
+
await waitFor(() => {
|
|
881
|
+
expect(queryByTestId('math-toolbar')).toBeInTheDocument();
|
|
882
|
+
});
|
|
883
|
+
|
|
884
|
+
document.body.removeChild(dropdown);
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
it('renders with empty latex', () => {
|
|
888
|
+
const nodeWithEmptyLatex = { attrs: { latex: '' } };
|
|
889
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} node={nodeWithEmptyLatex} />);
|
|
890
|
+
expect(getByTestId('math-preview')).toBeInTheDocument();
|
|
891
|
+
});
|
|
892
|
+
|
|
893
|
+
it('has correct styling on NodeViewWrapper', () => {
|
|
894
|
+
const { getByTestId } = render(<MathNodeView {...defaultProps} />);
|
|
895
|
+
const wrapper = getByTestId('node-view-wrapper');
|
|
896
|
+
expect(wrapper).toHaveStyle({ display: 'inline-flex', cursor: 'pointer' });
|
|
897
|
+
});
|
|
898
|
+
});
|