@emabuild/react 0.0.0-stage → 0.7.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/README.md +25 -2
- package/dist/EmabuildEditor.d.ts +95 -0
- package/dist/EmabuildEditor.js +94 -0
- package/package.json +36 -3
package/README.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @emabuild/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React (17+) wrapper for the `<mail-editor>` web component. Copy `EmabuildEditor.tsx` into your app.
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
import { useMemo, useRef } from 'react';
|
|
7
|
+
import { EmabuildEditor, type EmabuildEditorHandle } from './EmabuildEditor';
|
|
8
|
+
|
|
9
|
+
export function Newsletter({ design }) {
|
|
10
|
+
const ref = useRef<EmabuildEditorHandle>(null);
|
|
11
|
+
const options = useMemo(() => ({ locale: 'it', disabledTools: ['form'] }), []); // memoize!
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<>
|
|
15
|
+
<EmabuildEditor ref={ref} options={options} design={design}
|
|
16
|
+
onDesignUpdated={() => console.log('dirty')} style={{ height: '100vh' }} />
|
|
17
|
+
<button onClick={async () => console.log((await ref.current!.exportHtml({ inlineStyles: true })).html)}>
|
|
18
|
+
Export
|
|
19
|
+
</button>
|
|
20
|
+
</>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Props: `options`, `design`, `className`, `style`, `onReady`, `onDesignLoaded`, `onDesignUpdated`, `onSelectionChanged`, `onBlockSaved`, `onBlockRemoved`, `onAiStart`, `onAiEvent`, `onAiDone`, `onAiError`.
|
|
26
|
+
Ref: `exportHtml`, `exportPlainText`, `getDesign`, `loadDesign`, `applyOperations`, `runAI`, `showPreview`, `undo`, `redo`, `editor`.
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React wrapper for the <mail-editor> web component (@emabuild/core).
|
|
3
|
+
*
|
|
4
|
+
* Copy this file into your app (React 17+), or import it from
|
|
5
|
+
* `@emabuild/react` once published.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const ref = useRef<EmabuildEditorHandle>(null);
|
|
10
|
+
* <EmabuildEditor
|
|
11
|
+
* ref={ref}
|
|
12
|
+
* options={{ locale: 'it', disabledTools: ['form'] }}
|
|
13
|
+
* design={design}
|
|
14
|
+
* onDesignUpdated={() => setDirty(true)}
|
|
15
|
+
* style={{ height: '100vh' }}
|
|
16
|
+
* />
|
|
17
|
+
* const { html } = await ref.current!.exportHtml({ inlineStyles: true });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { type CSSProperties, type ReactNode } from 'react';
|
|
21
|
+
import '@emabuild/core';
|
|
22
|
+
import type { MailEditorElement } from '@emabuild/core';
|
|
23
|
+
import type { EmailDesign, MailEditorConfig, ExportOptions, ExportResult, PlainTextResult, DesignOperation, OperationResult, EditorSelection, SavedBlock, AIRunResult, AIStreamEvent, AIContext, AIAction, DesignUpdatedEvent, EmailHtmlReport, ImportHtmlOptions, ImportHtmlResult, AIUsage } from '@emabuild/types';
|
|
24
|
+
export interface EmabuildEditorProps {
|
|
25
|
+
/** Editor options — re-applied whenever the object identity changes (memoize it) */
|
|
26
|
+
options?: MailEditorConfig;
|
|
27
|
+
/** URL of a JSON config file, deep-merged under `options` */
|
|
28
|
+
configUrl?: string;
|
|
29
|
+
/** Design loaded on ready and whenever the object identity changes */
|
|
30
|
+
design?: EmailDesign;
|
|
31
|
+
className?: string;
|
|
32
|
+
style?: CSSProperties;
|
|
33
|
+
/**
|
|
34
|
+
* Your own elements inside the editor: give each a `slot` — a predefined area
|
|
35
|
+
* (`toolbar-start`, `toolbar-end`, `sidebar-top`, `sidebar-bottom`, `panel-top`,
|
|
36
|
+
* `panel-bottom`, `canvas-bottom`) or an extra tab (`tab-<name>`, label in `data-tab-label`).
|
|
37
|
+
*/
|
|
38
|
+
children?: ReactNode;
|
|
39
|
+
onReady?: () => void;
|
|
40
|
+
onDesignLoaded?: (detail: {
|
|
41
|
+
design: EmailDesign;
|
|
42
|
+
}) => void;
|
|
43
|
+
onDesignUpdated?: (detail: DesignUpdatedEvent) => void;
|
|
44
|
+
onSelectionChanged?: (selection: EditorSelection | null) => void;
|
|
45
|
+
onBlockSaved?: (block: SavedBlock) => void;
|
|
46
|
+
onBlockRemoved?: (blockId: string) => void;
|
|
47
|
+
onAiStart?: (detail: unknown) => void;
|
|
48
|
+
onAiEvent?: (event: AIStreamEvent) => void;
|
|
49
|
+
onAiDone?: (result: AIRunResult) => void;
|
|
50
|
+
onAiError?: (result: AIRunResult) => void;
|
|
51
|
+
onAiUsage?: (usage: AIUsage) => void;
|
|
52
|
+
onConfigLoaded?: (detail: {
|
|
53
|
+
config: MailEditorConfig;
|
|
54
|
+
issues: Array<{
|
|
55
|
+
path: string;
|
|
56
|
+
message: string;
|
|
57
|
+
}>;
|
|
58
|
+
}) => void;
|
|
59
|
+
onConfigError?: (detail: {
|
|
60
|
+
error: string;
|
|
61
|
+
}) => void;
|
|
62
|
+
}
|
|
63
|
+
export interface EmabuildEditorHandle {
|
|
64
|
+
/** The underlying element (full API) */
|
|
65
|
+
editor: MailEditorElement | null;
|
|
66
|
+
exportHtml(options?: ExportOptions): Promise<ExportResult>;
|
|
67
|
+
exportPlainText(options?: ExportOptions): Promise<PlainTextResult>;
|
|
68
|
+
getDesign(): EmailDesign | undefined;
|
|
69
|
+
loadDesign(design: EmailDesign): void;
|
|
70
|
+
applyOperations(ops: DesignOperation[], options?: {
|
|
71
|
+
reveal?: boolean;
|
|
72
|
+
}): Promise<OperationResult[]>;
|
|
73
|
+
reveal(id: string): Promise<boolean>;
|
|
74
|
+
runAI(input: {
|
|
75
|
+
prompt: string;
|
|
76
|
+
action?: AIAction;
|
|
77
|
+
context?: AIContext;
|
|
78
|
+
useSelection?: boolean;
|
|
79
|
+
}): Promise<AIRunResult>;
|
|
80
|
+
checkHtml(html: string, options?: {
|
|
81
|
+
baseUrl?: string;
|
|
82
|
+
}): Promise<EmailHtmlReport>;
|
|
83
|
+
importHtml(html: string, options?: ImportHtmlOptions): Promise<ImportHtmlResult>;
|
|
84
|
+
showPreview(device?: string): Promise<void>;
|
|
85
|
+
undo(): void;
|
|
86
|
+
redo(): void;
|
|
87
|
+
}
|
|
88
|
+
export declare const EmabuildEditor: import("react").ForwardRefExoticComponent<EmabuildEditorProps & import("react").RefAttributes<EmabuildEditorHandle>>;
|
|
89
|
+
declare global {
|
|
90
|
+
namespace JSX {
|
|
91
|
+
interface IntrinsicElements {
|
|
92
|
+
'mail-editor': Record<string, unknown>;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* React wrapper for the <mail-editor> web component (@emabuild/core).
|
|
4
|
+
*
|
|
5
|
+
* Copy this file into your app (React 17+), or import it from
|
|
6
|
+
* `@emabuild/react` once published.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* const ref = useRef<EmabuildEditorHandle>(null);
|
|
11
|
+
* <EmabuildEditor
|
|
12
|
+
* ref={ref}
|
|
13
|
+
* options={{ locale: 'it', disabledTools: ['form'] }}
|
|
14
|
+
* design={design}
|
|
15
|
+
* onDesignUpdated={() => setDirty(true)}
|
|
16
|
+
* style={{ height: '100vh' }}
|
|
17
|
+
* />
|
|
18
|
+
* const { html } = await ref.current!.exportHtml({ inlineStyles: true });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
|
|
22
|
+
import '@emabuild/core';
|
|
23
|
+
/** DOM event → prop name, plus how to unwrap the CustomEvent detail */
|
|
24
|
+
const EVENTS = [
|
|
25
|
+
['editor:ready', 'onReady', () => undefined],
|
|
26
|
+
['design:loaded', 'onDesignLoaded', (d) => d],
|
|
27
|
+
['design:updated', 'onDesignUpdated', (d) => d],
|
|
28
|
+
['selection:changed', 'onSelectionChanged', (d) => d?.selection ?? null],
|
|
29
|
+
['block:saved', 'onBlockSaved', (d) => d?.block],
|
|
30
|
+
['block:removed', 'onBlockRemoved', (d) => d?.blockId],
|
|
31
|
+
['ai:start', 'onAiStart', (d) => d],
|
|
32
|
+
['ai:event', 'onAiEvent', (d) => d],
|
|
33
|
+
['ai:done', 'onAiDone', (d) => d],
|
|
34
|
+
['ai:error', 'onAiError', (d) => d],
|
|
35
|
+
['ai:usage', 'onAiUsage', (d) => d],
|
|
36
|
+
['config:loaded', 'onConfigLoaded', (d) => d],
|
|
37
|
+
['config:error', 'onConfigError', (d) => d],
|
|
38
|
+
];
|
|
39
|
+
export const EmabuildEditor = forwardRef(function EmabuildEditor(props, ref) {
|
|
40
|
+
const elRef = useRef(null);
|
|
41
|
+
const propsRef = useRef(props);
|
|
42
|
+
propsRef.current = props;
|
|
43
|
+
const ready = useRef(false);
|
|
44
|
+
// Events: attach once, always call the latest callbacks
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
const el = elRef.current;
|
|
47
|
+
if (!el)
|
|
48
|
+
return;
|
|
49
|
+
const handlers = EVENTS.map(([name, prop, unwrap]) => {
|
|
50
|
+
const fn = (e) => {
|
|
51
|
+
if (name === 'editor:ready') {
|
|
52
|
+
ready.current = true;
|
|
53
|
+
if (propsRef.current.design)
|
|
54
|
+
el.loadDesign(propsRef.current.design);
|
|
55
|
+
}
|
|
56
|
+
const cb = propsRef.current[prop];
|
|
57
|
+
cb?.(unwrap(e.detail));
|
|
58
|
+
};
|
|
59
|
+
el.addEventListener(name, fn);
|
|
60
|
+
return [name, fn];
|
|
61
|
+
});
|
|
62
|
+
return () => handlers.forEach(([name, fn]) => el.removeEventListener(name, fn));
|
|
63
|
+
}, []);
|
|
64
|
+
// Options are a property, not an attribute
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (elRef.current && props.options)
|
|
67
|
+
elRef.current.options = props.options;
|
|
68
|
+
}, [props.options]);
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (elRef.current && props.configUrl)
|
|
71
|
+
elRef.current.configUrl = props.configUrl;
|
|
72
|
+
}, [props.configUrl]);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (ready.current && props.design)
|
|
75
|
+
elRef.current?.loadDesign(props.design);
|
|
76
|
+
}, [props.design]);
|
|
77
|
+
useImperativeHandle(ref, () => ({
|
|
78
|
+
get editor() { return elRef.current; },
|
|
79
|
+
exportHtml: (o) => elRef.current.exportHtmlAsync(o),
|
|
80
|
+
exportPlainText: (o) => elRef.current.exportPlainText(o),
|
|
81
|
+
getDesign: () => elRef.current?.getDesign(),
|
|
82
|
+
loadDesign: (d) => elRef.current?.loadDesign(d),
|
|
83
|
+
applyOperations: (ops, options) => elRef.current.applyOperations(ops, options),
|
|
84
|
+
reveal: (id) => elRef.current.reveal(id),
|
|
85
|
+
runAI: (input) => elRef.current.runAI(input),
|
|
86
|
+
checkHtml: (html, options) => elRef.current.checkHtml(html, options),
|
|
87
|
+
importHtml: (html, options) => elRef.current.importHtml(html, options),
|
|
88
|
+
showPreview: (device) => elRef.current.showPreview(device),
|
|
89
|
+
undo: () => elRef.current?.undo(),
|
|
90
|
+
redo: () => elRef.current?.redo(),
|
|
91
|
+
}), []);
|
|
92
|
+
// `ref` callback keeps working on React 17/18 (no native custom-element props support needed)
|
|
93
|
+
return _jsx("mail-editor", { ref: elRef, className: props.className, style: props.style, children: props.children });
|
|
94
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emabuild/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "React wrapper component for the @emabuild/core email editor",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"README.md"
|
|
8
|
+
],
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"react": ">=17",
|
|
11
|
+
"@emabuild/core": "^0.7.0",
|
|
12
|
+
"@emabuild/types": "^0.7.0"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"react": "^18.3.1",
|
|
16
|
+
"@types/react": "^18.3.12",
|
|
17
|
+
"typescript": "^5.7.0",
|
|
18
|
+
"@emabuild/core": "0.7.0",
|
|
19
|
+
"@emabuild/types": "0.7.0"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/EmabuildEditor.js",
|
|
24
|
+
"types": "./dist/EmabuildEditor.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/EmabuildEditor.d.ts",
|
|
28
|
+
"import": "./dist/EmabuildEditor.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -p tsconfig.build.json",
|
|
37
|
+
"typecheck": "tsc --noEmit -p ."
|
|
38
|
+
}
|
|
6
39
|
}
|