Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(IText): Draggable text #7802

Merged
merged 103 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
4a00d88
feat(IText): native like text dragging
ShaMan123 Mar 11, 2022
d6d1eae
fire event on object
ShaMan123 Mar 11, 2022
672f945
Update itext_click_behavior.mixin.js
ShaMan123 Mar 11, 2022
0298b3b
lint
ShaMan123 Mar 11, 2022
c7a39a0
dragend
ShaMan123 Mar 12, 2022
f5957c8
Update itext_click_behavior.mixin.js
ShaMan123 Mar 12, 2022
d4bdf7f
Update itext_click_behavior.mixin.js
ShaMan123 Mar 12, 2022
a77f365
dragover + drop
ShaMan123 Mar 12, 2022
51c06ae
Update itext_behavior.mixin.js
ShaMan123 Mar 12, 2022
a04e57d
Update itext_behavior.mixin.js
ShaMan123 Mar 12, 2022
04ee04f
Update itext_click_behavior.mixin.js
ShaMan123 Mar 12, 2022
afedfc2
fix(): drag end
ShaMan123 Mar 12, 2022
fc00090
Update itext_behavior.mixin.js
ShaMan123 Mar 12, 2022
7dfa629
support `editable`/`canDrop`
ShaMan123 Mar 12, 2022
934dd3c
Update itext_behavior.mixin.js
ShaMan123 Mar 12, 2022
f70651c
lint
ShaMan123 Mar 12, 2022
3d32720
Update itext_behavior.mixin.js
ShaMan123 Mar 12, 2022
34bc7b5
fix bug
ShaMan123 Mar 12, 2022
7ce1e7d
point
ShaMan123 Mar 13, 2022
8d500b3
drag image
ShaMan123 Mar 13, 2022
de9ca36
fix: drag over controls
ShaMan123 Mar 13, 2022
aa8c371
Update itext_behavior.mixin.js
ShaMan123 Mar 13, 2022
0efa15a
JSDOC
ShaMan123 Mar 13, 2022
4f457b9
Update canvas_events.js
ShaMan123 Mar 13, 2022
4d516ea
Update itext_behavior.mixin.js
ShaMan123 Mar 13, 2022
76d2a4d
Update itext_behavior.mixin.js
ShaMan123 Mar 14, 2022
cbe456c
cleanup
ShaMan123 Mar 14, 2022
6047c29
Update itext.class.js
ShaMan123 Mar 14, 2022
3df67f2
Update itext.class.js
ShaMan123 Mar 14, 2022
784a27a
setDragImage
ShaMan123 Mar 14, 2022
17b3f9a
revert `getRelativeCursorPosition`
ShaMan123 Mar 14, 2022
1ae3043
Update itext_behavior.mixin.js
ShaMan123 Mar 14, 2022
8fdf77b
retina scaling
ShaMan123 Mar 14, 2022
d112f3b
fix(): drag image offset
ShaMan123 Mar 14, 2022
57e81a1
Update itext_behavior.mixin.js
ShaMan123 Mar 14, 2022
63bbdfe
better drag image
ShaMan123 Mar 14, 2022
1b640f5
Update itext_behavior.mixin.js
ShaMan123 Mar 14, 2022
a2d2225
support styles
ShaMan123 Mar 14, 2022
48e2dde
dragstart + trailing spcae
ShaMan123 Mar 14, 2022
eef6895
drop + trailing space
ShaMan123 Mar 14, 2022
34b5686
fix: edit after drop + selection
ShaMan123 Mar 14, 2022
f6f701a
Update itext_behavior.mixin.js
ShaMan123 Mar 14, 2022
10a43d9
Update itext_behavior.mixin.js
ShaMan123 Mar 14, 2022
4642784
Update itext_behavior.mixin.js
ShaMan123 Mar 14, 2022
8e69763
refactor clipboard events
ShaMan123 Mar 14, 2022
4d441f3
finalize clipboard data
ShaMan123 Mar 14, 2022
e78310a
JSDOC
ShaMan123 Mar 14, 2022
f6d1311
lint
ShaMan123 Mar 14, 2022
04f379a
rtl support prep
ShaMan123 Mar 14, 2022
f9ed862
Update itext_key_behaviour.js
ShaMan123 Mar 14, 2022
c257c91
Update itext_key_behaviour.js
ShaMan123 Mar 14, 2022
72b9a77
Update itext_key_behavior.mixin.js
ShaMan123 Mar 14, 2022
df3f7f2
Update itext_behavior.mixin.js
ShaMan123 Mar 21, 2022
efdd61e
typo
ShaMan123 Mar 22, 2022
97cebb1
remove `disableStyleCopyPaste`
ShaMan123 Mar 22, 2022
ab637ac
Merge branch 'master' into draggable-text
ShaMan123 Mar 29, 2022
fe7cc4d
Merge branch 'master' into draggable-text
ShaMan123 Apr 2, 2022
8b06dfe
fix(): can't drop over selection
ShaMan123 Apr 2, 2022
1b12168
render drag selection
ShaMan123 Apr 2, 2022
6b8a1d9
renderDragStartSelection
ShaMan123 Apr 2, 2022
49aa851
better drag selection start rendering
ShaMan123 Apr 2, 2022
cfa7445
Update itext_behavior.mixin.js
ShaMan123 Apr 2, 2022
6033478
Update itext.class.js
ShaMan123 Apr 2, 2022
33d1cd6
Update itext.class.js
ShaMan123 Apr 2, 2022
f7ed62c
fix drag start selection
ShaMan123 Apr 2, 2022
46206b1
remove `trailingSpace` logic
ShaMan123 Apr 2, 2022
cdf9abd
fix(): clear context
ShaMan123 Apr 2, 2022
a9ca01e
Update itext_behavior.mixin.js
ShaMan123 Apr 2, 2022
de44140
lint
ShaMan123 Apr 2, 2022
c8b413b
fix: dragleave on canvas
ShaMan123 Apr 2, 2022
2069128
Update canvas_events.js
ShaMan123 Apr 2, 2022
c8c10f0
fix dragenter
ShaMan123 Apr 2, 2022
513199f
Update canvas_events.js
ShaMan123 Apr 2, 2022
d610383
Update canvas_events.js
ShaMan123 Apr 2, 2022
9ebdc62
fire always
ShaMan123 Apr 2, 2022
0d89e2b
Update canvas_events.mixin.js
ShaMan123 Apr 2, 2022
9734207
dragend
ShaMan123 Apr 2, 2022
c181512
Update itext_key_behavior.mixin.js
ShaMan123 Apr 2, 2022
29456b1
Update itext_behavior.mixin.js
ShaMan123 Apr 2, 2022
4058520
Update itext_behavior.mixin.js
ShaMan123 Apr 2, 2022
61c96fa
Update itext_behavior.mixin.js
ShaMan123 Apr 2, 2022
ea9c099
fix(): dragsource dragend edit mode state
ShaMan123 Apr 2, 2022
829bc67
Update itext_behavior.mixin.js
ShaMan123 Apr 2, 2022
43afb2e
Update canvas_events.mixin.js
ShaMan123 Apr 2, 2022
fd8515c
lint
ShaMan123 Apr 2, 2022
ecbf266
Update itext_behavior.mixin.js
ShaMan123 Apr 2, 2022
bd2b6c3
Update canvas_events.mixin.js
ShaMan123 Apr 2, 2022
a875dfe
Revert "Update itext_behavior.mixin.js"
ShaMan123 Apr 2, 2022
bc4e4c1
Update itext_behavior.mixin.js
ShaMan123 Apr 2, 2022
186d26d
fix(): always fire events
ShaMan123 Apr 3, 2022
d4fa70c
dragSource attr on drop event
ShaMan123 Apr 3, 2022
23f450f
fix(): firing `drag` event on drag source
ShaMan123 Apr 3, 2022
c60716d
lint
ShaMan123 Apr 3, 2022
0ebb4bb
fire dragstart on canvas
ShaMan123 Apr 3, 2022
61a48f7
Merge branch 'master' into draggable-text
ShaMan123 Apr 3, 2022
a6b5aa1
revert clipboard event changes
ShaMan123 Apr 25, 2022
51e15fe
revert: remove `disableStyleCopyPaste`
ShaMan123 Apr 25, 2022
d07d6d3
Merge branch 'master' into draggable-text
ShaMan123 May 1, 2022
be4cf22
Merge branch 'master' into draggable-text
ShaMan123 Jun 10, 2022
d6f9c8f
Merge branch 'master' into draggable-text
ShaMan123 Jul 5, 2022
b38b34c
add additional tweaks
ShaMan123 Jul 9, 2022
2db76ec
lint
ShaMan123 Jul 10, 2022
f00e547
Changes for text dragging (#8065)
asturur Jul 28, 2022
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
1 change: 1 addition & 0 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@

this._copyCanvasStyle(lowerCanvasEl, upperCanvasEl);
this._applyCanvasStyle(upperCanvasEl);
upperCanvasEl.setAttribute('draggable', 'true');
this.contextTop = upperCanvasEl.getContext('2d');
},

Expand Down
20 changes: 19 additions & 1 deletion src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
functor(canvasElement, 'wheel', this._onMouseWheel);
functor(canvasElement, 'contextmenu', this._onContextMenu);
functor(canvasElement, 'dblclick', this._onDoubleClick);
functor(canvasElement, 'dragstart', this._onDragStart);
functor(canvasElement, 'dragover', this._onDragOver);
functor(canvasElement, 'dragenter', this._onDragEnter);
functor(canvasElement, 'dragleave', this._onDragLeave);
Expand Down Expand Up @@ -103,6 +104,7 @@
this._onMouseEnter = this._onMouseEnter.bind(this);
this._onContextMenu = this._onContextMenu.bind(this);
this._onDoubleClick = this._onDoubleClick.bind(this);
this._onDragStart = this._onDragStart.bind(this);
this._onDragOver = this._onDragOver.bind(this);
this._onDragEnter = this._simpleEventHandler.bind(this, 'dragenter');
this._onDragLeave = this._simpleEventHandler.bind(this, 'dragleave');
Expand Down Expand Up @@ -207,6 +209,20 @@
this.__onLongPress && this.__onLongPress(e, self);
},

/**
* supports native like text dragging
* @private
* @param {DragEvent} e
*/
_onDragStart: function (e) {
var activeObject = this.getActiveObject();
if (activeObject && typeof activeObject.onDragStart === 'function' && activeObject.onDragStart(e)) {
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
e.preventDefault();
e.stopPropagation();
},
Copy link
Member

Choose a reason for hiding this comment

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

i would prefer to call this _onDragStartTextInternal ( or similar ) and short circuit if the active object type does not inherit from text rather than checking if the activeObject has onDragStart function.

If more drag/drop functionality get added in the future, each functionality can have its own handler and they can be turned off easily by not registering the event handler or de-registering it.

Copy link
Contributor Author

@ShaMan123 ShaMan123 Apr 25, 2022

Choose a reason for hiding this comment

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

I disagree because this way any object can utilize this logic out of the box.
The place to disable the event is from onDragStart - this is from the spec.

Copy link
Member

Choose a reason for hiding this comment

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

I don't want to open the doors to drag things that we didn't plan to support.
We are not adding text drag support, with a skeleton of infrastructure to do maybe more dragging, but we don't really know.
It is easier to add later than rollback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you agree with the naming onDragStart?
It is meant for override same as onSelect


/**
* prevent default to allow drop event to be fired
* @private
Expand Down Expand Up @@ -374,7 +390,9 @@
* @param {Event} e Event object fired on mousemove
*/
_onMouseMove: function (e) {
!this.allowTouchScrolling && e.preventDefault && e.preventDefault();
var activeObject = this.getActiveObject();
!this.allowTouchScrolling && (!activeObject || !activeObject.__isDragging)
&& e.preventDefault && e.preventDefault();
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved
this.__onMouseMove(e);
},

Expand Down
33 changes: 31 additions & 2 deletions src/mixins/itext_click_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
* current compositionMode. It will be set to false.
*/
_mouseDownHandler: function(options) {
if (!this.canvas || !this.editable || (options.e.button && options.e.button !== 1)) {
if (!this.canvas || !this.editable || this.__isDragging || (options.e.button && options.e.button !== 1)) {
return;
}

Expand Down Expand Up @@ -129,6 +129,9 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
// we want to avoid that an object that was selected and then becomes unselectable,
// may trigger editing mode in some way.
this.selected = this === this.canvas._activeObject;
// text dragging logic
var newSelection = this.getSelectionStartFromPointer(options.e);
this.__isDragging = newSelection > this.selectionStart && newSelection < this.selectionEnd;
},

/**
Expand All @@ -146,12 +149,38 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
this.on('mouseup', this.mouseUpHandler);
},

/**
* support native like text dragging
* @private
* @param {DragEvent} e
* @returns {boolean} should handle event
*/
onDragStart: function (e) {
this.__dragStartFired = true;
if (this.__isDragging) {
var selectedText = this.text.slice(this.selectionStart, this.selectionEnd);
e.dataTransfer.setData('text/plain', selectedText);
//e.dataTransfer.setDragImage(this._cacheCanvas, 0,0);
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved
this.fire('dragstart', { e: e, selectedText: selectedText });
}
return this.__isDragging;
},

/**
* standard handler for mouse up, overridable
* @private
*/
mouseUpHandler: function(options) {
mouseUpHandler: function (options) {
// restore selection state after dragging
if (this.__isDragging && !this.__dragStartFired) {
// drag didn't occur, so we revert to click behavior
this.__isDragging = false;
this._mouseDownHandler(options);
}
this.__isDragging = this.__dragStartFired = false;

this.__isMousedown = false;

if (!this.editable || this.group ||
(options.transform && options.transform.actionPerformed) ||
(options.e.button && options.e.button !== 1)) {
Expand Down
1 change: 1 addition & 0 deletions src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @fires selection:changed
* @fires editing:entered
* @fires editing:exited
* @fires dragstart
*
* @return {fabric.IText} thisArg
* @see {@link fabric.IText#initialize} for constructor definition
Expand Down