@react-email/editor 0.0.0-experimental.8 → 1.0.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/dist/columns-BItFgD76.mjs +273 -0
- package/dist/columns-BItFgD76.mjs.map +1 -0
- package/dist/columns-DqFvXgmK.cjs +394 -0
- package/dist/core/index.cjs +10 -0
- package/dist/core/index.d.cts +2 -0
- package/dist/core/index.d.mts +2 -0
- package/dist/core/index.mjs +4 -0
- package/dist/core-CbSTyrV4.mjs +151 -0
- package/dist/core-CbSTyrV4.mjs.map +1 -0
- package/dist/core-D7AzzMLI.cjs +167 -0
- package/dist/email-node-B-_g4X-S.mjs +254 -0
- package/dist/email-node-B-_g4X-S.mjs.map +1 -0
- package/dist/email-node-hLU5bGnX.cjs +288 -0
- package/dist/event-bus-CIJ___tH.cjs +65 -0
- package/dist/event-bus-CqrIUE24.mjs +55 -0
- package/dist/event-bus-CqrIUE24.mjs.map +1 -0
- package/dist/extension-BbjpP9mF.cjs +1625 -0
- package/dist/extension-C5qNDPZL.mjs +154 -0
- package/dist/extension-C5qNDPZL.mjs.map +1 -0
- package/dist/extension-Cs4Yq2gF.cjs +158 -0
- package/dist/extension-DlWknmE5.mjs +1495 -0
- package/dist/extension-DlWknmE5.mjs.map +1 -0
- package/dist/extensions/index.cjs +50 -0
- package/dist/extensions/index.d.cts +392 -0
- package/dist/extensions/index.d.cts.map +1 -0
- package/dist/extensions/index.d.mts +392 -0
- package/dist/extensions/index.d.mts.map +1 -0
- package/dist/extensions/index.mjs +4 -0
- package/dist/extensions-CHIQO3d-.mjs +1745 -0
- package/dist/extensions-CHIQO3d-.mjs.map +1 -0
- package/dist/extensions-DCrDvZ42.cjs +1981 -0
- package/dist/global-content-0-J-Te3N.mjs +77 -0
- package/dist/global-content-0-J-Te3N.mjs.map +1 -0
- package/dist/global-content-8M2KyrOL.cjs +87 -0
- package/dist/image-KwEYzhji.cjs +41 -0
- package/dist/image-Oc789YjV.mjs +37 -0
- package/dist/image-Oc789YjV.mjs.map +1 -0
- package/dist/index-CHAPu71g.d.cts +193 -0
- package/dist/index-CHAPu71g.d.cts.map +1 -0
- package/dist/index-D4UEWmJX.d.mts +193 -0
- package/dist/index-D4UEWmJX.d.mts.map +1 -0
- package/dist/index.cjs +216 -0
- package/dist/index.d.cts +38 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +33 -623
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +189 -2349
- package/dist/index.mjs.map +1 -1
- package/dist/plugins/index.cjs +56 -0
- package/dist/plugins/index.d.cts +114 -0
- package/dist/plugins/index.d.cts.map +1 -0
- package/dist/plugins/index.d.mts +114 -0
- package/dist/plugins/index.d.mts.map +1 -0
- package/dist/plugins/index.mjs +34 -0
- package/dist/plugins/index.mjs.map +1 -0
- package/dist/root-CT5d4CKj.cjs +2798 -0
- package/dist/root-uvdshbyT.mjs +2295 -0
- package/dist/root-uvdshbyT.mjs.map +1 -0
- package/dist/set-text-alignment-BCRnE5So.mjs +19 -0
- package/dist/set-text-alignment-BCRnE5So.mjs.map +1 -0
- package/dist/set-text-alignment-Twg1EG4-.cjs +22 -0
- package/dist/style.css +974 -0
- package/dist/types-BG5XsXt9.d.mts +37 -0
- package/dist/types-BG5XsXt9.d.mts.map +1 -0
- package/dist/types-DnOcnDoO.d.mts +56 -0
- package/dist/types-DnOcnDoO.d.mts.map +1 -0
- package/dist/types-DzPTbbDC.d.cts +56 -0
- package/dist/types-DzPTbbDC.d.cts.map +1 -0
- package/dist/types-hURXpEIO.d.cts +37 -0
- package/dist/types-hURXpEIO.d.cts.map +1 -0
- package/dist/ui/bubble-menu/bubble-menu.css +291 -0
- package/dist/ui/index.cjs +2899 -0
- package/dist/ui/index.d.cts +1155 -0
- package/dist/ui/index.d.cts.map +1 -0
- package/dist/ui/index.d.mts +1157 -0
- package/dist/ui/index.d.mts.map +1 -0
- package/dist/ui/index.mjs +2788 -0
- package/dist/ui/index.mjs.map +1 -0
- package/dist/ui/inspector/inspector.css +293 -0
- package/dist/ui/slash-command/slash-command.css +44 -0
- package/dist/ui/themes/default.css +1140 -0
- package/dist/utils/index.cjs +3 -0
- package/dist/utils/index.d.cts +7 -0
- package/dist/utils/index.d.cts.map +1 -0
- package/dist/utils/index.d.mts +7 -0
- package/dist/utils/index.d.mts.map +1 -0
- package/dist/utils/index.mjs +2 -0
- package/package.json +100 -16
- package/readme.md +69 -0
- package/dist/index.d.ts +0 -628
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2461
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
require("./columns-DqFvXgmK.cjs");
|
|
2
|
+
let _tiptap_core = require("@tiptap/core");
|
|
3
|
+
//#region src/utils/styles.ts
|
|
4
|
+
const WHITE_SPACE_REGEX = /\s+/;
|
|
5
|
+
const BORDER_WIDTH_TO_STYLE = {
|
|
6
|
+
borderWidth: "borderStyle",
|
|
7
|
+
borderTopWidth: "borderTopStyle",
|
|
8
|
+
borderRightWidth: "borderRightStyle",
|
|
9
|
+
borderBottomWidth: "borderBottomStyle",
|
|
10
|
+
borderLeftWidth: "borderLeftStyle"
|
|
11
|
+
};
|
|
12
|
+
const jsToInlineCss = (styleObject) => {
|
|
13
|
+
const parts = [];
|
|
14
|
+
for (const key in styleObject) {
|
|
15
|
+
const value = styleObject[key];
|
|
16
|
+
if (value !== 0 && value !== void 0 && value !== null && value !== "") {
|
|
17
|
+
const formattedKey = key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
18
|
+
parts.push(`${formattedKey}:${value}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return parts.join(";") + (parts.length ? ";" : "");
|
|
22
|
+
};
|
|
23
|
+
const splitDeclarations = (input) => {
|
|
24
|
+
const results = [];
|
|
25
|
+
let current = "";
|
|
26
|
+
let depth = 0;
|
|
27
|
+
for (const char of input) {
|
|
28
|
+
if (char === "(") depth++;
|
|
29
|
+
if (char === ")") depth--;
|
|
30
|
+
if (char === ";" && depth === 0) {
|
|
31
|
+
results.push(current);
|
|
32
|
+
current = "";
|
|
33
|
+
} else current += char;
|
|
34
|
+
}
|
|
35
|
+
if (current) results.push(current);
|
|
36
|
+
return results;
|
|
37
|
+
};
|
|
38
|
+
const inlineCssToJs = (inlineStyle, options = {}) => {
|
|
39
|
+
const styleObject = {};
|
|
40
|
+
if (!inlineStyle || inlineStyle === "" || typeof inlineStyle === "object") return styleObject;
|
|
41
|
+
splitDeclarations(inlineStyle).forEach((style) => {
|
|
42
|
+
const trimmed = style.trim();
|
|
43
|
+
if (trimmed) {
|
|
44
|
+
const separatorIndex = trimmed.indexOf(":");
|
|
45
|
+
if (separatorIndex === -1) return;
|
|
46
|
+
const key = trimmed.slice(0, separatorIndex);
|
|
47
|
+
const valueTrimmed = trimmed.slice(separatorIndex + 1).trim();
|
|
48
|
+
if (!valueTrimmed) return;
|
|
49
|
+
const formattedKey = key.trim().replace(/-\w/g, (match) => match[1].toUpperCase());
|
|
50
|
+
styleObject[formattedKey] = options?.removeUnit ? valueTrimmed.replace(/px|%/g, "") : valueTrimmed;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return styleObject;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Expands CSS shorthand properties (margin, padding) into their longhand equivalents.
|
|
57
|
+
* This prevents shorthand properties from overriding specific longhand properties in email clients.
|
|
58
|
+
*
|
|
59
|
+
* @param styles - Style object that may contain shorthand properties
|
|
60
|
+
* @returns New style object with shorthand properties expanded to longhand
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* expandShorthandProperties({ margin: '0', paddingTop: '10px' })
|
|
64
|
+
* // Returns: { marginTop: '0', marginRight: '0', marginBottom: '0', marginLeft: '0', paddingTop: '10px' }
|
|
65
|
+
*/
|
|
66
|
+
function expandShorthandProperties(styles) {
|
|
67
|
+
if (!styles || typeof styles !== "object") return {};
|
|
68
|
+
const expanded = {};
|
|
69
|
+
for (const key in styles) {
|
|
70
|
+
const value = styles[key];
|
|
71
|
+
if (value === void 0 || value === null || value === "") continue;
|
|
72
|
+
switch (key) {
|
|
73
|
+
case "margin": {
|
|
74
|
+
const values = parseShorthandValue(value);
|
|
75
|
+
expanded.marginTop = values.top;
|
|
76
|
+
expanded.marginRight = values.right;
|
|
77
|
+
expanded.marginBottom = values.bottom;
|
|
78
|
+
expanded.marginLeft = values.left;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case "padding": {
|
|
82
|
+
const values = parseShorthandValue(value);
|
|
83
|
+
expanded.paddingTop = values.top;
|
|
84
|
+
expanded.paddingRight = values.right;
|
|
85
|
+
expanded.paddingBottom = values.bottom;
|
|
86
|
+
expanded.paddingLeft = values.left;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case "border": {
|
|
90
|
+
const values = convertBorderValue(value);
|
|
91
|
+
expanded.borderStyle = values.style;
|
|
92
|
+
expanded.borderWidth = values.width;
|
|
93
|
+
expanded.borderColor = values.color;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
case "borderTopLeftRadius":
|
|
97
|
+
case "borderTopRightRadius":
|
|
98
|
+
case "borderBottomLeftRadius":
|
|
99
|
+
case "borderBottomRightRadius":
|
|
100
|
+
expanded[key] = value;
|
|
101
|
+
if (styles.borderTopLeftRadius && styles.borderTopRightRadius && styles.borderBottomLeftRadius && styles.borderBottomRightRadius) {
|
|
102
|
+
const values = [
|
|
103
|
+
styles.borderTopLeftRadius,
|
|
104
|
+
styles.borderTopRightRadius,
|
|
105
|
+
styles.borderBottomLeftRadius,
|
|
106
|
+
styles.borderBottomRightRadius
|
|
107
|
+
];
|
|
108
|
+
if (new Set(values).size === 1) expanded.borderRadius = values[0];
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
default: expanded[key] = value;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return expanded;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Parses CSS shorthand value (1-4 values) into individual side values.
|
|
118
|
+
* Follows CSS specification for shorthand property value parsing.
|
|
119
|
+
*
|
|
120
|
+
* @param value - Shorthand value string (e.g., '0', '10px 20px', '5px 10px 15px 20px')
|
|
121
|
+
* @returns Object with top, right, bottom, left values
|
|
122
|
+
*/
|
|
123
|
+
function parseShorthandValue(value) {
|
|
124
|
+
const stringValue = String(value).trim();
|
|
125
|
+
const parts = stringValue.split(WHITE_SPACE_REGEX);
|
|
126
|
+
const len = parts.length;
|
|
127
|
+
if (len === 1) return {
|
|
128
|
+
top: parts[0],
|
|
129
|
+
right: parts[0],
|
|
130
|
+
bottom: parts[0],
|
|
131
|
+
left: parts[0]
|
|
132
|
+
};
|
|
133
|
+
if (len === 2) return {
|
|
134
|
+
top: parts[0],
|
|
135
|
+
right: parts[1],
|
|
136
|
+
bottom: parts[0],
|
|
137
|
+
left: parts[1]
|
|
138
|
+
};
|
|
139
|
+
if (len === 3) return {
|
|
140
|
+
top: parts[0],
|
|
141
|
+
right: parts[1],
|
|
142
|
+
bottom: parts[2],
|
|
143
|
+
left: parts[1]
|
|
144
|
+
};
|
|
145
|
+
if (len === 4) return {
|
|
146
|
+
top: parts[0],
|
|
147
|
+
right: parts[1],
|
|
148
|
+
bottom: parts[2],
|
|
149
|
+
left: parts[3]
|
|
150
|
+
};
|
|
151
|
+
return {
|
|
152
|
+
top: stringValue,
|
|
153
|
+
right: stringValue,
|
|
154
|
+
bottom: stringValue,
|
|
155
|
+
left: stringValue
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function convertBorderValue(value) {
|
|
159
|
+
const stringValue = String(value).trim();
|
|
160
|
+
const parts = stringValue.split(WHITE_SPACE_REGEX);
|
|
161
|
+
switch (parts.length) {
|
|
162
|
+
case 1: return {
|
|
163
|
+
style: "solid",
|
|
164
|
+
width: parts[0],
|
|
165
|
+
color: "black"
|
|
166
|
+
};
|
|
167
|
+
case 2: return {
|
|
168
|
+
style: parts[1],
|
|
169
|
+
width: parts[0],
|
|
170
|
+
color: "black"
|
|
171
|
+
};
|
|
172
|
+
case 3: return {
|
|
173
|
+
style: parts[1],
|
|
174
|
+
width: parts[0],
|
|
175
|
+
color: parts[2]
|
|
176
|
+
};
|
|
177
|
+
case 4: return {
|
|
178
|
+
style: parts[1],
|
|
179
|
+
width: parts[0],
|
|
180
|
+
color: parts[2]
|
|
181
|
+
};
|
|
182
|
+
default: return {
|
|
183
|
+
style: "solid",
|
|
184
|
+
width: stringValue,
|
|
185
|
+
color: "black"
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* When a border-width is present but border-style is missing, browsers default
|
|
191
|
+
* to `none` and the border is invisible. This adds `solid` as a sensible
|
|
192
|
+
* fallback so borders show up immediately after setting a width.
|
|
193
|
+
*/
|
|
194
|
+
function ensureBorderStyleFallback(styles) {
|
|
195
|
+
for (const [widthKey, styleKey] of Object.entries(BORDER_WIDTH_TO_STYLE)) {
|
|
196
|
+
const widthValue = styles[widthKey];
|
|
197
|
+
if (!widthValue || widthValue === "0" || widthValue === "0px") continue;
|
|
198
|
+
if (!styles[styleKey]) {
|
|
199
|
+
if (styleKey !== "borderStyle" && styles.borderStyle) continue;
|
|
200
|
+
styles[styleKey] = "solid";
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return styles;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Resolves conflicts between reset styles and inline styles by expanding
|
|
207
|
+
* shorthand properties (margin, padding) to longhand before merging.
|
|
208
|
+
* This prevents shorthand properties from overriding specific longhand properties.
|
|
209
|
+
*
|
|
210
|
+
* @param resetStyles - Base reset styles that may contain shorthand properties
|
|
211
|
+
* @param inlineStyles - Inline styles that should override reset styles
|
|
212
|
+
* @returns Merged styles with inline styles taking precedence
|
|
213
|
+
*/
|
|
214
|
+
function resolveConflictingStyles(resetStyles, inlineStyles) {
|
|
215
|
+
const expandedResetStyles = expandShorthandProperties(resetStyles);
|
|
216
|
+
const expandedInlineStyles = expandShorthandProperties(inlineStyles);
|
|
217
|
+
return {
|
|
218
|
+
...expandedResetStyles,
|
|
219
|
+
...expandedInlineStyles
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/core/serializer/email-node.ts
|
|
224
|
+
var EmailNode = class EmailNode extends _tiptap_core.Node {
|
|
225
|
+
constructor(config) {
|
|
226
|
+
super(config);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Create a new Node instance
|
|
230
|
+
* @param config - Node configuration object or a function that returns a configuration object
|
|
231
|
+
*/
|
|
232
|
+
static create(config) {
|
|
233
|
+
return new EmailNode(typeof config === "function" ? config() : config);
|
|
234
|
+
}
|
|
235
|
+
static from(node, renderToReactEmail) {
|
|
236
|
+
const customNode = EmailNode.create({});
|
|
237
|
+
Object.assign(customNode, { ...node });
|
|
238
|
+
customNode.config = {
|
|
239
|
+
...node.config,
|
|
240
|
+
renderToReactEmail
|
|
241
|
+
};
|
|
242
|
+
return customNode;
|
|
243
|
+
}
|
|
244
|
+
configure(options) {
|
|
245
|
+
return super.configure(options);
|
|
246
|
+
}
|
|
247
|
+
extend(extendedConfig) {
|
|
248
|
+
const resolvedConfig = typeof extendedConfig === "function" ? extendedConfig() : extendedConfig;
|
|
249
|
+
return super.extend(resolvedConfig);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
//#endregion
|
|
253
|
+
Object.defineProperty(exports, "EmailNode", {
|
|
254
|
+
enumerable: true,
|
|
255
|
+
get: function() {
|
|
256
|
+
return EmailNode;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
Object.defineProperty(exports, "ensureBorderStyleFallback", {
|
|
260
|
+
enumerable: true,
|
|
261
|
+
get: function() {
|
|
262
|
+
return ensureBorderStyleFallback;
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
Object.defineProperty(exports, "expandShorthandProperties", {
|
|
266
|
+
enumerable: true,
|
|
267
|
+
get: function() {
|
|
268
|
+
return expandShorthandProperties;
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
Object.defineProperty(exports, "inlineCssToJs", {
|
|
272
|
+
enumerable: true,
|
|
273
|
+
get: function() {
|
|
274
|
+
return inlineCssToJs;
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
Object.defineProperty(exports, "jsToInlineCss", {
|
|
278
|
+
enumerable: true,
|
|
279
|
+
get: function() {
|
|
280
|
+
return jsToInlineCss;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
Object.defineProperty(exports, "resolveConflictingStyles", {
|
|
284
|
+
enumerable: true,
|
|
285
|
+
get: function() {
|
|
286
|
+
return resolveConflictingStyles;
|
|
287
|
+
}
|
|
288
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require("./columns-DqFvXgmK.cjs");
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/core/event-bus.ts
|
|
4
|
+
const EVENT_PREFIX = "@react-email/editor:";
|
|
5
|
+
var EditorEventBus = class {
|
|
6
|
+
prefixEventName(eventName) {
|
|
7
|
+
return `${EVENT_PREFIX}${String(eventName)}`;
|
|
8
|
+
}
|
|
9
|
+
dispatch(eventName, payload, options) {
|
|
10
|
+
const target = options?.target ?? window;
|
|
11
|
+
const prefixedEventName = this.prefixEventName(eventName);
|
|
12
|
+
const event = new CustomEvent(prefixedEventName, {
|
|
13
|
+
detail: payload,
|
|
14
|
+
bubbles: false,
|
|
15
|
+
cancelable: false
|
|
16
|
+
});
|
|
17
|
+
target.dispatchEvent(event);
|
|
18
|
+
}
|
|
19
|
+
on(eventName, handler, options) {
|
|
20
|
+
const target = options?.target ?? window;
|
|
21
|
+
const prefixedEventName = this.prefixEventName(eventName);
|
|
22
|
+
const abortController = new AbortController();
|
|
23
|
+
const wrappedHandler = (event) => {
|
|
24
|
+
const customEvent = event;
|
|
25
|
+
const result = handler(customEvent.detail);
|
|
26
|
+
if (result instanceof Promise) result.catch((error) => {
|
|
27
|
+
console.error(`Error in async event handler for ${prefixedEventName}:`, {
|
|
28
|
+
event: customEvent.detail,
|
|
29
|
+
error
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
target.addEventListener(prefixedEventName, wrappedHandler, {
|
|
34
|
+
...options,
|
|
35
|
+
signal: abortController.signal
|
|
36
|
+
});
|
|
37
|
+
return { unsubscribe: () => {
|
|
38
|
+
abortController.abort();
|
|
39
|
+
} };
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const editorEventBus = new EditorEventBus();
|
|
43
|
+
function useEditorEvent(eventName, handler, options) {
|
|
44
|
+
(0, react.useEffect)(() => {
|
|
45
|
+
const subscription = editorEventBus.on(eventName, handler, options);
|
|
46
|
+
return () => subscription.unsubscribe();
|
|
47
|
+
}, [
|
|
48
|
+
eventName,
|
|
49
|
+
handler,
|
|
50
|
+
options
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
Object.defineProperty(exports, "editorEventBus", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function() {
|
|
57
|
+
return editorEventBus;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(exports, "useEditorEvent", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function() {
|
|
63
|
+
return useEditorEvent;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
//#region src/core/event-bus.ts
|
|
3
|
+
const EVENT_PREFIX = "@react-email/editor:";
|
|
4
|
+
var EditorEventBus = class {
|
|
5
|
+
prefixEventName(eventName) {
|
|
6
|
+
return `${EVENT_PREFIX}${String(eventName)}`;
|
|
7
|
+
}
|
|
8
|
+
dispatch(eventName, payload, options) {
|
|
9
|
+
const target = options?.target ?? window;
|
|
10
|
+
const prefixedEventName = this.prefixEventName(eventName);
|
|
11
|
+
const event = new CustomEvent(prefixedEventName, {
|
|
12
|
+
detail: payload,
|
|
13
|
+
bubbles: false,
|
|
14
|
+
cancelable: false
|
|
15
|
+
});
|
|
16
|
+
target.dispatchEvent(event);
|
|
17
|
+
}
|
|
18
|
+
on(eventName, handler, options) {
|
|
19
|
+
const target = options?.target ?? window;
|
|
20
|
+
const prefixedEventName = this.prefixEventName(eventName);
|
|
21
|
+
const abortController = new AbortController();
|
|
22
|
+
const wrappedHandler = (event) => {
|
|
23
|
+
const customEvent = event;
|
|
24
|
+
const result = handler(customEvent.detail);
|
|
25
|
+
if (result instanceof Promise) result.catch((error) => {
|
|
26
|
+
console.error(`Error in async event handler for ${prefixedEventName}:`, {
|
|
27
|
+
event: customEvent.detail,
|
|
28
|
+
error
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
target.addEventListener(prefixedEventName, wrappedHandler, {
|
|
33
|
+
...options,
|
|
34
|
+
signal: abortController.signal
|
|
35
|
+
});
|
|
36
|
+
return { unsubscribe: () => {
|
|
37
|
+
abortController.abort();
|
|
38
|
+
} };
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const editorEventBus = new EditorEventBus();
|
|
42
|
+
function useEditorEvent(eventName, handler, options) {
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
const subscription = editorEventBus.on(eventName, handler, options);
|
|
45
|
+
return () => subscription.unsubscribe();
|
|
46
|
+
}, [
|
|
47
|
+
eventName,
|
|
48
|
+
handler,
|
|
49
|
+
options
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { useEditorEvent as n, editorEventBus as t };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=event-bus-CqrIUE24.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-bus-CqrIUE24.mjs","names":[],"sources":["../src/core/event-bus.ts"],"sourcesContent":["import type { Attrs } from '@tiptap/pm/model';\nimport { useEffect } from 'react';\n\nconst EVENT_PREFIX = '@react-email/editor:';\n\n/**\n * Base event map interface for the editor event bus.\n *\n * Components extend this via TypeScript module augmentation:\n * ```ts\n * declare module '@react-email/editor' {\n * interface EditorEventMap {\n * 'my-component:custom-event': { data: string };\n * }\n * }\n * ```\n */\nexport interface EditorEventMap {\n 'bubble-menu:add-link': undefined;\n 'node-clicked': NodeClickedEvent;\n}\n\nexport type NodeClickedEvent = {\n nodeType: string;\n nodeAttrs: Attrs;\n nodePos: { pos: number; inside: number };\n};\n\nexport type EditorEventName = keyof EditorEventMap;\n\nexport type EditorEventHandler<T extends EditorEventName> = (\n payload: EditorEventMap[T],\n) => void | Promise<void>;\n\nexport interface EditorEventSubscription {\n unsubscribe: () => void;\n}\n\nclass EditorEventBus {\n private prefixEventName(eventName: EditorEventName): string {\n return `${EVENT_PREFIX}${String(eventName)}`;\n }\n\n dispatch<T extends EditorEventName>(\n eventName: T,\n payload: EditorEventMap[T],\n options?: { target?: EventTarget },\n ): void {\n const target = options?.target ?? window;\n const prefixedEventName = this.prefixEventName(eventName);\n const event = new CustomEvent(prefixedEventName, {\n detail: payload,\n bubbles: false,\n cancelable: false,\n });\n target.dispatchEvent(event);\n }\n\n on<T extends EditorEventName>(\n eventName: T,\n handler: EditorEventHandler<T>,\n options?: AddEventListenerOptions & { target?: EventTarget },\n ): EditorEventSubscription {\n const target = options?.target ?? window;\n const prefixedEventName = this.prefixEventName(eventName);\n const abortController = new AbortController();\n\n const wrappedHandler = (event: Event) => {\n const customEvent = event as CustomEvent<EditorEventMap[T]>;\n const result = handler(customEvent.detail);\n\n if (result instanceof Promise) {\n result.catch((error) => {\n console.error(\n `Error in async event handler for ${prefixedEventName}:`,\n { event: customEvent.detail, error },\n );\n });\n }\n };\n\n target.addEventListener(prefixedEventName, wrappedHandler, {\n ...options,\n signal: abortController.signal,\n });\n\n return {\n unsubscribe: () => {\n abortController.abort();\n },\n };\n }\n}\n\nexport const editorEventBus = new EditorEventBus();\n\nexport function useEditorEvent<T extends EditorEventName>(\n eventName: T,\n handler: EditorEventHandler<T>,\n options?: AddEventListenerOptions & { target?: EventTarget },\n) {\n useEffect(() => {\n const subscription = editorEventBus.on(eventName, handler, options);\n\n return () => subscription.unsubscribe();\n }, [eventName, handler, options]);\n}\n"],"mappings":";;AAGA,MAAM,eAAe;AAmCrB,IAAM,iBAAN,MAAqB;CACnB,gBAAwB,WAAoC;AAC1D,SAAO,GAAG,eAAe,OAAO,UAAU;;CAG5C,SACE,WACA,SACA,SACM;EACN,MAAM,SAAS,SAAS,UAAU;EAClC,MAAM,oBAAoB,KAAK,gBAAgB,UAAU;EACzD,MAAM,QAAQ,IAAI,YAAY,mBAAmB;GAC/C,QAAQ;GACR,SAAS;GACT,YAAY;GACb,CAAC;AACF,SAAO,cAAc,MAAM;;CAG7B,GACE,WACA,SACA,SACyB;EACzB,MAAM,SAAS,SAAS,UAAU;EAClC,MAAM,oBAAoB,KAAK,gBAAgB,UAAU;EACzD,MAAM,kBAAkB,IAAI,iBAAiB;EAE7C,MAAM,kBAAkB,UAAiB;GACvC,MAAM,cAAc;GACpB,MAAM,SAAS,QAAQ,YAAY,OAAO;AAE1C,OAAI,kBAAkB,QACpB,QAAO,OAAO,UAAU;AACtB,YAAQ,MACN,oCAAoC,kBAAkB,IACtD;KAAE,OAAO,YAAY;KAAQ;KAAO,CACrC;KACD;;AAIN,SAAO,iBAAiB,mBAAmB,gBAAgB;GACzD,GAAG;GACH,QAAQ,gBAAgB;GACzB,CAAC;AAEF,SAAO,EACL,mBAAmB;AACjB,mBAAgB,OAAO;KAE1B;;;AAIL,MAAa,iBAAiB,IAAI,gBAAgB;AAElD,SAAgB,eACd,WACA,SACA,SACA;AACA,iBAAgB;EACd,MAAM,eAAe,eAAe,GAAG,WAAW,SAAS,QAAQ;AAEnE,eAAa,aAAa,aAAa;IACtC;EAAC;EAAW;EAAS;EAAQ,CAAC"}
|