Skip to content

Commit c708e8c

Browse files
committed
cssInjectedByJsPlugin added
Now the styles are built within the component itself so there is no need to worry.
1 parent fbe11c5 commit c708e8c

File tree

8 files changed

+209
-12
lines changed

8 files changed

+209
-12
lines changed

codeglynuikit-0.0.1.tgz

1.34 KB
Binary file not shown.

demo-app/src/App.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<template>
2-
<h1>Demo App is running</h1>
3-
<MyTest/>
2+
<SketchyDiv>INsied</SketchyDiv>
43
</template>
54

65
<script setup>
7-
import { MyTest } from "codeglynuikit"
8-
</script>
6+
import { SketchyDiv } from "codeglynuikit";
7+
</script>

demo-app/src/style.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ body {
1111
padding: 2rem;
1212
text-align: center;
1313
}
14-
15-

lib/package-lock.json

Lines changed: 50 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@
1919
"preview": "vite preview"
2020
},
2121
"dependencies": {
22+
"roughjs": "^4.6.6",
2223
"vue": "^3.4.31"
2324
},
2425
"devDependencies": {
2526
"@vitejs/plugin-vue": "^5.0.5",
26-
"vite": "^5.3.4"
27+
"vite": "^5.3.4",
28+
"vite-plugin-css-injected-by-js": "^3.5.1"
29+
},
30+
"peerDependencies": {
31+
"roughjs": "^4.6.6"
2732
}
2833
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<script>
2+
import {
3+
computed,
4+
h,
5+
nextTick,
6+
onMounted,
7+
onUpdated,
8+
ref,
9+
useCssModule,
10+
watch,
11+
} from "vue";
12+
import rough from "roughjs";
13+
14+
export default {
15+
props: {
16+
class: String,
17+
options: {
18+
type: Object,
19+
default: () => ({
20+
seed: 4,
21+
strokeWidth: 2,
22+
fill: null,
23+
roughness: null,
24+
hachureGap: null,
25+
fillStyle: null,
26+
hachureAngle: null,
27+
}),
28+
},
29+
},
30+
31+
setup(props, { slots }) {
32+
const containerRef = ref(null);
33+
const canvasRef = ref(null);
34+
const style = useCssModule();
35+
const size = ref({
36+
width: null,
37+
height: null,
38+
});
39+
const nodeProps = computed(() => ({
40+
ref: containerRef,
41+
class: classes.value,
42+
}));
43+
44+
const options = computed(() => {
45+
const temp = {
46+
seed: props.options.seed,
47+
strokeWidth: props.options.strokeWidth,
48+
fill: props.options.fill,
49+
roughness: props.options.roughness,
50+
hachureGap: props.options.hachureGap,
51+
fillStyle: props.options.fillStyle,
52+
hachureAngle: props.options.hachureAngle,
53+
};
54+
const filterTemp = Object.fromEntries(
55+
Object.entries(temp).filter(
56+
([_, value]) => value !== undefined && value !== null
57+
)
58+
);
59+
return filterTemp;
60+
});
61+
const classes = computed(() => {
62+
let updatedClasses;
63+
if (props.class) {
64+
updatedClasses = `${style.container}` + " " + props.class;
65+
} else {
66+
updatedClasses = `${style.container}`;
67+
}
68+
return updatedClasses;
69+
});
70+
watch(props, () => {
71+
clearCanvas();
72+
drawDiv();
73+
});
74+
75+
function drawDiv() {
76+
const rc = rough.canvas(canvasRef.value);
77+
rc.rectangle(0, 0, size.value.width, size.value.height, options.value);
78+
}
79+
function clearCanvas() {
80+
canvasRef.value
81+
.getContext("2d")
82+
.clearRect(0, 0, size.value.width, size.value.height);
83+
}
84+
85+
onMounted(() => {
86+
nextTick(() => {
87+
size.value.width = containerRef.value.clientWidth;
88+
size.value.height = containerRef.value.clientHeight;
89+
drawDiv();
90+
91+
// This part automatically update the convas as it's external parent's size is updated
92+
// const contResizeObserver = new ResizeObserver(() => {
93+
// if (containerRef.value) {
94+
// size.value.width = containerRef.value.clientWidth;
95+
// size.value.height = containerRef.value.clientHeight;
96+
// clearCanvas();
97+
// drawDiv();
98+
// }
99+
// });
100+
// contResizeObserver.observe(containerRef.value);
101+
});
102+
});
103+
onUpdated(() => {
104+
nextTick(() => {
105+
clearCanvas();
106+
drawDiv();
107+
});
108+
});
109+
110+
return () => {
111+
const child = [
112+
h("canvas", {
113+
ref: canvasRef,
114+
width: size.value.width,
115+
height: size.value.height,
116+
class: style.boxCanvas,
117+
}),
118+
slots.default ? slots.default() : () => [],
119+
];
120+
121+
return h("div", nodeProps.value, { default: () => child });
122+
};
123+
},
124+
};
125+
</script>
126+
127+
<style module>
128+
.container {
129+
position: relative;
130+
/* min-width: 100%;
131+
min-height: 100%; */
132+
}
133+
134+
.boxCanvas {
135+
position: absolute;
136+
z-index: -10;
137+
top: 0;
138+
bottom: 0;
139+
left: 0;
140+
right: 0;
141+
}
142+
</style>

lib/src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import MyTest from './components/test/MyTest.js'
1+
// import MyTest from './components/test/MyTest.js'
2+
import SketchyDiv from './components/sketchydiv/SketchyDiv.vue'
23

3-
export { MyTest }
4+
export { SketchyDiv }

lib/vite.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import { defineConfig } from "vite";
22
import vue from "@vitejs/plugin-vue";
33
import { resolve } from "path";
4+
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
45

56
// https://vitejs.dev/config/
67
export default defineConfig({
7-
plugins: [vue()],
8+
plugins: [vue(), cssInjectedByJsPlugin()],
89
build: {
910
lib: {
1011
entry: resolve(__dirname, "src/index.js"),
1112
name: "codeglynuikit",
1213
fileName: "codeglynuikit",
1314
},
1415
rollupOptions: {
15-
external: ["vue"],
16+
external: ["vue", 'roughjs'],
1617
output: {
1718
globals: {
1819
vue: "Vue",
20+
roughjs: "roughtjs"
1921
},
2022
},
2123
},
24+
cssCodeSplit: false
2225
},
2326
});

0 commit comments

Comments
 (0)