Repository navigation
Expand file tree
/
Copy pathplugin-elements.tsx
More file actions
194 lines (193 loc) · 5.07 KB
/
Copy pathplugin-elements.tsx
File metadata and controls
194 lines (193 loc) · 5.07 KB
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
import { bilingualLocale } from "@doca/i18n";
import {
Component,
useEffect,
useMemo,
useState,
useSyncExternalStore,
type ReactNode,
} from "react";
import { Modal } from "antd";
import type {
JsonObject,
PluginElementFormat,
PluginElementPayload,
} from "@smartdoca/plugin-contracts";
import {
createPluginElementPayload,
pluginElementState,
type PluginElementContribution,
type PluginElementContext,
type PluginElementEditorContext,
} from "@smartdoca/web-plugin-registry";
import { webPluginRegistry } from "@web/plugins/registry.js";
import { useI18n } from "@web/shared/i18n.js";
import "./plugin-elements.css";
export function usePluginElements(format: PluginElementFormat) {
useSyncExternalStore(
webPluginRegistry.elements.subscribe,
webPluginRegistry.elements.snapshot,
);
return webPluginRegistry.elements
.list()
.filter((item) => item.formats.includes(format));
}
export function ElementPlaceholder({
type,
reason = "unsupported",
}: {
type?: string;
reason?: "unsupported" | "invalid" | "failed";
}) {
const { t } = useI18n();
return (
<span className="plugin-element-error" role="alert" title={type}>
{t(`editor.element.${reason}`)}
{type ? ` · ${type}` : ""}
</span>
);
}
export class ElementBoundary extends Component<
{ children: ReactNode; type?: string },
{ failed: boolean }
> {
state = { failed: false };
static getDerivedStateFromError() {
return { failed: true };
}
render() {
return this.state.failed ? (
<ElementPlaceholder type={this.props.type} reason="failed" />
) : (
this.props.children
);
}
}
function ElementEditor({
provider,
context,
}: {
provider: PluginElementContribution<ReactNode>;
context: PluginElementEditorContext;
}) {
return provider.renderEditor(context);
}
export function ElementContent({
provider,
payload,
context,
}: {
provider: PluginElementContribution<ReactNode>;
payload: PluginElementPayload;
context: PluginElementContext;
}) {
// Invoke in a child component so hooks and descendant failures stay contained.
return provider.render!(structuredClone(payload), context);
}
export interface ElementDialogSession {
readonly unsupported?: boolean;
readonly type?: string;
readonly initialData?: JsonObject;
commit(payload: PluginElementPayload): void;
remove?(): void;
}
export function PluginElementDialog({
documentId,
format,
session,
close,
}: {
documentId: string;
format: PluginElementFormat;
session: ElementDialogSession;
close(): void;
}) {
const { locale, t } = useI18n();
const providers = usePluginElements(format);
const [selected, setSelected] = useState(session.type ?? "");
const [error, setError] = useState(false);
const provider = providers.find((item) => item.id === selected);
const abort = useMemo(() => new AbortController(), [provider]);
useEffect(() => () => abort.abort(), [abort]);
const context: PluginElementEditorContext = {
documentId,
format,
locale: bilingualLocale(locale),
readOnly: false,
signal: abort.signal,
initialData: session.initialData
? structuredClone(session.initialData)
: null,
cancel: close,
submit(data) {
if (
abort.signal.aborted ||
!provider ||
webPluginRegistry.elements.get(provider.id) !== provider
)
return;
try {
session.commit(createPluginElementPayload(provider, data, bilingualLocale(locale)));
close();
} catch {
setError(true);
}
},
};
return (
<Modal
open
title={provider?.title[bilingualLocale(locale)] ?? t("editor.element.insert")}
footer={null}
destroyOnHidden
onCancel={close}
>
{format === "spreadsheet" && (
<p className="subtle">{t("editor.element.cellReplacement")}</p>
)}
{error && <p role="alert">{t("editor.element.invalid")}</p>}
{session.unsupported ? (
<ElementPlaceholder type={selected} />
) : !selected ? (
<div className="plugin-element-choices">
{providers.map((item) => (
<button
type="button"
key={item.id}
onClick={() => {
setSelected(item.id);
setError(false);
}}
>
{item.title[bilingualLocale(locale)]}
</button>
))}
{!providers.length && <p>{t("editor.element.empty")}</p>}
</div>
) : provider ? (
<ElementBoundary key={provider.id} type={provider.id}>
<ElementEditor provider={provider} context={context} />
</ElementBoundary>
) : (
<ElementPlaceholder type={selected} />
)}
{session.remove && (
<button
type="button"
className="plugin-element-remove"
onClick={() => {
try {
session.remove!();
close();
} catch {
setError(true);
}
}}
>
{t("editor.element.remove")}
</button>
)}
</Modal>
);
}
export { pluginElementState };