[BUG] Unable to add 'space' character if zoom is applied on canvas
Cannot edit text components properly if a zoom value is applied on the canvas.
GrapesJS version:0.15.8
The issue can be reproduced in the demo as well: https://jsfiddle.net/1n9ruwa4/
Expected behavior: Zoom is applied on canvas using:editor.Canvas.setZoom() The text components can be still edited and 'space'(Keycode:32) characters added
Current behavior: Zoom is applied on canvas using:editor.Canvas.setZoom() Even though other characters can be added/edited, you are not able to add new 'space' characters since the space character is also used for 'repositioning' canvas if zoom is applied. The grab cursor style is shown and you could drag the canvas by 'space + drag'
Steps to reproduce: Open jsfiddle demo above(with editor.Canvas.setZoom(90)) Try to edit the hello world text Try to add a 'space' character
The size of the zoom level does not matter, the issue happens with any value.
Thanks @MartinPutz good catch
Noticed this affects all input fields on the page. For instance, when I zoom in with editor.Canvas.setZoom(90) on the demo page and select an element, I can't add a space to any of the input fields in the style manager like the Title under settings.
Changing the onKeyPress method in CanvasView.js to this seems to fix it:
onKeyPress(ev) {
const { em } = this;
const key = getKeyChar(ev);
if (key === ' ' && em.getZoomDecimal() !== 1 && !em.isEditing() && ev.target.nodeName !== 'INPUT') {
this.preventDefault(ev);
em.get('Editor').runCommand('core:canvas-move');
}
},
What I provided doesn't seem like the correct solution though since it could still trap space bar presses which could interfere with whatever else is running on the page. I'm open to working on this, but would need some direction.
Thanks @mattdeigh IMHO the solution is good, just try to see if instead of && !em.isEditing() && ev.target.nodeName !== 'INPUT' (here you check only for the input element) this might work && !em.get('Canvas').isInputFocused() (this should check also for text components). In that case I'd be glad to merge your PR
@artf Just added it and it looks like it works for anything inside Frame, but the StyleManager inputs and anything outside of Frame still get caught here.
It's catching all inputs on the page, even those outside of the editor. Looks like the onKeyPress method is bound to document which was added recently here in this commit. Is there another place the event listener could be bound to?
yeah, you're right, binding it to the document is not correct. I hope the div (the canvas container element itself) is able to catch keypress
fn(el, 'keypress', this.onKeyPress);
can you try and let me know
Looks like it works only if something inside of the Frame is selected.
The only thing I can think of is keeping it on document and tracking mouseover/mouseout on the CanvasView to flag whether or not the keypress is valid. Probably also need a check for if they start moving the canvas and then mouseout so it'll continue to track the move even when the mouse isn't over the CanvasView.
Looks like it works only if something inside of the Frame is selected.
Yeah correct, something inside should be "focused" to trigger the keypress
The only thing I can think of is keeping it on document and tracking mouseover/mouseout on the CanvasView to flag whether or not the keypress is valid. Probably also need a check for if they start moving the canvas and then mouseout so it'll continue to track the move even when the mouse isn't over the CanvasView.
Well it actually makes sense, but if you start typing outside and move the mouse back to the canvas, you're stuck again (if I got right your logic)
Another idea, in isInputFocused I check the activeElement only for the frame's document, what about checking also the main one?!
Well it actually makes sense, but if you start to typing outside and move the mouse back to the canvas, you're stuck again (if I got right your logic)
Oh man, that's true. Didn't even think about that.
Another idea, in isInputFocused I check the activeElement only for the frame's document, what about checking also the main one?!
I like it. Easy and simple. It seems like I could remove this line and isInputFocused would work for the entire document. Would this work? Looks like it is only used for Keymaps but I'm not sure what effect the change would have on it.
It seems like I could remove this line and isInputFocused would work for the entire document. Would this work?
mmm I'm not sure if I follow you, why would you remove that line? My idea was to find the activeElement between the main document and the one inside canvas iframes
Looks like it is only used for Keymaps
yeah, for example, when you're typing in a text component and hit ctrl+z for undo you would expect the undo managed by the browser for the text and not the one managed by the editor (which could undo the element itself)
Ahhh, I see it now. Wasn't tracking with the code completely. I'll play around with that and let you know what I come up with.
I'm writing on this issue, since you can't add a 'space', if you are focused on an <a> or a <button>. It is reproducable in the demo, if you add a Link: https://jsfiddle.net/1n9ruwa4/
Reason is because of the notTextable config from the canvas.
notTextable: ['button', 'a', 'input[type=checkbox]', 'input[type=radio]']
I don't see the point, why "button" and "a" are in this array, since the RTE also works on these elements. But you probably know more than me... Does it make sense to just remove them from the array, or do you see another solution?
Btw, thanks a lot for the work you do, I'm having a blast working with GrapesJS :D