Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Fixed image resize flashing unexpected size #112

Merged
merged 16 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/widgetresize.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ mix( WidgetResize, ObservableMixin );
* @interface ResizerOptions
*/

/**
* Editor instance associated with the resizer.
*
* @member {module:core/editor/editor~Editor} module:widget/widgetresize~ResizerOptions#editor
*/

/**
* @member {module:engine/model/element~Element} module:widget/widgetresize~ResizerOptions#modelElement
*/
Expand Down
88 changes: 57 additions & 31 deletions src/widgetresize/resizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export default class Resizer {
*/
this._domResizerWrapper = null;

/**
* View to a wrapper of an element controlled by this resizer.
*
* @private
* @type {module:engine/view/element~Element|null}
*/
this._resizerWrapperView = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "view" doesn't mean that it's a view but it comes from the view so it's an adjective and should be the first word. Just like in the variable which you actually use below:

this._resizerWrapperView = viewResizerWrapper;

Next to the viewResizerWrapper you have a domResizerWrapper.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also fix the description of this property.


/**
* @observable
*/
Expand All @@ -71,6 +79,14 @@ export default class Resizer {
this.decorate( 'cancel' );
this.decorate( 'commit' );
this.decorate( 'updateSize' );

this.on( 'commit', event => {
// State might not be initialized.In this case prevent further handling and make sure that resizer is cleaned up (#5195).
mlewand marked this conversation as resolved.
Show resolved Hide resolved
if ( !this.state.proposedWidth ) {
this._cleanup();
event.stop();
}
}, { priority: 'high' } );
}

/**
Expand Down Expand Up @@ -103,6 +119,8 @@ export default class Resizer {
// Append the resizer wrapper to the widget's wrapper.
writer.insert( writer.createPositionAt( widgetElement, 'end' ), viewResizerWrapper );
writer.addClass( 'ck-widget_with-resizer', widgetElement );

this._resizerWrapperView = viewResizerWrapper;
}

/**
Expand All @@ -128,13 +146,20 @@ export default class Resizer {
* @param {Event} domEventData
*/
updateSize( domEventData ) {
const domHandleHost = this._getHandleHost();
const domResizeHost = this._getResizeHost();
const unit = this._options.unit;
const newSize = this._proposeNewSize( domEventData );
const editingView = this._options.editor.editing.view;

domResizeHost.style.width = ( unit === '%' ? newSize.widthPercents : newSize.width ) + this._options.unit;
editingView.change( writer => {
const unit = this._options.unit;
const newWidth = ( unit === '%' ? newSize.widthPercents : newSize.width ) + unit;

writer.setStyle( 'width', newWidth, this._options.viewElement );
} );

// Get an actual image width, and:
// * reflect this size to the resize wrapper
// * apply this **real** size to the state
const domHandleHost = this._getHandleHost();
const domHandleHostRect = new Rect( domHandleHost );

newSize.handleHostWidth = Math.round( domHandleHostRect.width );
Expand Down Expand Up @@ -187,36 +212,37 @@ export default class Resizer {
* @param {module:utils/dom/rect~Rect} [handleHostRect] Handle host rectangle might be given to improve performance.
*/
redraw( handleHostRect ) {
// TODO review this
const domWrapper = this._domResizerWrapper;

if ( existsInDom( domWrapper ) ) {
// Refresh only if resizer exists in the DOM.
const widgetWrapper = domWrapper.parentElement;
const handleHost = this._getHandleHost();
const clientRect = handleHostRect || new Rect( handleHost );

domWrapper.style.width = clientRect.width + 'px';
domWrapper.style.height = clientRect.height + 'px';

const offsets = {
left: handleHost.offsetLeft,
top: handleHost.offsetTop,
height: handleHost.offsetHeight,
width: handleHost.offsetWidth
};

// In case a resizing host is not a widget wrapper, we need to compensate
// for any additional offsets the resize host might have. E.g. wrapper padding
// or simply another editable. By doing that the border and resizers are shown
// only around the resize host.
if ( !widgetWrapper.isSameNode( handleHost ) ) {
domWrapper.style.left = offsets.left + 'px';
domWrapper.style.top = offsets.top + 'px';

domWrapper.style.height = offsets.height + 'px';
domWrapper.style.width = offsets.width + 'px';
}
this._options.editor.editing.view.change( writer => {
// Refresh only if resizer exists in the DOM.
const widgetWrapper = domWrapper.parentElement;
const handleHost = this._getHandleHost();
const clientRect = handleHostRect || new Rect( handleHost );

writer.setStyle( 'width', clientRect.width + 'px', this._resizerWrapperView );
writer.setStyle( 'height', clientRect.height + 'px', this._resizerWrapperView );

const offsets = {
left: handleHost.offsetLeft,
top: handleHost.offsetTop,
height: handleHost.offsetHeight,
width: handleHost.offsetWidth
};

// In case a resizing host is not a widget wrapper, we need to compensate
// for any additional offsets the resize host might have. E.g. wrapper padding
// or simply another editable. By doing that the border and resizers are shown
// only around the resize host.
if ( !widgetWrapper.isSameNode( handleHost ) ) {
writer.setStyle( 'left', offsets.left + 'px', this._resizerWrapperView );
writer.setStyle( 'top', offsets.top + 'px', this._resizerWrapperView );

writer.setStyle( 'height', offsets.height + 'px', this._resizerWrapperView );
writer.setStyle( 'width', offsets.width + 'px', this._resizerWrapperView );
}
} );
}

function existsInDom( element ) {
Expand Down