Skip to content

Commit 08a3ef1

Browse files
committed
Initial commit
0 parents  commit 08a3ef1

20 files changed

+1693
-0
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
typings
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Shape Drawing - Lightweight Charts™ Plugin
2+
3+
This plugin adds enables for arbitrary shapes to be drawn on the chart. It requires LWC version `5.0.0` or greater.
4+
5+
## Running Locally
6+
7+
```shell
8+
npm install
9+
npm run dev
10+
```
11+
12+
Visit `localhost:5173` in the browser.
13+
14+
## Example:
15+
16+
```js
17+
// Draw a triangle
18+
const shape1 = new ShapeDrawing(
19+
[
20+
{ price: 100, time: 1755856347 },
21+
{ price: 100, time: 1755856347 },
22+
{ price: 500, time: 1755846347 },
23+
],
24+
{
25+
fillColor: '#ff0',
26+
fillOpacity: 0.5,
27+
borderColor: '#0ff',
28+
borderWidth: 5,
29+
borderStyle: LineStyle.Dashed,
30+
showTimeAxisLabels: true,
31+
showPriceAxisLabels: true,
32+
labelColor: '#aaa',
33+
labelTextColor: '#000',
34+
},
35+
);
36+
37+
lineSeries.attachPrimitive(shape1);
38+
```
39+
40+
In `./src/example/example.ts`, the following is shown:
41+
42+
<img width="1002" height="503" alt="demo screenshot" src="https://github.com/user-attachments/assets/cd751349-9a9f-4f4c-858d-6d650419ea9f" />
43+
44+
The file also shows an implementation of interactive drawing via clicking on the chart. E.g.
45+
46+
https://github.com/user-attachments/assets/97dc1993-6dd6-4c50-b951-a008db28c577

compile.mjs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { dirname, resolve } from 'node:path';
2+
import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';
3+
import { build, defineConfig } from 'vite';
4+
import { fileURLToPath } from 'url';
5+
import { generateDtsBundle } from 'dts-bundle-generator';
6+
7+
function buildPackageJson(packageName) {
8+
/*
9+
Define the contents of the package's package.json here.
10+
*/
11+
return {
12+
name: packageName,
13+
version: '1.0.0',
14+
keywords: ['lwc-plugin', 'lightweight-charts'],
15+
type: 'module',
16+
main: `./${packageName}.umd.cjs`,
17+
module: `./${packageName}.js`,
18+
types: `./${packageName}.d.ts`,
19+
exports: {
20+
import: {
21+
types: `./${packageName}.d.ts`,
22+
default: `./${packageName}.js`,
23+
},
24+
require: {
25+
types: `./${packageName}.d.cts`,
26+
default: `./${packageName}.umd.cjs`,
27+
},
28+
},
29+
};
30+
}
31+
32+
const __filename = fileURLToPath(import.meta.url);
33+
const currentDir = dirname(__filename);
34+
35+
const pluginFileName = 'shape-drawing';
36+
const pluginFile = resolve(currentDir, 'src', `${pluginFileName}.ts`);
37+
38+
const pluginsToBuild = [
39+
{
40+
filepath: pluginFile,
41+
exportName: 'lwc-plugin-shape-drawing',
42+
name: 'ShapeDrawing',
43+
},
44+
];
45+
46+
const compiledFolder = resolve(currentDir, 'dist');
47+
if (!existsSync(compiledFolder)) {
48+
mkdirSync(compiledFolder);
49+
}
50+
51+
const buildConfig = ({
52+
filepath,
53+
name,
54+
exportName,
55+
formats = ['es', 'umd'],
56+
}) => {
57+
return defineConfig({
58+
publicDir: false,
59+
build: {
60+
outDir: `dist`,
61+
emptyOutDir: true,
62+
copyPublicDir: false,
63+
lib: {
64+
entry: filepath,
65+
name,
66+
formats,
67+
fileName: exportName,
68+
},
69+
rollupOptions: {
70+
external: ['lightweight-charts', 'fancy-canvas'],
71+
output: {
72+
globals: {
73+
'lightweight-charts': 'LightweightCharts',
74+
},
75+
},
76+
},
77+
},
78+
});
79+
};
80+
81+
const startTime = Date.now().valueOf();
82+
console.log('⚡️ Starting');
83+
console.log('Bundling the plugin...');
84+
const promises = pluginsToBuild.map(file => {
85+
return build(buildConfig(file));
86+
});
87+
await Promise.all(promises);
88+
console.log('Generating the package.json file...');
89+
pluginsToBuild.forEach(file => {
90+
const packagePath = resolve(compiledFolder, 'package.json');
91+
const content = JSON.stringify(
92+
buildPackageJson(file.exportName),
93+
undefined,
94+
4
95+
);
96+
writeFileSync(packagePath, content, { encoding: 'utf-8' });
97+
});
98+
console.log('Generating the typings files...');
99+
pluginsToBuild.forEach(file => {
100+
try {
101+
const esModuleTyping = generateDtsBundle([
102+
{
103+
filePath: `./typings/${pluginFileName}.d.ts`,
104+
},
105+
]);
106+
const typingFilePath = resolve(compiledFolder, `${file.exportName}.d.ts`);
107+
writeFileSync(typingFilePath, esModuleTyping.join('\n'), {
108+
encoding: 'utf-8',
109+
});
110+
copyFileSync(typingFilePath, resolve(compiledFolder, `${file.exportName}.d.cts`));
111+
} catch (e) {
112+
console.error('Error generating typings for: ', file.exportName);
113+
}
114+
});
115+
const endTime = Date.now().valueOf();
116+
console.log(`🎉 Done (${endTime - startTime}ms)`);

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<!-- redirect to example page -->
5+
<meta http-equiv="refresh" content="0; URL=src/example/" />
6+
</head>
7+
</html>

0 commit comments

Comments
 (0)