@maily-to/shared 2.0.0-beta.2 → 2.0.0-beta.21
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/index.cjs +881 -692
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +475 -669
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +475 -669
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +849 -662
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -7
- package/readme.md +43 -7
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { collapse, expand, objectify, stringify } from "stylecast";
|
|
1
2
|
//#region src/alignment.ts
|
|
2
3
|
const TEXT_ALIGNMENTS = {
|
|
3
4
|
LEFT: "left",
|
|
@@ -10,55 +11,149 @@ function isAllowedTextAlignment(value) {
|
|
|
10
11
|
return typeof value === "string" && allowedTextAligns.includes(value);
|
|
11
12
|
}
|
|
12
13
|
//#endregion
|
|
13
|
-
//#region src/field-mode.ts
|
|
14
|
-
/**
|
|
15
|
-
* Controls how multi-value fields (padding, margin, border) are edited.
|
|
16
|
-
* "none" disables the field, "uniform" links all sides to one value,
|
|
17
|
-
* "mixed" allows independent per-side values, "preset" uses predefined
|
|
18
|
-
* size options (e.g. small/medium/large).
|
|
19
|
-
*/
|
|
20
|
-
const FIELD_MODE = {
|
|
21
|
-
NONE: "none",
|
|
22
|
-
MIXED: "mixed",
|
|
23
|
-
UNIFORM: "uniform",
|
|
24
|
-
PRESET: "preset"
|
|
25
|
-
};
|
|
26
|
-
const allowedFieldModes = Object.values(FIELD_MODE);
|
|
27
|
-
function isAllowedFieldMode(mode) {
|
|
28
|
-
return typeof mode === "string" && allowedFieldModes.includes(mode);
|
|
29
|
-
}
|
|
30
|
-
//#endregion
|
|
31
14
|
//#region src/border.ts
|
|
32
15
|
const BORDER_STYLES = {
|
|
33
16
|
SOLID: "solid",
|
|
34
17
|
DASHED: "dashed",
|
|
35
18
|
DOTTED: "dotted"
|
|
36
19
|
};
|
|
20
|
+
const allowedBorderStyles = Object.freeze(Object.values(BORDER_STYLES));
|
|
37
21
|
const DEFAULT_BORDER_STYLE = BORDER_STYLES.SOLID;
|
|
38
|
-
|
|
22
|
+
function isAllowedBorderStyle(value) {
|
|
23
|
+
return typeof value === "string" && allowedBorderStyles.includes(value);
|
|
24
|
+
}
|
|
39
25
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
26
|
+
* Reads the width, style and colour of a border from the `border` shorthand,
|
|
27
|
+
* letting any longhand already in the style win. The shorthand lists its
|
|
28
|
+
* parts in any order, so each is recognised by shape rather than position.
|
|
43
29
|
*/
|
|
44
|
-
function
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
30
|
+
function getBorderParts(style) {
|
|
31
|
+
const parts = (style.border ?? "").trim().split(/\s+/).filter(Boolean);
|
|
32
|
+
let color;
|
|
33
|
+
let borderStyle;
|
|
34
|
+
let width;
|
|
35
|
+
for (const part of parts) if (isAllowedBorderStyle(part) || part === "none") borderStyle = part;
|
|
36
|
+
else if (/^[\d.]/.test(part)) width = part;
|
|
37
|
+
else color = part;
|
|
48
38
|
return {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
borderBottomWidth: `${borderBottomWidth}px`,
|
|
53
|
-
borderLeftWidth: `${borderLeftWidth}px`,
|
|
54
|
-
borderStyle,
|
|
55
|
-
borderColor
|
|
56
|
-
},
|
|
57
|
-
...isBorderRadiusUniform ? { borderRadius: `${borderTopLeftRadius}px` } : { borderRadius: `${borderTopLeftRadius}px ${borderTopRightRadius}px ${borderBottomRightRadius}px ${borderBottomLeftRadius}px` }
|
|
39
|
+
color: style.borderColor ?? color,
|
|
40
|
+
style: style.borderStyle ?? borderStyle,
|
|
41
|
+
width: style.borderWidth ?? width
|
|
58
42
|
};
|
|
59
43
|
}
|
|
60
44
|
//#endregion
|
|
61
|
-
//#region src/
|
|
45
|
+
//#region src/box.ts
|
|
46
|
+
const BOX_SIDES = [
|
|
47
|
+
"top",
|
|
48
|
+
"right",
|
|
49
|
+
"bottom",
|
|
50
|
+
"left"
|
|
51
|
+
];
|
|
52
|
+
const BOX_CORNERS = [
|
|
53
|
+
"topLeft",
|
|
54
|
+
"topRight",
|
|
55
|
+
"bottomRight",
|
|
56
|
+
"bottomLeft"
|
|
57
|
+
];
|
|
58
|
+
function capitalize(value) {
|
|
59
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* CSS box shorthands repeat their values when fewer than four are given:
|
|
63
|
+
* one applies to every edge, two to the vertical then horizontal pair, and
|
|
64
|
+
* three to top, the horizontal pair, then bottom.
|
|
65
|
+
*/
|
|
66
|
+
function getShorthandEdges(shorthand) {
|
|
67
|
+
const parts = (shorthand ?? "").trim().split(/\s+/).filter(Boolean);
|
|
68
|
+
if (parts.length === 0) return null;
|
|
69
|
+
const [first, second = first, third = first, fourth = second] = parts;
|
|
70
|
+
return [
|
|
71
|
+
first,
|
|
72
|
+
second,
|
|
73
|
+
third,
|
|
74
|
+
fourth
|
|
75
|
+
].map(toLength);
|
|
76
|
+
}
|
|
77
|
+
function toLength(value) {
|
|
78
|
+
const parsed = Number.parseFloat(value);
|
|
79
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
80
|
+
}
|
|
81
|
+
function getLonghand(style, property, edge, suffix) {
|
|
82
|
+
return style[`${property}${capitalize(edge)}${suffix}`];
|
|
83
|
+
}
|
|
84
|
+
function toShorthand(edges) {
|
|
85
|
+
const [first, second, third, fourth] = edges;
|
|
86
|
+
return (first === third && second === fourth ? first === second ? [first] : [first, second] : [
|
|
87
|
+
first,
|
|
88
|
+
second,
|
|
89
|
+
third,
|
|
90
|
+
fourth
|
|
91
|
+
]).map((value) => `${value}px`).join(" ");
|
|
92
|
+
}
|
|
93
|
+
function getBoxSides(style, property) {
|
|
94
|
+
const [top, right, bottom, left] = getShorthandEdges(style[property]) ?? [
|
|
95
|
+
0,
|
|
96
|
+
0,
|
|
97
|
+
0,
|
|
98
|
+
0
|
|
99
|
+
];
|
|
100
|
+
const sides = {
|
|
101
|
+
bottom,
|
|
102
|
+
left,
|
|
103
|
+
right,
|
|
104
|
+
top
|
|
105
|
+
};
|
|
106
|
+
for (const side of BOX_SIDES) {
|
|
107
|
+
const longhand = getLonghand(style, property, side, "");
|
|
108
|
+
if (longhand !== void 0) sides[side] = toLength(longhand);
|
|
109
|
+
}
|
|
110
|
+
return sides;
|
|
111
|
+
}
|
|
112
|
+
function toBoxSides(property, values) {
|
|
113
|
+
const declarations = { [property]: toShorthand([
|
|
114
|
+
values.top,
|
|
115
|
+
values.right,
|
|
116
|
+
values.bottom,
|
|
117
|
+
values.left
|
|
118
|
+
]) };
|
|
119
|
+
for (const side of BOX_SIDES) declarations[`${property}${capitalize(side)}`] = null;
|
|
120
|
+
return declarations;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Radius shorthands run clockwise from the top-left corner, and repeat the
|
|
124
|
+
* same way box shorthands do.
|
|
125
|
+
*/
|
|
126
|
+
function getBoxCorners(style, property) {
|
|
127
|
+
const [topLeft, topRight, bottomRight, bottomLeft] = getShorthandEdges(style[property]) ?? [
|
|
128
|
+
0,
|
|
129
|
+
0,
|
|
130
|
+
0,
|
|
131
|
+
0
|
|
132
|
+
];
|
|
133
|
+
const corners = {
|
|
134
|
+
bottomLeft,
|
|
135
|
+
bottomRight,
|
|
136
|
+
topLeft,
|
|
137
|
+
topRight
|
|
138
|
+
};
|
|
139
|
+
for (const corner of BOX_CORNERS) {
|
|
140
|
+
const longhand = getLonghand(style, "border", corner, "Radius");
|
|
141
|
+
if (longhand !== void 0) corners[corner] = toLength(longhand);
|
|
142
|
+
}
|
|
143
|
+
return corners;
|
|
144
|
+
}
|
|
145
|
+
function toBoxCorners(property, values) {
|
|
146
|
+
const declarations = { [property]: toShorthand([
|
|
147
|
+
values.topLeft,
|
|
148
|
+
values.topRight,
|
|
149
|
+
values.bottomRight,
|
|
150
|
+
values.bottomLeft
|
|
151
|
+
]) };
|
|
152
|
+
for (const corner of BOX_CORNERS) declarations[`border${capitalize(corner)}Radius`] = null;
|
|
153
|
+
return declarations;
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/button.ts
|
|
62
157
|
/**
|
|
63
158
|
* Button layout kinds.
|
|
64
159
|
* "tight" renders the button at its content width,
|
|
@@ -74,14 +169,49 @@ function isAllowedButtonKind(value) {
|
|
|
74
169
|
return typeof value === "string" && allowedButtonKinds.includes(value);
|
|
75
170
|
}
|
|
76
171
|
//#endregion
|
|
172
|
+
//#region src/math.ts
|
|
173
|
+
function clamp(value, [min, max]) {
|
|
174
|
+
return Math.min(Math.max(value, min), max);
|
|
175
|
+
}
|
|
176
|
+
function percentage(portion, total) {
|
|
177
|
+
return clamp(portion / total * 100, [0, 100]);
|
|
178
|
+
}
|
|
179
|
+
function roundTo(value, decimals) {
|
|
180
|
+
const factor = 10 ** decimals;
|
|
181
|
+
return Math.round(value * factor) / factor;
|
|
182
|
+
}
|
|
183
|
+
function absoluteFromPercentage(percentage, total) {
|
|
184
|
+
return Math.round(total * percentage / 100);
|
|
185
|
+
}
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region src/column.ts
|
|
188
|
+
const PERCENTAGE_PATTERN = /^(\d+(?:\.\d+)?)%$/;
|
|
189
|
+
const PIXEL_PATTERN = /^(\d+(?:\.\d+)?)px$/;
|
|
190
|
+
const CSS_LENGTH_PATTERN = /^\d+(?:\.\d+)?(?:px|pt|pc|in|cm|mm|em|rem|ex|ch|vw|vh|vmin|vmax)$/;
|
|
191
|
+
function getColumnWidth(value) {
|
|
192
|
+
const declaration = value?.trim();
|
|
193
|
+
if (!declaration) return null;
|
|
194
|
+
const percentage = PERCENTAGE_PATTERN.exec(declaration);
|
|
195
|
+
if (percentage) return Number(percentage[1]);
|
|
196
|
+
return CSS_LENGTH_PATTERN.test(declaration) ? declaration : null;
|
|
197
|
+
}
|
|
198
|
+
function getColumnWidthValue(width) {
|
|
199
|
+
return typeof width === "number" ? `${width}%` : width;
|
|
200
|
+
}
|
|
201
|
+
function getColumnWidthPercent(options) {
|
|
202
|
+
const { containerWidth, width } = options;
|
|
203
|
+
if (width === null || width === void 0) return null;
|
|
204
|
+
if (typeof width === "number") return width;
|
|
205
|
+
const percentage = PERCENTAGE_PATTERN.exec(width);
|
|
206
|
+
if (percentage) return Number(percentage[1]);
|
|
207
|
+
const pixels = PIXEL_PATTERN.exec(width);
|
|
208
|
+
if (pixels && containerWidth > 0) return roundTo(Number(pixels[1]) / containerWidth * 100, 3);
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
//#endregion
|
|
77
212
|
//#region src/constants.ts
|
|
78
|
-
/** HTML data attribute key used to identify node types in rendered output. */
|
|
79
213
|
const DATA_NODE_TYPE_KEY = "data-node-type";
|
|
80
|
-
/** HTML data attribute key used to store serialized visibility rules. */
|
|
81
|
-
const DATA_VISIBILITY_RULE_KEY = "data-visibility-rule";
|
|
82
|
-
/** Prefix prepended to all Maily error messages for easy identification. */
|
|
83
214
|
const MAILY_ERROR_PREFIX = "[Maily Error]";
|
|
84
|
-
/** URL for filing bug reports against the Maily repository. */
|
|
85
215
|
const MAILY_BUG_REPORT_URL = "https://github.com/arikchakma/maily.to/issues/new";
|
|
86
216
|
//#endregion
|
|
87
217
|
//#region src/font.ts
|
|
@@ -102,14 +232,6 @@ const allowedFallbackFonts = [
|
|
|
102
232
|
"cursive",
|
|
103
233
|
"fantasy"
|
|
104
234
|
];
|
|
105
|
-
const allowedFontFormats = [
|
|
106
|
-
"woff",
|
|
107
|
-
"woff2",
|
|
108
|
-
"truetype",
|
|
109
|
-
"opentype",
|
|
110
|
-
"embedded-opentype",
|
|
111
|
-
"svg"
|
|
112
|
-
];
|
|
113
235
|
/**
|
|
114
236
|
* Default font configuration: Inter with sans-serif fallback,
|
|
115
237
|
* loaded from the Maily CDN as woff2.
|
|
@@ -118,19 +240,18 @@ const DEFAULT_FONT = {
|
|
|
118
240
|
fallbackFontFamily: "sans-serif",
|
|
119
241
|
fontFamily: "Inter",
|
|
120
242
|
webFont: {
|
|
121
|
-
|
|
122
|
-
|
|
243
|
+
format: "woff2",
|
|
244
|
+
url: "https://cdn.usemaily.com/fonts/v0/inter.woff2"
|
|
123
245
|
}
|
|
124
246
|
};
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
document.head.appendChild(styleElement);
|
|
247
|
+
function getFontFamilyNames(value) {
|
|
248
|
+
return value.split(",").map((font) => {
|
|
249
|
+
return font.trim().replace(/['"]/g, "");
|
|
250
|
+
}).filter(Boolean);
|
|
251
|
+
}
|
|
252
|
+
function getFontFamilyValue(options) {
|
|
253
|
+
const { fallbackFontFamily, fontFamily } = options;
|
|
254
|
+
return [fontFamily ? `'${fontFamily}'` : null, fallbackFontFamily].filter(Boolean).join(", ");
|
|
134
255
|
}
|
|
135
256
|
/**
|
|
136
257
|
* Generates a @font-face CSS rule string for the given font configuration.
|
|
@@ -138,242 +259,17 @@ function loadFont(font) {
|
|
|
138
259
|
* source URL when available.
|
|
139
260
|
*/
|
|
140
261
|
function getFontFaceStyle(font) {
|
|
141
|
-
const {
|
|
262
|
+
const { fallbackFontFamily, fontFamily, fontStyle = "normal", fontWeight = 400, webFont } = font;
|
|
142
263
|
return `@font-face {font-family: '${fontFamily}';font-style: ${fontStyle};font-weight: ${fontWeight};mso-font-alt: '${fallbackFontFamily}';${webFont ? `src: url(${webFont.url}) format('${webFont.format}');` : ""}}`;
|
|
143
264
|
}
|
|
144
|
-
//#endregion
|
|
145
|
-
//#region src/css-variables.ts
|
|
146
|
-
/**
|
|
147
|
-
* Returns a CSS variable object for the given name and value.
|
|
148
|
-
* If the value is nullish or empty, returns an empty object so that
|
|
149
|
-
* the default CSS variable value is not overridden. Numbers are
|
|
150
|
-
* automatically suffixed with "px".
|
|
151
|
-
*/
|
|
152
|
-
function getVariableValue(name, value) {
|
|
153
|
-
if (value === void 0 || value === null || value === "") return {};
|
|
154
|
-
return { [name]: typeof value === "number" ? `${value}px` : value };
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Converts an EditorThemeOptions object into a flat map of Maily CSS
|
|
158
|
-
* custom properties (--mly-*). Used to apply theme overrides as inline
|
|
159
|
-
* styles on the editor or renderer root element. Covers body background,
|
|
160
|
-
* container dimensions and borders, button colors, link color, and
|
|
161
|
-
* font family settings.
|
|
162
|
-
*/
|
|
163
|
-
function getMailyCssVariables(theme) {
|
|
164
|
-
const font = theme.font || DEFAULT_FONT;
|
|
165
|
-
return {
|
|
166
|
-
...getVariableValue("--mly-body-background-color", theme.body?.backgroundColor),
|
|
167
|
-
...getVariableValue("--mly-body-padding-top", theme.body?.paddingTop),
|
|
168
|
-
...getVariableValue("--mly-body-padding-right", theme.body?.paddingRight),
|
|
169
|
-
...getVariableValue("--mly-body-padding-bottom", theme.body?.paddingBottom),
|
|
170
|
-
...getVariableValue("--mly-body-padding-left", theme.body?.paddingLeft),
|
|
171
|
-
...getVariableValue("--mly-container-background-color", theme.container?.backgroundColor),
|
|
172
|
-
...getVariableValue("--mly-container-max-width", theme.container?.maxWidth),
|
|
173
|
-
...getVariableValue("--mly-container-min-width", theme.container?.minWidth),
|
|
174
|
-
...getVariableValue("--mly-container-padding-top", theme.container?.paddingTop),
|
|
175
|
-
...getVariableValue("--mly-container-padding-right", theme.container?.paddingRight),
|
|
176
|
-
...getVariableValue("--mly-container-padding-bottom", theme.container?.paddingBottom),
|
|
177
|
-
...getVariableValue("--mly-container-padding-left", theme.container?.paddingLeft),
|
|
178
|
-
...getVariableValue("--mly-container-border-radius", theme.container?.borderRadius),
|
|
179
|
-
...getVariableValue("--mly-container-border-width", theme.container?.borderWidth),
|
|
180
|
-
...getVariableValue("--mly-container-border-color", theme.container?.borderColor),
|
|
181
|
-
...getVariableValue("--mly-button-background-color", theme.button?.backgroundColor),
|
|
182
|
-
...getVariableValue("--mly-button-text-color", theme.button?.color),
|
|
183
|
-
...getVariableValue("--mly-button-padding-top", theme.button?.paddingTop),
|
|
184
|
-
...getVariableValue("--mly-button-padding-right", theme.button?.paddingRight),
|
|
185
|
-
...getVariableValue("--mly-button-padding-bottom", theme.button?.paddingBottom),
|
|
186
|
-
...getVariableValue("--mly-button-padding-left", theme.button?.paddingLeft),
|
|
187
|
-
...getVariableValue("--mly-link-color", theme.link?.color),
|
|
188
|
-
...getVariableValue("--mly-font-family", font.fontFamily),
|
|
189
|
-
...getVariableValue("--mly-font-fallback-family", font.fallbackFontFamily),
|
|
190
|
-
"--mly-font": `var(--mly-font-family), var(--mly-font-fallback-family)`
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
//#endregion
|
|
194
|
-
//#region src/html-code-block.ts
|
|
195
|
-
/**
|
|
196
|
-
* Tab options for the HTML code block node.
|
|
197
|
-
* "code" shows the raw HTML source, "preview" renders it visually.
|
|
198
|
-
*/
|
|
199
|
-
const HTML_CODE_BLOCK_TABS = {
|
|
200
|
-
CODE: "code",
|
|
201
|
-
PREVIEW: "preview"
|
|
202
|
-
};
|
|
203
|
-
const DEFAULT_HTML_CODE_BLOCK_TAB = HTML_CODE_BLOCK_TABS.CODE;
|
|
204
|
-
//#endregion
|
|
205
|
-
//#region src/math.ts
|
|
206
|
-
function clamp(value, [min, max]) {
|
|
207
|
-
return Math.min(Math.max(value, min), max);
|
|
208
|
-
}
|
|
209
|
-
function percentage(portion, total) {
|
|
210
|
-
return clamp(portion / total * 100, [0, 100]);
|
|
211
|
-
}
|
|
212
|
-
function absoluteFromPercentage(percentage, total) {
|
|
213
|
-
return Math.round(total * percentage / 100);
|
|
214
|
-
}
|
|
215
|
-
function roundTo(value, decimals) {
|
|
216
|
-
const factor = Math.pow(10, decimals);
|
|
217
|
-
return Math.round(value * factor) / factor;
|
|
218
|
-
}
|
|
219
|
-
//#endregion
|
|
220
|
-
//#region src/image-width.ts
|
|
221
|
-
const MIN_IMAGE_WIDTH_PERCENTAGE = 5;
|
|
222
|
-
const MAX_IMAGE_WIDTH_PERCENTAGE = 100;
|
|
223
|
-
const MAX_IMAGE_WIDTH_PIXELS = 600;
|
|
224
|
-
/**
|
|
225
|
-
* Converts any ImageWidth value to a clamped percentage number.
|
|
226
|
-
* Handles "auto" (→ 100), percentage strings (e.g. "50%" → 50),
|
|
227
|
-
* and legacy pixel numbers (converted relative to maxWidthPixels).
|
|
228
|
-
* Returns 100 for null or unrecognized values.
|
|
229
|
-
*/
|
|
230
|
-
function parseWidthToPercentage(width, maxWidthPixels = MAX_IMAGE_WIDTH_PIXELS) {
|
|
231
|
-
if (typeof width === "string") {
|
|
232
|
-
if (width === "auto") return 100;
|
|
233
|
-
return width.endsWith("%") ? clampImageWidthPercentage(parseInt(width)) : 100;
|
|
234
|
-
}
|
|
235
|
-
if (typeof width === "number") return clampImageWidthPercentage(Math.round(100 * width / maxWidthPixels));
|
|
236
|
-
return 100;
|
|
237
|
-
}
|
|
238
|
-
function clampImageWidthPercentage(width) {
|
|
239
|
-
return clamp(width, [5, 100]);
|
|
240
|
-
}
|
|
241
|
-
//#endregion
|
|
242
|
-
//#region src/mark.ts
|
|
243
|
-
/** Registry of all inline mark type strings used in the document JSON. */
|
|
244
|
-
const MAILY_MARK_TYPES = {
|
|
245
|
-
BOLD: "bold",
|
|
246
|
-
ITALIC: "italic",
|
|
247
|
-
STRIKE: "strike",
|
|
248
|
-
UNDERLINE: "underline",
|
|
249
|
-
LINK: "link",
|
|
250
|
-
CODE: "code",
|
|
251
|
-
TEXT_STYLE: "textStyle",
|
|
252
|
-
HIGHLIGHT: "highlight"
|
|
253
|
-
};
|
|
254
|
-
const allowedMarkTypes = Object.values(MAILY_MARK_TYPES);
|
|
255
|
-
//#endregion
|
|
256
|
-
//#region src/node.ts
|
|
257
265
|
/**
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* document JSON.
|
|
261
|
-
*/
|
|
262
|
-
const MAILY_NODE_TYPES = {
|
|
263
|
-
DOCUMENT: "doc",
|
|
264
|
-
PARAGRAPH: "paragraph",
|
|
265
|
-
TEXT: "text",
|
|
266
|
-
HEADING: "heading",
|
|
267
|
-
VARIABLE: "variable",
|
|
268
|
-
IMAGE: "image",
|
|
269
|
-
SPACER: "spacer",
|
|
270
|
-
SECTION: "section",
|
|
271
|
-
COLUMNS: "columns",
|
|
272
|
-
COLUMN: "column",
|
|
273
|
-
BULLET_LIST: "bulletList",
|
|
274
|
-
ORDERED_LIST: "orderedList",
|
|
275
|
-
LIST_ITEM: "listItem",
|
|
276
|
-
HORIZONTAL_RULE: "horizontalRule",
|
|
277
|
-
BUTTON: "button",
|
|
278
|
-
REPEAT: "repeat",
|
|
279
|
-
HTML_CODE_BLOCK: "htmlCodeBlock",
|
|
280
|
-
BLOCKQUOTE: "blockquote",
|
|
281
|
-
HARD_BREAK: "hardBreak",
|
|
282
|
-
FOOTER: "footer",
|
|
283
|
-
INLINE_IMAGE: "inlineImage",
|
|
284
|
-
LINK_CARD: "linkCard"
|
|
285
|
-
};
|
|
286
|
-
/**
|
|
287
|
-
* Tiptap extension types that attach attributes to nodes but are
|
|
288
|
-
* not document nodes themselves (e.g. fontStyle, visibility).
|
|
289
|
-
*/
|
|
290
|
-
const MAILY_EXTENSION_TYPES = {
|
|
291
|
-
FONT_STYLE: "fontStyle",
|
|
292
|
-
VISIBILITY: "visibility"
|
|
293
|
-
};
|
|
294
|
-
const allowedNodeTypes = Object.values(MAILY_NODE_TYPES);
|
|
295
|
-
//#endregion
|
|
296
|
-
//#region src/is.ts
|
|
297
|
-
function guard(type) {
|
|
298
|
-
return (node) => node.type === type;
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Type guards for Maily nodes and marks. Provides narrowing functions
|
|
302
|
-
* for every node type (is.paragraph, is.button, etc.) and mark type
|
|
303
|
-
* (is.bold, is.link, etc.), plus structural checks like is.parent
|
|
304
|
-
* and is.marked. Each guard narrows the input to its specific type.
|
|
305
|
-
*
|
|
306
|
-
* Example:
|
|
307
|
-
* if (is.paragraph(node)) {
|
|
308
|
-
* node.attrs.textAlign // narrowed to ParagraphNode
|
|
309
|
-
* }
|
|
266
|
+
* Injects a @font-face style element into the document head to load
|
|
267
|
+
* the given font. Only works in browser environments.
|
|
310
268
|
*/
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
parent(node) {
|
|
316
|
-
return Array.isArray(node?.content);
|
|
317
|
-
},
|
|
318
|
-
marked(node) {
|
|
319
|
-
return Array.isArray(node?.marks);
|
|
320
|
-
},
|
|
321
|
-
node(node) {
|
|
322
|
-
return typeof node === "object" && node !== null && "type" in node && allowedNodeTypes.includes(node.type);
|
|
323
|
-
},
|
|
324
|
-
doc: guard(MAILY_NODE_TYPES.DOCUMENT),
|
|
325
|
-
paragraph: guard(MAILY_NODE_TYPES.PARAGRAPH),
|
|
326
|
-
text: guard(MAILY_NODE_TYPES.TEXT),
|
|
327
|
-
heading: guard(MAILY_NODE_TYPES.HEADING),
|
|
328
|
-
variable: guard(MAILY_NODE_TYPES.VARIABLE),
|
|
329
|
-
image: guard(MAILY_NODE_TYPES.IMAGE),
|
|
330
|
-
inlineImage: guard(MAILY_NODE_TYPES.INLINE_IMAGE),
|
|
331
|
-
spacer: guard(MAILY_NODE_TYPES.SPACER),
|
|
332
|
-
section: guard(MAILY_NODE_TYPES.SECTION),
|
|
333
|
-
button: guard(MAILY_NODE_TYPES.BUTTON),
|
|
334
|
-
columns: guard(MAILY_NODE_TYPES.COLUMNS),
|
|
335
|
-
column: guard(MAILY_NODE_TYPES.COLUMN),
|
|
336
|
-
repeat: guard(MAILY_NODE_TYPES.REPEAT),
|
|
337
|
-
bulletList: guard(MAILY_NODE_TYPES.BULLET_LIST),
|
|
338
|
-
orderedList: guard(MAILY_NODE_TYPES.ORDERED_LIST),
|
|
339
|
-
listItem: guard(MAILY_NODE_TYPES.LIST_ITEM),
|
|
340
|
-
horizontalRule: guard(MAILY_NODE_TYPES.HORIZONTAL_RULE),
|
|
341
|
-
htmlCodeBlock: guard(MAILY_NODE_TYPES.HTML_CODE_BLOCK),
|
|
342
|
-
blockquote: guard(MAILY_NODE_TYPES.BLOCKQUOTE),
|
|
343
|
-
hardBreak: guard(MAILY_NODE_TYPES.HARD_BREAK),
|
|
344
|
-
footer: guard(MAILY_NODE_TYPES.FOOTER),
|
|
345
|
-
linkCard: guard(MAILY_NODE_TYPES.LINK_CARD),
|
|
346
|
-
mark(mark) {
|
|
347
|
-
return typeof mark === "object" && mark !== null && "type" in mark && allowedMarkTypes.includes(mark.type);
|
|
348
|
-
},
|
|
349
|
-
bold: guard(MAILY_MARK_TYPES.BOLD),
|
|
350
|
-
italic: guard(MAILY_MARK_TYPES.ITALIC),
|
|
351
|
-
strike: guard(MAILY_MARK_TYPES.STRIKE),
|
|
352
|
-
underline: guard(MAILY_MARK_TYPES.UNDERLINE),
|
|
353
|
-
link: guard(MAILY_MARK_TYPES.LINK),
|
|
354
|
-
code: guard(MAILY_MARK_TYPES.CODE),
|
|
355
|
-
textStyle: guard(MAILY_MARK_TYPES.TEXT_STYLE),
|
|
356
|
-
highlight: guard(MAILY_MARK_TYPES.HIGHLIGHT)
|
|
357
|
-
};
|
|
358
|
-
/** Type guard that checks if a value is defined (not null or undefined). */
|
|
359
|
-
function isDef(val) {
|
|
360
|
-
return val !== void 0 && val !== null;
|
|
361
|
-
}
|
|
362
|
-
/** Type guard that checks if a value is a boolean. */
|
|
363
|
-
function isBoolean(val) {
|
|
364
|
-
return typeof val === "boolean";
|
|
365
|
-
}
|
|
366
|
-
/** Type guard that checks if a value is a number. */
|
|
367
|
-
function isNumber(val) {
|
|
368
|
-
return typeof val === "number";
|
|
369
|
-
}
|
|
370
|
-
/** Type guard that checks if a value is a string. */
|
|
371
|
-
function isString(val) {
|
|
372
|
-
return typeof val === "string";
|
|
373
|
-
}
|
|
374
|
-
/** Type guard that checks if a value is a non-null object. */
|
|
375
|
-
function isObject(val) {
|
|
376
|
-
return typeof val === "object" && val !== null;
|
|
269
|
+
function loadFont(font) {
|
|
270
|
+
const styleElement = document.createElement("style");
|
|
271
|
+
styleElement.textContent = getFontFaceStyle(font);
|
|
272
|
+
document.head.appendChild(styleElement);
|
|
377
273
|
}
|
|
378
274
|
//#endregion
|
|
379
275
|
//#region src/font-style.ts
|
|
@@ -385,57 +281,57 @@ const DEFAULT_FONT_FAMILIES = [
|
|
|
385
281
|
{
|
|
386
282
|
fontFamily: "Inter",
|
|
387
283
|
webFont: {
|
|
388
|
-
|
|
389
|
-
|
|
284
|
+
format: "woff2",
|
|
285
|
+
url: "https://cdn.usemaily.com/fonts/v0/inter.woff2"
|
|
390
286
|
}
|
|
391
287
|
},
|
|
392
288
|
{
|
|
393
289
|
fontFamily: "Roboto",
|
|
394
290
|
webFont: {
|
|
395
|
-
|
|
396
|
-
|
|
291
|
+
format: "woff2",
|
|
292
|
+
url: "https://cdn.usemaily.com/fonts/v0/roboto.woff2"
|
|
397
293
|
}
|
|
398
294
|
},
|
|
399
295
|
{
|
|
400
296
|
fontFamily: "Open Sans",
|
|
401
297
|
webFont: {
|
|
402
|
-
|
|
403
|
-
|
|
298
|
+
format: "woff2",
|
|
299
|
+
url: "https://cdn.usemaily.com/fonts/v0/open-sans.woff2"
|
|
404
300
|
}
|
|
405
301
|
},
|
|
406
302
|
{
|
|
407
303
|
fontFamily: "Lato",
|
|
408
304
|
webFont: {
|
|
409
|
-
|
|
410
|
-
|
|
305
|
+
format: "woff2",
|
|
306
|
+
url: "https://cdn.usemaily.com/fonts/v0/lato.woff2"
|
|
411
307
|
}
|
|
412
308
|
},
|
|
413
309
|
{
|
|
414
310
|
fontFamily: "Montserrat",
|
|
415
311
|
webFont: {
|
|
416
|
-
|
|
417
|
-
|
|
312
|
+
format: "woff2",
|
|
313
|
+
url: "https://cdn.usemaily.com/fonts/v0/montserrat.woff2"
|
|
418
314
|
}
|
|
419
315
|
},
|
|
420
316
|
{
|
|
421
317
|
fontFamily: "Poppins",
|
|
422
318
|
webFont: {
|
|
423
|
-
|
|
424
|
-
|
|
319
|
+
format: "woff2",
|
|
320
|
+
url: "https://cdn.usemaily.com/fonts/v0/poppins.woff2"
|
|
425
321
|
}
|
|
426
322
|
},
|
|
427
323
|
{
|
|
428
324
|
fontFamily: "Raleway",
|
|
429
325
|
webFont: {
|
|
430
|
-
|
|
431
|
-
|
|
326
|
+
format: "woff2",
|
|
327
|
+
url: "https://cdn.usemaily.com/fonts/v0/raleway.woff2"
|
|
432
328
|
}
|
|
433
329
|
},
|
|
434
330
|
{
|
|
435
331
|
fontFamily: "Ubuntu",
|
|
436
332
|
webFont: {
|
|
437
|
-
|
|
438
|
-
|
|
333
|
+
format: "woff2",
|
|
334
|
+
url: "https://cdn.usemaily.com/fonts/v0/ubuntu.woff2"
|
|
439
335
|
}
|
|
440
336
|
},
|
|
441
337
|
{ fontFamily: "Arial" },
|
|
@@ -450,7 +346,6 @@ const FONT_STYLES = {
|
|
|
450
346
|
NORMAL: "normal",
|
|
451
347
|
ITALIC: "italic"
|
|
452
348
|
};
|
|
453
|
-
/** Preset options for the font size dropdown in the editor. */
|
|
454
349
|
const FONT_SIZE_PRESETS = [
|
|
455
350
|
{
|
|
456
351
|
value: 12,
|
|
@@ -477,7 +372,6 @@ const FONT_SIZE_PRESETS = [
|
|
|
477
372
|
label: "2XL"
|
|
478
373
|
}
|
|
479
374
|
];
|
|
480
|
-
/** Preset options for the line height dropdown in the editor. */
|
|
481
375
|
const LINE_HEIGHT_PRESETS = [
|
|
482
376
|
{
|
|
483
377
|
value: 1,
|
|
@@ -501,30 +395,7 @@ const LINE_HEIGHT_PRESETS = [
|
|
|
501
395
|
}
|
|
502
396
|
];
|
|
503
397
|
const DEFAULT_FONT_FAMILY = "Inter";
|
|
504
|
-
const DEFAULT_FONT_SIZE = 15;
|
|
505
|
-
const DEFAULT_FONT_WEIGHT = 400;
|
|
506
|
-
const DEFAULT_LINE_HEIGHT = 1.75;
|
|
507
398
|
const DEFAULT_FONT_FALLBACK = "sans-serif";
|
|
508
|
-
/**
|
|
509
|
-
* Builds a CSS properties object from a node's font attributes,
|
|
510
|
-
* falling back to the provided defaults for any null values.
|
|
511
|
-
* Only includes properties that have a resolved value.
|
|
512
|
-
*/
|
|
513
|
-
function getFontStyle(attrs, defaults) {
|
|
514
|
-
const { fontFamily, fontFallback, fontSize, fontWeight, lineHeight, fontStyle } = attrs;
|
|
515
|
-
const size = fontSize ?? defaults?.fontSize;
|
|
516
|
-
const weight = fontWeight ?? defaults?.fontWeight;
|
|
517
|
-
const line = lineHeight ?? defaults?.lineHeight;
|
|
518
|
-
const style = fontStyle ?? defaults?.fontStyle;
|
|
519
|
-
const fontFamilyParts = [fontFamily, fontFallback].filter(Boolean);
|
|
520
|
-
return {
|
|
521
|
-
...fontFamilyParts.length > 0 && { fontFamily: fontFamilyParts.join(", ") },
|
|
522
|
-
...isDef(size) ? { fontSize: `${size}px` } : {},
|
|
523
|
-
...isDef(weight) ? { fontWeight: weight } : {},
|
|
524
|
-
...isDef(line) ? { lineHeight: line } : {},
|
|
525
|
-
...isDef(style) ? { fontStyle: style } : {}
|
|
526
|
-
};
|
|
527
|
-
}
|
|
528
399
|
//#endregion
|
|
529
400
|
//#region src/font-weight.ts
|
|
530
401
|
/**
|
|
@@ -540,9 +411,42 @@ const FONT_WEIGHTS = {
|
|
|
540
411
|
};
|
|
541
412
|
//#endregion
|
|
542
413
|
//#region src/function.ts
|
|
543
|
-
/** A no-operation function. Useful as a default callback or placeholder. */
|
|
544
414
|
function noop() {}
|
|
545
415
|
//#endregion
|
|
416
|
+
//#region src/html-code-block.ts
|
|
417
|
+
/**
|
|
418
|
+
* Tab options for the HTML code block node.
|
|
419
|
+
* "code" shows the raw HTML source, "preview" renders it visually.
|
|
420
|
+
*/
|
|
421
|
+
const HTML_CODE_BLOCK_TABS = {
|
|
422
|
+
CODE: "code",
|
|
423
|
+
PREVIEW: "preview"
|
|
424
|
+
};
|
|
425
|
+
const DEFAULT_HTML_CODE_BLOCK_TAB = HTML_CODE_BLOCK_TABS.CODE;
|
|
426
|
+
//#endregion
|
|
427
|
+
//#region src/image.ts
|
|
428
|
+
const MIN_IMAGE_WIDTH_PERCENTAGE = 5;
|
|
429
|
+
const MAX_IMAGE_WIDTH_PERCENTAGE = 100;
|
|
430
|
+
const DEFAULT_CONTAINER_WIDTH_PIXELS = 600;
|
|
431
|
+
const PIXEL_SUFFIX = "px";
|
|
432
|
+
function getPixels(value) {
|
|
433
|
+
const length = value.endsWith(PIXEL_SUFFIX) ? value.slice(0, -2) : value;
|
|
434
|
+
if (length === "") return null;
|
|
435
|
+
const pixels = Number(length);
|
|
436
|
+
return Number.isFinite(pixels) ? pixels : null;
|
|
437
|
+
}
|
|
438
|
+
function parseWidthToPercentage(width, containerWidthPixels = 600) {
|
|
439
|
+
if (width === null || width === void 0 || width === "auto") return 100;
|
|
440
|
+
const value = String(width).trim();
|
|
441
|
+
if (value.endsWith("%")) return clampImageWidthPercentage(Number.parseFloat(value));
|
|
442
|
+
const pixels = getPixels(value);
|
|
443
|
+
if (pixels === null) return 100;
|
|
444
|
+
return clampImageWidthPercentage(Math.round(100 * pixels / containerWidthPixels));
|
|
445
|
+
}
|
|
446
|
+
function clampImageWidthPercentage(width) {
|
|
447
|
+
return clamp(width, [5, 100]);
|
|
448
|
+
}
|
|
449
|
+
//#endregion
|
|
546
450
|
//#region src/invariant.ts
|
|
547
451
|
function invariant(value, message) {
|
|
548
452
|
if (value === false || value === null || typeof value === "undefined") {
|
|
@@ -552,25 +456,133 @@ Please open an issue! ${MAILY_BUG_REPORT_URL}`);
|
|
|
552
456
|
}
|
|
553
457
|
}
|
|
554
458
|
//#endregion
|
|
555
|
-
//#region src/
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
459
|
+
//#region src/mark.ts
|
|
460
|
+
const MAILY_MARK_TYPES = {
|
|
461
|
+
BOLD: "bold",
|
|
462
|
+
ITALIC: "italic",
|
|
463
|
+
STRIKE: "strike",
|
|
464
|
+
UNDERLINE: "underline",
|
|
465
|
+
LINK: "link",
|
|
466
|
+
CODE: "code",
|
|
467
|
+
TEXT_STYLE: "textStyle",
|
|
468
|
+
HIGHLIGHT: "highlight",
|
|
469
|
+
SPAN: "span"
|
|
470
|
+
};
|
|
471
|
+
const allowedMarkTypes = Object.values(MAILY_MARK_TYPES);
|
|
472
|
+
//#endregion
|
|
473
|
+
//#region src/node.ts
|
|
474
|
+
const MAILY_NODE_TYPES = {
|
|
475
|
+
DOCUMENT: "doc",
|
|
476
|
+
BODY: "body",
|
|
477
|
+
CONTAINER: "container",
|
|
478
|
+
SECTION: "section",
|
|
479
|
+
DIV: "div",
|
|
480
|
+
COLUMNS: "columns",
|
|
481
|
+
COLUMN: "column",
|
|
482
|
+
IMAGE: "image",
|
|
483
|
+
INLINE_IMAGE: "inlineImage",
|
|
484
|
+
INLINE_SPAN: "inlineSpan",
|
|
485
|
+
SPACER: "spacer",
|
|
486
|
+
BUTTON: "button",
|
|
487
|
+
FOOTER: "footer",
|
|
488
|
+
PARAGRAPH: "paragraph",
|
|
489
|
+
HEADING: "heading",
|
|
490
|
+
BLOCKQUOTE: "blockquote",
|
|
491
|
+
HORIZONTAL_RULE: "horizontalRule",
|
|
492
|
+
HTML_CODE_BLOCK: "htmlCodeBlock",
|
|
493
|
+
REPEAT: "repeat",
|
|
494
|
+
HARD_BREAK: "hardBreak",
|
|
495
|
+
BULLET_LIST: "bulletList",
|
|
496
|
+
ORDERED_LIST: "orderedList",
|
|
497
|
+
LIST_ITEM: "listItem",
|
|
498
|
+
TEXT: "text",
|
|
499
|
+
VARIABLE: "variable",
|
|
500
|
+
TABLE: "table",
|
|
501
|
+
TABLE_ROW: "tableRow",
|
|
502
|
+
TABLE_CELL: "tableCell",
|
|
503
|
+
TABLE_HEADER: "tableHeader",
|
|
504
|
+
LINK_CARD: "linkCard"
|
|
505
|
+
};
|
|
506
|
+
const MAILY_EXTENSION_TYPES = {
|
|
507
|
+
STYLE: "style",
|
|
508
|
+
THEME: "theme",
|
|
509
|
+
VISIBILITY: "visibility"
|
|
510
|
+
};
|
|
511
|
+
const allowedNodeTypes = Object.values(MAILY_NODE_TYPES);
|
|
512
|
+
const DEFAULT_VARIABLE_ATTRIBUTES = {
|
|
513
|
+
fallback: null,
|
|
514
|
+
isDefaultEditable: true,
|
|
515
|
+
label: null,
|
|
516
|
+
required: true,
|
|
517
|
+
style: null
|
|
518
|
+
};
|
|
519
|
+
//#endregion
|
|
520
|
+
//#region src/is.ts
|
|
521
|
+
function guard(type) {
|
|
522
|
+
return (value) => value.type === type;
|
|
523
|
+
}
|
|
524
|
+
function isObject(val) {
|
|
525
|
+
return typeof val === "object" && val !== null;
|
|
526
|
+
}
|
|
527
|
+
const is = {
|
|
528
|
+
node(value) {
|
|
529
|
+
return typeof value === "object" && value !== null && "type" in value && allowedNodeTypes.includes(value.type);
|
|
530
|
+
},
|
|
531
|
+
mark(value) {
|
|
532
|
+
return typeof value === "object" && value !== null && "type" in value && allowedMarkTypes.includes(value.type);
|
|
533
|
+
},
|
|
534
|
+
parent(node) {
|
|
535
|
+
return Array.isArray(node?.content);
|
|
536
|
+
},
|
|
537
|
+
marked(node) {
|
|
538
|
+
return Array.isArray(node?.marks);
|
|
539
|
+
},
|
|
540
|
+
doc: guard(MAILY_NODE_TYPES.DOCUMENT),
|
|
541
|
+
body: guard(MAILY_NODE_TYPES.BODY),
|
|
542
|
+
container: guard(MAILY_NODE_TYPES.CONTAINER),
|
|
543
|
+
section: guard(MAILY_NODE_TYPES.SECTION),
|
|
544
|
+
div: guard(MAILY_NODE_TYPES.DIV),
|
|
545
|
+
columns: guard(MAILY_NODE_TYPES.COLUMNS),
|
|
546
|
+
column: guard(MAILY_NODE_TYPES.COLUMN),
|
|
547
|
+
image: guard(MAILY_NODE_TYPES.IMAGE),
|
|
548
|
+
inlineImage: guard(MAILY_NODE_TYPES.INLINE_IMAGE),
|
|
549
|
+
inlineSpan: guard(MAILY_NODE_TYPES.INLINE_SPAN),
|
|
550
|
+
spacer: guard(MAILY_NODE_TYPES.SPACER),
|
|
551
|
+
button: guard(MAILY_NODE_TYPES.BUTTON),
|
|
552
|
+
footer: guard(MAILY_NODE_TYPES.FOOTER),
|
|
553
|
+
paragraph: guard(MAILY_NODE_TYPES.PARAGRAPH),
|
|
554
|
+
heading: guard(MAILY_NODE_TYPES.HEADING),
|
|
555
|
+
blockquote: guard(MAILY_NODE_TYPES.BLOCKQUOTE),
|
|
556
|
+
horizontalRule: guard(MAILY_NODE_TYPES.HORIZONTAL_RULE),
|
|
557
|
+
hardBreak: guard(MAILY_NODE_TYPES.HARD_BREAK),
|
|
558
|
+
htmlCodeBlock: guard(MAILY_NODE_TYPES.HTML_CODE_BLOCK),
|
|
559
|
+
repeat: guard(MAILY_NODE_TYPES.REPEAT),
|
|
560
|
+
bulletList: guard(MAILY_NODE_TYPES.BULLET_LIST),
|
|
561
|
+
orderedList: guard(MAILY_NODE_TYPES.ORDERED_LIST),
|
|
562
|
+
listItem: guard(MAILY_NODE_TYPES.LIST_ITEM),
|
|
563
|
+
linkCard: guard(MAILY_NODE_TYPES.LINK_CARD),
|
|
564
|
+
text: guard(MAILY_NODE_TYPES.TEXT),
|
|
565
|
+
variable: guard(MAILY_NODE_TYPES.VARIABLE),
|
|
566
|
+
table: guard(MAILY_NODE_TYPES.TABLE),
|
|
567
|
+
tableRow: guard(MAILY_NODE_TYPES.TABLE_ROW),
|
|
568
|
+
tableCell: guard(MAILY_NODE_TYPES.TABLE_CELL),
|
|
569
|
+
tableHeader: guard(MAILY_NODE_TYPES.TABLE_HEADER),
|
|
570
|
+
bold: guard(MAILY_MARK_TYPES.BOLD),
|
|
571
|
+
italic: guard(MAILY_MARK_TYPES.ITALIC),
|
|
572
|
+
strike: guard(MAILY_MARK_TYPES.STRIKE),
|
|
573
|
+
underline: guard(MAILY_MARK_TYPES.UNDERLINE),
|
|
574
|
+
link: guard(MAILY_MARK_TYPES.LINK),
|
|
575
|
+
code: guard(MAILY_MARK_TYPES.CODE),
|
|
576
|
+
textStyle: guard(MAILY_MARK_TYPES.TEXT_STYLE),
|
|
577
|
+
highlight: guard(MAILY_MARK_TYPES.HIGHLIGHT),
|
|
578
|
+
span: guard(MAILY_MARK_TYPES.SPAN)
|
|
579
|
+
};
|
|
580
|
+
function isDef(value) {
|
|
581
|
+
return value !== void 0 && value !== null;
|
|
570
582
|
}
|
|
571
583
|
//#endregion
|
|
572
584
|
//#region src/object.ts
|
|
573
|
-
function
|
|
585
|
+
function isMergeableObject(value) {
|
|
574
586
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
575
587
|
}
|
|
576
588
|
/**
|
|
@@ -584,332 +596,256 @@ function isMergableObject(value) {
|
|
|
584
596
|
*/
|
|
585
597
|
function deepMerge(target, ...sources) {
|
|
586
598
|
const result = { ...target };
|
|
587
|
-
for (const source of sources) for (const [key, value] of Object.entries(source)) result[key] =
|
|
599
|
+
for (const source of sources) for (const [key, value] of Object.entries(source)) result[key] = isMergeableObject(value) && isMergeableObject(result[key]) ? {
|
|
588
600
|
...result[key],
|
|
589
601
|
...value
|
|
590
602
|
} : value;
|
|
591
603
|
return result;
|
|
592
604
|
}
|
|
593
|
-
function isObjectDeepEqual(first, second) {
|
|
594
|
-
if (first === second) return true;
|
|
595
|
-
for (const key of Object.keys(first)) if (first[key] !== second[key]) return false;
|
|
596
|
-
return true;
|
|
597
|
-
}
|
|
598
605
|
//#endregion
|
|
599
|
-
//#region src/
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
606
|
+
//#region src/spacing.ts
|
|
607
|
+
const BLOCK_CONTAINER_TYPES = [
|
|
608
|
+
MAILY_NODE_TYPES.DOCUMENT,
|
|
609
|
+
MAILY_NODE_TYPES.BODY,
|
|
610
|
+
MAILY_NODE_TYPES.CONTAINER,
|
|
611
|
+
MAILY_NODE_TYPES.SECTION,
|
|
612
|
+
MAILY_NODE_TYPES.DIV,
|
|
613
|
+
MAILY_NODE_TYPES.COLUMN,
|
|
614
|
+
MAILY_NODE_TYPES.REPEAT,
|
|
615
|
+
MAILY_NODE_TYPES.BULLET_LIST,
|
|
616
|
+
MAILY_NODE_TYPES.ORDERED_LIST
|
|
617
|
+
];
|
|
618
|
+
const MARGIN_TOP_ABSORBER_TYPES = [MAILY_NODE_TYPES.SPACER];
|
|
619
|
+
const MARGIN_BOTTOM_ABSORBER_TYPES = [MAILY_NODE_TYPES.SPACER, MAILY_NODE_TYPES.HORIZONTAL_RULE];
|
|
620
|
+
function getSuppressedMargins(position) {
|
|
621
|
+
const { isFirst, isLast, nextType, parentType, previousType } = position;
|
|
622
|
+
const stacks = parentType !== void 0 && BLOCK_CONTAINER_TYPES.includes(parentType);
|
|
608
623
|
return {
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
paddingBottom: `${paddingBottom}px`,
|
|
612
|
-
paddingLeft: `${paddingLeft}px`
|
|
624
|
+
marginBottom: !stacks || isLast || MARGIN_BOTTOM_ABSORBER_TYPES.includes(nextType ?? ""),
|
|
625
|
+
marginTop: !stacks || isFirst || MARGIN_TOP_ABSORBER_TYPES.includes(previousType ?? "")
|
|
613
626
|
};
|
|
614
627
|
}
|
|
615
628
|
//#endregion
|
|
616
|
-
//#region src/
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
629
|
+
//#region src/style.ts
|
|
630
|
+
function parseInlineStyles(style) {
|
|
631
|
+
return objectify(style, { reactify: true });
|
|
632
|
+
}
|
|
633
|
+
function stringifyInlineStyles(style) {
|
|
634
|
+
return stringify(style, { kebabCase: true });
|
|
635
|
+
}
|
|
636
|
+
function expandInlineStyles(style) {
|
|
637
|
+
return expand(style, { camelCase: true });
|
|
638
|
+
}
|
|
639
|
+
function collapseInlineStyles(style) {
|
|
640
|
+
return collapse(style, { camelCase: true });
|
|
641
|
+
}
|
|
642
|
+
function mergeInlineStyles(under, over) {
|
|
643
|
+
return collapseInlineStyles({
|
|
644
|
+
...expandInlineStyles(under),
|
|
645
|
+
...expandInlineStyles(over)
|
|
646
|
+
});
|
|
620
647
|
}
|
|
621
648
|
//#endregion
|
|
622
|
-
//#region src/
|
|
623
|
-
/** Text direction values matching the HTML `dir` attribute: ltr, rtl, auto. */
|
|
649
|
+
//#region src/direction.ts
|
|
624
650
|
const TEXT_DIRECTIONS = {
|
|
625
651
|
LTR: "ltr",
|
|
626
652
|
RTL: "rtl",
|
|
627
653
|
AUTO: "auto"
|
|
628
654
|
};
|
|
629
|
-
const allowedTextDirections = Object.freeze(Object.values(TEXT_DIRECTIONS));
|
|
630
655
|
const DEFAULT_TEXT_DIRECTION = TEXT_DIRECTIONS.AUTO;
|
|
631
|
-
function isAllowedTextDirection(value) {
|
|
632
|
-
return typeof value === "string" && allowedTextDirections.includes(value);
|
|
633
|
-
}
|
|
634
|
-
/**
|
|
635
|
-
* Returns a CSS `direction` property object for the given dir value.
|
|
636
|
-
* Returns an empty object for "auto" or nullish values, since the
|
|
637
|
-
* browser default is sufficient in those cases.
|
|
638
|
-
*/
|
|
639
656
|
function getCssDirection(dir) {
|
|
640
657
|
if (dir && dir !== TEXT_DIRECTIONS.AUTO) return { direction: dir };
|
|
641
658
|
return {};
|
|
642
659
|
}
|
|
643
660
|
//#endregion
|
|
644
|
-
//#region src/theme.ts
|
|
645
|
-
const DEFAULT_LINK_TEXT_COLOR = "#111827";
|
|
646
|
-
const DEFAULT_RENDERER_THEME = {
|
|
647
|
-
paragraph: {
|
|
648
|
-
color: "#374151",
|
|
649
|
-
fontSize: 15,
|
|
650
|
-
lineHeight: 1.75,
|
|
651
|
-
fontWeight: FONT_WEIGHTS.NORMAL
|
|
652
|
-
},
|
|
653
|
-
heading: {
|
|
654
|
-
defaults: { color: "#111827" },
|
|
655
|
-
h1: {
|
|
656
|
-
fontSize: 36,
|
|
657
|
-
lineHeight: 1.1111111,
|
|
658
|
-
fontWeight: FONT_WEIGHTS.SEMIBOLD
|
|
659
|
-
},
|
|
660
|
-
h2: {
|
|
661
|
-
fontSize: 30,
|
|
662
|
-
lineHeight: 1.3333333,
|
|
663
|
-
fontWeight: FONT_WEIGHTS.SEMIBOLD
|
|
664
|
-
},
|
|
665
|
-
h3: {
|
|
666
|
-
fontSize: 24,
|
|
667
|
-
lineHeight: 1.6,
|
|
668
|
-
fontWeight: FONT_WEIGHTS.SEMIBOLD
|
|
669
|
-
}
|
|
670
|
-
},
|
|
671
|
-
footer: {
|
|
672
|
-
color: "#64748B",
|
|
673
|
-
fontSize: 14,
|
|
674
|
-
lineHeight: 1.7142857,
|
|
675
|
-
fontWeight: FONT_WEIGHTS.NORMAL
|
|
676
|
-
},
|
|
677
|
-
blockquote: {
|
|
678
|
-
color: "#374151",
|
|
679
|
-
borderColor: "#D1D5DB",
|
|
680
|
-
fontSize: 15,
|
|
681
|
-
lineHeight: 1.75,
|
|
682
|
-
fontWeight: FONT_WEIGHTS.MEDIUM,
|
|
683
|
-
fontStyle: FONT_STYLES.ITALIC
|
|
684
|
-
},
|
|
685
|
-
horizontalRule: { color: "#EAEAEA" },
|
|
686
|
-
code: {
|
|
687
|
-
color: "#111827",
|
|
688
|
-
backgroundColor: "#EFEFEF"
|
|
689
|
-
},
|
|
690
|
-
linkCard: {
|
|
691
|
-
titleColor: "#111827",
|
|
692
|
-
descriptionColor: "#374151",
|
|
693
|
-
badgeTextColor: "#374151",
|
|
694
|
-
badgeBackgroundColor: "#fff085",
|
|
695
|
-
subTitleColor: "#6B7280",
|
|
696
|
-
borderColor: "#E5E7EB"
|
|
697
|
-
},
|
|
698
|
-
container: {
|
|
699
|
-
backgroundColor: "#ffffff",
|
|
700
|
-
maxWidth: 600,
|
|
701
|
-
minWidth: 300,
|
|
702
|
-
paddingTop: 8,
|
|
703
|
-
paddingRight: 8,
|
|
704
|
-
paddingBottom: 8,
|
|
705
|
-
paddingLeft: 8,
|
|
706
|
-
borderRadius: 0,
|
|
707
|
-
borderWidth: 0,
|
|
708
|
-
borderColor: "transparent"
|
|
709
|
-
},
|
|
710
|
-
body: {
|
|
711
|
-
backgroundColor: "#ffffff",
|
|
712
|
-
paddingTop: 0,
|
|
713
|
-
paddingRight: 0,
|
|
714
|
-
paddingBottom: 0,
|
|
715
|
-
paddingLeft: 0
|
|
716
|
-
},
|
|
717
|
-
button: {
|
|
718
|
-
backgroundColor: "#000000",
|
|
719
|
-
color: "#ffffff",
|
|
720
|
-
paddingTop: 10,
|
|
721
|
-
paddingRight: 32,
|
|
722
|
-
paddingBottom: 10,
|
|
723
|
-
paddingLeft: 32,
|
|
724
|
-
fontSize: 14,
|
|
725
|
-
lineHeight: 1.4285714,
|
|
726
|
-
fontWeight: FONT_WEIGHTS.SEMIBOLD
|
|
727
|
-
},
|
|
728
|
-
link: { color: DEFAULT_LINK_TEXT_COLOR },
|
|
729
|
-
listMarker: { color: "#d1d5dc" },
|
|
730
|
-
font: DEFAULT_FONT
|
|
731
|
-
};
|
|
732
|
-
const DEFAULT_EDITOR_THEME = {
|
|
733
|
-
container: {
|
|
734
|
-
backgroundColor: "#ffffff",
|
|
735
|
-
maxWidth: 600,
|
|
736
|
-
minWidth: 300,
|
|
737
|
-
paddingTop: 8,
|
|
738
|
-
paddingRight: 8,
|
|
739
|
-
paddingBottom: 8,
|
|
740
|
-
paddingLeft: 8,
|
|
741
|
-
borderRadius: 0,
|
|
742
|
-
borderWidth: 0,
|
|
743
|
-
borderColor: "transparent"
|
|
744
|
-
},
|
|
745
|
-
body: {
|
|
746
|
-
backgroundColor: "#ffffff",
|
|
747
|
-
paddingTop: 0,
|
|
748
|
-
paddingRight: 0,
|
|
749
|
-
paddingBottom: 0,
|
|
750
|
-
paddingLeft: 0
|
|
751
|
-
},
|
|
752
|
-
button: {
|
|
753
|
-
backgroundColor: "#000000",
|
|
754
|
-
color: "#ffffff",
|
|
755
|
-
paddingTop: 10,
|
|
756
|
-
paddingRight: 32,
|
|
757
|
-
paddingBottom: 10,
|
|
758
|
-
paddingLeft: 32
|
|
759
|
-
},
|
|
760
|
-
link: { color: DEFAULT_LINK_TEXT_COLOR },
|
|
761
|
-
font: DEFAULT_FONT
|
|
762
|
-
};
|
|
763
|
-
/**
|
|
764
|
-
* Resolves the baseline font defaults for a node type from the
|
|
765
|
-
* renderer theme. Falls back to paragraph defaults for unknown types.
|
|
766
|
-
*/
|
|
767
|
-
function getNodeFontStyleDefaults(nodeType, level) {
|
|
768
|
-
const paragraph = DEFAULT_RENDERER_THEME.paragraph;
|
|
769
|
-
const fallback = {
|
|
770
|
-
fontSize: paragraph.fontSize,
|
|
771
|
-
lineHeight: paragraph.lineHeight,
|
|
772
|
-
fontWeight: paragraph.fontWeight
|
|
773
|
-
};
|
|
774
|
-
if (nodeType === MAILY_NODE_TYPES.HEADING) {
|
|
775
|
-
const heading = DEFAULT_RENDERER_THEME.heading;
|
|
776
|
-
const key = `h${level ?? 1}`;
|
|
777
|
-
const entry = heading?.[key];
|
|
778
|
-
return {
|
|
779
|
-
fontSize: entry?.fontSize ?? fallback.fontSize,
|
|
780
|
-
lineHeight: entry?.lineHeight ?? fallback.lineHeight,
|
|
781
|
-
fontWeight: entry?.fontWeight ?? fallback.fontWeight
|
|
782
|
-
};
|
|
783
|
-
}
|
|
784
|
-
if (nodeType === MAILY_NODE_TYPES.BUTTON) {
|
|
785
|
-
const btn = DEFAULT_RENDERER_THEME.button;
|
|
786
|
-
return {
|
|
787
|
-
fontSize: btn?.fontSize ?? fallback.fontSize,
|
|
788
|
-
lineHeight: btn?.lineHeight ?? fallback.lineHeight,
|
|
789
|
-
fontWeight: btn?.fontWeight ?? fallback.fontWeight
|
|
790
|
-
};
|
|
791
|
-
}
|
|
792
|
-
if (nodeType === MAILY_NODE_TYPES.BLOCKQUOTE) {
|
|
793
|
-
const bq = DEFAULT_RENDERER_THEME.blockquote;
|
|
794
|
-
return {
|
|
795
|
-
fontSize: bq?.fontSize ?? fallback.fontSize,
|
|
796
|
-
lineHeight: bq?.lineHeight ?? fallback.lineHeight,
|
|
797
|
-
fontWeight: bq?.fontWeight ?? fallback.fontWeight,
|
|
798
|
-
fontStyle: bq?.fontStyle ?? void 0
|
|
799
|
-
};
|
|
800
|
-
}
|
|
801
|
-
if (nodeType === MAILY_NODE_TYPES.FOOTER) {
|
|
802
|
-
const ft = DEFAULT_RENDERER_THEME.footer;
|
|
803
|
-
return {
|
|
804
|
-
fontSize: ft?.fontSize ?? fallback.fontSize,
|
|
805
|
-
lineHeight: ft?.lineHeight ?? fallback.lineHeight,
|
|
806
|
-
fontWeight: ft?.fontWeight ?? fallback.fontWeight
|
|
807
|
-
};
|
|
808
|
-
}
|
|
809
|
-
return fallback;
|
|
810
|
-
}
|
|
811
|
-
//#endregion
|
|
812
661
|
//#region src/variable.ts
|
|
813
|
-
/** Default opening delimiter for variable placeholders in text. */
|
|
814
662
|
const DEFAULT_VARIABLE_START_TRIGGER = "{{";
|
|
815
|
-
/** Default closing delimiter for variable placeholders in text. */
|
|
816
663
|
const DEFAULT_VARIABLE_END_TRIGGER = "}}";
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
/**
|
|
828
|
-
* Serializes a variable node's attributes into a delimited string.
|
|
829
|
-
* Output format: `{{id|label|required|hideDefaultValue}}`.
|
|
830
|
-
* Used to represent structured variable data as a template string
|
|
831
|
-
* in plain text contexts.
|
|
832
|
-
*/
|
|
664
|
+
const LEGACY_HIDE_DEFAULT_VALUE = "hideDefaultValue";
|
|
665
|
+
function getQuotedValue(value) {
|
|
666
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
667
|
+
}
|
|
668
|
+
function getSerializedAttribute(options) {
|
|
669
|
+
const { name, value } = options;
|
|
670
|
+
if (value === void 0 || value === DEFAULT_VARIABLE_ATTRIBUTES[name]) return null;
|
|
671
|
+
if (typeof value === "boolean") return `${name}=${value}`;
|
|
672
|
+
return typeof value === "string" ? `${name}=${getQuotedValue(value)}` : null;
|
|
673
|
+
}
|
|
833
674
|
function serializeVariableToText(variable) {
|
|
834
|
-
const { id, label, required
|
|
675
|
+
const { fallback, id, isDefaultEditable, label, required, style } = variable;
|
|
835
676
|
return [
|
|
836
677
|
"{{",
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
678
|
+
[id, ...[
|
|
679
|
+
getSerializedAttribute({
|
|
680
|
+
name: "fallback",
|
|
681
|
+
value: fallback
|
|
682
|
+
}),
|
|
683
|
+
getSerializedAttribute({
|
|
684
|
+
name: "isDefaultEditable",
|
|
685
|
+
value: isDefaultEditable
|
|
686
|
+
}),
|
|
687
|
+
getSerializedAttribute({
|
|
688
|
+
name: "label",
|
|
689
|
+
value: label === id ? null : label
|
|
690
|
+
}),
|
|
691
|
+
getSerializedAttribute({
|
|
692
|
+
name: "required",
|
|
693
|
+
value: required
|
|
694
|
+
}),
|
|
695
|
+
getSerializedAttribute({
|
|
696
|
+
name: "style",
|
|
697
|
+
value: style
|
|
698
|
+
})
|
|
699
|
+
].filter((attribute) => {
|
|
700
|
+
return attribute !== null;
|
|
701
|
+
})].join(" "),
|
|
844
702
|
"}}"
|
|
845
703
|
].join("");
|
|
846
704
|
}
|
|
847
|
-
function
|
|
848
|
-
|
|
705
|
+
function getReferenceTokens(inner) {
|
|
706
|
+
const tokens = [];
|
|
707
|
+
let token = "";
|
|
708
|
+
let quoted = false;
|
|
709
|
+
for (let index = 0; index < inner.length; index += 1) {
|
|
710
|
+
const character = inner[index];
|
|
711
|
+
if (quoted && character === "\\" && index + 1 < inner.length) {
|
|
712
|
+
token += character + inner[index + 1];
|
|
713
|
+
index += 1;
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
if (character === "\"") {
|
|
717
|
+
quoted = !quoted;
|
|
718
|
+
token += character;
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
if (!quoted && /\s/.test(character)) {
|
|
722
|
+
if (token) {
|
|
723
|
+
tokens.push(token);
|
|
724
|
+
token = "";
|
|
725
|
+
}
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
token += character;
|
|
729
|
+
}
|
|
730
|
+
if (token) tokens.push(token);
|
|
731
|
+
return tokens;
|
|
732
|
+
}
|
|
733
|
+
function getUnquotedValue(value) {
|
|
734
|
+
if (value.startsWith("\"") && value.endsWith("\"") && value.length > 1) return value.slice(1, -1).replace(/\\"/g, "\"").replace(/\\\\/g, "\\");
|
|
735
|
+
if (value === "true") return true;
|
|
736
|
+
if (value === "false") return false;
|
|
737
|
+
return value;
|
|
849
738
|
}
|
|
850
|
-
/**
|
|
851
|
-
* Deserializes a variable string back into VariableAttributes.
|
|
852
|
-
* Expected input format: `{{id|label|required|hideDefaultValue}}`.
|
|
853
|
-
* Missing fields default to undefined; boolean fields are parsed
|
|
854
|
-
* from "true"/"false" strings.
|
|
855
|
-
*/
|
|
856
739
|
function deserializeVariableFromText(text) {
|
|
857
|
-
const [id
|
|
740
|
+
const [id = "", ...pairs] = getReferenceTokens(text.slice(2, text.length - 2).trim());
|
|
741
|
+
const attributes = { id };
|
|
742
|
+
for (const pair of pairs) {
|
|
743
|
+
const separator = pair.indexOf("=");
|
|
744
|
+
if (separator <= 0) continue;
|
|
745
|
+
const key = pair.slice(0, separator);
|
|
746
|
+
const value = getUnquotedValue(pair.slice(separator + 1));
|
|
747
|
+
if (key === LEGACY_HIDE_DEFAULT_VALUE) {
|
|
748
|
+
attributes.isDefaultEditable ??= value !== true;
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
attributes[key] = value;
|
|
752
|
+
}
|
|
858
753
|
return {
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
required: parseBoolean(required, true),
|
|
862
|
-
hideDefaultValue: parseBoolean(hideDefaultValue, false)
|
|
754
|
+
...DEFAULT_VARIABLE_ATTRIBUTES,
|
|
755
|
+
...attributes
|
|
863
756
|
};
|
|
864
757
|
}
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
758
|
+
function getVariableSegments(content) {
|
|
759
|
+
const segments = [];
|
|
760
|
+
let plain = "";
|
|
761
|
+
let index = 0;
|
|
762
|
+
while (index < content.length) {
|
|
763
|
+
if (!content.startsWith("{{", index)) {
|
|
764
|
+
plain += content[index];
|
|
765
|
+
index += 1;
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
const end = getReferenceEnd({
|
|
769
|
+
content,
|
|
770
|
+
start: index
|
|
771
|
+
});
|
|
772
|
+
if (end === -1) {
|
|
773
|
+
plain += content[index];
|
|
774
|
+
index += 1;
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
if (plain) {
|
|
778
|
+
segments.push({
|
|
779
|
+
isReference: false,
|
|
780
|
+
text: plain
|
|
781
|
+
});
|
|
782
|
+
plain = "";
|
|
783
|
+
}
|
|
784
|
+
segments.push({
|
|
785
|
+
isReference: true,
|
|
786
|
+
text: content.slice(index, end)
|
|
787
|
+
});
|
|
788
|
+
index = end;
|
|
789
|
+
}
|
|
790
|
+
if (plain) segments.push({
|
|
791
|
+
isReference: false,
|
|
792
|
+
text: plain
|
|
793
|
+
});
|
|
794
|
+
return segments;
|
|
795
|
+
}
|
|
796
|
+
function getReferenceEnd(options) {
|
|
797
|
+
const { content, start } = options;
|
|
798
|
+
let quoted = false;
|
|
799
|
+
for (let index = start + 2; index < content.length; index += 1) {
|
|
800
|
+
const character = content[index];
|
|
801
|
+
if (quoted && character === "\\") {
|
|
802
|
+
index += 1;
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
if (character === "\"") {
|
|
806
|
+
quoted = !quoted;
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
if (!quoted && content.startsWith("}}", index)) return index + 2;
|
|
810
|
+
}
|
|
811
|
+
return -1;
|
|
812
|
+
}
|
|
871
813
|
function buildDocFromVariableText(content) {
|
|
872
|
-
const parts = content.split(VARIABLE_PLACEHOLDER_REGEX);
|
|
873
814
|
return {
|
|
874
815
|
type: MAILY_NODE_TYPES.DOCUMENT,
|
|
875
816
|
content: [{
|
|
876
817
|
type: MAILY_NODE_TYPES.PARAGRAPH,
|
|
877
|
-
content:
|
|
878
|
-
|
|
818
|
+
content: getVariableSegments(content).map((segment) => {
|
|
819
|
+
return segment.isReference ? {
|
|
879
820
|
type: MAILY_NODE_TYPES.VARIABLE,
|
|
880
|
-
attrs: deserializeVariableFromText(
|
|
881
|
-
}
|
|
882
|
-
if (part) return {
|
|
821
|
+
attrs: deserializeVariableFromText(segment.text)
|
|
822
|
+
} : {
|
|
883
823
|
type: MAILY_NODE_TYPES.TEXT,
|
|
884
|
-
text:
|
|
824
|
+
text: segment.text
|
|
885
825
|
};
|
|
886
|
-
|
|
887
|
-
}).filter(Boolean)
|
|
826
|
+
})
|
|
888
827
|
}]
|
|
889
828
|
};
|
|
890
829
|
}
|
|
891
|
-
/**
|
|
892
|
-
* Checks if a string contains at least one variable placeholder.
|
|
893
|
-
* Returns true for strings like "Hello {{name}}", false for plain text.
|
|
894
|
-
* Uses index-based scanning instead of regex for performance.
|
|
895
|
-
*/
|
|
896
830
|
function hasVariableInText(text) {
|
|
897
831
|
const start = text.indexOf("{{");
|
|
898
832
|
if (start === -1) return false;
|
|
899
833
|
return text.indexOf("}}", start + 2) !== -1;
|
|
900
834
|
}
|
|
901
|
-
function parseBoolean(value, defaultValue) {
|
|
902
|
-
if (value === "true") return true;
|
|
903
|
-
if (value === "false") return false;
|
|
904
|
-
return defaultValue;
|
|
905
|
-
}
|
|
906
835
|
//#endregion
|
|
907
836
|
//#region src/visibility.ts
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
837
|
+
const VISIBILITY_ACTIONS = {
|
|
838
|
+
SHOW: "show",
|
|
839
|
+
HIDE: "hide"
|
|
840
|
+
};
|
|
841
|
+
const allowedVisibilityActions = Object.values(VISIBILITY_ACTIONS);
|
|
842
|
+
const VISIBILITY_ACTION_OPTIONS = [{
|
|
843
|
+
label: "Show if",
|
|
844
|
+
value: VISIBILITY_ACTIONS.SHOW
|
|
845
|
+
}, {
|
|
846
|
+
label: "Hide if",
|
|
847
|
+
value: VISIBILITY_ACTIONS.HIDE
|
|
848
|
+
}];
|
|
913
849
|
const VISIBILITY_OPERATORS = {
|
|
914
850
|
IS_TRUE: "is_true",
|
|
915
851
|
IS_FALSE: "is_false",
|
|
@@ -922,73 +858,324 @@ const VISIBILITY_OPERATORS = {
|
|
|
922
858
|
GREATER_THAN: "greater_than",
|
|
923
859
|
LESS_THAN: "less_than"
|
|
924
860
|
};
|
|
861
|
+
const allowedVisibilityOperators = Object.values(VISIBILITY_OPERATORS);
|
|
925
862
|
const VISIBILITY_OPERATOR_OPTIONS = [
|
|
926
863
|
{
|
|
927
|
-
|
|
928
|
-
|
|
864
|
+
label: "Is true",
|
|
865
|
+
value: VISIBILITY_OPERATORS.IS_TRUE
|
|
929
866
|
},
|
|
930
867
|
{
|
|
931
|
-
|
|
932
|
-
|
|
868
|
+
label: "Is false",
|
|
869
|
+
value: VISIBILITY_OPERATORS.IS_FALSE
|
|
933
870
|
},
|
|
934
871
|
{
|
|
935
|
-
|
|
936
|
-
|
|
872
|
+
label: "Equals",
|
|
873
|
+
value: VISIBILITY_OPERATORS.EQUALS
|
|
937
874
|
},
|
|
938
875
|
{
|
|
939
|
-
|
|
940
|
-
|
|
876
|
+
label: "Not equals",
|
|
877
|
+
value: VISIBILITY_OPERATORS.NOT_EQUALS
|
|
941
878
|
},
|
|
942
879
|
{
|
|
943
|
-
|
|
944
|
-
|
|
880
|
+
label: "Contains",
|
|
881
|
+
value: VISIBILITY_OPERATORS.CONTAINS
|
|
945
882
|
},
|
|
946
883
|
{
|
|
947
|
-
|
|
948
|
-
|
|
884
|
+
label: "Not contains",
|
|
885
|
+
value: VISIBILITY_OPERATORS.NOT_CONTAINS
|
|
949
886
|
},
|
|
950
887
|
{
|
|
951
|
-
|
|
952
|
-
|
|
888
|
+
label: "Starts with",
|
|
889
|
+
value: VISIBILITY_OPERATORS.STARTS_WITH
|
|
953
890
|
},
|
|
954
891
|
{
|
|
955
|
-
|
|
956
|
-
|
|
892
|
+
label: "Ends with",
|
|
893
|
+
value: VISIBILITY_OPERATORS.ENDS_WITH
|
|
957
894
|
},
|
|
958
895
|
{
|
|
959
|
-
|
|
960
|
-
|
|
896
|
+
label: "Greater than",
|
|
897
|
+
value: VISIBILITY_OPERATORS.GREATER_THAN
|
|
961
898
|
},
|
|
962
899
|
{
|
|
963
|
-
|
|
964
|
-
|
|
900
|
+
label: "Less than",
|
|
901
|
+
value: VISIBILITY_OPERATORS.LESS_THAN
|
|
965
902
|
}
|
|
966
903
|
];
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
NONE: "none",
|
|
974
|
-
SHOW: "show",
|
|
975
|
-
HIDE: "hide"
|
|
904
|
+
const VISIBILITY_OPERATORS_WITHOUT_VALUE = [VISIBILITY_OPERATORS.IS_TRUE, VISIBILITY_OPERATORS.IS_FALSE];
|
|
905
|
+
const DEFAULT_VISIBILITY_RULE = {
|
|
906
|
+
action: VISIBILITY_ACTIONS.SHOW,
|
|
907
|
+
operator: VISIBILITY_OPERATORS.EQUALS,
|
|
908
|
+
value: "",
|
|
909
|
+
variable: ""
|
|
976
910
|
};
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
911
|
+
function getVisibilityValueSupport(operator) {
|
|
912
|
+
return !VISIBILITY_OPERATORS_WITHOUT_VALUE.includes(operator);
|
|
913
|
+
}
|
|
914
|
+
function getVisibilityRule(options) {
|
|
915
|
+
const { action, operator, value, variable } = options;
|
|
916
|
+
const isAllowedAction = allowedVisibilityActions.some((candidate) => {
|
|
917
|
+
return candidate === action;
|
|
918
|
+
});
|
|
919
|
+
const isAllowedOperator = allowedVisibilityOperators.some((candidate) => {
|
|
920
|
+
return candidate === operator;
|
|
921
|
+
});
|
|
922
|
+
if (!(isAllowedAction && isAllowedOperator)) return null;
|
|
923
|
+
return {
|
|
924
|
+
action,
|
|
925
|
+
operator,
|
|
926
|
+
value: getVisibilityValueSupport(operator) ? value ?? "" : "",
|
|
927
|
+
variable: variable ?? ""
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
//#endregion
|
|
931
|
+
//#region src/theme.ts
|
|
932
|
+
const DEFAULT_CONTAINER_MAX_WIDTH = 600;
|
|
933
|
+
const BASIC_NODE_THEME = {
|
|
934
|
+
blockquote: {
|
|
935
|
+
borderColor: "#d1d5db",
|
|
936
|
+
borderLeftStyle: "solid",
|
|
937
|
+
borderLeftWidth: "4px",
|
|
938
|
+
color: "#374151",
|
|
939
|
+
fontSize: "15px",
|
|
940
|
+
fontStyle: "italic",
|
|
941
|
+
fontWeight: "500",
|
|
942
|
+
lineHeight: "26px",
|
|
943
|
+
marginBottom: "20px",
|
|
944
|
+
marginLeft: "0",
|
|
945
|
+
marginRight: "0",
|
|
946
|
+
marginTop: "20px",
|
|
947
|
+
paddingLeft: "16px"
|
|
981
948
|
},
|
|
982
|
-
{
|
|
983
|
-
|
|
984
|
-
|
|
949
|
+
body: { fontFamily: getFontFamilyValue({
|
|
950
|
+
fallbackFontFamily: DEFAULT_FONT.fallbackFontFamily,
|
|
951
|
+
fontFamily: DEFAULT_FONT.fontFamily
|
|
952
|
+
}) },
|
|
953
|
+
bulletList: {
|
|
954
|
+
fontSize: "14px",
|
|
955
|
+
lineHeight: "24px",
|
|
956
|
+
listStyleType: "disc",
|
|
957
|
+
margin: "0",
|
|
958
|
+
paddingLeft: "26px"
|
|
985
959
|
},
|
|
986
|
-
{
|
|
987
|
-
|
|
988
|
-
|
|
960
|
+
button: {
|
|
961
|
+
backgroundColor: "#000000",
|
|
962
|
+
borderRadius: "9999px",
|
|
963
|
+
color: "#ffffff",
|
|
964
|
+
fontSize: "14px",
|
|
965
|
+
fontWeight: "500",
|
|
966
|
+
lineHeight: "20px",
|
|
967
|
+
padding: "10px 32px"
|
|
968
|
+
},
|
|
969
|
+
code: {
|
|
970
|
+
borderRadius: "8px",
|
|
971
|
+
fontFamily: "ui-monospace, monospace",
|
|
972
|
+
fontSize: "85%",
|
|
973
|
+
padding: "2px 6px"
|
|
974
|
+
},
|
|
975
|
+
h1: {
|
|
976
|
+
fontSize: "36px",
|
|
977
|
+
fontWeight: "800",
|
|
978
|
+
lineHeight: "40px"
|
|
979
|
+
},
|
|
980
|
+
h2: {
|
|
981
|
+
fontSize: "30px",
|
|
982
|
+
fontWeight: "700",
|
|
983
|
+
lineHeight: "40px"
|
|
984
|
+
},
|
|
985
|
+
h3: {
|
|
986
|
+
fontSize: "24px",
|
|
987
|
+
fontWeight: "600",
|
|
988
|
+
lineHeight: "38px"
|
|
989
|
+
},
|
|
990
|
+
heading: {
|
|
991
|
+
color: "#101828",
|
|
992
|
+
marginBottom: "12px",
|
|
993
|
+
marginTop: "0"
|
|
994
|
+
},
|
|
995
|
+
horizontalRule: {
|
|
996
|
+
border: "none",
|
|
997
|
+
borderTop: "1px solid #eaeaea",
|
|
998
|
+
marginBottom: "32px",
|
|
999
|
+
marginTop: "32px"
|
|
1000
|
+
},
|
|
1001
|
+
htmlCodeBlock: { marginBottom: "32px" },
|
|
1002
|
+
link: {
|
|
1003
|
+
color: "#0000ee",
|
|
1004
|
+
textDecoration: "underline"
|
|
1005
|
+
},
|
|
1006
|
+
listItem: {
|
|
1007
|
+
marginBottom: "4px",
|
|
1008
|
+
paddingLeft: "6px"
|
|
1009
|
+
},
|
|
1010
|
+
orderedList: {
|
|
1011
|
+
fontSize: "14px",
|
|
1012
|
+
lineHeight: "24px",
|
|
1013
|
+
listStyleType: "decimal",
|
|
1014
|
+
margin: "0",
|
|
1015
|
+
paddingLeft: "26px"
|
|
1016
|
+
},
|
|
1017
|
+
footer: {
|
|
1018
|
+
color: "#64748B",
|
|
1019
|
+
display: "block",
|
|
1020
|
+
fontSize: "14px",
|
|
1021
|
+
fontWeight: "400",
|
|
1022
|
+
lineHeight: "24px",
|
|
1023
|
+
marginBottom: "0",
|
|
1024
|
+
marginTop: "0"
|
|
1025
|
+
},
|
|
1026
|
+
paragraph: {
|
|
1027
|
+
color: "#18181b",
|
|
1028
|
+
fontSize: "14px",
|
|
1029
|
+
lineHeight: "24px",
|
|
1030
|
+
marginBottom: "14px",
|
|
1031
|
+
marginTop: "0"
|
|
1032
|
+
},
|
|
1033
|
+
section: { padding: "8px" }
|
|
1034
|
+
};
|
|
1035
|
+
const MAILY_NODE_THEME = {
|
|
1036
|
+
blockquote: {
|
|
1037
|
+
borderLeftColor: "#d1d5db",
|
|
1038
|
+
borderLeftStyle: "solid",
|
|
1039
|
+
borderLeftWidth: "4px",
|
|
1040
|
+
color: "#374151",
|
|
1041
|
+
fontSize: "15px",
|
|
1042
|
+
fontStyle: "italic",
|
|
1043
|
+
fontWeight: "500",
|
|
1044
|
+
lineHeight: "1.75",
|
|
1045
|
+
marginBottom: "20px",
|
|
1046
|
+
marginLeft: "0",
|
|
1047
|
+
marginRight: "0",
|
|
1048
|
+
marginTop: "20px",
|
|
1049
|
+
paddingLeft: "16px"
|
|
1050
|
+
},
|
|
1051
|
+
body: {
|
|
1052
|
+
backgroundColor: "#ffffff",
|
|
1053
|
+
fontFamily: getFontFamilyValue({
|
|
1054
|
+
fallbackFontFamily: DEFAULT_FONT.fallbackFontFamily,
|
|
1055
|
+
fontFamily: DEFAULT_FONT.fontFamily
|
|
1056
|
+
}),
|
|
1057
|
+
padding: "0"
|
|
1058
|
+
},
|
|
1059
|
+
bulletList: {
|
|
1060
|
+
listStyleType: "disc",
|
|
1061
|
+
marginBottom: "20px",
|
|
1062
|
+
marginLeft: "0",
|
|
1063
|
+
marginRight: "0",
|
|
1064
|
+
marginTop: "0",
|
|
1065
|
+
paddingLeft: "26px"
|
|
1066
|
+
},
|
|
1067
|
+
button: {
|
|
1068
|
+
backgroundColor: "#000000",
|
|
1069
|
+
borderRadius: "9999px",
|
|
1070
|
+
color: "#ffffff",
|
|
1071
|
+
fontSize: "14px",
|
|
1072
|
+
fontWeight: "600",
|
|
1073
|
+
lineHeight: "1.4285714",
|
|
1074
|
+
marginBottom: "20px",
|
|
1075
|
+
padding: "10px 32px"
|
|
1076
|
+
},
|
|
1077
|
+
code: {
|
|
1078
|
+
backgroundColor: "#efefef",
|
|
1079
|
+
borderRadius: "8px",
|
|
1080
|
+
color: "#111827",
|
|
1081
|
+
fontFamily: "ui-monospace, monospace",
|
|
1082
|
+
fontSize: "85%",
|
|
1083
|
+
padding: "2px 6px"
|
|
1084
|
+
},
|
|
1085
|
+
container: {
|
|
1086
|
+
backgroundColor: "#ffffff",
|
|
1087
|
+
maxWidth: `600px`,
|
|
1088
|
+
minWidth: "300px",
|
|
1089
|
+
padding: "8px"
|
|
1090
|
+
},
|
|
1091
|
+
footer: {
|
|
1092
|
+
color: "#64748b",
|
|
1093
|
+
display: "block",
|
|
1094
|
+
fontSize: "14px",
|
|
1095
|
+
fontWeight: "400",
|
|
1096
|
+
lineHeight: "1.7142857",
|
|
1097
|
+
marginBottom: "20px",
|
|
1098
|
+
marginTop: "0"
|
|
1099
|
+
},
|
|
1100
|
+
h1: {
|
|
1101
|
+
fontSize: "36px",
|
|
1102
|
+
fontWeight: "600",
|
|
1103
|
+
lineHeight: "1.1111111"
|
|
1104
|
+
},
|
|
1105
|
+
h2: {
|
|
1106
|
+
fontSize: "30px",
|
|
1107
|
+
fontWeight: "600",
|
|
1108
|
+
lineHeight: "1.3333333"
|
|
1109
|
+
},
|
|
1110
|
+
h3: {
|
|
1111
|
+
fontSize: "24px",
|
|
1112
|
+
fontWeight: "600",
|
|
1113
|
+
lineHeight: "1.6"
|
|
1114
|
+
},
|
|
1115
|
+
heading: {
|
|
1116
|
+
color: "#111827",
|
|
1117
|
+
marginBottom: "12px",
|
|
1118
|
+
marginTop: "0"
|
|
1119
|
+
},
|
|
1120
|
+
horizontalRule: {
|
|
1121
|
+
border: "none",
|
|
1122
|
+
borderTop: "1px solid #eaeaea",
|
|
1123
|
+
marginBottom: "32px",
|
|
1124
|
+
marginTop: "32px"
|
|
1125
|
+
},
|
|
1126
|
+
htmlCodeBlock: { marginBottom: "32px" },
|
|
1127
|
+
link: {
|
|
1128
|
+
color: "#111827",
|
|
1129
|
+
textDecoration: "underline"
|
|
1130
|
+
},
|
|
1131
|
+
listItem: {
|
|
1132
|
+
color: "#d1d5dc",
|
|
1133
|
+
marginBottom: "8px",
|
|
1134
|
+
marginTop: "8px",
|
|
1135
|
+
paddingLeft: "6px"
|
|
1136
|
+
},
|
|
1137
|
+
orderedList: {
|
|
1138
|
+
listStyleType: "decimal",
|
|
1139
|
+
marginBottom: "20px",
|
|
1140
|
+
marginLeft: "0",
|
|
1141
|
+
marginRight: "0",
|
|
1142
|
+
marginTop: "0",
|
|
1143
|
+
paddingLeft: "26px"
|
|
1144
|
+
},
|
|
1145
|
+
paragraph: {
|
|
1146
|
+
color: "#374151",
|
|
1147
|
+
fontSize: "15px",
|
|
1148
|
+
fontWeight: "400",
|
|
1149
|
+
lineHeight: "1.75",
|
|
1150
|
+
marginBottom: "20px",
|
|
1151
|
+
marginTop: "0",
|
|
1152
|
+
MozOsxFontSmoothing: "grayscale",
|
|
1153
|
+
WebkitFontSmoothing: "antialiased"
|
|
1154
|
+
},
|
|
1155
|
+
section: {
|
|
1156
|
+
backgroundColor: "#f7f7f7",
|
|
1157
|
+
border: "2px solid #e2e2e2",
|
|
1158
|
+
borderRadius: "0",
|
|
1159
|
+
marginBottom: "20px",
|
|
1160
|
+
padding: "0"
|
|
989
1161
|
}
|
|
990
|
-
|
|
1162
|
+
};
|
|
1163
|
+
function extendNodeTheme(options) {
|
|
1164
|
+
const { extension, preset } = options;
|
|
1165
|
+
return deepMerge(preset, extension);
|
|
1166
|
+
}
|
|
1167
|
+
function getThemeDeclarations(options) {
|
|
1168
|
+
const { attrs, theme, type } = options;
|
|
1169
|
+
const declarations = theme[type];
|
|
1170
|
+
if (type !== MAILY_NODE_TYPES.HEADING) return declarations;
|
|
1171
|
+
const level = typeof attrs?.level === "number" ? theme[`h${attrs.level}`] : void 0;
|
|
1172
|
+
if (!(declarations || level)) return;
|
|
1173
|
+
return {
|
|
1174
|
+
...declarations,
|
|
1175
|
+
...level
|
|
1176
|
+
};
|
|
1177
|
+
}
|
|
991
1178
|
//#endregion
|
|
992
|
-
export { BORDER_STYLES,
|
|
1179
|
+
export { BASIC_NODE_THEME, BLOCK_CONTAINER_TYPES, BORDER_STYLES, BOX_CORNERS, BOX_SIDES, BUTTON_KINDS, DATA_NODE_TYPE_KEY, DEFAULT_BORDER_STYLE, DEFAULT_BUTTON_KIND, DEFAULT_CONTAINER_MAX_WIDTH, DEFAULT_CONTAINER_WIDTH_PIXELS, DEFAULT_FONT, DEFAULT_FONT_FALLBACK, DEFAULT_FONT_FAMILIES, DEFAULT_FONT_FAMILY, DEFAULT_HTML_CODE_BLOCK_TAB, DEFAULT_TEXT_ALIGN, DEFAULT_TEXT_DIRECTION, DEFAULT_VARIABLE_ATTRIBUTES, DEFAULT_VARIABLE_END_TRIGGER, DEFAULT_VARIABLE_START_TRIGGER, DEFAULT_VISIBILITY_RULE, FONT_SIZE_PRESETS, FONT_STYLES, FONT_WEIGHTS, HTML_CODE_BLOCK_TABS, LINE_HEIGHT_PRESETS, MAILY_BUG_REPORT_URL, MAILY_ERROR_PREFIX, MAILY_EXTENSION_TYPES, MAILY_MARK_TYPES, MAILY_NODE_THEME, MAILY_NODE_TYPES, MARGIN_BOTTOM_ABSORBER_TYPES, MARGIN_TOP_ABSORBER_TYPES, MAX_IMAGE_WIDTH_PERCENTAGE, MIN_IMAGE_WIDTH_PERCENTAGE, TEXT_ALIGNMENTS, TEXT_DIRECTIONS, VISIBILITY_ACTIONS, VISIBILITY_ACTION_OPTIONS, VISIBILITY_OPERATORS, VISIBILITY_OPERATOR_OPTIONS, absoluteFromPercentage, allowedFallbackFonts, allowedMarkTypes, allowedNodeTypes, buildDocFromVariableText, clamp, clampImageWidthPercentage, deepMerge, deserializeVariableFromText, expandInlineStyles, extendNodeTheme, getBorderParts, getBoxCorners, getBoxSides, getColumnWidth, getColumnWidthPercent, getColumnWidthValue, getCssDirection, getFontFaceStyle, getFontFamilyNames, getFontFamilyValue, getSuppressedMargins, getThemeDeclarations, getVariableSegments, getVisibilityRule, getVisibilityValueSupport, hasVariableInText, invariant, is, isAllowedBorderStyle, isAllowedButtonKind, isAllowedTextAlignment, isDef, isMergeableObject, isObject, loadFont, mergeInlineStyles, noop, parseInlineStyles, parseWidthToPercentage, percentage, roundTo, serializeVariableToText, stringifyInlineStyles, toBoxCorners, toBoxSides };
|
|
993
1180
|
|
|
994
1181
|
//# sourceMappingURL=index.mjs.map
|