@maily-to/shared 2.0.0-beta.6 → 2.0.0-beta.8
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 +872 -692
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +465 -669
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +465 -669
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +840 -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,131 @@ 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
|
+
COLUMNS: "columns",
|
|
480
|
+
COLUMN: "column",
|
|
481
|
+
IMAGE: "image",
|
|
482
|
+
INLINE_IMAGE: "inlineImage",
|
|
483
|
+
INLINE_SPAN: "inlineSpan",
|
|
484
|
+
SPACER: "spacer",
|
|
485
|
+
BUTTON: "button",
|
|
486
|
+
FOOTER: "footer",
|
|
487
|
+
PARAGRAPH: "paragraph",
|
|
488
|
+
HEADING: "heading",
|
|
489
|
+
BLOCKQUOTE: "blockquote",
|
|
490
|
+
HORIZONTAL_RULE: "horizontalRule",
|
|
491
|
+
HTML_CODE_BLOCK: "htmlCodeBlock",
|
|
492
|
+
REPEAT: "repeat",
|
|
493
|
+
HARD_BREAK: "hardBreak",
|
|
494
|
+
BULLET_LIST: "bulletList",
|
|
495
|
+
ORDERED_LIST: "orderedList",
|
|
496
|
+
LIST_ITEM: "listItem",
|
|
497
|
+
TEXT: "text",
|
|
498
|
+
VARIABLE: "variable",
|
|
499
|
+
TABLE: "table",
|
|
500
|
+
TABLE_ROW: "tableRow",
|
|
501
|
+
TABLE_CELL: "tableCell",
|
|
502
|
+
TABLE_HEADER: "tableHeader",
|
|
503
|
+
LINK_CARD: "linkCard"
|
|
504
|
+
};
|
|
505
|
+
const MAILY_EXTENSION_TYPES = {
|
|
506
|
+
STYLE: "style",
|
|
507
|
+
THEME: "theme",
|
|
508
|
+
VISIBILITY: "visibility"
|
|
509
|
+
};
|
|
510
|
+
const allowedNodeTypes = Object.values(MAILY_NODE_TYPES);
|
|
511
|
+
const DEFAULT_VARIABLE_ATTRIBUTES = {
|
|
512
|
+
fallback: null,
|
|
513
|
+
isDefaultEditable: true,
|
|
514
|
+
label: null,
|
|
515
|
+
required: true,
|
|
516
|
+
style: null
|
|
517
|
+
};
|
|
518
|
+
//#endregion
|
|
519
|
+
//#region src/is.ts
|
|
520
|
+
function guard(type) {
|
|
521
|
+
return (value) => value.type === type;
|
|
522
|
+
}
|
|
523
|
+
function isObject(val) {
|
|
524
|
+
return typeof val === "object" && val !== null;
|
|
525
|
+
}
|
|
526
|
+
const is = {
|
|
527
|
+
node(value) {
|
|
528
|
+
return typeof value === "object" && value !== null && "type" in value && allowedNodeTypes.includes(value.type);
|
|
529
|
+
},
|
|
530
|
+
mark(value) {
|
|
531
|
+
return typeof value === "object" && value !== null && "type" in value && allowedMarkTypes.includes(value.type);
|
|
532
|
+
},
|
|
533
|
+
parent(node) {
|
|
534
|
+
return Array.isArray(node?.content);
|
|
535
|
+
},
|
|
536
|
+
marked(node) {
|
|
537
|
+
return Array.isArray(node?.marks);
|
|
538
|
+
},
|
|
539
|
+
doc: guard(MAILY_NODE_TYPES.DOCUMENT),
|
|
540
|
+
body: guard(MAILY_NODE_TYPES.BODY),
|
|
541
|
+
container: guard(MAILY_NODE_TYPES.CONTAINER),
|
|
542
|
+
section: guard(MAILY_NODE_TYPES.SECTION),
|
|
543
|
+
columns: guard(MAILY_NODE_TYPES.COLUMNS),
|
|
544
|
+
column: guard(MAILY_NODE_TYPES.COLUMN),
|
|
545
|
+
image: guard(MAILY_NODE_TYPES.IMAGE),
|
|
546
|
+
inlineImage: guard(MAILY_NODE_TYPES.INLINE_IMAGE),
|
|
547
|
+
inlineSpan: guard(MAILY_NODE_TYPES.INLINE_SPAN),
|
|
548
|
+
spacer: guard(MAILY_NODE_TYPES.SPACER),
|
|
549
|
+
button: guard(MAILY_NODE_TYPES.BUTTON),
|
|
550
|
+
footer: guard(MAILY_NODE_TYPES.FOOTER),
|
|
551
|
+
paragraph: guard(MAILY_NODE_TYPES.PARAGRAPH),
|
|
552
|
+
heading: guard(MAILY_NODE_TYPES.HEADING),
|
|
553
|
+
blockquote: guard(MAILY_NODE_TYPES.BLOCKQUOTE),
|
|
554
|
+
horizontalRule: guard(MAILY_NODE_TYPES.HORIZONTAL_RULE),
|
|
555
|
+
hardBreak: guard(MAILY_NODE_TYPES.HARD_BREAK),
|
|
556
|
+
htmlCodeBlock: guard(MAILY_NODE_TYPES.HTML_CODE_BLOCK),
|
|
557
|
+
repeat: guard(MAILY_NODE_TYPES.REPEAT),
|
|
558
|
+
bulletList: guard(MAILY_NODE_TYPES.BULLET_LIST),
|
|
559
|
+
orderedList: guard(MAILY_NODE_TYPES.ORDERED_LIST),
|
|
560
|
+
listItem: guard(MAILY_NODE_TYPES.LIST_ITEM),
|
|
561
|
+
linkCard: guard(MAILY_NODE_TYPES.LINK_CARD),
|
|
562
|
+
text: guard(MAILY_NODE_TYPES.TEXT),
|
|
563
|
+
variable: guard(MAILY_NODE_TYPES.VARIABLE),
|
|
564
|
+
table: guard(MAILY_NODE_TYPES.TABLE),
|
|
565
|
+
tableRow: guard(MAILY_NODE_TYPES.TABLE_ROW),
|
|
566
|
+
tableCell: guard(MAILY_NODE_TYPES.TABLE_CELL),
|
|
567
|
+
tableHeader: guard(MAILY_NODE_TYPES.TABLE_HEADER),
|
|
568
|
+
bold: guard(MAILY_MARK_TYPES.BOLD),
|
|
569
|
+
italic: guard(MAILY_MARK_TYPES.ITALIC),
|
|
570
|
+
strike: guard(MAILY_MARK_TYPES.STRIKE),
|
|
571
|
+
underline: guard(MAILY_MARK_TYPES.UNDERLINE),
|
|
572
|
+
link: guard(MAILY_MARK_TYPES.LINK),
|
|
573
|
+
code: guard(MAILY_MARK_TYPES.CODE),
|
|
574
|
+
textStyle: guard(MAILY_MARK_TYPES.TEXT_STYLE),
|
|
575
|
+
highlight: guard(MAILY_MARK_TYPES.HIGHLIGHT),
|
|
576
|
+
span: guard(MAILY_MARK_TYPES.SPAN)
|
|
577
|
+
};
|
|
578
|
+
function isDef(value) {
|
|
579
|
+
return value !== void 0 && value !== null;
|
|
570
580
|
}
|
|
571
581
|
//#endregion
|
|
572
582
|
//#region src/object.ts
|
|
573
|
-
function
|
|
583
|
+
function isMergeableObject(value) {
|
|
574
584
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
575
585
|
}
|
|
576
586
|
/**
|
|
@@ -584,332 +594,255 @@ function isMergableObject(value) {
|
|
|
584
594
|
*/
|
|
585
595
|
function deepMerge(target, ...sources) {
|
|
586
596
|
const result = { ...target };
|
|
587
|
-
for (const source of sources) for (const [key, value] of Object.entries(source)) result[key] =
|
|
597
|
+
for (const source of sources) for (const [key, value] of Object.entries(source)) result[key] = isMergeableObject(value) && isMergeableObject(result[key]) ? {
|
|
588
598
|
...result[key],
|
|
589
599
|
...value
|
|
590
600
|
} : value;
|
|
591
601
|
return result;
|
|
592
602
|
}
|
|
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
603
|
//#endregion
|
|
599
|
-
//#region src/
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
604
|
+
//#region src/spacing.ts
|
|
605
|
+
const BLOCK_CONTAINER_TYPES = [
|
|
606
|
+
MAILY_NODE_TYPES.DOCUMENT,
|
|
607
|
+
MAILY_NODE_TYPES.BODY,
|
|
608
|
+
MAILY_NODE_TYPES.CONTAINER,
|
|
609
|
+
MAILY_NODE_TYPES.SECTION,
|
|
610
|
+
MAILY_NODE_TYPES.COLUMN,
|
|
611
|
+
MAILY_NODE_TYPES.REPEAT,
|
|
612
|
+
MAILY_NODE_TYPES.BULLET_LIST,
|
|
613
|
+
MAILY_NODE_TYPES.ORDERED_LIST
|
|
614
|
+
];
|
|
615
|
+
const MARGIN_TOP_ABSORBER_TYPES = [MAILY_NODE_TYPES.SPACER];
|
|
616
|
+
const MARGIN_BOTTOM_ABSORBER_TYPES = [MAILY_NODE_TYPES.SPACER, MAILY_NODE_TYPES.HORIZONTAL_RULE];
|
|
617
|
+
function getSuppressedMargins(position) {
|
|
618
|
+
const { isFirst, isLast, nextType, parentType, previousType } = position;
|
|
619
|
+
const stacks = parentType !== void 0 && BLOCK_CONTAINER_TYPES.includes(parentType);
|
|
608
620
|
return {
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
paddingBottom: `${paddingBottom}px`,
|
|
612
|
-
paddingLeft: `${paddingLeft}px`
|
|
621
|
+
marginBottom: !stacks || isLast || MARGIN_BOTTOM_ABSORBER_TYPES.includes(nextType ?? ""),
|
|
622
|
+
marginTop: !stacks || isFirst || MARGIN_TOP_ABSORBER_TYPES.includes(previousType ?? "")
|
|
613
623
|
};
|
|
614
624
|
}
|
|
615
625
|
//#endregion
|
|
616
|
-
//#region src/
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
626
|
+
//#region src/style.ts
|
|
627
|
+
function parseInlineStyles(style) {
|
|
628
|
+
return objectify(style, { reactify: true });
|
|
629
|
+
}
|
|
630
|
+
function stringifyInlineStyles(style) {
|
|
631
|
+
return stringify(style, { kebabCase: true });
|
|
632
|
+
}
|
|
633
|
+
function expandInlineStyles(style) {
|
|
634
|
+
return expand(style, { camelCase: true });
|
|
635
|
+
}
|
|
636
|
+
function collapseInlineStyles(style) {
|
|
637
|
+
return collapse(style, { camelCase: true });
|
|
638
|
+
}
|
|
639
|
+
function mergeInlineStyles(under, over) {
|
|
640
|
+
return collapseInlineStyles({
|
|
641
|
+
...expandInlineStyles(under),
|
|
642
|
+
...expandInlineStyles(over)
|
|
643
|
+
});
|
|
620
644
|
}
|
|
621
645
|
//#endregion
|
|
622
|
-
//#region src/
|
|
623
|
-
/** Text direction values matching the HTML `dir` attribute: ltr, rtl, auto. */
|
|
646
|
+
//#region src/direction.ts
|
|
624
647
|
const TEXT_DIRECTIONS = {
|
|
625
648
|
LTR: "ltr",
|
|
626
649
|
RTL: "rtl",
|
|
627
650
|
AUTO: "auto"
|
|
628
651
|
};
|
|
629
|
-
const allowedTextDirections = Object.freeze(Object.values(TEXT_DIRECTIONS));
|
|
630
652
|
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
653
|
function getCssDirection(dir) {
|
|
640
654
|
if (dir && dir !== TEXT_DIRECTIONS.AUTO) return { direction: dir };
|
|
641
655
|
return {};
|
|
642
656
|
}
|
|
643
657
|
//#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
658
|
//#region src/variable.ts
|
|
813
|
-
/** Default opening delimiter for variable placeholders in text. */
|
|
814
659
|
const DEFAULT_VARIABLE_START_TRIGGER = "{{";
|
|
815
|
-
/** Default closing delimiter for variable placeholders in text. */
|
|
816
660
|
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
|
-
*/
|
|
661
|
+
const LEGACY_HIDE_DEFAULT_VALUE = "hideDefaultValue";
|
|
662
|
+
function getQuotedValue(value) {
|
|
663
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
664
|
+
}
|
|
665
|
+
function getSerializedAttribute(options) {
|
|
666
|
+
const { name, value } = options;
|
|
667
|
+
if (value === void 0 || value === DEFAULT_VARIABLE_ATTRIBUTES[name]) return null;
|
|
668
|
+
if (typeof value === "boolean") return `${name}=${value}`;
|
|
669
|
+
return typeof value === "string" ? `${name}=${getQuotedValue(value)}` : null;
|
|
670
|
+
}
|
|
833
671
|
function serializeVariableToText(variable) {
|
|
834
|
-
const { id, label, required
|
|
672
|
+
const { fallback, id, isDefaultEditable, label, required, style } = variable;
|
|
835
673
|
return [
|
|
836
674
|
"{{",
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
675
|
+
[id, ...[
|
|
676
|
+
getSerializedAttribute({
|
|
677
|
+
name: "fallback",
|
|
678
|
+
value: fallback
|
|
679
|
+
}),
|
|
680
|
+
getSerializedAttribute({
|
|
681
|
+
name: "isDefaultEditable",
|
|
682
|
+
value: isDefaultEditable
|
|
683
|
+
}),
|
|
684
|
+
getSerializedAttribute({
|
|
685
|
+
name: "label",
|
|
686
|
+
value: label === id ? null : label
|
|
687
|
+
}),
|
|
688
|
+
getSerializedAttribute({
|
|
689
|
+
name: "required",
|
|
690
|
+
value: required
|
|
691
|
+
}),
|
|
692
|
+
getSerializedAttribute({
|
|
693
|
+
name: "style",
|
|
694
|
+
value: style
|
|
695
|
+
})
|
|
696
|
+
].filter((attribute) => {
|
|
697
|
+
return attribute !== null;
|
|
698
|
+
})].join(" "),
|
|
844
699
|
"}}"
|
|
845
700
|
].join("");
|
|
846
701
|
}
|
|
847
|
-
function
|
|
848
|
-
|
|
702
|
+
function getReferenceTokens(inner) {
|
|
703
|
+
const tokens = [];
|
|
704
|
+
let token = "";
|
|
705
|
+
let quoted = false;
|
|
706
|
+
for (let index = 0; index < inner.length; index += 1) {
|
|
707
|
+
const character = inner[index];
|
|
708
|
+
if (quoted && character === "\\" && index + 1 < inner.length) {
|
|
709
|
+
token += character + inner[index + 1];
|
|
710
|
+
index += 1;
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
if (character === "\"") {
|
|
714
|
+
quoted = !quoted;
|
|
715
|
+
token += character;
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
if (!quoted && /\s/.test(character)) {
|
|
719
|
+
if (token) {
|
|
720
|
+
tokens.push(token);
|
|
721
|
+
token = "";
|
|
722
|
+
}
|
|
723
|
+
continue;
|
|
724
|
+
}
|
|
725
|
+
token += character;
|
|
726
|
+
}
|
|
727
|
+
if (token) tokens.push(token);
|
|
728
|
+
return tokens;
|
|
729
|
+
}
|
|
730
|
+
function getUnquotedValue(value) {
|
|
731
|
+
if (value.startsWith("\"") && value.endsWith("\"") && value.length > 1) return value.slice(1, -1).replace(/\\"/g, "\"").replace(/\\\\/g, "\\");
|
|
732
|
+
if (value === "true") return true;
|
|
733
|
+
if (value === "false") return false;
|
|
734
|
+
return value;
|
|
849
735
|
}
|
|
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
736
|
function deserializeVariableFromText(text) {
|
|
857
|
-
const [id
|
|
737
|
+
const [id = "", ...pairs] = getReferenceTokens(text.slice(2, text.length - 2).trim());
|
|
738
|
+
const attributes = { id };
|
|
739
|
+
for (const pair of pairs) {
|
|
740
|
+
const separator = pair.indexOf("=");
|
|
741
|
+
if (separator <= 0) continue;
|
|
742
|
+
const key = pair.slice(0, separator);
|
|
743
|
+
const value = getUnquotedValue(pair.slice(separator + 1));
|
|
744
|
+
if (key === LEGACY_HIDE_DEFAULT_VALUE) {
|
|
745
|
+
attributes.isDefaultEditable ??= value !== true;
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
attributes[key] = value;
|
|
749
|
+
}
|
|
858
750
|
return {
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
required: parseBoolean(required, true),
|
|
862
|
-
hideDefaultValue: parseBoolean(hideDefaultValue, false)
|
|
751
|
+
...DEFAULT_VARIABLE_ATTRIBUTES,
|
|
752
|
+
...attributes
|
|
863
753
|
};
|
|
864
754
|
}
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
755
|
+
function getVariableSegments(content) {
|
|
756
|
+
const segments = [];
|
|
757
|
+
let plain = "";
|
|
758
|
+
let index = 0;
|
|
759
|
+
while (index < content.length) {
|
|
760
|
+
if (!content.startsWith("{{", index)) {
|
|
761
|
+
plain += content[index];
|
|
762
|
+
index += 1;
|
|
763
|
+
continue;
|
|
764
|
+
}
|
|
765
|
+
const end = getReferenceEnd({
|
|
766
|
+
content,
|
|
767
|
+
start: index
|
|
768
|
+
});
|
|
769
|
+
if (end === -1) {
|
|
770
|
+
plain += content[index];
|
|
771
|
+
index += 1;
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
if (plain) {
|
|
775
|
+
segments.push({
|
|
776
|
+
isReference: false,
|
|
777
|
+
text: plain
|
|
778
|
+
});
|
|
779
|
+
plain = "";
|
|
780
|
+
}
|
|
781
|
+
segments.push({
|
|
782
|
+
isReference: true,
|
|
783
|
+
text: content.slice(index, end)
|
|
784
|
+
});
|
|
785
|
+
index = end;
|
|
786
|
+
}
|
|
787
|
+
if (plain) segments.push({
|
|
788
|
+
isReference: false,
|
|
789
|
+
text: plain
|
|
790
|
+
});
|
|
791
|
+
return segments;
|
|
792
|
+
}
|
|
793
|
+
function getReferenceEnd(options) {
|
|
794
|
+
const { content, start } = options;
|
|
795
|
+
let quoted = false;
|
|
796
|
+
for (let index = start + 2; index < content.length; index += 1) {
|
|
797
|
+
const character = content[index];
|
|
798
|
+
if (quoted && character === "\\") {
|
|
799
|
+
index += 1;
|
|
800
|
+
continue;
|
|
801
|
+
}
|
|
802
|
+
if (character === "\"") {
|
|
803
|
+
quoted = !quoted;
|
|
804
|
+
continue;
|
|
805
|
+
}
|
|
806
|
+
if (!quoted && content.startsWith("}}", index)) return index + 2;
|
|
807
|
+
}
|
|
808
|
+
return -1;
|
|
809
|
+
}
|
|
871
810
|
function buildDocFromVariableText(content) {
|
|
872
|
-
const parts = content.split(VARIABLE_PLACEHOLDER_REGEX);
|
|
873
811
|
return {
|
|
874
812
|
type: MAILY_NODE_TYPES.DOCUMENT,
|
|
875
813
|
content: [{
|
|
876
814
|
type: MAILY_NODE_TYPES.PARAGRAPH,
|
|
877
|
-
content:
|
|
878
|
-
|
|
815
|
+
content: getVariableSegments(content).map((segment) => {
|
|
816
|
+
return segment.isReference ? {
|
|
879
817
|
type: MAILY_NODE_TYPES.VARIABLE,
|
|
880
|
-
attrs: deserializeVariableFromText(
|
|
881
|
-
}
|
|
882
|
-
if (part) return {
|
|
818
|
+
attrs: deserializeVariableFromText(segment.text)
|
|
819
|
+
} : {
|
|
883
820
|
type: MAILY_NODE_TYPES.TEXT,
|
|
884
|
-
text:
|
|
821
|
+
text: segment.text
|
|
885
822
|
};
|
|
886
|
-
|
|
887
|
-
}).filter(Boolean)
|
|
823
|
+
})
|
|
888
824
|
}]
|
|
889
825
|
};
|
|
890
826
|
}
|
|
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
827
|
function hasVariableInText(text) {
|
|
897
828
|
const start = text.indexOf("{{");
|
|
898
829
|
if (start === -1) return false;
|
|
899
830
|
return text.indexOf("}}", start + 2) !== -1;
|
|
900
831
|
}
|
|
901
|
-
function parseBoolean(value, defaultValue) {
|
|
902
|
-
if (value === "true") return true;
|
|
903
|
-
if (value === "false") return false;
|
|
904
|
-
return defaultValue;
|
|
905
|
-
}
|
|
906
832
|
//#endregion
|
|
907
833
|
//#region src/visibility.ts
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
834
|
+
const VISIBILITY_ACTIONS = {
|
|
835
|
+
SHOW: "show",
|
|
836
|
+
HIDE: "hide"
|
|
837
|
+
};
|
|
838
|
+
const allowedVisibilityActions = Object.values(VISIBILITY_ACTIONS);
|
|
839
|
+
const VISIBILITY_ACTION_OPTIONS = [{
|
|
840
|
+
label: "Show if",
|
|
841
|
+
value: VISIBILITY_ACTIONS.SHOW
|
|
842
|
+
}, {
|
|
843
|
+
label: "Hide if",
|
|
844
|
+
value: VISIBILITY_ACTIONS.HIDE
|
|
845
|
+
}];
|
|
913
846
|
const VISIBILITY_OPERATORS = {
|
|
914
847
|
IS_TRUE: "is_true",
|
|
915
848
|
IS_FALSE: "is_false",
|
|
@@ -922,73 +855,318 @@ const VISIBILITY_OPERATORS = {
|
|
|
922
855
|
GREATER_THAN: "greater_than",
|
|
923
856
|
LESS_THAN: "less_than"
|
|
924
857
|
};
|
|
858
|
+
const allowedVisibilityOperators = Object.values(VISIBILITY_OPERATORS);
|
|
925
859
|
const VISIBILITY_OPERATOR_OPTIONS = [
|
|
926
860
|
{
|
|
927
|
-
|
|
928
|
-
|
|
861
|
+
label: "Is true",
|
|
862
|
+
value: VISIBILITY_OPERATORS.IS_TRUE
|
|
929
863
|
},
|
|
930
864
|
{
|
|
931
|
-
|
|
932
|
-
|
|
865
|
+
label: "Is false",
|
|
866
|
+
value: VISIBILITY_OPERATORS.IS_FALSE
|
|
933
867
|
},
|
|
934
868
|
{
|
|
935
|
-
|
|
936
|
-
|
|
869
|
+
label: "Equals",
|
|
870
|
+
value: VISIBILITY_OPERATORS.EQUALS
|
|
937
871
|
},
|
|
938
872
|
{
|
|
939
|
-
|
|
940
|
-
|
|
873
|
+
label: "Not equals",
|
|
874
|
+
value: VISIBILITY_OPERATORS.NOT_EQUALS
|
|
941
875
|
},
|
|
942
876
|
{
|
|
943
|
-
|
|
944
|
-
|
|
877
|
+
label: "Contains",
|
|
878
|
+
value: VISIBILITY_OPERATORS.CONTAINS
|
|
945
879
|
},
|
|
946
880
|
{
|
|
947
|
-
|
|
948
|
-
|
|
881
|
+
label: "Not contains",
|
|
882
|
+
value: VISIBILITY_OPERATORS.NOT_CONTAINS
|
|
949
883
|
},
|
|
950
884
|
{
|
|
951
|
-
|
|
952
|
-
|
|
885
|
+
label: "Starts with",
|
|
886
|
+
value: VISIBILITY_OPERATORS.STARTS_WITH
|
|
953
887
|
},
|
|
954
888
|
{
|
|
955
|
-
|
|
956
|
-
|
|
889
|
+
label: "Ends with",
|
|
890
|
+
value: VISIBILITY_OPERATORS.ENDS_WITH
|
|
957
891
|
},
|
|
958
892
|
{
|
|
959
|
-
|
|
960
|
-
|
|
893
|
+
label: "Greater than",
|
|
894
|
+
value: VISIBILITY_OPERATORS.GREATER_THAN
|
|
961
895
|
},
|
|
962
896
|
{
|
|
963
|
-
|
|
964
|
-
|
|
897
|
+
label: "Less than",
|
|
898
|
+
value: VISIBILITY_OPERATORS.LESS_THAN
|
|
965
899
|
}
|
|
966
900
|
];
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
NONE: "none",
|
|
974
|
-
SHOW: "show",
|
|
975
|
-
HIDE: "hide"
|
|
901
|
+
const VISIBILITY_OPERATORS_WITHOUT_VALUE = [VISIBILITY_OPERATORS.IS_TRUE, VISIBILITY_OPERATORS.IS_FALSE];
|
|
902
|
+
const DEFAULT_VISIBILITY_RULE = {
|
|
903
|
+
action: VISIBILITY_ACTIONS.SHOW,
|
|
904
|
+
operator: VISIBILITY_OPERATORS.EQUALS,
|
|
905
|
+
value: "",
|
|
906
|
+
variable: ""
|
|
976
907
|
};
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
908
|
+
function getVisibilityValueSupport(operator) {
|
|
909
|
+
return !VISIBILITY_OPERATORS_WITHOUT_VALUE.includes(operator);
|
|
910
|
+
}
|
|
911
|
+
function getVisibilityRule(options) {
|
|
912
|
+
const { action, operator, value, variable } = options;
|
|
913
|
+
const isAllowedAction = allowedVisibilityActions.some((candidate) => {
|
|
914
|
+
return candidate === action;
|
|
915
|
+
});
|
|
916
|
+
const isAllowedOperator = allowedVisibilityOperators.some((candidate) => {
|
|
917
|
+
return candidate === operator;
|
|
918
|
+
});
|
|
919
|
+
if (!(isAllowedAction && isAllowedOperator)) return null;
|
|
920
|
+
return {
|
|
921
|
+
action,
|
|
922
|
+
operator,
|
|
923
|
+
value: getVisibilityValueSupport(operator) ? value ?? "" : "",
|
|
924
|
+
variable: variable ?? ""
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
//#endregion
|
|
928
|
+
//#region src/theme.ts
|
|
929
|
+
const DEFAULT_CONTAINER_MAX_WIDTH = 600;
|
|
930
|
+
const BASIC_NODE_THEME = {
|
|
931
|
+
blockquote: {
|
|
932
|
+
borderColor: "#d1d5db",
|
|
933
|
+
borderLeftStyle: "solid",
|
|
934
|
+
borderLeftWidth: "4px",
|
|
935
|
+
color: "#374151",
|
|
936
|
+
fontSize: "15px",
|
|
937
|
+
fontStyle: "italic",
|
|
938
|
+
fontWeight: "500",
|
|
939
|
+
lineHeight: "26px",
|
|
940
|
+
marginBottom: "20px",
|
|
941
|
+
marginLeft: "0",
|
|
942
|
+
marginRight: "0",
|
|
943
|
+
marginTop: "20px",
|
|
944
|
+
paddingLeft: "16px"
|
|
981
945
|
},
|
|
982
|
-
{
|
|
983
|
-
|
|
984
|
-
|
|
946
|
+
body: { fontFamily: getFontFamilyValue({
|
|
947
|
+
fallbackFontFamily: DEFAULT_FONT.fallbackFontFamily,
|
|
948
|
+
fontFamily: DEFAULT_FONT.fontFamily
|
|
949
|
+
}) },
|
|
950
|
+
bulletList: {
|
|
951
|
+
listStyleType: "disc",
|
|
952
|
+
margin: "0",
|
|
953
|
+
paddingLeft: "26px"
|
|
985
954
|
},
|
|
986
|
-
{
|
|
987
|
-
|
|
988
|
-
|
|
955
|
+
button: {
|
|
956
|
+
backgroundColor: "#000000",
|
|
957
|
+
borderRadius: "9999px",
|
|
958
|
+
color: "#ffffff",
|
|
959
|
+
fontSize: "14px",
|
|
960
|
+
fontWeight: "500",
|
|
961
|
+
lineHeight: "20px",
|
|
962
|
+
padding: "10px 32px"
|
|
963
|
+
},
|
|
964
|
+
code: {
|
|
965
|
+
borderRadius: "8px",
|
|
966
|
+
fontFamily: "ui-monospace, monospace",
|
|
967
|
+
fontSize: "85%",
|
|
968
|
+
padding: "2px 6px"
|
|
969
|
+
},
|
|
970
|
+
h1: {
|
|
971
|
+
fontSize: "36px",
|
|
972
|
+
fontWeight: "800",
|
|
973
|
+
lineHeight: "40px"
|
|
974
|
+
},
|
|
975
|
+
h2: {
|
|
976
|
+
fontSize: "30px",
|
|
977
|
+
fontWeight: "700",
|
|
978
|
+
lineHeight: "40px"
|
|
979
|
+
},
|
|
980
|
+
h3: {
|
|
981
|
+
fontSize: "24px",
|
|
982
|
+
fontWeight: "600",
|
|
983
|
+
lineHeight: "38px"
|
|
984
|
+
},
|
|
985
|
+
heading: {
|
|
986
|
+
color: "#101828",
|
|
987
|
+
marginBottom: "12px",
|
|
988
|
+
marginTop: "0"
|
|
989
|
+
},
|
|
990
|
+
horizontalRule: {
|
|
991
|
+
border: "none",
|
|
992
|
+
borderTop: "1px solid #eaeaea",
|
|
993
|
+
marginBottom: "32px",
|
|
994
|
+
marginTop: "32px"
|
|
995
|
+
},
|
|
996
|
+
htmlCodeBlock: { marginBottom: "32px" },
|
|
997
|
+
link: {
|
|
998
|
+
color: "#0000ee",
|
|
999
|
+
textDecoration: "underline"
|
|
1000
|
+
},
|
|
1001
|
+
listItem: {
|
|
1002
|
+
marginBottom: "8px",
|
|
1003
|
+
paddingLeft: "6px"
|
|
1004
|
+
},
|
|
1005
|
+
orderedList: {
|
|
1006
|
+
listStyleType: "decimal",
|
|
1007
|
+
margin: "0",
|
|
1008
|
+
paddingLeft: "26px"
|
|
1009
|
+
},
|
|
1010
|
+
footer: {
|
|
1011
|
+
color: "#64748B",
|
|
1012
|
+
display: "block",
|
|
1013
|
+
fontSize: "14px",
|
|
1014
|
+
fontWeight: "400",
|
|
1015
|
+
lineHeight: "24px",
|
|
1016
|
+
marginBottom: "0",
|
|
1017
|
+
marginTop: "0"
|
|
1018
|
+
},
|
|
1019
|
+
paragraph: {
|
|
1020
|
+
color: "#18181b",
|
|
1021
|
+
fontSize: "14px",
|
|
1022
|
+
lineHeight: "24px"
|
|
1023
|
+
},
|
|
1024
|
+
section: { padding: "8px" }
|
|
1025
|
+
};
|
|
1026
|
+
const MAILY_NODE_THEME = {
|
|
1027
|
+
blockquote: {
|
|
1028
|
+
borderLeftColor: "#d1d5db",
|
|
1029
|
+
borderLeftStyle: "solid",
|
|
1030
|
+
borderLeftWidth: "4px",
|
|
1031
|
+
color: "#374151",
|
|
1032
|
+
fontSize: "15px",
|
|
1033
|
+
fontStyle: "italic",
|
|
1034
|
+
fontWeight: "500",
|
|
1035
|
+
lineHeight: "1.75",
|
|
1036
|
+
marginBottom: "20px",
|
|
1037
|
+
marginLeft: "0",
|
|
1038
|
+
marginRight: "0",
|
|
1039
|
+
marginTop: "20px",
|
|
1040
|
+
paddingLeft: "16px"
|
|
1041
|
+
},
|
|
1042
|
+
body: {
|
|
1043
|
+
backgroundColor: "#ffffff",
|
|
1044
|
+
fontFamily: getFontFamilyValue({
|
|
1045
|
+
fallbackFontFamily: DEFAULT_FONT.fallbackFontFamily,
|
|
1046
|
+
fontFamily: DEFAULT_FONT.fontFamily
|
|
1047
|
+
}),
|
|
1048
|
+
padding: "0"
|
|
1049
|
+
},
|
|
1050
|
+
bulletList: {
|
|
1051
|
+
listStyleType: "disc",
|
|
1052
|
+
marginBottom: "20px",
|
|
1053
|
+
marginLeft: "0",
|
|
1054
|
+
marginRight: "0",
|
|
1055
|
+
marginTop: "0",
|
|
1056
|
+
paddingLeft: "26px"
|
|
1057
|
+
},
|
|
1058
|
+
button: {
|
|
1059
|
+
backgroundColor: "#000000",
|
|
1060
|
+
borderRadius: "9999px",
|
|
1061
|
+
color: "#ffffff",
|
|
1062
|
+
fontSize: "14px",
|
|
1063
|
+
fontWeight: "600",
|
|
1064
|
+
lineHeight: "1.4285714",
|
|
1065
|
+
marginBottom: "20px",
|
|
1066
|
+
padding: "10px 32px"
|
|
1067
|
+
},
|
|
1068
|
+
code: {
|
|
1069
|
+
backgroundColor: "#efefef",
|
|
1070
|
+
borderRadius: "8px",
|
|
1071
|
+
color: "#111827",
|
|
1072
|
+
fontFamily: "ui-monospace, monospace",
|
|
1073
|
+
fontSize: "85%",
|
|
1074
|
+
padding: "2px 6px"
|
|
1075
|
+
},
|
|
1076
|
+
container: {
|
|
1077
|
+
backgroundColor: "#ffffff",
|
|
1078
|
+
maxWidth: `600px`,
|
|
1079
|
+
minWidth: "300px",
|
|
1080
|
+
padding: "8px"
|
|
1081
|
+
},
|
|
1082
|
+
footer: {
|
|
1083
|
+
color: "#64748b",
|
|
1084
|
+
display: "block",
|
|
1085
|
+
fontSize: "14px",
|
|
1086
|
+
fontWeight: "400",
|
|
1087
|
+
lineHeight: "1.7142857",
|
|
1088
|
+
marginBottom: "20px",
|
|
1089
|
+
marginTop: "0"
|
|
1090
|
+
},
|
|
1091
|
+
h1: {
|
|
1092
|
+
fontSize: "36px",
|
|
1093
|
+
fontWeight: "600",
|
|
1094
|
+
lineHeight: "1.1111111"
|
|
1095
|
+
},
|
|
1096
|
+
h2: {
|
|
1097
|
+
fontSize: "30px",
|
|
1098
|
+
fontWeight: "600",
|
|
1099
|
+
lineHeight: "1.3333333"
|
|
1100
|
+
},
|
|
1101
|
+
h3: {
|
|
1102
|
+
fontSize: "24px",
|
|
1103
|
+
fontWeight: "600",
|
|
1104
|
+
lineHeight: "1.6"
|
|
1105
|
+
},
|
|
1106
|
+
heading: {
|
|
1107
|
+
color: "#111827",
|
|
1108
|
+
marginBottom: "12px",
|
|
1109
|
+
marginTop: "0"
|
|
1110
|
+
},
|
|
1111
|
+
horizontalRule: {
|
|
1112
|
+
border: "none",
|
|
1113
|
+
borderTop: "1px solid #eaeaea",
|
|
1114
|
+
marginBottom: "32px",
|
|
1115
|
+
marginTop: "32px"
|
|
1116
|
+
},
|
|
1117
|
+
htmlCodeBlock: { marginBottom: "32px" },
|
|
1118
|
+
link: {
|
|
1119
|
+
color: "#111827",
|
|
1120
|
+
textDecoration: "underline"
|
|
1121
|
+
},
|
|
1122
|
+
listItem: {
|
|
1123
|
+
color: "#d1d5dc",
|
|
1124
|
+
marginBottom: "8px",
|
|
1125
|
+
marginTop: "8px",
|
|
1126
|
+
paddingLeft: "6px"
|
|
1127
|
+
},
|
|
1128
|
+
orderedList: {
|
|
1129
|
+
listStyleType: "decimal",
|
|
1130
|
+
marginBottom: "20px",
|
|
1131
|
+
marginLeft: "0",
|
|
1132
|
+
marginRight: "0",
|
|
1133
|
+
marginTop: "0",
|
|
1134
|
+
paddingLeft: "26px"
|
|
1135
|
+
},
|
|
1136
|
+
paragraph: {
|
|
1137
|
+
color: "#374151",
|
|
1138
|
+
fontSize: "15px",
|
|
1139
|
+
fontWeight: "400",
|
|
1140
|
+
lineHeight: "1.75",
|
|
1141
|
+
marginBottom: "20px",
|
|
1142
|
+
marginTop: "0",
|
|
1143
|
+
MozOsxFontSmoothing: "grayscale",
|
|
1144
|
+
WebkitFontSmoothing: "antialiased"
|
|
1145
|
+
},
|
|
1146
|
+
section: {
|
|
1147
|
+
backgroundColor: "#f7f7f7",
|
|
1148
|
+
border: "2px solid #e2e2e2",
|
|
1149
|
+
borderRadius: "0",
|
|
1150
|
+
marginBottom: "20px",
|
|
1151
|
+
padding: "0"
|
|
989
1152
|
}
|
|
990
|
-
|
|
1153
|
+
};
|
|
1154
|
+
function extendNodeTheme(options) {
|
|
1155
|
+
const { extension, preset } = options;
|
|
1156
|
+
return deepMerge(preset, extension);
|
|
1157
|
+
}
|
|
1158
|
+
function getThemeDeclarations(options) {
|
|
1159
|
+
const { attrs, theme, type } = options;
|
|
1160
|
+
const declarations = theme[type];
|
|
1161
|
+
if (type !== MAILY_NODE_TYPES.HEADING) return declarations;
|
|
1162
|
+
const level = typeof attrs?.level === "number" ? theme[`h${attrs.level}`] : void 0;
|
|
1163
|
+
if (!(declarations || level)) return;
|
|
1164
|
+
return {
|
|
1165
|
+
...declarations,
|
|
1166
|
+
...level
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
991
1169
|
//#endregion
|
|
992
|
-
export { BORDER_STYLES,
|
|
1170
|
+
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
1171
|
|
|
994
1172
|
//# sourceMappingURL=index.mjs.map
|