@emabuild/react 0.7.1 → 0.7.2

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.
@@ -19,9 +19,13 @@
19
19
  */
20
20
  import { type CSSProperties, type ReactNode } from 'react';
21
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 {
22
+ import { type FrameworkEventName, type FrameworkEventPayloads, type MailEditorElement } from '@emabuild/core';
23
+ import type { EmailDesign, MailEditorConfig, ExportOptions, ExportResult, PlainTextResult, DesignOperation, OperationResult, AIRunResult, AIContext, AIAction, EmailHtmlReport, ImportHtmlOptions, ImportHtmlResult } from '@emabuild/types';
24
+ /** `onReady`, `onDesignLoaded`, … — one optional callback per re-emitted editor event */
25
+ export type EmabuildEditorEventProps = {
26
+ [K in FrameworkEventName as `on${Capitalize<K>}`]?: (payload: FrameworkEventPayloads[K]) => void;
27
+ };
28
+ export interface EmabuildEditorProps extends EmabuildEditorEventProps {
25
29
  /** Editor options — re-applied whenever the object identity changes (memoize it) */
26
30
  options?: MailEditorConfig;
27
31
  /** URL of a JSON config file, deep-merged under `options` */
@@ -36,29 +40,6 @@ export interface EmabuildEditorProps {
36
40
  * `panel-bottom`, `canvas-bottom`) or an extra tab (`tab-<name>`, label in `data-tab-label`).
37
41
  */
38
42
  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
43
  }
63
44
  export interface EmabuildEditorHandle {
64
45
  /** The underlying element (full API) */
@@ -20,22 +20,9 @@ import { jsx as _jsx } from "react/jsx-runtime";
20
20
  */
21
21
  import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
22
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
- ];
23
+ import { connectFrameworkEvents } from '@emabuild/core';
24
+ /** Prop name of the callback for an event, e.g. `designLoaded` → `onDesignLoaded` */
25
+ const callbackProp = (name) => `on${name.charAt(0).toUpperCase()}${name.slice(1)}`;
39
26
  export const EmabuildEditor = forwardRef(function EmabuildEditor(props, ref) {
40
27
  const elRef = useRef(null);
41
28
  const propsRef = useRef(props);
@@ -46,23 +33,15 @@ export const EmabuildEditor = forwardRef(function EmabuildEditor(props, ref) {
46
33
  const el = elRef.current;
47
34
  if (!el)
48
35
  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];
36
+ return connectFrameworkEvents(el, (name, payload) => {
37
+ if (name === 'ready') {
38
+ ready.current = true;
39
+ if (propsRef.current.design)
40
+ el.loadDesign(propsRef.current.design);
41
+ }
42
+ // `name` and `payload` correlate, but TS cannot follow that through the prop lookup
43
+ propsRef.current[callbackProp(name)]?.(payload);
61
44
  });
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
45
  }, []);
67
46
  // Options are a property, not an attribute
68
47
  useEffect(() => {
@@ -77,22 +56,28 @@ export const EmabuildEditor = forwardRef(function EmabuildEditor(props, ref) {
77
56
  if (ready.current && props.design)
78
57
  elRef.current?.loadDesign(props.design);
79
58
  }, [props.design]);
59
+ /** The mounted element; handle methods that need it throw a clear error before mount */
60
+ const editor = () => {
61
+ const el = elRef.current;
62
+ if (!el)
63
+ throw new Error('EmabuildEditor is not mounted');
64
+ return el;
65
+ };
80
66
  useImperativeHandle(ref, () => ({
81
67
  get editor() { return elRef.current; },
82
- exportHtml: (o) => elRef.current.exportHtmlAsync(o),
83
- exportPlainText: (o) => elRef.current.exportPlainText(o),
68
+ exportHtml: (o) => editor().exportHtmlAsync(o),
69
+ exportPlainText: (o) => editor().exportPlainText(o),
84
70
  getDesign: () => elRef.current?.getDesign(),
85
71
  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),
72
+ applyOperations: (ops, options) => editor().applyOperations(ops, options),
73
+ reveal: (id) => editor().reveal(id),
74
+ runAI: (input) => editor().runAI(input),
75
+ checkHtml: (html, options) => editor().checkHtml(html, options),
76
+ importHtml: (html, options) => editor().importHtml(html, options),
77
+ showPreview: (device) => editor().showPreview(device),
92
78
  undo: () => elRef.current?.undo(),
93
79
  redo: () => elRef.current?.redo(),
94
80
  }), []);
95
81
  // `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
82
  return _jsx("mail-editor", { ref: elRef, className: props.className, style: props.style, children: props.children });
98
83
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emabuild/react",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "React wrapper component for the @emabuild/core email editor",
5
5
  "files": [
6
6
  "dist",
@@ -9,15 +9,15 @@
9
9
  ],
10
10
  "peerDependencies": {
11
11
  "react": ">=17",
12
- "@emabuild/core": "^0.8.0",
13
- "@emabuild/types": "^0.8.0"
12
+ "@emabuild/types": "^0.8.1",
13
+ "@emabuild/core": "^0.9.0"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@types/react": "^18.3.12",
17
17
  "react": "^18.3.1",
18
18
  "typescript": "^5.7.0",
19
- "@emabuild/types": "0.8.0",
20
- "@emabuild/core": "0.8.0"
19
+ "@emabuild/core": "0.9.0",
20
+ "@emabuild/types": "0.8.1"
21
21
  },
22
22
  "license": "MIT",
23
23
  "type": "module",