@handlewithcare/react-prosemirror 2.0.0
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/LICENSE.txt +12 -0
- package/README.md +705 -0
- package/dist/cjs/browser.js +53 -0
- package/dist/cjs/components/ChildNodeViews.js +376 -0
- package/dist/cjs/components/CursorWrapper.js +91 -0
- package/dist/cjs/components/CustomNodeView.js +79 -0
- package/dist/cjs/components/DocNodeView.js +104 -0
- package/dist/cjs/components/LayoutGroup.js +111 -0
- package/dist/cjs/components/MarkView.js +115 -0
- package/dist/cjs/components/NativeWidgetView.js +109 -0
- package/dist/cjs/components/NodeView.js +196 -0
- package/dist/cjs/components/NodeViewComponentProps.js +4 -0
- package/dist/cjs/components/OutputSpec.js +88 -0
- package/dist/cjs/components/ProseMirror.js +103 -0
- package/dist/cjs/components/ProseMirrorDoc.js +92 -0
- package/dist/cjs/components/SeparatorHackView.js +100 -0
- package/dist/cjs/components/TextNodeView.js +112 -0
- package/dist/cjs/components/TrailingHackView.js +90 -0
- package/dist/cjs/components/WidgetView.js +95 -0
- package/dist/cjs/components/WidgetViewComponentProps.js +4 -0
- package/dist/cjs/components/__tests__/ProseMirror.composition.test.js +398 -0
- package/dist/cjs/components/__tests__/ProseMirror.domchange.test.js +270 -0
- package/dist/cjs/components/__tests__/ProseMirror.draw-decoration.test.js +1010 -0
- package/dist/cjs/components/__tests__/ProseMirror.draw.test.js +337 -0
- package/dist/cjs/components/__tests__/ProseMirror.node-view.test.js +315 -0
- package/dist/cjs/components/__tests__/ProseMirror.selection.test.js +444 -0
- package/dist/cjs/components/__tests__/ProseMirror.test.js +382 -0
- package/dist/cjs/contexts/ChildDescriptorsContext.js +19 -0
- package/dist/cjs/contexts/EditorContext.js +12 -0
- package/dist/cjs/contexts/EditorStateContext.js +12 -0
- package/dist/cjs/contexts/LayoutGroupContext.js +12 -0
- package/dist/cjs/contexts/NodeViewContext.js +12 -0
- package/dist/cjs/contexts/SelectNodeContext.js +12 -0
- package/dist/cjs/contexts/StopEventContext.js +12 -0
- package/dist/cjs/contexts/__tests__/DeferredLayoutEffects.test.js +141 -0
- package/dist/cjs/decorations/ReactWidgetType.js +58 -0
- package/dist/cjs/decorations/computeDocDeco.js +44 -0
- package/dist/cjs/decorations/internalTypes.js +4 -0
- package/dist/cjs/decorations/iterDeco.js +79 -0
- package/dist/cjs/decorations/viewDecorations.js +163 -0
- package/dist/cjs/dom.js +142 -0
- package/dist/cjs/hooks/__tests__/useEditorViewLayoutEffect.test.js +108 -0
- package/dist/cjs/hooks/useClientOnly.js +18 -0
- package/dist/cjs/hooks/useComponentEventListeners.js +39 -0
- package/dist/cjs/hooks/useEditor.js +287 -0
- package/dist/cjs/hooks/useEditorEffect.js +35 -0
- package/dist/cjs/hooks/useEditorEventCallback.js +33 -0
- package/dist/cjs/hooks/useEditorEventListener.js +34 -0
- package/dist/cjs/hooks/useEditorState.js +16 -0
- package/dist/cjs/hooks/useForceUpdate.js +15 -0
- package/dist/cjs/hooks/useLayoutGroupEffect.js +19 -0
- package/dist/cjs/hooks/useNodeViewDescriptor.js +115 -0
- package/dist/cjs/hooks/useReactKeys.js +17 -0
- package/dist/cjs/hooks/useSelectNode.js +28 -0
- package/dist/cjs/hooks/useStopEvent.js +24 -0
- package/dist/cjs/index.js +53 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/plugins/__tests__/reactKeys.test.js +81 -0
- package/dist/cjs/plugins/beforeInputPlugin.js +143 -0
- package/dist/cjs/plugins/componentEventListeners.js +35 -0
- package/dist/cjs/plugins/componentEventListenersPlugin.js +35 -0
- package/dist/cjs/plugins/reactKeys.js +96 -0
- package/dist/cjs/props.js +269 -0
- package/dist/cjs/selection/SelectionDOMObserver.js +174 -0
- package/dist/cjs/selection/hasFocusAndSelection.js +35 -0
- package/dist/cjs/selection/selectionFromDOM.js +77 -0
- package/dist/cjs/selection/selectionToDOM.js +226 -0
- package/dist/cjs/ssr.js +85 -0
- package/dist/cjs/testing/editorViewTestHelpers.js +111 -0
- package/dist/cjs/testing/setupProseMirrorView.js +94 -0
- package/dist/cjs/viewdesc.js +664 -0
- package/dist/esm/browser.js +43 -0
- package/dist/esm/components/ChildNodeViews.js +318 -0
- package/dist/esm/components/CursorWrapper.js +40 -0
- package/dist/esm/components/CustomNodeView.js +28 -0
- package/dist/esm/components/DocNodeView.js +53 -0
- package/dist/esm/components/LayoutGroup.js +66 -0
- package/dist/esm/components/MarkView.js +64 -0
- package/dist/esm/components/NativeWidgetView.js +58 -0
- package/dist/esm/components/NodeView.js +145 -0
- package/dist/esm/components/NodeViewComponentProps.js +1 -0
- package/dist/esm/components/OutputSpec.js +38 -0
- package/dist/esm/components/ProseMirror.js +52 -0
- package/dist/esm/components/ProseMirrorDoc.js +34 -0
- package/dist/esm/components/SeparatorHackView.js +49 -0
- package/dist/esm/components/TextNodeView.js +102 -0
- package/dist/esm/components/TrailingHackView.js +39 -0
- package/dist/esm/components/WidgetView.js +44 -0
- package/dist/esm/components/WidgetViewComponentProps.js +1 -0
- package/dist/esm/components/__tests__/ProseMirror.composition.test.js +395 -0
- package/dist/esm/components/__tests__/ProseMirror.domchange.test.js +266 -0
- package/dist/esm/components/__tests__/ProseMirror.draw-decoration.test.js +967 -0
- package/dist/esm/components/__tests__/ProseMirror.draw.test.js +294 -0
- package/dist/esm/components/__tests__/ProseMirror.node-view.test.js +272 -0
- package/dist/esm/components/__tests__/ProseMirror.selection.test.js +440 -0
- package/dist/esm/components/__tests__/ProseMirror.test.js +339 -0
- package/dist/esm/contexts/ChildDescriptorsContext.js +9 -0
- package/dist/esm/contexts/EditorContext.js +7 -0
- package/dist/esm/contexts/EditorStateContext.js +2 -0
- package/dist/esm/contexts/LayoutGroupContext.js +2 -0
- package/dist/esm/contexts/NodeViewContext.js +2 -0
- package/dist/esm/contexts/SelectNodeContext.js +2 -0
- package/dist/esm/contexts/StopEventContext.js +2 -0
- package/dist/esm/contexts/__tests__/DeferredLayoutEffects.test.js +98 -0
- package/dist/esm/decorations/ReactWidgetType.js +40 -0
- package/dist/esm/decorations/computeDocDeco.js +44 -0
- package/dist/esm/decorations/internalTypes.js +1 -0
- package/dist/esm/decorations/iterDeco.js +73 -0
- package/dist/esm/decorations/viewDecorations.js +163 -0
- package/dist/esm/dom.js +105 -0
- package/dist/esm/hooks/__tests__/useEditorViewLayoutEffect.test.js +99 -0
- package/dist/esm/hooks/useClientOnly.js +8 -0
- package/dist/esm/hooks/useComponentEventListeners.js +54 -0
- package/dist/esm/hooks/useEditor.js +278 -0
- package/dist/esm/hooks/useEditorEffect.js +38 -0
- package/dist/esm/hooks/useEditorEventCallback.js +35 -0
- package/dist/esm/hooks/useEditorEventListener.js +28 -0
- package/dist/esm/hooks/useEditorState.js +8 -0
- package/dist/esm/hooks/useForceUpdate.js +8 -0
- package/dist/esm/hooks/useLayoutGroupEffect.js +9 -0
- package/dist/esm/hooks/useNodeViewDescriptor.js +105 -0
- package/dist/esm/hooks/useReactKeys.js +7 -0
- package/dist/esm/hooks/useSelectNode.js +18 -0
- package/dist/esm/hooks/useStopEvent.js +14 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/plugins/__tests__/reactKeys.test.js +77 -0
- package/dist/esm/plugins/beforeInputPlugin.js +133 -0
- package/dist/esm/plugins/componentEventListeners.js +25 -0
- package/dist/esm/plugins/componentEventListenersPlugin.js +25 -0
- package/dist/esm/plugins/reactKeys.js +81 -0
- package/dist/esm/props.js +251 -0
- package/dist/esm/selection/SelectionDOMObserver.js +164 -0
- package/dist/esm/selection/hasFocusAndSelection.js +17 -0
- package/dist/esm/selection/selectionFromDOM.js +59 -0
- package/dist/esm/selection/selectionToDOM.js +196 -0
- package/dist/esm/ssr.js +82 -0
- package/dist/esm/testing/editorViewTestHelpers.js +88 -0
- package/dist/esm/testing/setupProseMirrorView.js +76 -0
- package/dist/esm/viewdesc.js +654 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/browser.d.ts +15 -0
- package/dist/types/components/ChildNodeViews.d.ts +9 -0
- package/dist/types/components/CursorWrapper.d.ts +5 -0
- package/dist/types/components/CustomNodeView.d.ts +21 -0
- package/dist/types/components/DocNodeView.d.ts +20 -0
- package/dist/types/components/LayoutGroup.d.ts +12 -0
- package/dist/types/components/MarkView.d.ts +9 -0
- package/dist/types/components/NativeWidgetView.d.ts +8 -0
- package/dist/types/components/NodeView.d.ts +11 -0
- package/dist/types/components/NodeViewComponentProps.d.ts +12 -0
- package/dist/types/components/OutputSpec.d.ts +8 -0
- package/dist/types/components/ProseMirror.d.ts +15 -0
- package/dist/types/components/ProseMirrorDoc.d.ts +10 -0
- package/dist/types/components/SeparatorHackView.d.ts +6 -0
- package/dist/types/components/TextNodeView.d.ts +23 -0
- package/dist/types/components/TrailingHackView.d.ts +6 -0
- package/dist/types/components/WidgetView.d.ts +8 -0
- package/dist/types/components/WidgetViewComponentProps.d.ts +6 -0
- package/dist/types/components/__tests__/ProseMirror.composition.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.domchange.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.draw-decoration.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.draw.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.node-view.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.selection.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.test.d.ts +1 -0
- package/dist/types/contexts/ChildDescriptorsContext.d.ts +6 -0
- package/dist/types/contexts/EditorContext.d.ts +14 -0
- package/dist/types/contexts/EditorStateContext.d.ts +2 -0
- package/dist/types/contexts/LayoutGroupContext.d.ts +5 -0
- package/dist/types/contexts/NodeViewContext.d.ts +6 -0
- package/dist/types/contexts/SelectNodeContext.d.ts +3 -0
- package/dist/types/contexts/StopEventContext.d.ts +3 -0
- package/dist/types/contexts/__tests__/DeferredLayoutEffects.test.d.ts +1 -0
- package/dist/types/decorations/ReactWidgetType.d.ts +39 -0
- package/dist/types/decorations/computeDocDeco.d.ts +13 -0
- package/dist/types/decorations/internalTypes.d.ts +16 -0
- package/dist/types/decorations/iterDeco.d.ts +3 -0
- package/dist/types/decorations/viewDecorations.d.ts +13 -0
- package/dist/types/dom.d.ts +22 -0
- package/dist/types/hooks/__tests__/useEditorViewLayoutEffect.test.d.ts +1 -0
- package/dist/types/hooks/useClientOnly.d.ts +1 -0
- package/dist/types/hooks/useComponentEventListeners.d.ts +33 -0
- package/dist/types/hooks/useEditor.d.ts +66 -0
- package/dist/types/hooks/useEditorEffect.d.ts +17 -0
- package/dist/types/hooks/useEditorEventCallback.d.ts +15 -0
- package/dist/types/hooks/useEditorEventListener.d.ts +8 -0
- package/dist/types/hooks/useEditorState.d.ts +5 -0
- package/dist/types/hooks/useForceUpdate.d.ts +5 -0
- package/dist/types/hooks/useLayoutGroupEffect.d.ts +3 -0
- package/dist/types/hooks/useNodeViewDescriptor.d.ts +11 -0
- package/dist/types/hooks/useReactKeys.d.ts +5 -0
- package/dist/types/hooks/useSelectNode.d.ts +1 -0
- package/dist/types/hooks/useStopEvent.d.ts +2 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/plugins/__tests__/reactKeys.test.d.ts +1 -0
- package/dist/types/plugins/beforeInputPlugin.d.ts +3 -0
- package/dist/types/plugins/componentEventListeners.d.ts +4 -0
- package/dist/types/plugins/componentEventListenersPlugin.d.ts +4 -0
- package/dist/types/plugins/reactKeys.d.ts +19 -0
- package/dist/types/props.d.ts +1174 -0
- package/dist/types/selection/SelectionDOMObserver.d.ts +34 -0
- package/dist/types/selection/hasFocusAndSelection.d.ts +3 -0
- package/dist/types/selection/selectionFromDOM.d.ts +4 -0
- package/dist/types/selection/selectionToDOM.d.ts +9 -0
- package/dist/types/ssr.d.ts +19 -0
- package/dist/types/testing/editorViewTestHelpers.d.ts +23 -0
- package/dist/types/testing/setupProseMirrorView.d.ts +2 -0
- package/dist/types/viewdesc.d.ts +131 -0
- package/package.json +113 -0
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/* eslint-disable jest/no-disabled-tests */ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-non-null-assertion */ import { act, screen } from "@testing-library/react";
|
|
2
|
+
import { NodeSelection, Selection } from "prosemirror-state";
|
|
3
|
+
import { blockquote, br, code, code_block, doc, em, hr, img as img_, li, p, strong, ul } from "prosemirror-test-builder";
|
|
4
|
+
import { Decoration, DecorationSet } from "prosemirror-view";
|
|
5
|
+
import { tempEditor } from "../../testing/editorViewTestHelpers.js";
|
|
6
|
+
const img = img_({
|
|
7
|
+
src: "data:image/gif;base64,R0lGODlhBQAFAIABAAAAAP///yH5BAEKAAEALAAAAAAFAAUAAAIEjI+pWAA7"
|
|
8
|
+
});
|
|
9
|
+
async function findTextNode(_, text) {
|
|
10
|
+
const parent = await screen.findByText(text);
|
|
11
|
+
return parent.firstChild;
|
|
12
|
+
}
|
|
13
|
+
function allPositions(doc) {
|
|
14
|
+
const found = [];
|
|
15
|
+
function scan(node, start) {
|
|
16
|
+
if (node.isTextblock) {
|
|
17
|
+
for(let i = 0; i <= node.content.size; i++)found.push(start + i);
|
|
18
|
+
} else {
|
|
19
|
+
node.forEach((child, offset)=>scan(child, start + offset + 1));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
scan(doc, 0);
|
|
23
|
+
return found;
|
|
24
|
+
}
|
|
25
|
+
function setDOMSel(node, offset) {
|
|
26
|
+
const range = document.createRange();
|
|
27
|
+
range.setEnd(node, offset);
|
|
28
|
+
range.setStart(node, offset);
|
|
29
|
+
const sel = window.getSelection();
|
|
30
|
+
sel.removeAllRanges();
|
|
31
|
+
sel.addRange(range);
|
|
32
|
+
}
|
|
33
|
+
function getSel() {
|
|
34
|
+
const sel = window.getSelection();
|
|
35
|
+
let node = sel.focusNode, offset = sel.focusOffset;
|
|
36
|
+
while(node && node.nodeType != 3){
|
|
37
|
+
const after = offset < node.childNodes.length && node.childNodes[offset];
|
|
38
|
+
const before = offset > 0 && node.childNodes[offset - 1];
|
|
39
|
+
if (after) {
|
|
40
|
+
node = after;
|
|
41
|
+
offset = 0;
|
|
42
|
+
} else if (before) {
|
|
43
|
+
node = before;
|
|
44
|
+
offset = node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length;
|
|
45
|
+
} else break;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
node: node,
|
|
49
|
+
offset: offset
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function setSel(view, sel) {
|
|
53
|
+
const selection = typeof sel == "number" ? Selection.near(view.state.doc.resolve(sel)) : sel;
|
|
54
|
+
act(()=>{
|
|
55
|
+
view.dispatch(view.state.tr.setSelection(selection));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function event(code) {
|
|
59
|
+
const event = document.createEvent("Event");
|
|
60
|
+
event.initEvent("keydown", true, true);
|
|
61
|
+
event.keyCode = code;
|
|
62
|
+
return event;
|
|
63
|
+
}
|
|
64
|
+
const LEFT = 37, RIGHT = 39, UP = 38, DOWN = 40;
|
|
65
|
+
describe("ProseMirror", ()=>{
|
|
66
|
+
it("can read the DOM selection", async ()=>{
|
|
67
|
+
const { view } = tempEditor({
|
|
68
|
+
doc: doc(p("one"), hr(), blockquote(p("two")))
|
|
69
|
+
});
|
|
70
|
+
function test(node, offset, expected) {
|
|
71
|
+
setDOMSel(node, offset);
|
|
72
|
+
view.dom.focus();
|
|
73
|
+
act(()=>{
|
|
74
|
+
view.domObserver.flush();
|
|
75
|
+
});
|
|
76
|
+
const sel = view.state.selection;
|
|
77
|
+
expect(sel.head == null ? sel.from : sel.head).toBe(expected);
|
|
78
|
+
}
|
|
79
|
+
const one = await findTextNode(view.dom, "one");
|
|
80
|
+
const two = await findTextNode(view.dom, "two");
|
|
81
|
+
test(one, 0, 1);
|
|
82
|
+
test(one, 1, 2);
|
|
83
|
+
test(one, 3, 4);
|
|
84
|
+
test(one.parentNode, 0, 1);
|
|
85
|
+
test(one.parentNode, 1, 4);
|
|
86
|
+
test(two, 0, 8);
|
|
87
|
+
test(two, 3, 11);
|
|
88
|
+
test(two.parentNode, 1, 11);
|
|
89
|
+
test(view.dom, 1, 4);
|
|
90
|
+
test(view.dom, 2, 8);
|
|
91
|
+
test(view.dom, 3, 11);
|
|
92
|
+
});
|
|
93
|
+
it("syncs the DOM selection with the editor selection", async ()=>{
|
|
94
|
+
const { view } = tempEditor({
|
|
95
|
+
doc: doc(p("one"), hr(), blockquote(p("two")))
|
|
96
|
+
});
|
|
97
|
+
function test(pos, node, offset) {
|
|
98
|
+
setSel(view, pos);
|
|
99
|
+
const sel = getSel();
|
|
100
|
+
expect(sel.node).toBe(node);
|
|
101
|
+
expect(sel.offset).toBe(offset);
|
|
102
|
+
}
|
|
103
|
+
const one = await findTextNode(view.dom, "one");
|
|
104
|
+
const two = await findTextNode(view.dom, "two");
|
|
105
|
+
view.focus();
|
|
106
|
+
test(1, one, 0);
|
|
107
|
+
test(2, one, 1);
|
|
108
|
+
test(4, one, 3);
|
|
109
|
+
test(8, two, 0);
|
|
110
|
+
test(10, two, 2);
|
|
111
|
+
});
|
|
112
|
+
it("returns sensible screen coordinates", async ()=>{
|
|
113
|
+
const { view } = tempEditor({
|
|
114
|
+
doc: doc(p("one"), p("two"))
|
|
115
|
+
});
|
|
116
|
+
const p00 = view.coordsAtPos(1);
|
|
117
|
+
const p01 = view.coordsAtPos(2);
|
|
118
|
+
const p03 = view.coordsAtPos(4);
|
|
119
|
+
const p10 = view.coordsAtPos(6);
|
|
120
|
+
const p13 = view.coordsAtPos(9);
|
|
121
|
+
expect(p00.bottom).toBeGreaterThan(p00.top);
|
|
122
|
+
expect(p13.bottom).toBeGreaterThan(p13.top);
|
|
123
|
+
expect(p00.top).toEqual(p01.top);
|
|
124
|
+
expect(p01.top).toEqual(p03.top);
|
|
125
|
+
expect(p00.bottom).toEqual(p03.bottom);
|
|
126
|
+
expect(p10.top).toEqual(p13.top);
|
|
127
|
+
expect(p01.left).toBeGreaterThan(p00.left);
|
|
128
|
+
expect(p03.left).toBeGreaterThan(p01.left);
|
|
129
|
+
expect(p10.top).toBeGreaterThan(p00.top);
|
|
130
|
+
expect(p13.left).toBeGreaterThan(p10.left);
|
|
131
|
+
});
|
|
132
|
+
it("returns proper coordinates in code blocks", async ()=>{
|
|
133
|
+
const { view } = tempEditor({
|
|
134
|
+
doc: doc(code_block("a\nb\n"))
|
|
135
|
+
}), p = [];
|
|
136
|
+
for(let i = 1; i <= 5; i++)p.push(view.coordsAtPos(i));
|
|
137
|
+
const [p0, p1, p2, p3, p4] = p;
|
|
138
|
+
expect(p0.top).toBe(p1.top);
|
|
139
|
+
expect(p0.left).toBeLessThan(p1.left);
|
|
140
|
+
expect(p2.top).toBeGreaterThan(p1.top);
|
|
141
|
+
expect(p2.top).toBe(p3.top);
|
|
142
|
+
expect(p2.left).toBeLessThan(p3.left);
|
|
143
|
+
expect(p2.left).toBe(p0.left);
|
|
144
|
+
expect(p4.top).toBeGreaterThan(p3.top);
|
|
145
|
+
// This one shows a small (0.01 pixel) difference in Firefox for
|
|
146
|
+
// some reason.
|
|
147
|
+
expect(Math.round(p4.left)).toBe(Math.round(p2.left));
|
|
148
|
+
});
|
|
149
|
+
// TODO: This test fails on the position between the img and the br (it returns
|
|
150
|
+
// the position before the img, instead of after).
|
|
151
|
+
it.skip("produces sensible screen coordinates in corner cases", async ()=>{
|
|
152
|
+
const { view } = tempEditor({
|
|
153
|
+
doc: doc(p("one", em("two", strong("three"), img), br(), code("foo")), p())
|
|
154
|
+
});
|
|
155
|
+
return new Promise((ok)=>{
|
|
156
|
+
setTimeout(()=>{
|
|
157
|
+
allPositions(view.state.doc).forEach((pos)=>{
|
|
158
|
+
const coords = view.coordsAtPos(pos);
|
|
159
|
+
const found = view.posAtCoords({
|
|
160
|
+
top: coords.top + 1,
|
|
161
|
+
left: coords.left
|
|
162
|
+
}).pos;
|
|
163
|
+
expect(found).toBe(pos);
|
|
164
|
+
setSel(view, pos);
|
|
165
|
+
});
|
|
166
|
+
ok(null);
|
|
167
|
+
}, 20);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
it("doesn't return zero-height rectangles after leaves", async ()=>{
|
|
171
|
+
const { view } = tempEditor({
|
|
172
|
+
doc: doc(p(img))
|
|
173
|
+
});
|
|
174
|
+
const coords = view.coordsAtPos(2, 1);
|
|
175
|
+
expect(coords.bottom - coords.top).toBeGreaterThan(5);
|
|
176
|
+
});
|
|
177
|
+
it("produces horizontal rectangles for positions between blocks", async ()=>{
|
|
178
|
+
const { view } = tempEditor({
|
|
179
|
+
doc: doc(p("ha"), hr(), blockquote(p("ba")))
|
|
180
|
+
});
|
|
181
|
+
const a = view.coordsAtPos(0);
|
|
182
|
+
expect(a.top).toBe(a.bottom);
|
|
183
|
+
expect(a.top).toBe(view.dom.firstChild.getBoundingClientRect().top);
|
|
184
|
+
expect(a.left).toBeLessThan(a.right);
|
|
185
|
+
const b = view.coordsAtPos(4);
|
|
186
|
+
expect(b.top).toBe(b.bottom);
|
|
187
|
+
expect(b.top).toBeGreaterThan(a.top);
|
|
188
|
+
expect(b.left).toBeLessThan(b.right);
|
|
189
|
+
const c = view.coordsAtPos(5);
|
|
190
|
+
expect(c.top).toBe(c.bottom);
|
|
191
|
+
expect(c.top).toBeGreaterThan(b.top);
|
|
192
|
+
const d = view.coordsAtPos(6);
|
|
193
|
+
expect(d.top).toBe(d.bottom);
|
|
194
|
+
expect(d.left).toBeLessThan(d.right);
|
|
195
|
+
expect(d.top).toBeLessThan(view.dom.getBoundingClientRect().bottom);
|
|
196
|
+
});
|
|
197
|
+
it("produces sensible screen coordinates around line breaks", async ()=>{
|
|
198
|
+
const { view } = tempEditor({
|
|
199
|
+
doc: doc(p("one two three four five-six-seven-eight"))
|
|
200
|
+
});
|
|
201
|
+
function afterSpace(pos) {
|
|
202
|
+
return pos > 0 && view.state.doc.textBetween(pos - 1, pos) == " ";
|
|
203
|
+
}
|
|
204
|
+
view.dom.style.width = "4em";
|
|
205
|
+
let prevBefore;
|
|
206
|
+
let prevAfter;
|
|
207
|
+
allPositions(view.state.doc).forEach((pos)=>{
|
|
208
|
+
const coords = view.coordsAtPos(pos, 1);
|
|
209
|
+
if (prevAfter) // eslint-disable-next-line jest/no-conditional-expect
|
|
210
|
+
expect(prevAfter.top < coords.top || prevAfter.top == coords.top && prevAfter.left < coords.left).toBeTruthy();
|
|
211
|
+
prevAfter = coords;
|
|
212
|
+
const found = view.posAtCoords({
|
|
213
|
+
top: coords.top + 1,
|
|
214
|
+
left: coords.left
|
|
215
|
+
}).pos;
|
|
216
|
+
expect(found).toBe(pos);
|
|
217
|
+
const coordsBefore = view.coordsAtPos(pos, -1);
|
|
218
|
+
if (prevBefore) // eslint-disable-next-line jest/no-conditional-expect
|
|
219
|
+
expect(prevBefore.top < coordsBefore.top || prevBefore.top == coordsBefore.top && (prevBefore.left < coordsBefore.left || afterSpace(pos) && prevBefore.left == coordsBefore.left)).toBeTruthy();
|
|
220
|
+
prevBefore = coordsBefore;
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
it("can find coordinates on node boundaries", async ()=>{
|
|
224
|
+
const { view } = tempEditor({
|
|
225
|
+
doc: doc(p("one ", em("two"), " ", em(strong("three"))))
|
|
226
|
+
});
|
|
227
|
+
let prev;
|
|
228
|
+
allPositions(view.state.doc).forEach((pos)=>{
|
|
229
|
+
const coords = view.coordsAtPos(pos, 1);
|
|
230
|
+
if (prev) // eslint-disable-next-line jest/no-conditional-expect
|
|
231
|
+
expect(prev.top < coords.top || Math.abs(prev.top - coords.top) < 4 && prev.left < coords.left).toBeTruthy();
|
|
232
|
+
prev = coords;
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
it("finds proper coordinates in RTL text", async ()=>{
|
|
236
|
+
const { view } = tempEditor({
|
|
237
|
+
doc: doc(p("مرآة نثرية"))
|
|
238
|
+
});
|
|
239
|
+
view.dom.style.direction = "rtl";
|
|
240
|
+
let prev;
|
|
241
|
+
allPositions(view.state.doc).forEach((pos)=>{
|
|
242
|
+
const coords = view.coordsAtPos(pos, 1);
|
|
243
|
+
if (prev) // eslint-disable-next-line jest/no-conditional-expect
|
|
244
|
+
expect(prev.top < coords.top || Math.abs(prev.top - coords.top) < 4 && prev.left > coords.left).toBeTruthy();
|
|
245
|
+
prev = coords;
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
it("can go back and forth between screen coordsa and document positions", async ()=>{
|
|
249
|
+
const { view } = tempEditor({
|
|
250
|
+
doc: doc(p("one"), blockquote(p("two"), p("three")))
|
|
251
|
+
});
|
|
252
|
+
[
|
|
253
|
+
1,
|
|
254
|
+
2,
|
|
255
|
+
4,
|
|
256
|
+
7,
|
|
257
|
+
14,
|
|
258
|
+
15
|
|
259
|
+
].forEach((pos)=>{
|
|
260
|
+
const coords = view.coordsAtPos(pos);
|
|
261
|
+
const found = view.posAtCoords({
|
|
262
|
+
top: coords.top + 1,
|
|
263
|
+
left: coords.left
|
|
264
|
+
}).pos;
|
|
265
|
+
expect(found).toBe(pos);
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
it("returns correct screen coordinates for wrapped lines", async ()=>{
|
|
269
|
+
const { view } = tempEditor({});
|
|
270
|
+
const top = view.coordsAtPos(1);
|
|
271
|
+
let pos = 1, end;
|
|
272
|
+
for(let i = 0; i < 100; i++){
|
|
273
|
+
view.dispatch(view.state.tr.insertText("a bc de fg h"));
|
|
274
|
+
pos += 12;
|
|
275
|
+
end = view.coordsAtPos(pos);
|
|
276
|
+
if (end.bottom > top.bottom + 4) break;
|
|
277
|
+
}
|
|
278
|
+
expect(view.posAtCoords({
|
|
279
|
+
left: end.left + 50,
|
|
280
|
+
top: end.top + 5
|
|
281
|
+
}).pos).toBe(pos);
|
|
282
|
+
});
|
|
283
|
+
it("makes arrow motion go through selectable inline nodes", async ()=>{
|
|
284
|
+
const { view } = tempEditor({
|
|
285
|
+
doc: doc(p("foo<a>", img, "bar"))
|
|
286
|
+
});
|
|
287
|
+
act(()=>{
|
|
288
|
+
view.dispatchEvent(event(RIGHT));
|
|
289
|
+
});
|
|
290
|
+
expect(view.state.selection.from).toBe(4);
|
|
291
|
+
act(()=>{
|
|
292
|
+
view.dispatchEvent(event(RIGHT));
|
|
293
|
+
});
|
|
294
|
+
expect(view.state.selection.head).toBe(5);
|
|
295
|
+
expect(view.state.selection.anchor).toBe(5);
|
|
296
|
+
act(()=>{
|
|
297
|
+
view.dispatchEvent(event(LEFT));
|
|
298
|
+
});
|
|
299
|
+
expect(view.state.selection.from).toBe(4);
|
|
300
|
+
act(()=>{
|
|
301
|
+
view.dispatchEvent(event(LEFT));
|
|
302
|
+
});
|
|
303
|
+
expect(view.state.selection.head).toBe(4);
|
|
304
|
+
expect(view.state.selection.anchor).toBe(4);
|
|
305
|
+
});
|
|
306
|
+
it("makes arrow motion go through selectable block nodes", async ()=>{
|
|
307
|
+
const { view } = tempEditor({
|
|
308
|
+
doc: doc(p("hello<a>"), hr(), ul(li(p("there"))))
|
|
309
|
+
});
|
|
310
|
+
act(()=>{
|
|
311
|
+
view.dispatchEvent(event(DOWN));
|
|
312
|
+
});
|
|
313
|
+
expect(view.state.selection.from).toBe(7);
|
|
314
|
+
setSel(view, 11);
|
|
315
|
+
act(()=>{
|
|
316
|
+
view.dispatchEvent(event(UP));
|
|
317
|
+
});
|
|
318
|
+
expect(view.state.selection.from).toBe(7);
|
|
319
|
+
});
|
|
320
|
+
it("supports arrow motion through adjacent blocks", async ()=>{
|
|
321
|
+
const { view } = tempEditor({
|
|
322
|
+
doc: doc(blockquote(p("hello<a>")), hr(), hr(), p("there"))
|
|
323
|
+
});
|
|
324
|
+
act(()=>{
|
|
325
|
+
view.dispatchEvent(event(DOWN));
|
|
326
|
+
});
|
|
327
|
+
expect(view.state.selection.from).toBe(9);
|
|
328
|
+
act(()=>{
|
|
329
|
+
view.dispatchEvent(event(DOWN));
|
|
330
|
+
});
|
|
331
|
+
expect(view.state.selection.from).toBe(10);
|
|
332
|
+
setSel(view, 14);
|
|
333
|
+
act(()=>{
|
|
334
|
+
view.dispatchEvent(event(UP));
|
|
335
|
+
});
|
|
336
|
+
expect(view.state.selection.from).toBe(10);
|
|
337
|
+
act(()=>{
|
|
338
|
+
view.dispatchEvent(event(UP));
|
|
339
|
+
});
|
|
340
|
+
expect(view.state.selection.from).toBe(9);
|
|
341
|
+
});
|
|
342
|
+
it("support horizontal motion through blocks", async ()=>{
|
|
343
|
+
const { view } = tempEditor({
|
|
344
|
+
doc: doc(p("foo<a>"), hr(), hr(), p("bar"))
|
|
345
|
+
});
|
|
346
|
+
act(()=>{
|
|
347
|
+
view.dispatchEvent(event(RIGHT));
|
|
348
|
+
});
|
|
349
|
+
expect(view.state.selection.from).toBe(5);
|
|
350
|
+
act(()=>{
|
|
351
|
+
view.dispatchEvent(event(RIGHT));
|
|
352
|
+
});
|
|
353
|
+
expect(view.state.selection.from).toBe(6);
|
|
354
|
+
act(()=>{
|
|
355
|
+
view.dispatchEvent(event(RIGHT));
|
|
356
|
+
});
|
|
357
|
+
expect(view.state.selection.head).toBe(8);
|
|
358
|
+
act(()=>{
|
|
359
|
+
view.dispatchEvent(event(LEFT));
|
|
360
|
+
});
|
|
361
|
+
expect(view.state.selection.from).toBe(6);
|
|
362
|
+
act(()=>{
|
|
363
|
+
view.dispatchEvent(event(LEFT));
|
|
364
|
+
});
|
|
365
|
+
expect(view.state.selection.from).toBe(5);
|
|
366
|
+
act(()=>{
|
|
367
|
+
view.dispatchEvent(event(LEFT));
|
|
368
|
+
});
|
|
369
|
+
expect(view.state.selection.head).toBe(4);
|
|
370
|
+
});
|
|
371
|
+
it("allows moving directly from an inline node to a block node", async ()=>{
|
|
372
|
+
const { view } = tempEditor({
|
|
373
|
+
doc: doc(p("foo", img), hr(), p(img, "bar"))
|
|
374
|
+
});
|
|
375
|
+
setSel(view, NodeSelection.create(view.state.doc, 4));
|
|
376
|
+
act(()=>{
|
|
377
|
+
view.dispatchEvent(event(DOWN));
|
|
378
|
+
});
|
|
379
|
+
expect(view.state.selection.from).toBe(6);
|
|
380
|
+
setSel(view, NodeSelection.create(view.state.doc, 8));
|
|
381
|
+
act(()=>{
|
|
382
|
+
view.dispatchEvent(event(UP));
|
|
383
|
+
});
|
|
384
|
+
expect(view.state.selection.from).toBe(6);
|
|
385
|
+
});
|
|
386
|
+
it("updates the selection even if the DOM parameters look unchanged", async ()=>{
|
|
387
|
+
const { view , rerender } = tempEditor({
|
|
388
|
+
doc: doc(p("foobar<a>"))
|
|
389
|
+
});
|
|
390
|
+
view.focus();
|
|
391
|
+
const decos = DecorationSet.create(view.state.doc, [
|
|
392
|
+
Decoration.inline(1, 4, {
|
|
393
|
+
color: "green"
|
|
394
|
+
})
|
|
395
|
+
]);
|
|
396
|
+
rerender({
|
|
397
|
+
decorations () {
|
|
398
|
+
return decos;
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
rerender({
|
|
402
|
+
decorations: undefined
|
|
403
|
+
});
|
|
404
|
+
rerender({
|
|
405
|
+
decorations () {
|
|
406
|
+
return decos;
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
const range = document.createRange();
|
|
410
|
+
range.setEnd(document.getSelection().anchorNode, document.getSelection().anchorOffset);
|
|
411
|
+
range.setStart(view.dom, 0);
|
|
412
|
+
expect(range.toString()).toBe("foobar");
|
|
413
|
+
});
|
|
414
|
+
it("sets selection even if Selection.extend throws DOMException", async ()=>{
|
|
415
|
+
const originalExtend = window.Selection.prototype.extend;
|
|
416
|
+
window.Selection.prototype.extend = ()=>{
|
|
417
|
+
// declare global: DOMException
|
|
418
|
+
throw new DOMException("failed");
|
|
419
|
+
};
|
|
420
|
+
try {
|
|
421
|
+
const { view } = tempEditor({
|
|
422
|
+
doc: doc(p("foo", img), hr(), p(img, "bar"))
|
|
423
|
+
});
|
|
424
|
+
setSel(view, NodeSelection.create(view.state.doc, 4));
|
|
425
|
+
act(()=>{
|
|
426
|
+
view.dispatchEvent(event(DOWN));
|
|
427
|
+
});
|
|
428
|
+
expect(view.state.selection.from).toBe(6);
|
|
429
|
+
} finally{
|
|
430
|
+
window.Selection.prototype.extend = originalExtend;
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
it("doesn't put the cursor after BR hack nodes", async ()=>{
|
|
434
|
+
const { view } = tempEditor({
|
|
435
|
+
doc: doc(p())
|
|
436
|
+
});
|
|
437
|
+
view.focus();
|
|
438
|
+
expect(getSelection().focusOffset).toBe(0);
|
|
439
|
+
});
|
|
440
|
+
});
|