Skip to content

Commit

Permalink
🏗 Drop prebuild in favor of dynamic components
Browse files Browse the repository at this point in the history
  • Loading branch information
GMartigny committed Nov 19, 2020
1 parent c36faa9 commit 1a274b2
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 79 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea/
dist/
node_modules/
src/components/generated/
64 changes: 0 additions & 64 deletions build.js

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"unpkg": "dist/main.js",
"jsdelivr": "dist/main.js",
"scripts": {
"build": "node ./build.js",
"web": "webpack -p",
"prepublishOnly": "npm run build && npm run web"
"dev": "webpack --mode=development --watch",
"build": "webpack --mode=production",
"prepublishOnly": "npm run build"
},
"eslintConfig": {
"extends": "@gmartigny/eslint-config"
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ new Vue({
<body>
<div id="app"></div>
<!-- Vue script tag-->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/vue"></script>
<!-- Pencil.js script tag-->
<script src="https://unpkg.com/pencil.js"></script>
<!-- vue-pencil.js script tag-->
<script src="https://unpkg.com/vue-pencil.js"></script>
<script>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PContainer from "./Container";

export default (Pencil) => ({
export default Pencil => ({
extends: PContainer(Pencil),
props: ["draggable"],
mounted () {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const methods = mirroredFunctions.reduce((obj, prop) => {
return obj;
}, {});

export default (Pencil) => ({
export default Pencil => ({
extends: PContainer(Pencil),
template: "<div><slot/></div>",
watch: {
Expand Down
41 changes: 41 additions & 0 deletions src/components/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import PComponent from "./Component";

const map = {
// Path: ["instructions", "isClosed"], // FIXME: find a way to declare instructions
Heart: ["radius"],
Line: ["points"],
Spline: ["points", "tension"],
Polygon: ["points"],
RegularPolygon: ["nbSides", "radius"],
Star: ["radius", "nbBranches", "bevelRatio"],
Triangle: ["radius"],
Rectangle: ["width", "height"],
Image: ["url"],
Square: ["size"],
Arc: ["width", "height", "startAngle", "endAngle"],
Ellipse: ["width", "height"],
Circle: ["radius"],
Text: ["text"],
Button: [],
Checkbox: [],
// Knob: [], // FIXME: draggable is not working
// Slider: [], // FIXME: same
ProgressBar: [],
ProgressPie: [],
Select: ["items"],
};

const classes = {};
Object.keys(map).forEach((className) => {
const pName = `P${className}`;
classes[pName] = Pencil => ({
extends: PComponent(Pencil),
props: map[className],
beforeMount () {
const props = ["position", ...map[className], "options"].map(p => this[p]);
this.$pencil = new Pencil[className](...props);
},
});
});

export default classes;
5 changes: 3 additions & 2 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import PContainer from "./Container";
import PScene from "./Scene";
import components from "./factory";

export {
export default {
PContainer,
PScene,
...components,
};
export * from "./generated";
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This should be resolved by the user's bundler
import Pencil from "pencil.js";
import * as ComponentsDefinition from "./components";
import ComponentsDefinition from "./components";
import install from "./install";

const Components = {};
Expand Down
2 changes: 1 addition & 1 deletion src/install.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ComponentsDefinition from "./components";
import ComponentsDefinition from "./components";

export default pencil => (Vue, { prefix } = {}) => {
Object.keys(ComponentsDefinition).forEach((key) => {
Expand Down
11 changes: 7 additions & 4 deletions src/web.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import install from "./install";

// eslint-disable-next-line no-restricted-globals
const global = self;

const errors = [];
if (!window.Vue) {
if (!global.Vue) {
errors.push(`Vue.js should be installed before vue-pencil.js using a <script> tag like:
<script src="https://cdn.jsdelivr.net/npm/vue"></script>`);
}
if (!window.Pencil) {
if (!global.Pencil) {
errors.push(`Pencil.js should be installed before vue-pencil.js using a <script> tag like:
<script src="https://unpkg.com/vue-pencil.js"></script>`);
<script src="https://unpkg.com/pencil.js"></script>`);
}

if (errors.length) {
errors.forEach(message => console.error(message));
}
else {
install(window.Pencil)(window.Vue);
install(global.Pencil)(global.Vue);
}

0 comments on commit 1a274b2

Please sign in to comment.