@emabuild/react 0.0.0-stage → 0.7.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/LICENSE +21 -0
- package/README.md +25 -2
- package/dist/EmabuildEditor.d.ts +95 -0
- package/dist/EmabuildEditor.js +98 -0
- package/package.json +42 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fabio Varrenti
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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,98 @@
|
|
|
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
|
+
// Already ready before the listeners were attached: same as the event
|
|
63
|
+
if (el.isReady)
|
|
64
|
+
handlers.find(([name]) => name === 'editor:ready')?.[1](new CustomEvent('editor:ready'));
|
|
65
|
+
return () => handlers.forEach(([name, fn]) => el.removeEventListener(name, fn));
|
|
66
|
+
}, []);
|
|
67
|
+
// Options are a property, not an attribute
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (elRef.current && props.options)
|
|
70
|
+
elRef.current.options = props.options;
|
|
71
|
+
}, [props.options]);
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (elRef.current && props.configUrl)
|
|
74
|
+
elRef.current.configUrl = props.configUrl;
|
|
75
|
+
}, [props.configUrl]);
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (ready.current && props.design)
|
|
78
|
+
elRef.current?.loadDesign(props.design);
|
|
79
|
+
}, [props.design]);
|
|
80
|
+
useImperativeHandle(ref, () => ({
|
|
81
|
+
get editor() { return elRef.current; },
|
|
82
|
+
exportHtml: (o) => elRef.current.exportHtmlAsync(o),
|
|
83
|
+
exportPlainText: (o) => elRef.current.exportPlainText(o),
|
|
84
|
+
getDesign: () => elRef.current?.getDesign(),
|
|
85
|
+
loadDesign: (d) => elRef.current?.loadDesign(d),
|
|
86
|
+
applyOperations: (ops, options) => elRef.current.applyOperations(ops, options),
|
|
87
|
+
reveal: (id) => elRef.current.reveal(id),
|
|
88
|
+
runAI: (input) => elRef.current.runAI(input),
|
|
89
|
+
checkHtml: (html, options) => elRef.current.checkHtml(html, options),
|
|
90
|
+
importHtml: (html, options) => elRef.current.importHtml(html, options),
|
|
91
|
+
showPreview: (device) => elRef.current.showPreview(device),
|
|
92
|
+
undo: () => elRef.current?.undo(),
|
|
93
|
+
redo: () => elRef.current?.redo(),
|
|
94
|
+
}), []);
|
|
95
|
+
// `ref` callback keeps working on React 17/18 (no native custom-element props support needed)
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- custom element ref/style typing differs across React 17/18/19
|
|
97
|
+
return _jsx("mail-editor", { ref: elRef, className: props.className, style: props.style, children: props.children });
|
|
98
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emabuild/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.7.1",
|
|
4
|
+
"description": "React wrapper component for the @emabuild/core email editor",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"README.md",
|
|
8
|
+
"LICENSE"
|
|
9
|
+
],
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"react": ">=17",
|
|
12
|
+
"@emabuild/core": "^0.8.0",
|
|
13
|
+
"@emabuild/types": "^0.8.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/react": "^18.3.12",
|
|
17
|
+
"react": "^18.3.1",
|
|
18
|
+
"typescript": "^5.7.0",
|
|
19
|
+
"@emabuild/types": "0.8.0",
|
|
20
|
+
"@emabuild/core": "0.8.0"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/EmabuildEditor.js",
|
|
25
|
+
"types": "./dist/EmabuildEditor.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/EmabuildEditor.d.ts",
|
|
29
|
+
"import": "./dist/EmabuildEditor.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/Fabiovarrenti/emabuild.git",
|
|
39
|
+
"directory": "wrappers/react"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc -p tsconfig.build.json",
|
|
43
|
+
"typecheck": "tsc --noEmit -p ."
|
|
44
|
+
}
|
|
6
45
|
}
|