[QUESTIONS] resizable, using unitWidth in px output into percent.
Question
Hi @artf,
Thanks for making this amazing framework. Recently I made a custom component based on your flexbox plugin.
The red arrow is the actual value of the model.
The green arrow is what I expected to be.
Is that possible to set unitWidth as px but the output to be percent?
Here is what I've tried
- I separated them as 2 component,
gridcomponent, only as a container that will then I set the display intoflex,columncomponent, which I put the resizable.
- set resizable
const keyWidth = 'width'
const step = 1
const minDim = 1
const unitWidth = 'px'
const resizerRight = {
...resizerBtm,
tl: 0,
tc: 0,
tr: 0,
cl: 0,
cr: 1,
bl: 0,
bc: 0,
br: 0,
keyWidth,
currentUnit,
minDim,
step
unitWidth
}
- Makes the
columnlisten to the resizing event. (I am not sure if this was correct, I might need to make a new different post for this question)
init () {
this.listenTo(this, 'change:style ', this.updateWidth)
//...
}
- I want to update the model width from pixel into percent using simple math.
updateWidth(model) {
//...
model.setStyle({
width: parseFloat(selectedWidth, 10) / containerWidth * 100 + '%'
})
//...
}
I am aware that the code above would create an infinite resizing. So, I am looking for another solution which leads me to the resizable source code.
I am wondering if I could make use of the methods listed there
var defaultOpts = {
// ...
mousePosFetcher: null,
// Indicates custom target updating strategy
updateTarget: null,
// Function which gets HTMLElement as an arg and returns it relative position
ratioDefault: 0,
posFetcher: null,
onStart: null,
onMove: null,
onEnd: null,
// ...
}
here is the demo link and the code
Sorry for this long post, I just want to make it easier for you to understand my case.
Answers (3)
Ah wait, my fault, opts is the 3rd one
updateWidth(model, value, opts = {}) {
btw the second argument in setStyle are options (eg, { silent: true } to avoid triggering change listeners) and you get those options also in the callback of your listeners so I just placed a custom key fromPxToPerc to know when the change is triggered from your custom logic
You are the man!!! 👍 👍 Thank you so much @artf.
Hi @afdallah, from the resizer point of view you'd need to use updateTarget option to customize the target but unfortunately, you can't as it already heavily used by the SelectCommand
So I'd ask you to try to use this in your updateWidth function to avoid the infinite loop:
updateWidth(model, opts = {}) {
if (opts.fromPxToPerc) return;
//...
model.setStyle({
width: parseFloat(selectedWidth, 10) / containerWidth * 100 + '%'
}, { fromPxToPerc: 1 })
//...
}
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1113
[QUESTIONS] New trait type is not working with rendered components?
Hello, thanks for the great framework. I'm trying to apply this to my project. I want to add a new trait for existing LINK '<a>' component....
Issue #1690
Flexbox icons missing in the distribution, but demonstrated in the grapesjs.com demo page.
Hi, It appears that the icons / images for the flexbox properties are missing (row, column, etc). These icons are present on the demo page...
Issue #444
How to load css link into the head tag?
Hello, First of all, thanks for contributing such an amazing js framework. I am very new to grapesjs. And github too. https://github.com/ar...
Issue #827
[QUESTION] Change location of command panel
Amazing library, thank you!! I have two questions I'm hoping you can help with.How can I move the command panel from the top to the left as...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.