Replies: 4 comments 4 replies
-
The real issue is I want code completion on my canvas object: const canvas = vanX.reactive({
id: canvasId || genID(),
width: 500,
height: 500,
x: 0,
y: 0,
zoom: 1,
activeLayer: 'layer1',
commands: [],
loadProject() {
prnt('load canvas', this.id)
},
undo() {
prnt('undo canvas', this.id)
},
setLayer(id) {
let foundLayer
for (const layer of layers) {
if (layer !== undefined) {
foundLayer = layer
if (layer.id === id) {
foundLayer = layer
break
}
}
}
this.activeLayer = foundLayer.id
this.addCommand({
type: 'setLayer',
layer: foundLayer.id
})
prnt('set layer', this.activeLayer)
},
addCommand(command) {
command.time = Date.now()
this.commands.push(command)
}
}) So I can do canvas. >> code complete >> setLayer() Is there a way to do this? Or if there is a better way to organize this I'm open to suggestions! |
Beta Was this translation helpful? Give feedback.
-
trying another way: /**
* @typedef {Object} Canvas
* @property {string} id - Unique identifier for the canvas
* @property {number} width - Canvas width
* @property {number} height - Canvas height
* @property {number} x - X-coordinate of the canvas
* @property {number} y - Y-coordinate of the canvas
* @property {number} zoom - Zoom level of the canvas
* @property {string} activeLayer - ID of the currently active layer
* @property {Array<{type: string, layer?: string, time: number}>} commands - List of commands
*/
/**
* Creates a reactive canvas object
* @returns {Canvas}
*/
const canvas = vanX.reactive({
id: canvasId || genID(),
width: 500,
height: 500,
x: 0,
y: 0,
zoom: 1,
activeLayer: 'layer1',
commands: [],
})
/**
* @typedef {Object} CanvasDo
* @property {function(): void} loadProject - Loads the canvas project
* @property {function(): void} undo - Undoes the last action
* @property {function(id: string): void} setLayer - Sets the active layer by ID
* @property {function(command: {type: string, layer?: string}): void} addCommand - Adds a command to the commands list
*/
const canvasDo = {
loadProject() {
prnt('load canvas', canvas.id)
},
undo() {
prnt('undo canvas', canvas.id)
},
setLayer(id) {
let foundLayer
for (const layer of layers) {
if (layer !== undefined) {
foundLayer = layer
if (layer.id === id) {
foundLayer = layer
break
}
}
}
canvas.activeLayer = foundLayer.id
this.addCommand({
type: 'setLayer',
layer: foundLayer.id
})
prnt('set layer', canvas.activeLayer)
},
addCommand(command) {
command.time = Date.now()
prnt('add command', command)
canvas.commands.push(command)
}
} |
Beta Was this translation helpful? Give feedback.
-
Is there a way to get code completion / jump to with const canvas = vanX.reactive({ canvas.x |
Beta Was this translation helpful? Give feedback.
-
Ah I see. Here's my setup for van. It seems when I try autocomplete on a vanX.reactive object it autocompletes everything on window object as well. I am not using a build step, just plain js <script type="importmap">
{
"imports": {
"vanx": "https://cdn.jsdelivr.net/npm/[email protected]/src/van-x.min.js",
"vanjs-core": "https://cdn.jsdelivr.net/gh/vanjs-org/van/public/van-1.5.3.min.js"
}
}
</script> app.js
import van from "vanjs-core";
import * as vanX from 'vanx'
const { div, span, h1, h2, button, input, select, option, textarea } = van.tags; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, is there a way to get a JS Class constructor to be vanJS reactive?
For example getting this.x, this.y this.width etc to be reactive? I know you could do this.state = vanX.reactive({}) but would prefer to do canvas.width vs canvas.state.width
Beta Was this translation helpful? Give feedback.
All reactions