@thanhphi/unistyles-ui 0.0.0-stage → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +374 -2
- package/dist/choices.d.ts +52 -0
- package/dist/choices.js +143 -0
- package/dist/colors/index.d.ts +79 -0
- package/dist/colors/index.js +2 -0
- package/dist/date.d.ts +40 -0
- package/dist/date.js +153 -0
- package/dist/fields.d.ts +138 -0
- package/dist/fields.js +240 -0
- package/dist/form/index.d.ts +14 -0
- package/dist/form/index.js +68 -0
- package/dist/icons.d.ts +10 -0
- package/dist/icons.js +51 -0
- package/dist/image.d.ts +20 -0
- package/dist/image.js +34 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +30 -0
- package/dist/module/choices.js +135 -0
- package/dist/module/colors/index.js +1 -0
- package/dist/module/colors/package.json +1 -0
- package/dist/module/date.js +145 -0
- package/dist/module/fields.js +232 -0
- package/dist/module/form/index.js +62 -0
- package/dist/module/form/package.json +1 -0
- package/dist/module/icons.js +15 -0
- package/dist/module/image.js +31 -0
- package/dist/module/index.js +10 -0
- package/dist/module/overlays.js +102 -0
- package/dist/module/package.json +1 -0
- package/dist/module/primitives.js +432 -0
- package/dist/module/process-graph.js +125 -0
- package/dist/module/rich-html/html-styles.js +55 -0
- package/dist/module/rich-html/image-renderers.js +141 -0
- package/dist/module/rich-html/index.js +54 -0
- package/dist/module/rich-html/normalize-inline-colors.js +93 -0
- package/dist/module/rich-html/package.json +1 -0
- package/dist/module/rich-html/rich-html.js +118 -0
- package/dist/module/theme.js +62 -0
- package/dist/module/toast.js +30 -0
- package/dist/overlays.d.ts +52 -0
- package/dist/overlays.js +109 -0
- package/dist/primitives.d.ts +157 -0
- package/dist/primitives.js +480 -0
- package/dist/process-graph.d.ts +33 -0
- package/dist/process-graph.js +161 -0
- package/dist/rich-html/html-styles.d.ts +23 -0
- package/dist/rich-html/html-styles.js +63 -0
- package/dist/rich-html/image-renderers.d.ts +12 -0
- package/dist/rich-html/image-renderers.js +144 -0
- package/dist/rich-html/index.d.ts +18 -0
- package/dist/rich-html/index.js +92 -0
- package/dist/rich-html/normalize-inline-colors.d.ts +8 -0
- package/dist/rich-html/normalize-inline-colors.js +99 -0
- package/dist/rich-html/rich-html.d.ts +7 -0
- package/dist/rich-html/rich-html.js +123 -0
- package/dist/theme.d.ts +67 -0
- package/dist/theme.js +68 -0
- package/dist/toast.d.ts +3 -0
- package/dist/toast.js +69 -0
- package/package.json +35 -4
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { StyleSheet, TouchableOpacity, View } from "react-native";
|
|
4
|
+
import { IMGElementContainer, isDomElement, isDomText, useIMGElementStateWithCache, useInternalRenderer, useNormalizedUrl, } from "react-native-render-html";
|
|
5
|
+
import { ImageWithSkeleton } from "../image.js";
|
|
6
|
+
import { scale } from "./html-styles.js";
|
|
7
|
+
const DEFAULT_ASPECT_RATIO = 16 / 9;
|
|
8
|
+
const PHOTO_GAP = scale(6);
|
|
9
|
+
const getImageUri = (value) => {
|
|
10
|
+
const uri = typeof value === "string" ? value.trim() : "";
|
|
11
|
+
return /^(https?:\/\/|data:image\/)/i.test(uri) ? uri : undefined;
|
|
12
|
+
};
|
|
13
|
+
function readDimensions(width, height) {
|
|
14
|
+
const w = Number(width);
|
|
15
|
+
const h = Number(height);
|
|
16
|
+
if (Number.isFinite(w) && Number.isFinite(h) && w > 0 && h > 0) {
|
|
17
|
+
return { width: w, height: h };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const imageContentFit = {
|
|
21
|
+
stretch: "fill",
|
|
22
|
+
center: "none",
|
|
23
|
+
contain: "contain",
|
|
24
|
+
};
|
|
25
|
+
function InlineImage({ options, image, }) {
|
|
26
|
+
const [naturalSize, setNaturalSize] = useState();
|
|
27
|
+
const [failed, setFailed] = useState(false);
|
|
28
|
+
const width = image.contentWidth || scale(200);
|
|
29
|
+
const htmlSize = readDimensions(image.width, image.height);
|
|
30
|
+
// Start the request with a reserved frame and read missing dimensions from
|
|
31
|
+
// Expo Image's load event instead of requesting the same image via getSize.
|
|
32
|
+
const state = useIMGElementStateWithCache({
|
|
33
|
+
...image,
|
|
34
|
+
cachedNaturalDimensions: naturalSize ??
|
|
35
|
+
htmlSize ?? {
|
|
36
|
+
width,
|
|
37
|
+
height: width / DEFAULT_ASPECT_RATIO,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
const uri = getImageUri(state.source.uri);
|
|
41
|
+
const onImagePress = options.onImagePress;
|
|
42
|
+
const canOpenImage = Boolean(uri && !failed && onImagePress);
|
|
43
|
+
const resizeMode = state.type === "success" ? state.imageStyle.resizeMode : undefined;
|
|
44
|
+
const contentFit = (resizeMode && imageContentFit[resizeMode]) || "cover";
|
|
45
|
+
return (_jsx(IMGElementContainer, { ...image.containerProps, testID: image.testID, style: state.containerStyle, accessibilityRole: canOpenImage ? "button" : undefined, accessibilityLabel: image.alt || options.imageAccessibilityLabel, onPress: uri && !failed && onImagePress ? () => onImagePress(uri) : undefined, children: _jsx(ImageWithSkeleton, { source: state.source.uri ? state.source : undefined, style: state.dimensions, contentFit: contentFit, testID: "article-image", fallbackSource: options.fallbackSource, waitForDisplay: true, skeletonProps: {
|
|
46
|
+
animated: options.animateSkeleton,
|
|
47
|
+
pointerEvents: "none",
|
|
48
|
+
}, onLoad: ({ source }) => {
|
|
49
|
+
if (!htmlSize && !failed) {
|
|
50
|
+
setNaturalSize(readDimensions(source.width, source.height));
|
|
51
|
+
}
|
|
52
|
+
}, onError: () => setFailed(true) }) }));
|
|
53
|
+
}
|
|
54
|
+
const significantChildren = (element) => element.children.filter((child) => child.type !== "comment" && !(isDomText(child) && !child.data.trim()));
|
|
55
|
+
function findCellImage(node) {
|
|
56
|
+
if (!isDomElement(node))
|
|
57
|
+
return;
|
|
58
|
+
if (node.name === "img" && node.attribs.src?.trim())
|
|
59
|
+
return node;
|
|
60
|
+
if (!["td", "p", "span", "div"].includes(node.name))
|
|
61
|
+
return;
|
|
62
|
+
const children = significantChildren(node);
|
|
63
|
+
if (children.length === 1)
|
|
64
|
+
return findCellImage(children[0]);
|
|
65
|
+
}
|
|
66
|
+
function getPhotoRows(table) {
|
|
67
|
+
const rows = significantChildren(table).flatMap((node) => isDomElement(node) && ["thead", "tbody", "tfoot"].includes(node.name)
|
|
68
|
+
? significantChildren(node)
|
|
69
|
+
: [node]);
|
|
70
|
+
const photoRows = [];
|
|
71
|
+
let columnCount;
|
|
72
|
+
for (const row of rows) {
|
|
73
|
+
if (!isDomElement(row) || row.name !== "tr")
|
|
74
|
+
return;
|
|
75
|
+
const photos = [];
|
|
76
|
+
let rowColumns = 0;
|
|
77
|
+
for (const cell of significantChildren(row)) {
|
|
78
|
+
if (!isDomElement(cell) ||
|
|
79
|
+
cell.name !== "td" ||
|
|
80
|
+
Number(cell.attribs.rowspan ?? 1) !== 1) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const columnSpan = Number(cell.attribs.colspan ?? 1);
|
|
84
|
+
if (!Number.isSafeInteger(columnSpan) || columnSpan < 1)
|
|
85
|
+
return;
|
|
86
|
+
const photo = findCellImage(cell);
|
|
87
|
+
if (!photo)
|
|
88
|
+
return;
|
|
89
|
+
photos.push({ image: photo, columnSpan });
|
|
90
|
+
rowColumns += columnSpan;
|
|
91
|
+
}
|
|
92
|
+
if (!photos.length ||
|
|
93
|
+
(columnCount !== undefined && columnCount !== rowColumns))
|
|
94
|
+
return;
|
|
95
|
+
columnCount = rowColumns;
|
|
96
|
+
photoRows.push(photos);
|
|
97
|
+
}
|
|
98
|
+
if (photoRows.length)
|
|
99
|
+
return photoRows;
|
|
100
|
+
}
|
|
101
|
+
function GridImage({ image, columnSpan, options, }) {
|
|
102
|
+
const { src, alt, width, height } = image.attribs;
|
|
103
|
+
const uri = getImageUri(useNormalizedUrl(src));
|
|
104
|
+
const [failed, setFailed] = useState(false);
|
|
105
|
+
const size = readDimensions(width, height);
|
|
106
|
+
const onImagePress = options.onImagePress;
|
|
107
|
+
// A spanning cell includes the gutters between the columns it covers.
|
|
108
|
+
const cellStyle = [
|
|
109
|
+
styles.cell,
|
|
110
|
+
{ flexGrow: columnSpan, flexBasis: PHOTO_GAP * (columnSpan - 1) },
|
|
111
|
+
];
|
|
112
|
+
const photo = (_jsx(ImageWithSkeleton, { source: uri, style: {
|
|
113
|
+
aspectRatio: size ? size.width / size.height : DEFAULT_ASPECT_RATIO,
|
|
114
|
+
}, contentFit: "contain", testID: "article-image", fallbackSource: options.fallbackSource, waitForDisplay: true, skeletonProps: {
|
|
115
|
+
animated: options.animateSkeleton,
|
|
116
|
+
pointerEvents: "none",
|
|
117
|
+
}, onError: () => setFailed(true), accessibilityLabel: onImagePress ? undefined : alt || options.imageAccessibilityLabel }));
|
|
118
|
+
if (!uri || !onImagePress)
|
|
119
|
+
return _jsx(View, { style: cellStyle, children: photo });
|
|
120
|
+
return (_jsx(TouchableOpacity, { style: cellStyle, disabled: failed, onPress: () => onImagePress(uri), accessibilityRole: "button", accessibilityLabel: alt || options.imageAccessibilityLabel, activeOpacity: 0.85, children: photo }));
|
|
121
|
+
}
|
|
122
|
+
/** Opt in to skeletons, image-only tables and tappable images in rich HTML. */
|
|
123
|
+
export function createRichHtmlImageRenderers(options = {}) {
|
|
124
|
+
const img = function ArticleImageRenderer(props) {
|
|
125
|
+
const { rendererProps } = useInternalRenderer("img", props);
|
|
126
|
+
return (_jsx(InlineImage, { image: rendererProps, options: options }, rendererProps.source.uri));
|
|
127
|
+
};
|
|
128
|
+
const table = function ArticleTableRenderer(props) {
|
|
129
|
+
const { tnode, TDefaultRenderer } = props;
|
|
130
|
+
const rows = tnode.domNode && getPhotoRows(tnode.domNode);
|
|
131
|
+
if (!rows)
|
|
132
|
+
return _jsx(TDefaultRenderer, { ...props });
|
|
133
|
+
return (_jsx(View, { style: styles.grid, children: rows.map((row, rowIndex) => (_jsx(View, { style: styles.row, testID: "article-photo-row", children: row.map(({ image, columnSpan }, columnIndex) => (_jsx(GridImage, { image: image, columnSpan: columnSpan, options: options }, `${columnIndex}:${image.attribs.src}`))) }, rowIndex))) }));
|
|
134
|
+
};
|
|
135
|
+
return { img, table };
|
|
136
|
+
}
|
|
137
|
+
const styles = StyleSheet.create({
|
|
138
|
+
grid: { gap: PHOTO_GAP, marginBottom: PHOTO_GAP },
|
|
139
|
+
row: { flexDirection: "row", alignItems: "flex-start", gap: PHOTO_GAP },
|
|
140
|
+
cell: { flex: 1, minWidth: 0 },
|
|
141
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { useMemo } from "react";
|
|
3
|
+
import RenderHTML, { RenderHTMLConfigProvider, RenderHTMLSource, TRenderEngineProvider, } from "react-native-render-html";
|
|
4
|
+
import { buildRichHtml, richHtmlClassesStyles, richHtmlDomVisitors, richHtmlTagsStyles, } from "./rich-html.js";
|
|
5
|
+
import { mergeRichHtmlStyle, mergeRichHtmlStyleRecord, richHtmlTextSize, } from "./html-styles.js";
|
|
6
|
+
export { createRichHtmlImageRenderers, } from "./image-renderers.js";
|
|
7
|
+
const defaultTextProps = { allowFontScaling: false };
|
|
8
|
+
const renderersProps = { img: { enableExperimentalPercentWidth: true } };
|
|
9
|
+
const SharedHtmlContext = React.createContext(null);
|
|
10
|
+
function useRichHtmlStyles({ textColor, linkColor, baseStyle, tagsStyles, classesStyles, }, inherited) {
|
|
11
|
+
return useMemo(() => {
|
|
12
|
+
const defaultBaseStyle = {
|
|
13
|
+
color: textColor,
|
|
14
|
+
...richHtmlTextSize(14),
|
|
15
|
+
textAlign: "left",
|
|
16
|
+
};
|
|
17
|
+
const defaultTagsStyles = {
|
|
18
|
+
...richHtmlTagsStyles,
|
|
19
|
+
a: { color: linkColor, textDecorationLine: "underline" },
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
baseStyle: mergeRichHtmlStyle(mergeRichHtmlStyle(defaultBaseStyle, inherited?.baseStyle), baseStyle),
|
|
23
|
+
tagsStyles: mergeRichHtmlStyleRecord(mergeRichHtmlStyleRecord(defaultTagsStyles, inherited?.tagsStyles), tagsStyles),
|
|
24
|
+
classesStyles: mergeRichHtmlStyleRecord(mergeRichHtmlStyleRecord(richHtmlClassesStyles, inherited?.classesStyles), classesStyles),
|
|
25
|
+
};
|
|
26
|
+
}, [textColor, linkColor, baseStyle, tagsStyles, classesStyles, inherited]);
|
|
27
|
+
}
|
|
28
|
+
/** Reuse one render engine and config for a list of articles. */
|
|
29
|
+
export const RichHtmlProvider = ({ children, textColor, linkColor, baseStyle, tagsStyles, classesStyles, }) => {
|
|
30
|
+
const config = useMemo(() => ({ textColor, linkColor, baseStyle, tagsStyles, classesStyles }), [textColor, linkColor, baseStyle, tagsStyles, classesStyles]);
|
|
31
|
+
const styles = useRichHtmlStyles(config);
|
|
32
|
+
return (_jsx(TRenderEngineProvider, { enableCSSInlineProcessing: true, ...styles, domVisitors: richHtmlDomVisitors, children: _jsx(RenderHTMLConfigProvider, { defaultTextProps: defaultTextProps, renderersProps: renderersProps, enableExperimentalGhostLinesPrevention: true, enableExperimentalBRCollapsing: true, children: _jsx(SharedHtmlContext.Provider, { value: config, children: children }) }) }));
|
|
33
|
+
};
|
|
34
|
+
const StandaloneRichHtml = ({ source, contentWidth, renderers, inherited, ...config }) => {
|
|
35
|
+
const styles = useRichHtmlStyles(config, inherited);
|
|
36
|
+
return (_jsx(RenderHTML, { contentWidth: contentWidth, source: source, enableCSSInlineProcessing: true, ...styles, domVisitors: richHtmlDomVisitors, defaultTextProps: defaultTextProps, enableExperimentalGhostLinesPrevention: true, enableExperimentalBRCollapsing: true, renderers: renderers, renderersProps: renderersProps }));
|
|
37
|
+
};
|
|
38
|
+
export const RichHtmlContent = React.memo(function RichHtmlContent({ content, contentWidth, imageUrls, renderers, textColor, linkColor, baseStyle, tagsStyles, classesStyles, }) {
|
|
39
|
+
const sharedConfig = React.use(SharedHtmlContext);
|
|
40
|
+
const source = useMemo(() => ({ html: buildRichHtml(content, imageUrls) }), [content, imageUrls]);
|
|
41
|
+
if (!source.html)
|
|
42
|
+
return null;
|
|
43
|
+
const width = Math.max(0, contentWidth);
|
|
44
|
+
// A document with its own rendering options uses a separate engine.
|
|
45
|
+
if (!renderers &&
|
|
46
|
+
!baseStyle &&
|
|
47
|
+
!tagsStyles &&
|
|
48
|
+
!classesStyles &&
|
|
49
|
+
sharedConfig?.textColor === textColor &&
|
|
50
|
+
sharedConfig.linkColor === linkColor) {
|
|
51
|
+
return _jsx(RenderHTMLSource, { source: source, contentWidth: width });
|
|
52
|
+
}
|
|
53
|
+
return (_jsx(StandaloneRichHtml, { contentWidth: width, source: source, renderers: renderers, textColor: textColor, linkColor: linkColor, baseStyle: baseStyle, tagsStyles: tagsStyles, classesStyles: classesStyles, inherited: sharedConfig ?? undefined }));
|
|
54
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export const convertInlineTagsToHtml = (html) => {
|
|
2
|
+
if (!html || typeof html !== "string") {
|
|
3
|
+
return "";
|
|
4
|
+
}
|
|
5
|
+
let result = html;
|
|
6
|
+
// 1. Convert <text> to <span> (plain text wrapper)
|
|
7
|
+
result = result.replace(/<text>([^<]*)<\/text>/gi, "<span>$1</span>");
|
|
8
|
+
// 2. Convert <bold> to <strong>
|
|
9
|
+
result = result.replace(/<bold>([^<]*)<\/bold>/gi, "<strong>$1</strong>");
|
|
10
|
+
// 3. Convert <italic> to <i>
|
|
11
|
+
result = result.replace(/<italic>([^<]*)<\/italic>/gi, "<i>$1</i>");
|
|
12
|
+
// 4. Convert <underline> to <u>
|
|
13
|
+
result = result.replace(/<underline>([^<]*)<\/underline>/gi, "<u>$1</u>");
|
|
14
|
+
// 5. Convert <strike> to <s>
|
|
15
|
+
result = result.replace(/<strike>([^<]*)<\/strike>/gi, "<s>$1</s>");
|
|
16
|
+
// 6. Convert <highlightColor backgroundColor="..."> to span with background-color
|
|
17
|
+
result = result.replace(/<highlightColor\s+backgroundColor="([^"]+)">([^<]*)<\/highlightColor>/gi, (_, bgColor, content) => `<span style="background-color: ${bgColor};">${content}</span>`);
|
|
18
|
+
// Map custom tags tới inline style
|
|
19
|
+
const tagStyleMap = {
|
|
20
|
+
// Single style (no color)
|
|
21
|
+
italic: () => `font-style: italic;`,
|
|
22
|
+
// Single style + color
|
|
23
|
+
colorSpan: (color) => `color: ${color};`,
|
|
24
|
+
boldColor: (color) => `font-weight: bold; color: ${color};`,
|
|
25
|
+
italicColor: (color) => `font-style: italic; color: ${color};`,
|
|
26
|
+
underlineColor: (color) => `text-decoration: underline; color: ${color};`,
|
|
27
|
+
strikeColor: (color) => `text-decoration: line-through; color: ${color};`,
|
|
28
|
+
// Combo 2 styles (không màu)
|
|
29
|
+
boldItalic: () => `font-weight: bold; font-style: italic;`,
|
|
30
|
+
boldUnderline: () => `font-weight: bold; text-decoration: underline;`,
|
|
31
|
+
italicUnderline: () => `font-style: italic; text-decoration: underline;`,
|
|
32
|
+
// Combo 2 styles + color
|
|
33
|
+
boldItalicColor: (color) => `font-weight: bold; font-style: italic; color: ${color};`,
|
|
34
|
+
boldUnderlineColor: (color) => `font-weight: bold; text-decoration: underline; color: ${color};`,
|
|
35
|
+
italicUnderlineColor: (color) => `font-style: italic; text-decoration: underline; color: ${color};`,
|
|
36
|
+
// Combo 3 styles (không màu)
|
|
37
|
+
boldItalicUnderline: () => `font-weight: bold; font-style: italic; text-decoration: underline;`,
|
|
38
|
+
// Combo 3 styles + color
|
|
39
|
+
boldItalicUnderlineColor: (color) => `font-weight: bold; font-style: italic; text-decoration: underline; color: ${color};`,
|
|
40
|
+
};
|
|
41
|
+
// Process each custom tag
|
|
42
|
+
Object.keys(tagStyleMap).forEach((tagName) => {
|
|
43
|
+
// Match tags with color attribute: <tagName color="...">content</tagName>
|
|
44
|
+
const regexWithColor = new RegExp(`<${tagName}\\s+color="([^"]+)">([^<]*)</${tagName}>`, "gi");
|
|
45
|
+
result = result.replace(regexWithColor, (_, color, content) => {
|
|
46
|
+
const style = tagStyleMap[tagName](color);
|
|
47
|
+
return `<span style="${style}">${content}</span>`;
|
|
48
|
+
});
|
|
49
|
+
// Match tags without color attribute: <tagName>content</tagName>
|
|
50
|
+
const regexWithoutColor = new RegExp(`<${tagName}>([^<]*)</${tagName}>`, "gi");
|
|
51
|
+
result = result.replace(regexWithoutColor, (_, content) => {
|
|
52
|
+
const style = tagStyleMap[tagName]();
|
|
53
|
+
return style ? `<span style="${style}">${content}</span>` : content;
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
return result;
|
|
57
|
+
};
|
|
58
|
+
const clamp255 = (n) => Math.max(0, Math.min(255, n));
|
|
59
|
+
const rgbToHex = (r, g, b) => {
|
|
60
|
+
const toHex = (v) => clamp255(v).toString(16).padStart(2, "0");
|
|
61
|
+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Converts text entered in a multiline input into safe HTML for RenderHTML.
|
|
65
|
+
* HTML collapses newline characters, so each newline must be represented by a
|
|
66
|
+
* line-break element to retain the author's formatting after it is stored.
|
|
67
|
+
*/
|
|
68
|
+
export const textToHtml = (text) => {
|
|
69
|
+
if (!text || typeof text !== "string") {
|
|
70
|
+
return "";
|
|
71
|
+
}
|
|
72
|
+
return text
|
|
73
|
+
.replace(/&/g, "&")
|
|
74
|
+
.replace(/</g, "<")
|
|
75
|
+
.replace(/>/g, ">")
|
|
76
|
+
.replace(/\r\n?|\n/g, "<br />");
|
|
77
|
+
};
|
|
78
|
+
export const normalizeInlineColors = (html) => {
|
|
79
|
+
if (!html || typeof html !== "string") {
|
|
80
|
+
return "";
|
|
81
|
+
}
|
|
82
|
+
// Step 1: Convert custom inline tags to HTML span tags with inline styles
|
|
83
|
+
let normalized = convertInlineTagsToHtml(html);
|
|
84
|
+
// Step 2: Convert rgb() colors to hex format
|
|
85
|
+
normalized = normalized.replace(/color\s*:\s*rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*;?/gi, (_, r, g, b) => `color: ${rgbToHex(Number(r), Number(g), Number(b))};`);
|
|
86
|
+
// Step 3: Convert rgb() background-colors to hex format
|
|
87
|
+
normalized = normalized.replace(/background-color\s*:\s*rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*;?/gi, (_, r, g, b) => `background-color: ${rgbToHex(Number(r), Number(g), Number(b))};`);
|
|
88
|
+
// Older posts can be stored as plain text. Preserve their line breaks when
|
|
89
|
+
// rendering while leaving rich HTML content unchanged.
|
|
90
|
+
return /<\/?[a-z][^>]*>/i.test(normalized)
|
|
91
|
+
? normalized
|
|
92
|
+
: textToHtml(normalized);
|
|
93
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { isDomElement, isDomText, } from "react-native-render-html";
|
|
2
|
+
import { normalizeInlineColors } from "./normalize-inline-colors.js";
|
|
3
|
+
import { quillClassesStyles, richHtmlTextSize, safeLineHeight, scale, } from "./html-styles.js";
|
|
4
|
+
const addLineHeightForInlineFontSize = (html) => html.replace(/<[^>]+>/g, (tag) => tag.replace(/\bstyle\s*=\s*(['"])([\s\S]*?)\1/gi, (attribute, quote, style) => {
|
|
5
|
+
if (/line-height\s*:/i.test(style))
|
|
6
|
+
return attribute;
|
|
7
|
+
const match = style.match(/font-size\s*:\s*(\d*\.?\d+)\s*(px|em|rem)\b/i);
|
|
8
|
+
if (!match)
|
|
9
|
+
return attribute;
|
|
10
|
+
const size = Number(match[1]);
|
|
11
|
+
const fontSize = match[2].toLowerCase() === "px" ? size : size * scale(14);
|
|
12
|
+
const lineHeight = safeLineHeight(Math.round(fontSize * 1.5 * 100) / 100);
|
|
13
|
+
const separator = /;\s*$/.test(style) ? " " : "; ";
|
|
14
|
+
// Preserve an authored line-height; otherwise prevent large inline text
|
|
15
|
+
// from inheriting the smaller body line-height and being clipped.
|
|
16
|
+
return `style=${quote}${style}${separator}line-height: ${lineHeight}px${quote}`;
|
|
17
|
+
}));
|
|
18
|
+
/** Shared display-only cleanup; never decode escaped HTML into executable markup. */
|
|
19
|
+
export const normalizeRichHtml = (content) => {
|
|
20
|
+
// Some legacy responses still escape attribute quotes after JSON decoding.
|
|
21
|
+
const html = typeof content === "string"
|
|
22
|
+
? content.replace(/<[^>]+>/g, (tag) => tag.replace(/\\+"/g, '"'))
|
|
23
|
+
: "";
|
|
24
|
+
return addLineHeightForInlineFontSize(normalizeInlineColors(html));
|
|
25
|
+
};
|
|
26
|
+
const escapeAttribute = (value) => value
|
|
27
|
+
.replace(/&/g, "&")
|
|
28
|
+
.replace(/"/g, """)
|
|
29
|
+
.replace(/</g, "<")
|
|
30
|
+
.replace(/>/g, ">");
|
|
31
|
+
export const buildRichHtml = (content, imageUrls = []) => {
|
|
32
|
+
// Normalize the body BEFORE appending images: a plain-text body still needs
|
|
33
|
+
// escaping and explicit line breaks. Do not normalize it twice.
|
|
34
|
+
const body = normalizeRichHtml(content);
|
|
35
|
+
const images = imageUrls
|
|
36
|
+
.filter((url) => typeof url === "string" && Boolean(url))
|
|
37
|
+
.map((url) => `<img src="${escapeAttribute(url)}" style="width: 100%; height: auto;" />`)
|
|
38
|
+
.join("");
|
|
39
|
+
return body + images;
|
|
40
|
+
};
|
|
41
|
+
const hasChildren = (node) => "children" in node;
|
|
42
|
+
const flowTags = /^(article|aside|section|div|p|h[1-6]|blockquote|figure|figcaption|span|li)$/;
|
|
43
|
+
const fixedFlowStyles = /(^|;)\s*(?:(?:min-|max-)?(?:width|height)|overflow(?:-[xy])?|(?:-webkit-)?line-clamp|text-overflow)\s*:[^;]*/gi;
|
|
44
|
+
const noWrapStyles = /(^|;)\s*white-space\s*:\s*nowrap(?:\s*!important)?(?=\s*(?:;|$))/gi;
|
|
45
|
+
const mediaSpacingStyles = /(^|;)\s*(?:margin|padding)(?:-(?:block(?:-start|-end)?|top|bottom))?\s*:[^;]*/gi;
|
|
46
|
+
const paragraphSpacingStyles = /(^|;)\s*margin(?:-(?:block(?:-start|-end)?|top|bottom))?\s*:[^;]*/gi;
|
|
47
|
+
export const richHtmlDomVisitors = {
|
|
48
|
+
onDocument(document) {
|
|
49
|
+
const visit = (node, preserveWhitespace = false) => {
|
|
50
|
+
const tagName = isDomElement(node) ? node.name : undefined;
|
|
51
|
+
const preserve = preserveWhitespace || tagName === "pre" || tagName === "code";
|
|
52
|
+
if (isDomElement(node) && node.attribs.style) {
|
|
53
|
+
// Native justification stretches glyphs on narrow screens. Keep other
|
|
54
|
+
// authored alignment (center/right), colors and font sizes unchanged.
|
|
55
|
+
node.attribs.style = node.attribs.style.replace(/(^|;)\s*text-align\s*:\s*justify(?:\s*!important)?(?=\s*(?:;|$))/gi, "$1text-align: left");
|
|
56
|
+
// The web editor can save fixed desktop dimensions and clipping styles.
|
|
57
|
+
// Let mobile paragraphs grow and wrap. Keep media dimensions intact.
|
|
58
|
+
if (!preserve && flowTags.test(node.name)) {
|
|
59
|
+
node.attribs.style = node.attribs.style
|
|
60
|
+
.replace(fixedFlowStyles, "$1")
|
|
61
|
+
.replace(noWrapStyles, "$1");
|
|
62
|
+
}
|
|
63
|
+
if (node.name === "figure" ||
|
|
64
|
+
node.name === "img" ||
|
|
65
|
+
node.name === "figcaption") {
|
|
66
|
+
node.attribs.style = node.attribs.style.replace(mediaSpacingStyles, "$1");
|
|
67
|
+
}
|
|
68
|
+
if (node.name === "p" && !preserve) {
|
|
69
|
+
node.attribs.style = node.attribs.style.replace(paragraphSpacingStyles, "$1");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (isDomText(node) && !preserve) {
|
|
73
|
+
// Quill posts can contain NBSP between every word. Normalize only text
|
|
74
|
+
// nodes, not link/image attributes, so native text can wrap naturally.
|
|
75
|
+
node.data = node.data.replace(/\u00a0/g, " ");
|
|
76
|
+
}
|
|
77
|
+
if (hasChildren(node)) {
|
|
78
|
+
node.children.forEach((child) => visit(child, preserve));
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
visit(document);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
export const richHtmlClassesStyles = {
|
|
85
|
+
...quillClassesStyles,
|
|
86
|
+
"ql-align-justify": { textAlign: "left" },
|
|
87
|
+
"ql-align-center": { textAlign: "center" },
|
|
88
|
+
"ql-align-right": { textAlign: "right" },
|
|
89
|
+
};
|
|
90
|
+
// Inline tags inherit font size, color and weight from their parent. In
|
|
91
|
+
// particular, <strong><em>...</em></strong> must remain both bold and italic.
|
|
92
|
+
export const richHtmlTagsStyles = {
|
|
93
|
+
p: { marginTop: 0, marginBottom: 6 },
|
|
94
|
+
strong: { fontWeight: "bold" },
|
|
95
|
+
b: { fontWeight: "bold" },
|
|
96
|
+
em: { fontStyle: "italic" },
|
|
97
|
+
i: { fontStyle: "italic" },
|
|
98
|
+
u: { textDecorationLine: "underline" },
|
|
99
|
+
s: { textDecorationLine: "line-through" },
|
|
100
|
+
h1: richHtmlTextSize(24),
|
|
101
|
+
h2: richHtmlTextSize(19),
|
|
102
|
+
h3: richHtmlTextSize(18),
|
|
103
|
+
h4: richHtmlTextSize(16),
|
|
104
|
+
h5: richHtmlTextSize(14),
|
|
105
|
+
h6: richHtmlTextSize(14),
|
|
106
|
+
ul: { paddingLeft: 20, marginTop: 0, marginBottom: 8 },
|
|
107
|
+
ol: { paddingLeft: 20, marginTop: 0, marginBottom: 8 },
|
|
108
|
+
li: { paddingLeft: 0 },
|
|
109
|
+
figure: { marginTop: 6, marginBottom: 8, marginLeft: 0, marginRight: 0 },
|
|
110
|
+
img: { marginTop: 4, marginBottom: 4, borderRadius: 12 },
|
|
111
|
+
figcaption: {
|
|
112
|
+
fontSize: scale(12),
|
|
113
|
+
fontStyle: "italic",
|
|
114
|
+
textAlign: "center",
|
|
115
|
+
marginTop: 2,
|
|
116
|
+
marginBottom: 0,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
function withDefined(base, overrides) {
|
|
4
|
+
const output = { ...base };
|
|
5
|
+
if (overrides)
|
|
6
|
+
for (const key of Object.keys(overrides)) {
|
|
7
|
+
if (overrides[key] !== undefined)
|
|
8
|
+
output[key] = overrides[key];
|
|
9
|
+
}
|
|
10
|
+
return output;
|
|
11
|
+
}
|
|
12
|
+
/** Returns a fresh, frozen value. It never configures a global native theme. */
|
|
13
|
+
export function createUITheme(options) {
|
|
14
|
+
if (!options?.colors) {
|
|
15
|
+
throw new Error('UI360 requires an app-owned palette in createUITheme({ colors }).');
|
|
16
|
+
}
|
|
17
|
+
const scale = options.scale ?? 1;
|
|
18
|
+
if (!Number.isFinite(scale) || scale <= 0)
|
|
19
|
+
throw new RangeError("UI theme scale must be positive and finite");
|
|
20
|
+
const mode = options.mode ?? "light";
|
|
21
|
+
return Object.freeze({
|
|
22
|
+
mode,
|
|
23
|
+
colors: Object.freeze({ ...options.colors }),
|
|
24
|
+
spacing: Object.freeze(withDefined({
|
|
25
|
+
xs: 4 * scale,
|
|
26
|
+
sm: 8 * scale,
|
|
27
|
+
md: 12 * scale,
|
|
28
|
+
lg: 16 * scale,
|
|
29
|
+
xl: 20 * scale,
|
|
30
|
+
xxl: 24 * scale,
|
|
31
|
+
}, options.spacing)),
|
|
32
|
+
radius: Object.freeze(withDefined({
|
|
33
|
+
sm: 4 * scale,
|
|
34
|
+
md: 8 * scale,
|
|
35
|
+
lg: 12 * scale,
|
|
36
|
+
sheet: 24 * scale,
|
|
37
|
+
full: 999,
|
|
38
|
+
}, options.radius)),
|
|
39
|
+
typography: Object.freeze(withDefined({
|
|
40
|
+
caption: 10 * scale,
|
|
41
|
+
label: 12 * scale,
|
|
42
|
+
body: 14 * scale,
|
|
43
|
+
subtitle: 16 * scale,
|
|
44
|
+
title: 18 * scale,
|
|
45
|
+
}, options.typography)),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** Pure theme value usable by Unistyles. Configuration belongs to app entries. */
|
|
49
|
+
export function create360Theme(options) {
|
|
50
|
+
const theme = createUITheme(options);
|
|
51
|
+
return { ...theme, type: theme.mode, color: theme.colors };
|
|
52
|
+
}
|
|
53
|
+
const UIThemeContext = createContext(null);
|
|
54
|
+
export function UIThemeProvider({ theme, children }) {
|
|
55
|
+
return (_jsx(UIThemeContext.Provider, { value: theme, children: children }));
|
|
56
|
+
}
|
|
57
|
+
export function useUITheme() {
|
|
58
|
+
const theme = useContext(UIThemeContext);
|
|
59
|
+
if (!theme)
|
|
60
|
+
throw new Error('UIThemeProvider with an app-owned palette is required.');
|
|
61
|
+
return theme;
|
|
62
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { StyleSheet, useWindowDimensions } from "react-native";
|
|
4
|
+
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
5
|
+
import Toast, { BaseToast, } from "react-native-toast-message";
|
|
6
|
+
function MultilineToast({ variant, ...props }) {
|
|
7
|
+
const { width } = useWindowDimensions();
|
|
8
|
+
return (_jsx(BaseToast, { ...props, text1NumberOfLines: 0, text2NumberOfLines: 0, style: [
|
|
9
|
+
styles.container,
|
|
10
|
+
styles[variant],
|
|
11
|
+
{ width: Math.min(width - 32, 420) },
|
|
12
|
+
], contentContainerStyle: styles.content, text1Style: [styles.message, props.text1Style], text2Style: [styles.description, props.text2Style] }));
|
|
13
|
+
}
|
|
14
|
+
const defaults = {
|
|
15
|
+
multilineSuccess: (props) => _jsx(MultilineToast, { ...props, variant: "success" }),
|
|
16
|
+
multilineError: (props) => _jsx(MultilineToast, { ...props, variant: "error" }),
|
|
17
|
+
};
|
|
18
|
+
export function AppToast({ config, topOffset, ...props }) {
|
|
19
|
+
const insets = useSafeAreaInsets();
|
|
20
|
+
const resolvedConfig = React.useMemo(() => (config ? { ...defaults, ...config } : defaults), [config]);
|
|
21
|
+
return (_jsx(Toast, { ...props, config: resolvedConfig, topOffset: topOffset ?? insets.top }));
|
|
22
|
+
}
|
|
23
|
+
const styles = StyleSheet.create({
|
|
24
|
+
container: { height: "auto", minHeight: 60 },
|
|
25
|
+
success: { borderLeftColor: "#69C779" },
|
|
26
|
+
error: { borderLeftColor: "#FE6301" },
|
|
27
|
+
content: { paddingHorizontal: 20, paddingVertical: 14 },
|
|
28
|
+
message: { fontSize: 14, lineHeight: 20, fontWeight: "500" },
|
|
29
|
+
description: { fontSize: 12, lineHeight: 18 },
|
|
30
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { StyleProp, ViewStyle } from "react-native";
|
|
3
|
+
export interface SheetProps {
|
|
4
|
+
visible: boolean;
|
|
5
|
+
onClose: () => void;
|
|
6
|
+
title?: string;
|
|
7
|
+
closeAccessibilityLabel?: string;
|
|
8
|
+
children?: React.ReactNode;
|
|
9
|
+
style?: StyleProp<ViewStyle>;
|
|
10
|
+
animationType?: "none" | "slide" | "fade";
|
|
11
|
+
keyboardAvoiding?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** Native Modal children retain the provider of the app that rendered them. */
|
|
14
|
+
export declare function Sheet({ visible, onClose, title, closeAccessibilityLabel, children, style, animationType, keyboardAvoiding, }: SheetProps): React.JSX.Element;
|
|
15
|
+
export interface ActionSheetItem {
|
|
16
|
+
id: string;
|
|
17
|
+
label: string;
|
|
18
|
+
icon?: React.ReactNode;
|
|
19
|
+
destructive?: boolean;
|
|
20
|
+
disabled?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface ActionSheetProps extends Omit<SheetProps, "children"> {
|
|
23
|
+
items: readonly ActionSheetItem[];
|
|
24
|
+
onSelect: (item: ActionSheetItem) => void;
|
|
25
|
+
cancelText?: string;
|
|
26
|
+
}
|
|
27
|
+
export declare function ActionSheet({ items, onSelect, cancelText, ...props }: ActionSheetProps): React.JSX.Element;
|
|
28
|
+
export interface ConfirmDialogProps {
|
|
29
|
+
visible: boolean;
|
|
30
|
+
title?: string;
|
|
31
|
+
message?: React.ReactNode;
|
|
32
|
+
confirmText: string;
|
|
33
|
+
cancelText?: string;
|
|
34
|
+
destructive?: boolean;
|
|
35
|
+
loading?: boolean;
|
|
36
|
+
onConfirm: () => void;
|
|
37
|
+
onClose: () => void;
|
|
38
|
+
illustration?: React.ReactNode;
|
|
39
|
+
}
|
|
40
|
+
export declare function ConfirmDialog({ visible, title, message, confirmText, cancelText, destructive, loading, onConfirm, onClose, illustration, }: ConfirmDialogProps): React.JSX.Element;
|
|
41
|
+
export interface ResultDialogProps extends Omit<ConfirmDialogProps, "confirmText" | "cancelText" | "onConfirm" | "destructive" | "loading"> {
|
|
42
|
+
buttonText: string;
|
|
43
|
+
}
|
|
44
|
+
export declare function ResultDialog({ buttonText, ...props }: ResultDialogProps): React.JSX.Element;
|
|
45
|
+
export interface LoadingOverlayProps {
|
|
46
|
+
visible: boolean;
|
|
47
|
+
label?: string;
|
|
48
|
+
/** Inline is useful inside an app-owned FullWindowOverlay or portal. */
|
|
49
|
+
presentation?: "modal" | "inline";
|
|
50
|
+
testID?: string;
|
|
51
|
+
}
|
|
52
|
+
export declare function LoadingOverlay({ visible, label, presentation, testID, }: LoadingOverlayProps): React.JSX.Element | null;
|