Skip to content

Commit 9a2f56d

Browse files
committed
fix issues on android
1 parent 08ff6f9 commit 9a2f56d

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

scripts/replace-uplot-assets.cjs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ const fs = require('fs');
33
const path = require('path');
44

55
const projectRoot = path.resolve(__dirname, '..');
6+
const chartPath = path.join(projectRoot, 'src', 'components', 'ChartUPlot.tsx');
7+
const chartPathOut = path.join(
8+
projectRoot,
9+
'dist',
10+
'components',
11+
'ChartUPlot.tsx',
12+
);
613
const htmlPath = path.join(projectRoot, 'src', 'components', 'uplot.html');
714
const htmlPathOut = path.join(projectRoot, 'dist', 'components', 'uplot.html');
815
const cssPath = path.join(
@@ -22,7 +29,8 @@ const jsPath = path.join(
2229

2330
async function main() {
2431
try {
25-
const [html, css, js] = await Promise.all([
32+
const [chart, html, css, js] = await Promise.all([
33+
fs.promises.readFile(chartPath, 'utf8'),
2634
fs.promises.readFile(htmlPath, 'utf8'),
2735
fs.promises.readFile(cssPath, 'utf8'),
2836
fs.promises.readFile(jsPath, 'utf8'),
@@ -35,8 +43,22 @@ async function main() {
3543
.replace(/'\{\{UPLOT_CSS\}\}'/, cssTag)
3644
.replace(/'\{\{UPLOT_JS\}\}'/, jsTag);
3745

38-
await fs.promises.writeFile(htmlPathOut, replaced, 'utf8');
39-
console.log('uplot assets injected into uplot.html');
46+
// var chartReplaced = chart.replace(
47+
// '{{UPLOT_HTML}}',
48+
// replaced.replace(/`/g, '\\`'),
49+
// );
50+
51+
// const escapedHtmlForTemplate = replaced
52+
// .replace(/`/g, '\\`') // escape backticks
53+
// .replace(/\$\{/g, '\\${');
54+
// var chartReplaced = chart.replace('{{UPLOT_HTML}}', escapedHtmlForTemplate);
55+
56+
var chartReplaced = chart.replace("'UPLOT_HTML'", JSON.stringify(replaced));
57+
58+
await fs.promises.writeFile(chartPathOut, chartReplaced, 'utf8');
59+
60+
// await fs.promises.writeFile(htmlPathOut, replaced, 'utf8');
61+
// console.log('uplot assets injected into uplot.html');
4062
} catch (err) {
4163
console.error('Error injecting uplot assets:', err);
4264
process.exit(1);

src/components/ChartUPlot.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import React, {
1010
import { Platform, useWindowDimensions, View } from 'react-native';
1111
import { WebView } from 'react-native-webview';
1212

13-
import html from './uplot.html';
13+
// import html from './uplot.html';
1414
import 'uplot/dist/uPlot.min.css';
1515
var isWeb = Platform.OS === 'web';
1616

17+
var html: string = 'UPLOT_HTML';
18+
1719
var uPlot: any = null;
1820
if (isWeb) {
1921
uPlot = require('uplot').default;
@@ -855,6 +857,21 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
855857
`;
856858
}, [injectedJavaScript]);
857859

860+
const finalHtml = useMemo(() => {
861+
var finalHtml = html;
862+
if (injectedJavaScript) {
863+
finalHtml = html.replace(
864+
"'{{CUSTOM_JS}}'",
865+
`<script>
866+
${injectedJavaScriptWithFunctions}
867+
</script>`,
868+
);
869+
} else {
870+
finalHtml = html.replace("'{{CUSTOM_JS}}'", '');
871+
}
872+
return finalHtml;
873+
}, [injectedJavaScript, injectedJavaScriptWithFunctions]);
874+
858875
useImperativeHandle(ref, () => ({
859876
createChart,
860877
updateOptions,
@@ -883,7 +900,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
883900
<WebView
884901
{...webviewProps}
885902
originWhitelist={['*']}
886-
source={html}
903+
source={{ html: finalHtml }}
887904
allowingReadAccessToURLs={true}
888905
style={memoizedContainerStyle}
889906
scrollEnabled={false}

src/components/uplot.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
return data.map((series) => series.slice(start, end + 1));
6060
}
6161
</script>
62-
'{{UPLOT_CSS}}' '{{UPLOT_JS}}'
62+
'{{UPLOT_CSS}}' '{{UPLOT_JS}}' '{{CUSTOM_JS}}'
6363
<style>
6464
body {
6565
margin: 0;

0 commit comments

Comments
 (0)