-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathContextEditor.tsx
201 lines (183 loc) · 5.85 KB
/
ContextEditor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"use client";
import React, { useEffect, useMemo, useRef, useState } from "react";
import {
ProsemirrorAdapterProvider,
useNodeViewFactory,
usePluginViewFactory,
useWidgetViewFactory,
} from "@prosemirror-adapter/react";
import { Node } from "prosemirror-model";
import { EditorState, Plugin } from "prosemirror-state";
import { EditorView } from "prosemirror-view";
import { MenuBar } from "./components/MenuBar";
import { basePlugins } from "./plugins";
import { attributePanelKey } from "./plugins/attributePanel";
import { reactPropsKey } from "./plugins/reactProps";
import { baseSchema } from "./schemas";
import "prosemirror-view/style/prosemirror.css";
import "prosemirror-gapcursor/style/gapcursor.css";
// For math
import "@benrbray/prosemirror-math/dist/prosemirror-math.css";
import "katex/dist/katex.min.css";
import { cn } from "utils";
import { AttributePanelToggleWidget } from "./components/AttributePanelToggleWidget";
import SuggestPanel from "./components/SuggestPanel";
const MENU_BAR_ID = "context-editor-menu-container";
export interface ContextEditorProps {
placeholder?: string;
className?: string /* classname for the editor view */;
disabled?: boolean;
initialDoc?: object;
pubId: string /* id of the current pub whose field is being directly edited */;
pubTypeId: string /* id of the current pubType of the pub whose field is being directly edited */;
pubTypes: object /* pub types in given context */;
getPubs: (filter: string) => Promise<any[]>;
getPubById: (
id: string
) => {} | undefined /* function to get a pub, both for autocomplete, and for id? */;
onChange: (
state: any
) => void /* Function that passes up editorState so parent can handle onSave, etc */;
atomRenderingComponent: React.ComponentType<{
nodeProp: any;
}> /* A react component that is given the ContextAtom pubtype and renders it accordingly */;
hideMenu?: boolean;
upload: (fileName: string) => Promise<string | { error: string }>;
}
export interface PanelProps {
top: number;
left: number;
right: number;
bottom: number;
pos: number;
// isLink: boolean;
isOpen: boolean;
node?: Partial<Node>;
}
const initPanelProps: PanelProps = {
top: 0,
left: 0,
right: 0,
bottom: 0,
pos: 0,
node: undefined,
isOpen: false,
// isLink: false,
};
export interface SuggestProps {
isOpen: boolean;
selectedIndex: number;
items: any[];
filter: string;
}
const initSuggestProps: SuggestProps = {
isOpen: false,
selectedIndex: 0,
items: [],
filter: "",
};
export default function ContextEditor(props: ContextEditorProps) {
const memoEditor = useMemo(() => {
return <UnwrappedEditor {...props} />;
}, [props]);
return <ProsemirrorAdapterProvider>{memoEditor}</ProsemirrorAdapterProvider>;
}
function UnwrappedEditor(props: ContextEditorProps) {
const Renderer = useMemo(() => {
return () => {
const AtomRenderingComponent = props.atomRenderingComponent;
return <AtomRenderingComponent nodeProp={undefined} />;
};
}, [props.atomRenderingComponent]);
const ToggleWidget = useMemo(() => AttributePanelToggleWidget, []);
const nodeViewFactory = useNodeViewFactory();
const pluginViewFactory = usePluginViewFactory();
const widgetViewFactory = useWidgetViewFactory();
const viewHost = useRef<HTMLDivElement | null>(null);
const view = useRef<EditorView | null>(null);
const [panelPosition, setPanelPosition] = useState<PanelProps>(initPanelProps);
const [suggestData, setSuggestData] = useState<SuggestProps>(initSuggestProps);
/* This useEffect approach of making the props available to prosemirror */
/* plugins from: https://discuss.prosemirror.net/t/lightweight-react-integration-example/2680 */
useEffect(() => {
/* Initial Render */
const getToggleWidget = widgetViewFactory({
component: ToggleWidget,
as: "span",
});
const state = EditorState.create({
doc: props.initialDoc ? baseSchema.nodeFromJSON(props.initialDoc) : undefined,
schema: baseSchema,
plugins: [
...basePlugins({
schema: baseSchema,
props,
panelPosition,
setPanelPosition,
suggestData,
setSuggestData,
getToggleWidget,
pluginViewFactory,
}),
...(props.hideMenu
? []
: [
new Plugin({
view: pluginViewFactory({
component: () => <MenuBar upload={props.upload} />,
root: () => {
return document.getElementById(MENU_BAR_ID) as HTMLElement;
},
}),
}),
]),
],
});
if (viewHost.current) {
view.current = new EditorView(viewHost.current, {
state,
nodeViews: {
contextAtom: nodeViewFactory({
component: Renderer,
}),
},
handleDOMEvents: {
focus: () => {
/* Reset the panelProps when the editor is focused */
const { pos, ...props } = initPanelProps;
setPanelPosition(initPanelProps);
},
},
});
}
// applyDevTools(view.current);
return () => view.current?.destroy();
}, []);
useEffect(() => {
/* Every Render */
setTimeout(() => {
if (view.current) {
view.current.setProps({
editable: () => !props.disabled,
});
const tr = view.current.state.tr
.setMeta(reactPropsKey, { ...props, suggestData, setSuggestData })
.setMeta(attributePanelKey, { panelPosition, setPanelPosition });
view.current?.dispatch(tr);
}
}, 0);
/* It's not clear to me that any of the props need to trigger this to re-render. */
/* Doing so in some cases (onChange for the EditorDash) cause an infinite re-render loop */
/* Figure out what I actually need to render on, and then clean up any useMemo calls if necessary */
}, [props, suggestData, panelPosition]);
return (
<div
id="context-editor-container"
className={`relative isolate max-w-screen-sm ${props.disabled ? "disabled" : ""}`}
>
<div id={MENU_BAR_ID} className="sticky top-0 z-10"></div>
<div ref={viewHost} className={cn("font-serif", props.className)} />
<SuggestPanel {...suggestData} />
</div>
);
}