1 import {positionHandlesAtCorners, removeHandles, renderHandlesAtCorners} from "./node-view-utils";
2 import {NodeSelection} from "prosemirror-state";
8 * @param {(function(): number)} getPos
10 constructor(node, view, getPos) {
11 this.dom = document.createElement('div');
12 this.dom.classList.add('ProseMirror-imagewrap');
14 this.image = document.createElement("img");
15 this.image.src = node.attrs.src;
16 this.image.alt = node.attrs.alt;
17 if (node.attrs.width) {
18 this.image.width = node.attrs.width;
20 if (node.attrs.height) {
21 this.image.height = node.attrs.height;
24 this.dom.appendChild(this.image);
27 this.handleDragStartInfo = null;
28 this.handleDragMoveDimensions = null;
29 this.removeHandlesListener = this.removeHandlesListener.bind(this);
30 this.handleMouseMove = this.handleMouseMove.bind(this);
31 this.handleMouseUp = this.handleMouseUp.bind(this);
32 this.handleMouseDown = this.handleMouseDown.bind(this);
34 this.dom.addEventListener("click", event => {
38 // Show handles if selected
39 if (view.state.selection.node === node) {
40 window.setTimeout(() => {
45 this.updateImageDimensions = function (width, height) {
46 const attrs = Object.assign({}, node.attrs, {width, height});
47 let tr = view.state.tr;
48 const position = getPos();
49 tr = tr.setNodeMarkup(position, null, attrs)
50 tr = tr.setSelection(NodeSelection.create(tr.doc, position));
57 if (this.handles.length === 0) {
58 this.image.dataset.showHandles = 'true';
59 window.addEventListener('click', this.removeHandlesListener);
60 this.handles = renderHandlesAtCorners(this.image);
61 for (const handle of this.handles) {
62 handle.addEventListener('mousedown', this.handleMouseDown);
67 removeHandlesListener(event) {
68 if (!this.dom.contains(event.target)) {
75 removeHandles(this.handles);
76 window.removeEventListener('click', this.removeHandlesListener);
77 delete this.image.dataset.showHandles;
85 * @param {MouseEvent} event
87 handleMouseDown(event) {
88 event.preventDefault();
90 const imageBounds = this.image.getBoundingClientRect();
91 const handle = event.target;
92 this.handleDragStartInfo = {
95 ratio: imageBounds.width / imageBounds.height,
97 handleX: handle.dataset.x,
98 handleY: handle.dataset.y,
101 this.createDragDummy(imageBounds);
102 this.dom.appendChild(this.dragDummy);
104 window.addEventListener('mousemove', this.handleMouseMove);
105 window.addEventListener('mouseup', this.handleMouseUp);
109 * @param {DOMRect} bounds
111 createDragDummy(bounds) {
112 this.dragDummy = this.image.cloneNode();
113 this.dragDummy.style.opacity = '0.5';
114 this.dragDummy.classList.add('ProseMirror-dragdummy');
115 this.dragDummy.style.width = bounds.width + 'px';
116 this.dragDummy.style.height = bounds.height + 'px';
120 * @param {MouseEvent} event
122 handleMouseUp(event) {
123 if (this.handleDragMoveDimensions) {
124 const {width, height} = this.handleDragMoveDimensions;
125 this.updateImageDimensions(String(width), String(height));
128 window.removeEventListener('mousemove', this.handleMouseMove);
129 window.removeEventListener('mouseup', this.handleMouseUp);
130 this.handleDragStartInfo = null;
131 this.handleDragMoveDimensions = null;
132 this.dragDummy.remove();
133 positionHandlesAtCorners(this.image, this.handles);
137 * @param {MouseEvent} event
139 handleMouseMove(event) {
140 const originalBounds = this.handleDragStartInfo.bounds;
142 // Calculate change in x & y, flip amounts depending on handle
143 let xChange = event.screenX - this.handleDragStartInfo.x;
144 if (this.handleDragStartInfo.handleX === 'left') {
147 let yChange = event.screenY - this.handleDragStartInfo.y;
148 if (this.handleDragStartInfo.handleY === 'top') {
152 // Prevent images going too small or into negative bounds
153 if (originalBounds.width + xChange < 10) {
154 xChange = -originalBounds.width + 10;
156 if (originalBounds.height + yChange < 10) {
157 yChange = -originalBounds.height + 10;
160 // Choose the larger dimension change and align the other to keep
161 // image aspect ratio, aligning growth/reduction direction
162 if (Math.abs(xChange) > Math.abs(yChange)) {
163 yChange = Math.floor(xChange * this.handleDragStartInfo.ratio);
164 if (yChange * xChange < 0) {
168 xChange = Math.floor(yChange / this.handleDragStartInfo.ratio);
169 if (xChange * yChange < 0) {
174 // Calculate our new sizes
175 const newWidth = originalBounds.width + xChange;
176 const newHeight = originalBounds.height + yChange;
178 // Apply the sizes and positioning to our ghost dummy
179 this.dragDummy.style.width = `${newWidth}px`;
180 if (this.handleDragStartInfo.handleX === 'left') {
181 this.dragDummy.style.left = `${-xChange}px`;
183 this.dragDummy.style.height = `${newHeight}px`;
184 if (this.handleDragStartInfo.handleY === 'top') {
185 this.dragDummy.style.top = `${-yChange}px`;
188 // Update corners and track dimension changes for later application
189 positionHandlesAtCorners(this.dragDummy, this.handles);
190 this.handleDragMoveDimensions = {
197 export default ImageView;