/
image-view.ts
282 lines (236 loc) · 8.27 KB
/
image-view.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* image-view.ts
*
* Copyright (C) 2019-20 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
import { Node as ProsemirrorNode } from 'prosemirror-model';
import { NodeView, EditorView } from 'prosemirror-view';
import { NodeSelection } from 'prosemirror-state';
import { EditorUI, ImageType } from '../../api/ui';
import { PandocExtensions, imageAttributesAvailable } from '../../api/pandoc';
import { imageDialog } from './image-dialog';
import {
attachResizeUI,
initResizeContainer,
ResizeUI,
isResizeUICompatible,
updateImageViewSize,
} from './image-resize';
import { imageDimensionsFromImg, imageContainerWidth } from './image-util';
import './image-styles.css';
export class ImageNodeView implements NodeView {
private readonly type: ImageType;
private node: ProsemirrorNode;
private readonly view: EditorView;
private readonly getPos: () => number;
private readonly editorUI: EditorUI;
private readonly imageAttributes: boolean;
public readonly dom: HTMLElement;
private readonly img: HTMLImageElement;
public readonly contentDOM: HTMLElement | null;
private readonly figcaption: HTMLElement | null;
private resizeUI: ResizeUI | null;
constructor(
node: ProsemirrorNode,
view: EditorView,
getPos: () => number,
editorUI: EditorUI,
pandocExtensions: PandocExtensions,
) {
// determine type
const schema = node.type.schema;
this.type = node.type === schema.nodes.image ? ImageType.Image : ImageType.Figure;
// save references
this.node = node;
this.view = view;
this.getPos = getPos;
this.imageAttributes = imageAttributesAvailable(pandocExtensions);
this.editorUI = editorUI;
this.resizeUI = null;
// set node selection on click
const selectOnClick = () => {
const tr = view.state.tr;
tr.setSelection(NodeSelection.create(view.state.doc, getPos()));
view.dispatch(tr);
};
// show image dialog on double-click
const editOnDblClick = () => {
selectOnClick();
imageDialog(
this.node,
imageDimensionsFromImg(this.img, this.containerWidth()),
this.node.type,
this.view.state,
this.view.dispatch,
this.view,
editorUI,
this.imageAttributes,
);
};
// stop propagation from child elmeents that need to handle click
// (e.g. figcaption element)
const noPropagateClick = (ev: MouseEvent) => {
ev.stopPropagation();
};
// create the image (used by both image and figure node types)
this.img = document.createElement('img');
this.img.onclick = selectOnClick;
this.img.ondblclick = editOnDblClick;
// wrap in figure if appropriate
if (this.type === ImageType.Figure) {
// create figure wrapper
this.dom = document.createElement('figure');
// create container
const container = document.createElement('div');
container.contentEditable = 'false';
this.dom.append(container);
// initialize the image
container.append(this.img);
this.updateImg();
// create the caption and make it our contentDOM
this.figcaption = document.createElement('figcaption');
this.figcaption.classList.add('pm-figcaption');
this.figcaption.classList.add('pm-node-caption');
this.figcaption.onclick = noPropagateClick;
this.figcaption.ondblclick = noPropagateClick;
this.contentDOM = this.figcaption;
this.dom.append(this.figcaption);
// if there is no support for implicit_figures then hide the caption
if (!pandocExtensions.implicit_figures) {
this.figcaption.style.display = 'none';
}
// standard inline image
} else {
this.dom = document.createElement('span');
this.dom.append(this.img);
this.updateImg();
this.contentDOM = null;
this.figcaption = null;
}
// prevent drag/drop if the event doesn't target the image
this.dom.ondragstart = (event: DragEvent) => {
if (event.target !== this.img) {
event.preventDefault();
event.stopPropagation();
}
};
// init resize if we support imageAttributes
if (this.imageAttributes) {
initResizeContainer(this.dom);
}
}
public selectNode() {
// mirror default implementation
this.dom.classList.add('ProseMirror-selectednode');
if (this.contentDOM || !this.node.type.spec.draggable) {
this.dom.draggable = true;
}
// attach resize UI
this.attachResizeUI();
}
public deselectNode() {
// mirror default implementation
this.dom.classList.remove('ProseMirror-selectednode');
if (this.contentDOM || !this.node.type.spec.draggable) {
this.dom.draggable = false;
}
// remove resize UI
if (this.resizeUI) {
this.resizeUI.detach();
this.resizeUI = null;
}
}
// update image with latest node/attributes
public update(node: ProsemirrorNode) {
// boilerplate type check
if (node.type !== this.node.type) {
return false;
}
// set new node and update the image
this.node = node;
this.updateImg();
// if we already have resize UI then either update it
// or detach it (if e.g. the units are no longer compatible)
if (this.resizeUI) {
if (isResizeUICompatible(this.img!)) {
this.resizeUI.update();
} else {
this.resizeUI.detach();
this.resizeUI = null;
}
// attach if the node is selected
} else if (this.isNodeSelected()) {
this.attachResizeUI();
}
return true;
}
// ignore mutations outside of the content dom so sizing actions don't cause PM re-render
public ignoreMutation(mutation: MutationRecord | { type: 'selection'; target: Element }) {
// always ignore if we have no contentDOM
if (!this.contentDOM) {
return true;
}
// always ignore mutations of the image
if (mutation.target === this.img) {
return true;
}
// ignore any changes inside the contentDOM
return !this.contentDOM.contains(mutation.target);
}
// prevent bubbling of events into editor
public stopEvent(event: Event) {
// allow drag events if they target the image (otherwise mouse events
// on the shelf can end up bubbling up)
if (event instanceof DragEvent && event.target instanceof HTMLImageElement) {
return false;
}
// filter other events that target our element or it's children
if (event.target instanceof HTMLElement) {
const stop = this.dom === event.target || this.dom.contains(event.target as HTMLElement);
return stop;
} else {
return false;
}
}
// map node to img tag
private updateImg() {
// map to path reachable within current editing frame
this.img.src = this.editorUI.context.mapResourcePath(this.node.attrs.src);
// title/tooltip
this.img.title = this.node.attrs.title;
// ensure alt attribute so that we get default browser broken image treatment
this.img.alt = this.node.textContent || this.node.attrs.src;
// update size
this.updateImageSize();
}
private updateImageSize() {
const containerWidth = this.img.isConnected ? this.containerWidth() : 0;
updateImageViewSize(this.node, this.img, this.isFigure() ? this.dom : null, containerWidth);
}
// attach resize UI if appropriate
private attachResizeUI() {
if (this.imageAttributes && isResizeUICompatible(this.img!)) {
const imageNode = () => ({ pos: this.getPos(), node: this.node });
const imgContainerWidth = () => this.containerWidth();
this.resizeUI = attachResizeUI(imageNode, this.dom, this.img!, imgContainerWidth, this.view, this.editorUI);
}
}
private isNodeSelected() {
return this.dom.classList.contains('ProseMirror-selectednode');
}
private isFigure() {
return this.type === ImageType.Figure;
}
private containerWidth() {
return imageContainerWidth(this.getPos(), this.view);
}
}