Browse Source

Move canvas events

up-style-manager
Artur Arseniev 4 years ago
parent
commit
b524bc348c
  1. 53
      src/canvas/index.js
  2. 7
      src/editor/index.js

53
src/canvas/index.js

@ -8,12 +8,25 @@
* }) * })
* ``` * ```
* *
* Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance * Once the editor is instantiated you can use its API and listen to its events. Before using these methods, you should get the module from the instance.
* *
* ```js * ```js
* // Listen to events
* editor.on('canvas:drop', () => { ... });
*
* // Use the API
* const canvas = editor.Canvas; * const canvas = editor.Canvas;
* canvas.setCoords(...);
* ``` * ```
* ## Available Events
* * `canvas:dragenter` - When something is dragged inside the canvas, `DataTransfer` instance passed as an argument
* * `canvas:dragover` - When something is dragging on canvas, `DataTransfer` instance passed as an argument
* * `canvas:drop` - Something is dropped in canvas, `DataTransfer` instance and the dropped model are passed as arguments
* * `canvas:dragend` - When a drag operation is ended, `DataTransfer` instance passed as an argument
* * `canvas:dragdata` - On any dataTransfer parse, `DataTransfer` instance and the `result` are passed as arguments.
* By changing `result.content` you're able to customize what is dropped
* *
* ## Methods
* * [getConfig](#getconfig) * * [getConfig](#getconfig)
* * [getElement](#getelement) * * [getElement](#getelement)
* * [getFrameEl](#getframeel) * * [getFrameEl](#getframeel)
@ -65,7 +78,7 @@ export default () => {
c = { c = {
...defaults, ...defaults,
...config, ...config,
module: this module: this,
}; };
this.em = c.em; this.em = c.em;
@ -238,11 +251,7 @@ export default () => {
* @private * @private
*/ */
getOffsetViewerEl(compView) { getOffsetViewerEl(compView) {
return this._getLocalEl( return this._getLocalEl(CanvasView.offsetEl, compView, 'getOffsetViewerEl');
CanvasView.offsetEl,
compView,
'getOffsetViewerEl'
);
}, },
/** /**
@ -258,7 +267,7 @@ export default () => {
CanvasView && CanvasView.remove(); CanvasView && CanvasView.remove();
CanvasView = new canvasView({ CanvasView = new canvasView({
model: canvas, model: canvas,
config: c config: c,
}); });
return CanvasView.render().el; return CanvasView.render().el;
}, },
@ -273,7 +282,7 @@ export default () => {
var canvasOff = this.offset(this.getElement()); var canvasOff = this.offset(this.getElement());
return { return {
top: frameOff.top - canvasOff.top, top: frameOff.top - canvasOff.top,
left: frameOff.left - canvasOff.left left: frameOff.left - canvasOff.left,
}; };
}, },
@ -328,7 +337,7 @@ export default () => {
return { return {
...CanvasView.getCanvasOffset(), ...CanvasView.getCanvasOffset(),
topScroll: top, topScroll: top,
leftScroll: left leftScroll: left,
}; };
}, },
@ -379,7 +388,7 @@ export default () => {
canvasTop: canvasPos.top, canvasTop: canvasPos.top,
canvasLeft: canvasPos.left, canvasLeft: canvasPos.left,
canvasWidth: canvasPos.width, canvasWidth: canvasPos.width,
canvasHeight: canvasPos.height canvasHeight: canvasPos.height,
}; };
// In this way I can catch data and also change the position strategy // In this way I can catch data and also change the position strategy
@ -400,9 +409,7 @@ export default () => {
const zoom = this.em.getZoomDecimal(); const zoom = this.em.getZoomDecimal();
const side = top ? 'top' : 'left'; const side = top ? 'top' : 'left';
const doc = el.ownerDocument; const doc = el.ownerDocument;
const { offsetTop = 0, offsetLeft = 0 } = opts.offset const { offsetTop = 0, offsetLeft = 0 } = opts.offset ? getFrameElFromDoc(doc) : {};
? getFrameElFromDoc(doc)
: {};
const { scrollTop = 0, scrollLeft = 0 } = doc.body || {}; const { scrollTop = 0, scrollLeft = 0 } = doc.body || {};
const scroll = top ? scrollTop : scrollLeft; const scroll = top ? scrollTop : scrollLeft;
const offset = top ? offsetTop : offsetLeft; const offset = top ? offsetTop : offsetLeft;
@ -416,7 +423,7 @@ export default () => {
return { return {
top: rectOff(el, 1, pos), top: rectOff(el, 1, pos),
left: rectOff(el, 0, pos) left: rectOff(el, 0, pos),
}; };
}, },
@ -453,7 +460,7 @@ export default () => {
top, top,
left, left,
canvasOffsetTop: cvOff.top, canvasOffsetTop: cvOff.top,
canvasOffsetLeft: cvOff.left canvasOffsetLeft: cvOff.left,
}; };
// In this way I can catch data and also change the position strategy // In this way I can catch data and also change the position strategy
@ -490,7 +497,7 @@ export default () => {
return { return {
y: e.clientY + addTop - yOffset, y: e.clientY + addTop - yOffset,
x: e.clientX + addLeft - xOffset x: e.clientX + addLeft - xOffset,
}; };
}, },
@ -506,7 +513,7 @@ export default () => {
return { return {
y: ev.clientY * zoom + top, y: ev.clientY * zoom + top,
x: ev.clientX * zoom + left x: ev.clientX * zoom + left,
}; };
}, },
@ -528,9 +535,7 @@ export default () => {
const frame = this.getFrameEl(); const frame = this.getFrameEl();
const toIgnore = ['body', ...this.getConfig().notTextable]; const toIgnore = ['body', ...this.getConfig().notTextable];
const docActive = frame && document.activeElement === frame; const docActive = frame && document.activeElement === frame;
const focused = docActive const focused = docActive ? doc && doc.activeElement : document.activeElement;
? doc && doc.activeElement
: document.activeElement;
return focused && !toIgnore.some(item => focused.matches(item)); return focused && !toIgnore.some(item => focused.matches(item));
}, },
@ -666,11 +671,11 @@ export default () => {
addFrame(props = {}, opts = {}) { addFrame(props = {}, opts = {}) {
return canvas.get('frames').add( return canvas.get('frames').add(
{ {
...props ...props,
}, },
{ {
...opts, ...opts,
em: this.em em: this.em,
} }
); );
}, },
@ -680,6 +685,6 @@ export default () => {
CanvasView && CanvasView.remove(); CanvasView && CanvasView.remove();
[c, canvas, CanvasView].forEach(i => (i = {})); [c, canvas, CanvasView].forEach(i => (i = {}));
['em', 'model', 'droppable'].forEach(i => (this[i] = {})); ['em', 'model', 'droppable'].forEach(i => (this[i] = {}));
} },
}; };
}; };

7
src/editor/index.js

@ -58,12 +58,7 @@
* * `storage:error:store` - Error on store request, passes the error as an argument * * `storage:error:store` - Error on store request, passes the error as an argument
* * `storage:error:load` - Error on load request, passes the error as an argument * * `storage:error:load` - Error on load request, passes the error as an argument
* ### Canvas * ### Canvas
* * `canvas:dragenter` - When something is dragged inside the canvas, `DataTransfer` instance passed as an argument * Check the [Canvas](/api/canvas.html) module.
* * `canvas:dragover` - When something is dragging on canvas, `DataTransfer` instance passed as an argument
* * `canvas:drop` - Something is dropped in canvas, `DataTransfer` instance and the dropped model are passed as arguments
* * `canvas:dragend` - When a drag operation is ended, `DataTransfer` instance passed as an argument
* * `canvas:dragdata` - On any dataTransfer parse, `DataTransfer` instance and the `result` are passed as arguments.
* By changing `result.content` you're able to customize what is dropped
* ### RTE * ### RTE
* Check the [Rich Text Editor](/api/rich_text_editor.html) module. * Check the [Rich Text Editor](/api/rich_text_editor.html) module.
* ### Commands * ### Commands

Loading…
Cancel
Save