@pie-lib/editable-html-tip-tap 1.2.0-next.9 → 2.0.1
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.md +176 -0
- package/lib/components/CharacterPicker.js +1 -0
- package/lib/components/CharacterPicker.js.map +1 -1
- package/lib/components/EditableHtml.js +84 -43
- package/lib/components/EditableHtml.js.map +1 -1
- package/lib/components/MenuBar.js +74 -43
- package/lib/components/MenuBar.js.map +1 -1
- package/lib/components/TiptapContainer.js +9 -8
- package/lib/components/TiptapContainer.js.map +1 -1
- package/lib/components/icons/TextAlign.js +2 -2
- package/lib/components/icons/TextAlign.js.map +1 -1
- package/lib/components/image/InsertImageHandler.js +10 -13
- package/lib/components/image/InsertImageHandler.js.map +1 -1
- package/lib/components/media/MediaDialog.js.map +1 -1
- package/lib/components/respArea/DragInTheBlank/DragInTheBlank.js +6 -1
- package/lib/components/respArea/DragInTheBlank/DragInTheBlank.js.map +1 -1
- package/lib/components/respArea/DragInTheBlank/choice.js +15 -7
- package/lib/components/respArea/DragInTheBlank/choice.js.map +1 -1
- package/lib/components/respArea/ExplicitConstructedResponse.js +29 -11
- package/lib/components/respArea/ExplicitConstructedResponse.js.map +1 -1
- package/lib/components/respArea/InlineDropdown.js +35 -6
- package/lib/components/respArea/InlineDropdown.js.map +1 -1
- package/lib/extensions/custom-toolbar-wrapper.js +3 -2
- package/lib/extensions/custom-toolbar-wrapper.js.map +1 -1
- 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 +50 -1
- package/lib/extensions/extended-table.js.map +1 -1
- package/lib/extensions/image-component.js +102 -51
- package/lib/extensions/image-component.js.map +1 -1
- package/lib/extensions/image.js +51 -2
- package/lib/extensions/image.js.map +1 -1
- package/lib/extensions/math.js +50 -9
- package/lib/extensions/math.js.map +1 -1
- package/lib/extensions/media.js +3 -1
- package/lib/extensions/media.js.map +1 -1
- package/lib/extensions/responseArea.js +12 -7
- package/lib/extensions/responseArea.js.map +1 -1
- package/lib/styles/editorContainerStyles.js +5 -4
- package/lib/styles/editorContainerStyles.js.map +1 -1
- package/lib/utils/helper.js +17 -0
- package/lib/utils/helper.js.map +1 -0
- package/package.json +8 -8
- package/src/__tests__/EditableHtml.test.jsx +90 -7
- package/src/__tests__/index.test.jsx +11 -3
- package/src/components/CharacterPicker.jsx +1 -0
- package/src/components/EditableHtml.jsx +91 -41
- package/src/components/MenuBar.jsx +57 -24
- package/src/components/TiptapContainer.jsx +10 -8
- package/src/components/__tests__/CharacterPicker.test.jsx +22 -0
- package/src/components/__tests__/ExplicitConstructedResponse.test.jsx +55 -12
- package/src/components/__tests__/InlineDropdown.test.jsx +203 -10
- package/src/components/__tests__/InsertImageHandler.test.js +28 -21
- package/src/components/__tests__/MenuBar.test.jsx +32 -0
- package/src/components/icons/TextAlign.jsx +1 -1
- package/src/components/image/InsertImageHandler.js +9 -13
- package/src/components/respArea/DragInTheBlank/DragInTheBlank.jsx +6 -1
- package/src/components/respArea/DragInTheBlank/choice.jsx +32 -4
- package/src/components/respArea/ExplicitConstructedResponse.jsx +33 -10
- package/src/components/respArea/InlineDropdown.jsx +45 -10
- 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 +98 -1
- package/src/extensions/__tests__/image-component.test.jsx +105 -9
- package/src/extensions/__tests__/image.test.js +109 -8
- package/src/extensions/__tests__/math.test.js +348 -0
- package/src/extensions/__tests__/media-node-view.test.jsx +10 -8
- package/src/extensions/__tests__/responseArea.test.js +291 -0
- package/src/extensions/custom-toolbar-wrapper.jsx +2 -2
- 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 +37 -1
- package/src/extensions/image-component.jsx +114 -69
- package/src/extensions/image.js +56 -1
- package/src/extensions/math.js +62 -10
- package/src/extensions/media.js +1 -1
- package/src/extensions/responseArea.js +13 -11
- package/src/styles/editorContainerStyles.js +5 -4
- package/src/utils/helper.js +17 -0
- /package/src/components/media/{MediaDialog.js → MediaDialog.jsx} +0 -0
|
@@ -15,7 +15,7 @@ const StyledContent = styled('span')(({ theme }) => ({
|
|
|
15
15
|
},
|
|
16
16
|
}));
|
|
17
17
|
|
|
18
|
-
export function BlankContent({ n, children, isDragging, isOver, dragItem, value }) {
|
|
18
|
+
export function BlankContent({ n, children, isDragging, isOver, dragItem, value, selected }) {
|
|
19
19
|
const [hoveredElementSize, setHoveredElementSize] = useState(null);
|
|
20
20
|
const elementRef = useRef(null);
|
|
21
21
|
|
|
@@ -56,15 +56,22 @@ export function BlankContent({ n, children, isDragging, isOver, dragItem, value
|
|
|
56
56
|
const hasGrip = finalLabel !== '\u00A0';
|
|
57
57
|
const isPreview = dragItem && isOver;
|
|
58
58
|
|
|
59
|
+
const borderStyle = selected
|
|
60
|
+
? `2px solid ${color.primaryDark()}`
|
|
61
|
+
: isPreview
|
|
62
|
+
? `1px solid ${color.defaults.BORDER_DARK}`
|
|
63
|
+
: `1px solid ${color.defaults.BORDER_LIGHT}`;
|
|
64
|
+
|
|
59
65
|
return (
|
|
60
66
|
<div
|
|
61
67
|
ref={elementRef}
|
|
68
|
+
className={selected ? 'selected' : undefined}
|
|
62
69
|
style={{
|
|
63
70
|
display: 'inline-flex',
|
|
64
71
|
minWidth: '178px',
|
|
65
72
|
minHeight: '36px',
|
|
66
73
|
background: isPreview ? `${color.defaults.BORDER_LIGHT}` : `${color.defaults.WHITE}`,
|
|
67
|
-
border:
|
|
74
|
+
border: borderStyle,
|
|
68
75
|
boxSizing: 'border-box',
|
|
69
76
|
borderRadius: '3px',
|
|
70
77
|
overflow: 'hidden',
|
|
@@ -72,6 +79,7 @@ export function BlankContent({ n, children, isDragging, isOver, dragItem, value
|
|
|
72
79
|
padding: '8px 8px 8px 35px',
|
|
73
80
|
width: hoveredElementSize ? hoveredElementSize.width : undefined,
|
|
74
81
|
height: hoveredElementSize ? hoveredElementSize.height : undefined,
|
|
82
|
+
touchAction: 'none',
|
|
75
83
|
}}
|
|
76
84
|
data-key={n.index}
|
|
77
85
|
contentEditable={false}
|
|
@@ -104,9 +112,21 @@ BlankContent.propTypes = {
|
|
|
104
112
|
isOver: PropTypes.bool,
|
|
105
113
|
dragItem: PropTypes.object,
|
|
106
114
|
value: PropTypes.object,
|
|
115
|
+
selected: PropTypes.bool,
|
|
107
116
|
};
|
|
108
117
|
|
|
109
|
-
function DragDropChoice({
|
|
118
|
+
function DragDropChoice({
|
|
119
|
+
value,
|
|
120
|
+
disabled,
|
|
121
|
+
instanceId,
|
|
122
|
+
children,
|
|
123
|
+
n,
|
|
124
|
+
onChange,
|
|
125
|
+
removeResponse,
|
|
126
|
+
duplicates,
|
|
127
|
+
pos,
|
|
128
|
+
selected,
|
|
129
|
+
}) {
|
|
110
130
|
const {
|
|
111
131
|
attributes: dragAttributes,
|
|
112
132
|
listeners: dragListeners,
|
|
@@ -196,7 +216,14 @@ function DragDropChoice({ value, disabled, instanceId, children, n, onChange, re
|
|
|
196
216
|
};
|
|
197
217
|
|
|
198
218
|
const dragContent = (
|
|
199
|
-
<BlankContent
|
|
219
|
+
<BlankContent
|
|
220
|
+
n={n}
|
|
221
|
+
isDragging={isDragging}
|
|
222
|
+
isOver={isOver}
|
|
223
|
+
dragItem={dragItem?.data?.current}
|
|
224
|
+
value={value}
|
|
225
|
+
selected={selected}
|
|
226
|
+
>
|
|
200
227
|
{children}
|
|
201
228
|
</BlankContent>
|
|
202
229
|
);
|
|
@@ -223,6 +250,7 @@ DragDropChoice.propTypes = {
|
|
|
223
250
|
onChange: PropTypes.func.isRequired,
|
|
224
251
|
removeResponse: PropTypes.func.isRequired,
|
|
225
252
|
duplicates: PropTypes.bool,
|
|
253
|
+
selected: PropTypes.bool,
|
|
226
254
|
};
|
|
227
255
|
|
|
228
256
|
export default DragDropChoice;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import React, { useEffect, useRef, useState } from 'react';
|
|
2
2
|
import { NodeViewWrapper } from '@tiptap/react';
|
|
3
|
+
import ReactDOM from 'react-dom';
|
|
3
4
|
import PropTypes from 'prop-types';
|
|
5
|
+
import CustomToolbarWrapper from '../../extensions/custom-toolbar-wrapper';
|
|
4
6
|
|
|
5
7
|
const ExplicitConstructedResponse = (props) => {
|
|
6
8
|
const { editor, node, getPos, options, selected } = props;
|
|
@@ -9,7 +11,7 @@ const ExplicitConstructedResponse = (props) => {
|
|
|
9
11
|
const { respAreaToolbar, error: errorFn } = options;
|
|
10
12
|
const pos = getPos();
|
|
11
13
|
const [showToolbar, setShowToolbar] = useState(false);
|
|
12
|
-
const EcrToolbar = respAreaToolbar(node, editor, () => {});
|
|
14
|
+
const EcrToolbar = respAreaToolbar([node, pos], editor, () => {});
|
|
13
15
|
const toolbarRef = useRef(null);
|
|
14
16
|
|
|
15
17
|
let error;
|
|
@@ -21,12 +23,6 @@ const ExplicitConstructedResponse = (props) => {
|
|
|
21
23
|
error = !!errorValue?.[respIndex]?.[0];
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
const handleDone = (newLatex) => {
|
|
25
|
-
updateAttributes({ latex: newLatex });
|
|
26
|
-
setShowToolbar(false);
|
|
27
|
-
editor.commands.focus();
|
|
28
|
-
};
|
|
29
|
-
|
|
30
26
|
useEffect(() => {
|
|
31
27
|
const { selection } = editor.state;
|
|
32
28
|
const onlyThisNodeSelected = selection.from + node.nodeSize === selection.to;
|
|
@@ -42,7 +38,11 @@ const ExplicitConstructedResponse = (props) => {
|
|
|
42
38
|
|
|
43
39
|
useEffect(() => {
|
|
44
40
|
const handleClickOutside = (event) => {
|
|
41
|
+
const insideCharacterPicker =
|
|
42
|
+
event.target.closest('.insert-character-dialog') || event.target.closest('[data-toolbar-for]');
|
|
43
|
+
|
|
45
44
|
if (
|
|
45
|
+
!insideCharacterPicker &&
|
|
46
46
|
toolbarRef.current &&
|
|
47
47
|
!toolbarRef.current.contains(event.target) &&
|
|
48
48
|
!event.target.closest('[data-inline-node]')
|
|
@@ -94,9 +94,32 @@ const ExplicitConstructedResponse = (props) => {
|
|
|
94
94
|
}}
|
|
95
95
|
/>
|
|
96
96
|
{showToolbar && (
|
|
97
|
-
<
|
|
98
|
-
<
|
|
99
|
-
|
|
97
|
+
<React.Fragment>
|
|
98
|
+
<div ref={toolbarRef} className="absolute z-50 bg-white shadow-lg rounded p-2" style={{ zIndex: 1 }}>
|
|
99
|
+
<EcrToolbar />
|
|
100
|
+
</div>
|
|
101
|
+
{editor._tiptapContainerEl &&
|
|
102
|
+
ReactDOM.createPortal(
|
|
103
|
+
<CustomToolbarWrapper
|
|
104
|
+
deletable
|
|
105
|
+
toolbarOpts={{ minWidth: 'auto' }}
|
|
106
|
+
autoWidth
|
|
107
|
+
style={{ top: -40, left: 0, right: 0 }}
|
|
108
|
+
onDelete={() => {
|
|
109
|
+
const { tr } = editor.state;
|
|
110
|
+
tr.delete(pos, pos + node.nodeSize);
|
|
111
|
+
// Prevent the debounced onBlur/onDone from firing into the
|
|
112
|
+
// now-deleted node's stale position
|
|
113
|
+
editor._toolbarOpened = false;
|
|
114
|
+
editor.view.dispatch(tr);
|
|
115
|
+
setShowToolbar(false);
|
|
116
|
+
editor.commands.focus();
|
|
117
|
+
}}
|
|
118
|
+
showDone={false}
|
|
119
|
+
/>,
|
|
120
|
+
editor._tiptapContainerEl,
|
|
121
|
+
)}
|
|
122
|
+
</React.Fragment>
|
|
100
123
|
)}
|
|
101
124
|
</NodeViewWrapper>
|
|
102
125
|
);
|
|
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
|
|
|
3
3
|
import { NodeViewWrapper } from '@tiptap/react';
|
|
4
4
|
import { Chevron } from '../icons/RespArea';
|
|
5
5
|
import ReactDOM from 'react-dom';
|
|
6
|
+
import CustomToolbarWrapper from '../../extensions/custom-toolbar-wrapper';
|
|
6
7
|
|
|
7
8
|
const InlineDropdown = (props) => {
|
|
8
9
|
const { editor, node, getPos, options, selected } = props;
|
|
@@ -11,10 +12,12 @@ const InlineDropdown = (props) => {
|
|
|
11
12
|
// TODO: Investigate
|
|
12
13
|
// Needed because items with values inside have different positioning for some reason
|
|
13
14
|
const html = value || '<div> </div>';
|
|
15
|
+
const pos = getPos();
|
|
14
16
|
const toolbarRef = useRef(null);
|
|
17
|
+
const toolbarEditor = useRef(null);
|
|
15
18
|
const [showToolbar, setShowToolbar] = useState(false);
|
|
16
19
|
const [position, setPosition] = useState({ top: 0, left: 0 });
|
|
17
|
-
const InlineDropdownToolbar = options.respAreaToolbar(node, editor, () => {});
|
|
20
|
+
const InlineDropdownToolbar = options.respAreaToolbar([node, pos], editor, () => {});
|
|
18
21
|
|
|
19
22
|
useEffect(() => {
|
|
20
23
|
const { selection } = editor.state;
|
|
@@ -41,7 +44,11 @@ const InlineDropdown = (props) => {
|
|
|
41
44
|
});
|
|
42
45
|
|
|
43
46
|
const handleClickOutside = (event) => {
|
|
47
|
+
const insideSomeEditor = event.target.closest('[data-toolbar-for]');
|
|
48
|
+
|
|
44
49
|
if (
|
|
50
|
+
(!insideSomeEditor || insideSomeEditor.dataset.toolbarFor !== toolbarEditor.current.instanceId) &&
|
|
51
|
+
!editor._toolbarOpened &&
|
|
45
52
|
toolbarRef.current &&
|
|
46
53
|
!toolbarRef.current.contains(event.target) &&
|
|
47
54
|
!event.target.closest('[data-inline-node]')
|
|
@@ -66,7 +73,6 @@ const InlineDropdown = (props) => {
|
|
|
66
73
|
style={{
|
|
67
74
|
display: 'inline-flex',
|
|
68
75
|
height: '50px',
|
|
69
|
-
margin: '0 5px',
|
|
70
76
|
cursor: 'pointer',
|
|
71
77
|
}}
|
|
72
78
|
>
|
|
@@ -79,7 +85,7 @@ const InlineDropdown = (props) => {
|
|
|
79
85
|
border: '1px solid #C0C3CF',
|
|
80
86
|
boxSizing: 'border-box',
|
|
81
87
|
borderRadius: '3px',
|
|
82
|
-
margin: '0
|
|
88
|
+
margin: '0 2px',
|
|
83
89
|
position: 'relative',
|
|
84
90
|
alignItems: 'center',
|
|
85
91
|
}}
|
|
@@ -113,13 +119,42 @@ const InlineDropdown = (props) => {
|
|
|
113
119
|
}}
|
|
114
120
|
/>
|
|
115
121
|
</div>
|
|
116
|
-
{showToolbar &&
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
<
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
{showToolbar && (
|
|
123
|
+
<React.Fragment>
|
|
124
|
+
{ReactDOM.createPortal(
|
|
125
|
+
<div ref={toolbarRef} style={{ zIndex: 1 }}>
|
|
126
|
+
<InlineDropdownToolbar
|
|
127
|
+
editorCallback={(instance) => {
|
|
128
|
+
toolbarEditor.current = instance;
|
|
129
|
+
}}
|
|
130
|
+
/>
|
|
131
|
+
</div>,
|
|
132
|
+
document.body,
|
|
133
|
+
)}
|
|
134
|
+
|
|
135
|
+
{editor._tiptapContainerEl &&
|
|
136
|
+
ReactDOM.createPortal(
|
|
137
|
+
<CustomToolbarWrapper
|
|
138
|
+
deletable
|
|
139
|
+
toolbarOpts={{ minWidth: 'auto' }}
|
|
140
|
+
autoWidth
|
|
141
|
+
style={{ top: -40, left: 0, right: 0 }}
|
|
142
|
+
onDelete={() => {
|
|
143
|
+
const { tr } = editor.state;
|
|
144
|
+
tr.delete(pos, pos + node.nodeSize);
|
|
145
|
+
// Prevent the debounced onBlur/onDone from firing into the
|
|
146
|
+
// now-deleted node's stale position
|
|
147
|
+
editor._toolbarOpened = false;
|
|
148
|
+
editor.view.dispatch(tr);
|
|
149
|
+
setShowToolbar(false);
|
|
150
|
+
editor.commands.focus();
|
|
151
|
+
}}
|
|
152
|
+
showDone={false}
|
|
153
|
+
/>,
|
|
154
|
+
editor._tiptapContainerEl,
|
|
155
|
+
)}
|
|
156
|
+
</React.Fragment>
|
|
157
|
+
)}
|
|
123
158
|
</NodeViewWrapper>
|
|
124
159
|
);
|
|
125
160
|
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { DivNode } from '../div-node';
|
|
2
|
+
|
|
3
|
+
jest.mock('@tiptap/core', () => ({
|
|
4
|
+
Node: {
|
|
5
|
+
create: jest.fn((config) => config),
|
|
6
|
+
},
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
describe('DivNode', () => {
|
|
10
|
+
describe('configuration', () => {
|
|
11
|
+
it('has correct name', () => {
|
|
12
|
+
expect(DivNode.name).toBe('div');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('is block level', () => {
|
|
16
|
+
expect(DivNode.group).toBe('block');
|
|
17
|
+
expect(DivNode.content).toBe('inline*');
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('parseHTML', () => {
|
|
22
|
+
it('parses <div> tags', () => {
|
|
23
|
+
const rules = DivNode.parseHTML();
|
|
24
|
+
|
|
25
|
+
expect(Array.isArray(rules)).toBe(true);
|
|
26
|
+
expect(rules).toHaveLength(1);
|
|
27
|
+
expect(rules[0]).toHaveProperty('tag', 'div');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('renderHTML', () => {
|
|
32
|
+
it('renders a <div> tag', () => {
|
|
33
|
+
const result = DivNode.renderHTML({ HTMLAttributes: { class: 'foo' } });
|
|
34
|
+
|
|
35
|
+
expect(result[0]).toBe('div');
|
|
36
|
+
expect(result[1]).toEqual({ class: 'foo' });
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('addKeyboardShortcuts', () => {
|
|
41
|
+
it('does nothing when current block is not div', () => {
|
|
42
|
+
const mockEditor = {
|
|
43
|
+
state: {
|
|
44
|
+
selection: {
|
|
45
|
+
$from: {
|
|
46
|
+
parent: { type: { name: 'paragraph' } },
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const shortcuts = DivNode.addKeyboardShortcuts.call({ editor: mockEditor });
|
|
53
|
+
const handled = shortcuts.Enter();
|
|
54
|
+
|
|
55
|
+
expect(handled).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('turns div into paragraph and splits when Enter is pressed in a div', () => {
|
|
59
|
+
const chain = {
|
|
60
|
+
focus: jest.fn(() => chain),
|
|
61
|
+
setNode: jest.fn(() => chain),
|
|
62
|
+
splitBlock: jest.fn(() => chain),
|
|
63
|
+
run: jest.fn(() => true),
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const mockEditor = {
|
|
67
|
+
state: {
|
|
68
|
+
selection: {
|
|
69
|
+
$from: {
|
|
70
|
+
parent: { type: { name: 'div' } },
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
chain: () => chain,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const shortcuts = DivNode.addKeyboardShortcuts.call({ editor: mockEditor });
|
|
78
|
+
const handled = shortcuts.Enter();
|
|
79
|
+
|
|
80
|
+
expect(chain.focus).toHaveBeenCalled();
|
|
81
|
+
expect(chain.setNode).toHaveBeenCalledWith('paragraph');
|
|
82
|
+
expect(chain.splitBlock).toHaveBeenCalled();
|
|
83
|
+
expect(chain.run).toHaveBeenCalled();
|
|
84
|
+
expect(handled).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Editor } from '@tiptap/core';
|
|
2
|
+
import StarterKit from '@tiptap/starter-kit';
|
|
3
|
+
import { DivNode } from '../div-node';
|
|
4
|
+
import { EnsureEmptyRootIsDiv } from '../ensure-empty-root-div';
|
|
5
|
+
|
|
6
|
+
/** Match EditableHtml so TrailingNode does not add a second empty block next to an empty paragraph. */
|
|
7
|
+
const starterKitConfigured = StarterKit.configure({
|
|
8
|
+
trailingNode: {
|
|
9
|
+
node: 'paragraph',
|
|
10
|
+
notAfter: ['paragraph', 'div'],
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('EnsureEmptyRootIsDiv', () => {
|
|
15
|
+
const extensions = [starterKitConfigured, DivNode, EnsureEmptyRootIsDiv];
|
|
16
|
+
|
|
17
|
+
let editor;
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
if (editor) {
|
|
21
|
+
editor.destroy();
|
|
22
|
+
editor = null;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('replaces a lone root paragraph with an empty div after select-all and delete (full delete flow)', () => {
|
|
27
|
+
editor = new Editor({
|
|
28
|
+
extensions,
|
|
29
|
+
content: '<p>hello</p>',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
editor.chain().focus().selectAll().deleteSelection().run();
|
|
33
|
+
|
|
34
|
+
expect(editor.state.doc.childCount).toBe(1);
|
|
35
|
+
expect(editor.state.doc.firstChild.type.name).toBe('div');
|
|
36
|
+
expect(editor.state.doc.firstChild.textContent).toBe('');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('does not replace a root paragraph that still has text', () => {
|
|
40
|
+
editor = new Editor({
|
|
41
|
+
extensions,
|
|
42
|
+
content: '<p>x</p>',
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
expect(editor.state.doc.firstChild.type.name).toBe('paragraph');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('does not run when there is more than one top-level block', () => {
|
|
49
|
+
editor = new Editor({
|
|
50
|
+
extensions,
|
|
51
|
+
content: '<p>a</p><p>b</p>',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
expect(editor.state.doc.childCount).toBe(2);
|
|
55
|
+
expect(editor.state.doc.firstChild.type.name).toBe('paragraph');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { EnsureListItemContentIsDiv } from '../ensure-list-item-content-is-div';
|
|
2
|
+
|
|
3
|
+
describe('EnsureListItemContentIsDiv', () => {
|
|
4
|
+
it('registers a ProseMirror plugin that rewrites listItem paragraph children to div', () => {
|
|
5
|
+
const plugins = EnsureListItemContentIsDiv.config.addProseMirrorPlugins();
|
|
6
|
+
expect(Array.isArray(plugins)).toBe(true);
|
|
7
|
+
expect(plugins).toHaveLength(1);
|
|
8
|
+
|
|
9
|
+
const plugin = plugins[0];
|
|
10
|
+
const divType = { name: 'div' };
|
|
11
|
+
const tr = { setNodeMarkup: jest.fn() };
|
|
12
|
+
|
|
13
|
+
const paragraphChild = {
|
|
14
|
+
type: { name: 'paragraph' },
|
|
15
|
+
attrs: { class: 'x' },
|
|
16
|
+
nodeSize: 6,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const listItemNode = {
|
|
20
|
+
type: { name: 'listItem' },
|
|
21
|
+
forEach: (cb) => cb(paragraphChild),
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const newState = {
|
|
25
|
+
schema: { nodes: { div: divType } },
|
|
26
|
+
tr,
|
|
27
|
+
doc: {
|
|
28
|
+
descendants: (cb) => cb(listItemNode, 10),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const result = plugin.spec.appendTransaction([{ docChanged: true }], {}, newState);
|
|
33
|
+
|
|
34
|
+
expect(tr.setNodeMarkup).toHaveBeenCalledWith(11, divType, paragraphChild.attrs);
|
|
35
|
+
expect(result).toBe(tr);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('returns null when there is no document change', () => {
|
|
39
|
+
const plugin = EnsureListItemContentIsDiv.config.addProseMirrorPlugins()[0];
|
|
40
|
+
|
|
41
|
+
const result = plugin.spec.appendTransaction([{ docChanged: false }], {}, {});
|
|
42
|
+
expect(result).toBeNull();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ExtendedListItem } from '../extended-list-item';
|
|
2
|
+
|
|
3
|
+
describe('ExtendedListItem', () => {
|
|
4
|
+
it('extends listItem and prefers div over paragraph in content spec', () => {
|
|
5
|
+
expect(ExtendedListItem.name).toBe('listItem');
|
|
6
|
+
|
|
7
|
+
const content =
|
|
8
|
+
ExtendedListItem.options?.content ?? ExtendedListItem.config?.content ?? ExtendedListItem.extendOptions?.content;
|
|
9
|
+
|
|
10
|
+
expect(String(content)).toMatch(/^\(div \| paragraph/);
|
|
11
|
+
expect(String(content)).toContain('imageUploadNode');
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ExtendedTableCell, ExtendedTableHeader } from '../extended-table-cell';
|
|
2
|
+
|
|
3
|
+
describe('ExtendedTableCell / ExtendedTableHeader', () => {
|
|
4
|
+
it('exports cell and header extensions with div preferred over paragraph in content spec', () => {
|
|
5
|
+
expect(ExtendedTableCell.name).toBe('tableCell');
|
|
6
|
+
expect(ExtendedTableHeader.name).toBe('tableHeader');
|
|
7
|
+
|
|
8
|
+
const cellContent =
|
|
9
|
+
ExtendedTableCell.options?.content ??
|
|
10
|
+
ExtendedTableCell.config?.content ??
|
|
11
|
+
ExtendedTableCell.extendOptions?.content;
|
|
12
|
+
|
|
13
|
+
expect(String(cellContent)).toMatch(/^\(div \| paragraph/);
|
|
14
|
+
|
|
15
|
+
const headerContent =
|
|
16
|
+
ExtendedTableHeader.options?.content ??
|
|
17
|
+
ExtendedTableHeader.config?.content ??
|
|
18
|
+
ExtendedTableHeader.extendOptions?.content;
|
|
19
|
+
|
|
20
|
+
expect(headerContent).toBe(cellContent);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -1,12 +1,109 @@
|
|
|
1
|
-
import ExtendedTable from '../extended-table';
|
|
1
|
+
import ExtendedTable, { applyPresentationToTableElement, ExtendedTableView } from '../extended-table';
|
|
2
|
+
|
|
3
|
+
const mockTableViewState = { updateReturns: true };
|
|
2
4
|
|
|
3
5
|
jest.mock('@tiptap/extension-table', () => ({
|
|
4
6
|
Table: {
|
|
5
7
|
extend: jest.fn((config) => config),
|
|
6
8
|
},
|
|
9
|
+
TableView: class TableView {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.table = {
|
|
12
|
+
style: { setProperty: jest.fn() },
|
|
13
|
+
setAttribute: jest.fn(),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
update() {
|
|
18
|
+
return mockTableViewState.updateReturns;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
7
21
|
}));
|
|
8
22
|
|
|
9
23
|
describe('ExtendedTable', () => {
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
mockTableViewState.updateReturns = true;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('applyPresentationToTableElement', () => {
|
|
29
|
+
it('sets default border and theme styles', () => {
|
|
30
|
+
const table = document.createElement('table');
|
|
31
|
+
const setProperty = jest.spyOn(table.style, 'setProperty');
|
|
32
|
+
|
|
33
|
+
applyPresentationToTableElement(table, {});
|
|
34
|
+
|
|
35
|
+
expect(table.getAttribute('border')).toBe('1');
|
|
36
|
+
expect(setProperty).toHaveBeenCalledWith('color', 'var(--pie-text, black)');
|
|
37
|
+
expect(setProperty).toHaveBeenCalledWith('background-color', 'var(--pie-background, rgba(255, 255, 255))');
|
|
38
|
+
setProperty.mockRestore();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('uses explicit border from attrs', () => {
|
|
42
|
+
const table = document.createElement('table');
|
|
43
|
+
applyPresentationToTableElement(table, { border: '0' });
|
|
44
|
+
|
|
45
|
+
expect(table.getAttribute('border')).toBe('0');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('treats empty border as default', () => {
|
|
49
|
+
const table = document.createElement('table');
|
|
50
|
+
applyPresentationToTableElement(table, { border: '' });
|
|
51
|
+
|
|
52
|
+
expect(table.getAttribute('border')).toBe('1');
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('ExtendedTableView', () => {
|
|
57
|
+
it('applies presentation after construction', () => {
|
|
58
|
+
const node = { attrs: { border: '2' } };
|
|
59
|
+
const view = new ExtendedTableView(node, 25);
|
|
60
|
+
|
|
61
|
+
expect(view.table.setAttribute).toHaveBeenCalledWith('border', '2');
|
|
62
|
+
expect(view.table.style.setProperty).toHaveBeenCalledWith('color', 'var(--pie-text, black)');
|
|
63
|
+
expect(view.table.style.setProperty).toHaveBeenCalledWith(
|
|
64
|
+
'background-color',
|
|
65
|
+
'var(--pie-background, rgba(255, 255, 255))',
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('reapplies presentation when super.update returns true', () => {
|
|
70
|
+
const view = new ExtendedTableView({ attrs: { border: '1' } }, 25);
|
|
71
|
+
view.table.setAttribute.mockClear();
|
|
72
|
+
view.table.style.setProperty.mockClear();
|
|
73
|
+
|
|
74
|
+
view.update({ attrs: { border: '0' } });
|
|
75
|
+
|
|
76
|
+
expect(view.table.setAttribute).toHaveBeenCalledWith('border', '0');
|
|
77
|
+
expect(view.table.style.setProperty).toHaveBeenCalledWith('color', 'var(--pie-text, black)');
|
|
78
|
+
expect(view.table.style.setProperty).toHaveBeenCalledWith(
|
|
79
|
+
'background-color',
|
|
80
|
+
'var(--pie-background, rgba(255, 255, 255))',
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('does not reapply presentation when super.update returns false', () => {
|
|
85
|
+
mockTableViewState.updateReturns = false;
|
|
86
|
+
const view = new ExtendedTableView({ attrs: { border: '1' } }, 25);
|
|
87
|
+
view.table.setAttribute.mockClear();
|
|
88
|
+
view.table.style.setProperty.mockClear();
|
|
89
|
+
|
|
90
|
+
const ok = view.update({ attrs: { border: '0' } });
|
|
91
|
+
|
|
92
|
+
expect(ok).toBe(false);
|
|
93
|
+
expect(view.table.setAttribute).not.toHaveBeenCalled();
|
|
94
|
+
expect(view.table.style.setProperty).not.toHaveBeenCalled();
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('addOptions', () => {
|
|
99
|
+
it('enables resizable table view with zero handle width for colgroup sync', () => {
|
|
100
|
+
const options = ExtendedTable.addOptions();
|
|
101
|
+
expect(options.resizable).toBe(true);
|
|
102
|
+
expect(options.handleWidth).toBe(0);
|
|
103
|
+
expect(options.View).toBe(ExtendedTableView);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
10
107
|
describe('addAttributes', () => {
|
|
11
108
|
it('returns border attribute with default value', () => {
|
|
12
109
|
const attributes = ExtendedTable.addAttributes();
|