Skip to content

Instantly share code, notes, and snippets.

@msjavan
Created January 27, 2020 11:28
Show Gist options
  • Save msjavan/5a30b1855542916037cbe35f863cb293 to your computer and use it in GitHub Desktop.
Save msjavan/5a30b1855542916037cbe35f863cb293 to your computer and use it in GitHub Desktop.
MyWidget
import {Widget} from '@osjs/widgets';
export default class MyWidget extends Widget {
constructor(core, options) {
super(core, options, {
// This widget uses a canvas
canvas: true,
fps: 1,
// Our default dimension
dimension: {
width: 300,
height: 50
}
}, {
// Custom options that can be saved
myText: 'Hello World'
});
// Other attributes are registered on your class:
this.color = '#ffffff';
}
// When widget is destructed
onDestroy() {}
// When widget was resized
onResize() {}
// When widget was moved
onMove() {}
// Every rendering tick (or just once if no canvas)
render({canvas, context, width, height}) {
const text = this.options.myText;
context.font = 'monospace';
context.fillStyle = this.color;
context.textAlign = 'center';
context.textBaseline = 'middle';
context.clearRect(0, 0, width, height);
context.fillText(text, width / 2, height / 2);
// The DOM elements is:
//this.$element
}
// A custom set of menu entries
getContextMenu() {
return [{
label: 'My Menu Item',
onclick: () => console.log('Hello!')
}];
}
}
@msjavan
Copy link
Author

msjavan commented Jan 27, 2020

I put this into src/widgets/MyWidget/index.js, and run build install.
got this error:

Uncaught TypeError: (0 , n.registry[t.name]) is not a constructor
at Object.create (provider.js:82)
at Object.onclick (provider.js:130)
at Object.onclick [as click] (Menu.js:90)
at HTMLDivElement.g (index.js:156)

@msjavan
Copy link
Author

msjavan commented Jan 27, 2020

src/client/index.js :

import {WidgetServiceProvider} from '@osjs/widgets';
import {MyWidget} from '@osjs/widgets';
osjs.register(WidgetServiceProvider, { args: { registry: { 'my-widget': MyWidget } } });

@chaojian-zhang
Copy link

What else are inside your folder src/widgets/MyWidget/?

@chaojian-zhang
Copy link

Is src/widgets some sort of registered path with the build system?
I only see src/package mentioned in documentation.

@msjavan
Copy link
Author

msjavan commented Dec 25, 2020

Sorry, This is very old issue, I can't remember the details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment